课程设计:学生上机管理系统

 
 

大一的课设,和大家分享一下。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<io.h>

#define N 100

/*
** 最多只能优化排序算法,这里的数据结构时空复杂度还不好说  
** 还可以写一个防溢出函数 
*/
 
struct student	//因为没有客户端程序,所以上次登陆时间和计算机编号无法使用 
{
	char time[ 20 ];	//上次登录时间
	char name[ 200 ];	//学生姓名
	char num[ 200 ];		//学生学号
	char computer[ 5 ];		//上次登录的计算机编号
	char fee[ 200 ];		//余额,学生账户初始余额为20元 
	char sex;		//学生性别
} student[ N ];

/*
** 以下只是一系列函数声明,后面定义中有功能注释
*/

int menu();
int initial_password();
int login( int k );
int change_password();
int browse();
int add();
int search();
int search_byname( int z );
int search_bynum( int z );
int modify();
int save( int n );
int load();
int input( int i );
int ensure_input( int i, int n );
char modify_data( int i, int n );
int print_head();
int print_record( int i );
int after_print();
int syscom();
int sort();
int date(); 
int del();
	
int
main()
{
	int k;	//判断是否已初始化密码 
		
	/*
	** 主函数开始实现其功能
	*/
	
	printf( "\n" );
	printf( "\n" );
	printf( "\n" );
	printf( "\n" );
	printf( "          ---------------------------------------------------------------" );
	printf( "\n" );
	printf( "          |  ***-------------------***大家好***--------------------***  |" );
	printf( "\n" );
	printf( "          ---------------------------------------------------------------" );
	printf( "\n" );
	printf( "\n" );
	printf( "\n" );
	printf( "          ---------------------------------------------------------------" );
	printf( "\n" );
	printf( "          |  ***-------------------***老师好***--------------------***  |" );
	printf( "\n" );
	printf( "          ---------------------------------------------------------------" );
	printf( "\n" );
	printf( "\n" );
	printf( "\n" );
	printf( "          ---------------------------------------------------------------" );
	printf( "\n" );
	printf( "          |  ***--------------欢迎使用学生上机管理系统-------------***  |" );
	printf( "\n" );
	printf( "          ---------------------------------------------------------------" );
	printf( "\n" );
	printf( "\n" );
	printf( "\n" );
	printf( "\n" );
	
	/*
	** 实现程序主界面 
	*/
	
	system( "pause" );
	system( "cls" );
	
	if( ( access( "password.dat", 0 ) ) == -1 )
	{
		k = initial_password();
	}
	else
		k = 1;
		
	login( k );
	
	return 0;
	}

int
menu()	//主菜单
{
	int n;	//流程控制变量
	int w = 1;	//判断输入正误
	
	system( "cls" );
		
	getchar();	
	printf( "\n\n\n" );
	printf( "\t\t|***************** 主菜单 *********************|\n\n" );
	printf( "\t\t|***************** 1.修改密码       ***********|\n\n" );
	printf( "\t\t|***************** 2.系统管理       ***********|\n\n" );
	printf( "\t\t|***************** 3.查询学生信息   ***********|\n\n" );
	printf( "\t\t|***************** 4.当前时间与日期 ***********|\n\n" );
	printf( "\t\t|***************** 5.退出           ***********|\n\n" );
	printf( "\t\t|**********************************************|\n" );

	do
	{
		printf( "\n\t请输入您的选择(1~5):[ ]\b\b" );
		scanf( "%d", &n );
			
		if( n < 1 || n > 5 )
		{
			if( ( w % 2 ) == 0 )
			{
				system( "cls" );
		
				getchar();
				printf( "\n\n\n" );
				printf( "\t\t|***************** 主菜单 *********************|\n\n" );
				printf( "\t\t|***************** 1.修改密码       ***********|\n\n" );
				printf( "\t\t|***************** 2.系统管理       ***********|\n\n" );
				printf( "\t\t|***************** 3.查询学生信息   ***********|\n\n" );
				printf( "\t\t|***************** 4.当前时间与日期 ***********|\n\n" );
				printf( "\t\t|***************** 5.退出           ***********|\n\n" );
				printf( "\t\t|**********************************************|\n" );				
			}
		
			w++;
			printf( "\n\t抱歉,请输入1~5中的数字。\n" );
		}
		else
			w = 0;
		} while( w >= 1 );
		
		switch( n )
		{
			case 1:	change_password();break;
			case 2: syscom();break;
			case 3:	search();break;
			case 4:	date();break;
			case 5:	printf( "\n" );exit( 0 );
		}	
		
		return 0;	
}

int
initial_password()	//初始化密码,这种加密方式太脆弱了!!! 
{
	char key_word[ 200 ];
	FILE *key;
	int w;
	
	if( ( key = fopen( "password.dat", "wb" ) ) == NULL )
	{
		printf( "初始化密码失败!\n" );
		printf( "程序中断。\n" );
		exit( 0 );
	}
		
	//未解决输入函数溢出的问题 
	
	printf( "第一次登录,请设置管理员密码(密码只取前15位)\n" );
	printf( ":" );
	scanf( "%s", key_word );
		
	w = strlen( key_word );
	if( w >= 15 )
		key_word[ 15 ] = '\0';
	
	if( fwrite( &key_word, sizeof( key_word ), 1, key ) != 1 ) 
	{	
		printf( "初始化密码失败!\n" );
		printf( "程序中断。\n" );
		exit( 0 );
	}
	
	fclose( key );
	return 1;
}

int 
login( int k )	//验证密码并登录
{
	int w = 0;	//测试密码正误 
	int w1 = 0;	//限制密码输入次数 
	char temp_key[ 200 ];
	char key_word[ 200 ];
	FILE *key;
	
	fflush( stdin );
	
	if( k == 1 )
	{
		system( "cls" );
		printf( "          ---------------------------------------------------------------" );
		printf( "\n" );
		printf( "          |  ***------请输入管理员密码(三次失败后,程序将中止)-----***  |" );
		printf( "\n" );
		printf( "          ---------------------------------------------------------------" );
		printf( "\n" );
		printf( "          :" );
		
		if( ( key = fopen( "password.dat", "rb" ) ) == NULL )
		{
			printf( "读取密码失败!\n" );
			printf( "程序中断。\n" );
			exit( 0 );
		}
		
		fread( &key_word, sizeof( key_word ), 1, key );
		
		do
		{
			scanf( "%s", &temp_key );
			
			if( ( strcmp( temp_key, key_word ) ) == 0 )
			{
				menu();
				exit( 0 );
			}
			else
			{
				if( w1 > 2 )
				{
					printf( "\n输入密码次数过多,程序自动中止。\n" );
					system( "pause" );
					exit( 0 );
				}
				printf( "\n抱歉,密码有误。\n请重新输入:" );
				w = 1;
				w1++;
			}
		} while( w == 1 ); 
							
	}
	else
	{
		printf( "密码未初始化,请重新启动程序\n" );
		exit( 0 );
	}

	fclose( key );	
	return 0;
}

int
change_password()	//修改密码
{
	FILE *key;
	char temp_key[ 200 ];	//临时存储密码 
	char cmp_key[ 200 ];	//确认输入密码
	int w = 0;	//判断两次输入的新密码 
	
	do
	{
		if( w != 0 )
		{
			printf( "\n\t两次输入的新密码不同!!!\n" );
		}
		
		printf( "\n\t请输入新密码:" );
		scanf( "%s", temp_key ); 
	
		printf( "\t请再次确认新密码:" );
		scanf( "%s", cmp_key );
	} while( ( w = strcmp( temp_key, cmp_key ) ) != 0 );
	
	if( ( key = fopen( "password.dat", "wb" ) ) == NULL )
	{
		printf( "密码文件打开失败!\n" );
		printf( "程序中断。\n" );
		exit( 0 );
	}
	
	if( fwrite( &cmp_key, sizeof( cmp_key ), 1, key ) != 1 ) 
	{	
		printf( "更改密码失败!\n" );
		printf( "程序中断。\n" );
		exit( 0 );
	}
	else
		printf( "\n\t密码更改成功!!!" ); 
	
	fclose( key );
	printf( "按任意键返回主菜单\n\n" );
	system( "pause" );
	menu();
	
	return 0;
}

int
syscom()	//系统管理子菜单
{
	int n;	//流程控制变量
	int w = 1;	//判断输入正误
	
	system( "cls" );
	
	getchar();	
	printf( "\n\n\n" );
	printf( "\t\t|***************** 系统管理菜单 *****************|\n\n" );
	printf( "\t\t|***************** 1.添加学生账户         *******|\n\n" );
	printf( "\t\t|***************** 2.修改学生信息         *******|\n\n" );
	printf( "\t\t|***************** 3.删除学生账户         *******|\n\n" );
	printf( "\t\t|***************** 4.浏览全体学生信息     *******|\n\n" );
	printf( "\t\t|***************** 5.数据排序             *******|\n\n" );
	printf( "\t\t|***************** 6.返回主菜单           *******|\n\n" );
	printf( "\t\t|***************** 7.退出                 *******|\n\n" );
	printf( "\t\t|************************************************|\n" );

	do
	{
		printf( "\n\t请输入您的选择(1~7):[ ]\b\b" );
		scanf( "%d", &n );
			
		if( n < 1 || n > 7 )
		{
			if( ( w % 2 ) == 0 )
			{
				system( "cls" );
		
				getchar();
				printf( "\n\n\n" );
				printf( "\t\t|***************** 系统管理菜单 *****************|\n\n" );
				printf( "\t\t|***************** 1.添加学生账户         *******|\n\n" );
				printf( "\t\t|***************** 2.修改学生信息         *******|\n\n" );
				printf( "\t\t|***************** 3.删除学生账户         *******|\n\n" );
				printf( "\t\t|***************** 4.浏览全体学生信息     *******|\n\n" );
				printf( "\t\t|***************** 5.数据排序             *******|\n\n" );
				printf( "\t\t|***************** 6.返回主菜单           *******|\n\n" );
				printf( "\t\t|***************** 7.退出                 *******|\n\n" );
				printf( "\t\t|************************************************|\n" );
			}
		
			w++;
			printf( "\n\t抱歉,请输入1~7中的数字。\n" );
		}
		else
			w = 0;
		} while( w >= 1 );
		
		switch( n )
		{
			case 1:	add();break;
			case 2: modify();break;
			case 3:	del();break;
			case 4:	browse();break;
			case 5:	sort();break;
			case 6: menu();break;
			case 7: exit( 0 );
		}	
		
		return 0;	
}

int
search()	//查找数据项
{
	int z = -1;	//决定返回为主或管理菜单 
	int n;	//载入学生账户数量 
	int m;	//分支变量
	int i;	//记录将删除的数据项	
	
	n = load();
	
	if( n == 0 )
	{
		system( "cls" );
		printf( "没有数据!!!" );
		printf( "\n\n按任意键返回主菜单\n\n" );
		system( "pause" );
		menu();
	}
	
	system( "cls" );
	printf( "您想通过什么方式查询学生账户?(如果忘记了,可以进入系统管理浏览全体学生信息)\n" );
	printf( "1.通过学号查询\t2.通过名字查询\t3.返回主菜单: [ ]\b\b" );	
	scanf( "%d", &m );
	
	if( m == 1 )
		i = search_bynum( z );
	else if( m == 2 )
		i = search_byname( z );
	else if( m == 3 )
		menu();
	else	
	{
		printf( "\n输入错误!\n" );
		printf( "按任意键返回主菜单\n" );
		system( "pause" );
		menu();
	}
	
	printf( "\n\n该学生信息为: \n");
	print_head();
	print_record( i );
	
	printf( "\n按任意键返回主菜单\n" );
	system( "pause" );
	menu();
	
	return 0;
}

int 
search_byname( int z )
{
	int w0;	//用于比较 
	int w = -1;	//判断变量 
	int i;	//循环变量 
	int n;	//载入账户总数
	struct student temp;
	
	n = load();
	
	printf( "\n请输入学生的姓名:" );
	scanf( "%s", temp.name );
	
	for( i = 0; i < n; i++ )
		if( !( w0 = strcmp( temp.name, student[ i ].name ) ) )
		{
			w = 1;
			break;
		}
	
	if( w == 1 )
		return i;
	else if( w == -1 )
	{
		if( z == -1 )
		{
			printf( "\n没有这个学生!\n" );
			printf( "按任意键返回主菜单\n" );
			system( "pause" );
			menu();
		}
		else if ( z == 1 )
			{
			printf( "\n没有这个学生!\n" );
			printf( "按任意键返回管理菜单\n" );
			system( "pause" );
			syscom();
		}
	}
	
	return 0;
}

int
search_bynum( int z )
{
	int w0;	//用于比较 
	int w = -1;	//判断变量 
	int i;	//循环变量 
	int n;	//载入账户总数
	int flag = 0;	//判断变量 
	int m = 0;	//计算错误次数 
	struct student temp;
	
	n = load();
	
	do
	{	
		printf( "\n请输入学生的学号:" );
		scanf( "%s", temp.num );
		
		for( i = 0; temp.num[ i ] != '\0'; i++ )
			if( temp.num[ i ] > '9' )
			{
				printf( "\n输入错误,学号应该由0~9这些数字组成。\n请重新输入!\n\n" );
				flag = 1;
				m++;
				break;
			}
			else	
				flag = 0;
		if( m > 2 )
		{
			if( z == -1 )
			{
				printf( "\n没有这个学生!\n" );
				printf( "按任意键返回主菜单\n" );
				system( "pause" );
				menu();
			}
			else if ( z == 1 )
			{
				printf( "\n没有这个学生!\n" );
				printf( "按任意键返回管理菜单\n" );
				system( "pause" );
				syscom();
			}
		}
	} while( flag );
	
	for( i = 0; i < n; i++ )
		if( !( w0 = strcmp( temp.num, student[ i ].num ) ) )
		{
			w = 1;
			break;
		}
	
	if( w == 1 )
		return i;
	else if( w == -1 )
	{
			if( z == -1 )
			{
				printf( "\n没有这个学生!\n" );
				printf( "按任意键返回主菜单\n" );
				system( "pause" );
				menu();
			}
			else if ( z == 1 )
			{
				printf( "\n没有这个学生!\n" );
				printf( "按任意键返回管理菜单\n" );
				system( "pause" );
				syscom();
			}
	}
	
	return 0;
}

int 
date()
{
	system( "cls" );
	
	printf( "当前日期为:" );
	system( "echo %date:~0,14%" );
	printf( "当前时间为:");
	system( "echo %time:~0,5%" );
	
	printf( "\n按任意键返回主菜单\n" );
	system( "pause" );
	menu();
	
	return 0;
}

int 
add()	//增加数据项	
{
	int i;	//循环变量
	int n;	//已存学生账号个数
	int m;	//将存学生账号个数
	int k;	//学生账号总数
	FILE *stu;	
	
	n = load();
	
	printf( "\t您想增加多少个账号呢(0~%d)?(直接输入0的话,会跳转哦^,^)\n\t:", N - 1 - n );
	scanf( "%d", &m );	
	
	while( m < 0 || m > ( N - 1 - n ) )
	{
		printf( "\n\t输入错误!\n\t请重新输入!\n\n\t您想增加多少个账号呢(0~%d)?\n\t:", N - 1 - n );
		getchar();
		scanf( "%d", &m );	
	} 
	
	k = m + n;
	
	for( i = n; i < k; i++ )
	{
		system( "cls" );
		printf( "请输入第%d个学生的信息\n", i - n + 1 );
		input( i );
	}
	
	/*
	** 添加数据进文件 
	*/
	
	if( ( stu = fopen( "student_ID.dat", "a" ) ) == NULL )
	{	
		printf( "\n打开文件失败!\n" );
		printf( "按任意键返回管理菜单\n" );
		system( "pause" );
		syscom();
	}
	for( i = n; i < k; i++ )
		if( fwrite( &student[ i ], sizeof( struct student ), 1, stu ) != 1 )
		{
			printf( "\n写入文件失败!\n" );
			printf( "按任意键返回管理菜单\n" );
			system( "pause" );
			syscom();
		}
	fclose( stu );
	
	after_print();

	return 0;
}

int
modify()	//修改数据项
{
	int z = 1;	//决定返回为主或管理菜单 
	char w;	//判断变量 
	int i;	//接受返回值,记录学生 
	int j;	//循环变量
	int n;	//载入数据项个数
	struct student temp;
	
	n = load();
	
	if( n == 0 )
	{
		system( "cls" );
		printf( "没有数据!!!" );
		printf( "\n\n按任意键返回管理菜单\n\n" );
		system( "pause" );
		syscom();
	}
	
	print_head();
	
	for( j = 0; j < n; j++ )
	{
		if( ( j != 0 ) && ( j % 10 == 0 ) )
		{
			printf( "\n请记住哪个账户的信息需要修改(按学号修改)\n" );
			system( "pause" );
			system( "cls" );
		}
		
		print_record( j );
	}
	
	printf( "\n您想修改多少号学生的信息?\n" );
	i = search_bynum( z );
	w = modify_data( i, n );
	
	if( w == '1' )
	{	
		save( n );
		after_print();
	}
	else if( w == '3' )
	{
		printf( "\n\n按任意键返回管理菜单\n" );
		system( "pause" );
		syscom();
	}
	
	return 0;
}

char
modify_data( int i, int n )	//实际修改数据操作
{
	int m;	//分支变量 
	int j = 0;	//计数
	int flag = 0;	//判断变量 
	char w;	//判断变量 
	int a;	//循环变量 
	int k;	//循环变量
	 
	do	//后面用整型做判断变量就不行!!!??? 
	{
		system( "cls" );
		printf( "您想修改该学生的: 1.学号 2.姓名 3.性别 4.余额 5.返回管理菜单\n" );
		printf( "请输入您的选择: [ ]\b\b" );	
		scanf( "%d", &m );
		
		while( m < 1 || m > 5 )	
		{
			getchar();
			printf( "\n\n输入错误! 请重新输入您的选择!\n\n");
			printf( "您想修改该学生的: 1.学号 2.姓名 3.性别 4.余额 5.返回管理菜单\n" );
			printf( "请输入您的选择: [ ]\b\b" );	
			scanf( "%d", &m );
		
		}
	
		switch( m )
		{
			case 1:	
				do
				{	
					printf( "\n该学生的学号更改为:" );
					scanf( "%s", student[ i ].num );
			
					for( a = 0; student[ i ].num[ a ] != '\0'; a++ )
						if( student[ i ].num[ a ] > '9' )
						{
							printf( "\n输入错误,学号应该由0~9这些数字组成。\n请重新输入!\n\n" );
							flag = 1;
							j++;
							break;
						}
						else	
							flag = 0;
			
					if( flag != 1 )
						for( k = 0; k < n; k++ )
							if( k != i )
								if( strcmp( student[ k ].num, student[ i ].num ) == 0 )
								{
									printf( "\n这个学生账户已存在。\n请重新输入!\n\n" );
									flag = 1;
								}
			
						if( j > 2 )
						{
							printf( "\n错误次数过多!\n" );
							printf( "按任意键返回管理菜单\n" );
							system( "pause" );
							syscom();
						}
				} while( flag );
				break;
		
			case 2:
				printf( "\n该学生的姓名更改为:" );
				scanf( "%s", student[ i ].name );
				break;
		
			case 3:
				getchar();
				
				do	 
				{
					printf( "\n该学生的性别更改为(男为m,女为f):" );
					scanf( "%c", &student[ i ].sex );
		
					if( student[ i ].sex != 'm' && student[ i ].sex != 'f' )
					{
						getchar();
						printf( "\n输入错误,性别应为男m,女f \n请重新输入!\n\n" );
						flag = 1;
					}
					else
						flag = 0;
				} while( flag );
				break;
				
			case 4:	
				do
				{	
					printf( "\n该学生的账户余额更改为:" );
					scanf( "%s", student[ i ].fee );
			
					for( a = 0; student[ i ].fee[ a ] != '\0'; a++ )
						if( student[ i ].fee[ a ] > '9' )
						{
							printf( "\n输入错误,余额应该由0~9这些数字组成。\n请重新输入!\n\n" );
							flag = 1;
							j++;
							break;
						}
						else	
							flag = 0;
			
						if( j > 2 )
						{
							printf( "\n错误次数过多!\n" );
							printf( "按任意键返回管理菜单\n" );
							system( "pause" );
							syscom();
						}
				} while( flag );
				break;
			
			case 5:
						printf( "\n按任意键返回管理菜单\n" );
						system( "pause" );
						syscom();
						break;
		}
		
		print_head();
		print_record( i );
		
		printf( "\n确定要修改该账户信息么?\n" );
		printf( "1.确定 2.不,重新修改 3.返回并不保存改动: [ ]\b\b" );
		getchar();
		scanf( "%c", &w );
		
		while( w < '1' || w > '3' )	
		{
			getchar();
			printf( "\n\n输入错误! 请重新输入您的选择!\n\n");
			printf( "\n确定要修改该账户信息么?\n" );
			printf( "1.确定 2.不,重新修改 3.返回并不保存改动: [ ]\b\b" );
			scanf( "%c", &w );
			
			if( isspace( w ) )
			getchar();
		}
		
	} while( w == '2' );
	
	return w;
}

int 
del()	//可以用链表优化 
{
	int z = 1;	//决定返回为主或管理菜单 
	int j;	//循环变量 
	int m;	//分支变量 
	char w;	//分支变量 
	int n;	//载入数据项总数 
	int i = -1;	//记录将删除的数据项 
	
	n = load();
	
	if( n == 0 )
	{
		system( "cls" );
		printf( "没有数据!!!" );
		printf( "\n\n按任意键返回管理菜单\n\n" );
		system( "pause" );
		syscom();
	}
	
	print_head();
	
	for( j = 0; j < n; j++ )
	{
		if( ( j != 0 ) && ( j % 10 == 0 ) )
		{
			printf( "\n请记住哪个账户的信息需要删除(按学号删除)\n" );
			system( "pause" );
			system( "cls" );
		}
		
		print_record( j );
	}
	
	printf( "\n您想修改多少号学生的信息?\n" );

	i = search_bynum( z );
	
	printf( "\n确定要删除该账户么?\n" );
	printf( "1.确定\t2.返回管理菜单: [ ]\b\b" );
	getchar();
	scanf( "%c", &w );
	
	while( w < '1' || w > '2' )
	{
		getchar();
		printf( "\n\n输入错误! 请重新输入您的选择!\n\n");
		printf( "\n确定要删除该账户么?\n" );
		printf( "1.确定\t2.返回管理菜单: [ ]\b\b" );
		scanf( "%c", &w );
	}
	
	if( w == '1' )
	{
		print_head();
		print_record( i );
		 
		for( j = i; j < n; j++ )
		{
			strcpy( student[ j ].name, student[ j + 1 ].name );
			strcpy( student[ j ].num, student[ j + 1 ].num );
			student[ j ].sex = student[ j + 1 ].sex;
		} 
		
		save( n - 1 );
	}
	else if( w == '2' )
		syscom();
	else	
	{
		printf( "\n输入错误!\n" );
		printf( "按任意键返回管理菜单\n" );
		system( "pause" );
		syscom();
	}
	
	system( "pause" );
	after_print();
	return 0;
}

int 
browse()	//浏览已存数据
{
	int i;	//循环变量
	int n;	//载入数据项个数
	
	n = load();
	print_head();
	
	for( i = 0; i < n; i++ )
	{
		if( ( i != 0 ) && ( i % 10 == 0 ) )
		{
			system( "pause" );
			system( "cls" );
		}
		
		print_record( i );
	}
	
	printf( "总共有%d个学生账户。\n", n );
	printf( "\n按任意键返回管理菜单。\n" );
	system( "pause" );
	syscom();
	
	return 0;
}

int 
sort()	//排序(使用冒泡排序) 
{
	int i, j;	//循环变量
	int n;	//载入学生账户总数
	char w;	//判断变量 
	char tmp[ 200 ];
	char temp;
	
	n = load();
	system( "cls" );
	
	if( n == 0 )
	{
		printf( "没有数据!!!" );
		printf( "\n\n按任意键返回管理菜单\n\n" );
		system( "pause" );
		syscom();
	}
	
	printf( "您想对学生账户的什么信息进行排序?\n" );
	printf( "1.按学号排序\t2.按账户余额排序\t3.返回管理菜单: [ ]\b\b" );
	getchar();
	scanf( "%c", &w );
	
	while( w < '1' || w > '3' )
	{
		getchar();
		printf( "\n\n输入错误! 请重新输入您的选择!\n\n");
		printf( "您想对学生账户的什么信息进行排序?\n" );
		printf( "1.按学号排序\t2.按账户余额排序\t3.返回管理菜单: [ ]\b\b" );
		scanf( "%c", &w );
	}
	
	if( w == '1' )
	{	
		for( i = 0; i < n - 1; i++ )	
			for( j = 0; j < n - 1 - i; j++ )
				if( atoi( student[ j ].num ) > atoi( student[ j + 1 ].num ) )
				{
					strcpy( tmp, student[ j + 1 ].num );
					strcpy( student[ j + 1 ].num, student[ j ].num );
					strcpy( student[ j ].num, tmp );
				
					strcpy( tmp, student[ j + 1 ].name );
					strcpy( student[ j + 1 ].name, student[ j ].name );
					strcpy( student[ j ].name, tmp );

					strcpy( tmp, student[ j + 1 ].fee );
					strcpy( student[ j + 1 ].fee, student[ j ].fee );
					strcpy( student[ j ].fee, tmp );
				
					temp = student[ j + 1 ].sex;
					student[ j + 1 ].sex = student[ j ].sex;
					student[ j ].sex = temp;
				}
	}
	else if( w == '2' )
	{
		for( i = 0; i < n - 1; i++ )	
			for( j = 0; j < n - 1 - i; j++ )
				if( atoi( student[ j ].fee ) > atoi( student[ j + 1 ].fee ) )
				{
					strcpy( tmp, student[ j + 1 ].num );
					strcpy( student[ j + 1 ].num, student[ j ].num );
					strcpy( student[ j ].num, tmp );
				
					strcpy( tmp, student[ j + 1 ].name );
					strcpy( student[ j + 1 ].name, student[ j ].name );
					strcpy( student[ j ].name, tmp );

					strcpy( tmp, student[ j + 1 ].fee );
					strcpy( student[ j + 1 ].fee, student[ j ].fee );
					strcpy( student[ j ].fee, tmp );
				
					temp = student[ j + 1 ].sex;
					student[ j + 1 ].sex = student[ j ].sex;
					student[ j ].sex = temp;
				}
	} 
	else if( w == '3' )
		syscom();
	else	
	{
		printf( "\n输入错误!\n" );
		printf( "按任意键返回管理菜单\n" );
		system( "pause" );
		syscom();
	}
	
	save( n );
	after_print();
	
	return 0;
}

int 
save( int n )	//保持数据到文件
{
	FILE *stu;	//保存学生数据的流
	int i;	//循环变量
	
	if( ( stu = fopen( "student_ID.dat", "w" ) ) == NULL )
	{	
		printf( "\n打开文件失败!\n" );
		printf( "按任意键返回主菜单\n" );
		system( "pause" );
		menu();
	}
	
	for( i = 0; i < n; i++ )
		if( fwrite( &student[ i ], sizeof( struct student ), 1, stu ) != 1 )
		{
			printf( "\n写入文件失败!\n" );
			printf( "按任意键返回主菜单\n" );
			system( "pause" );
			menu();
		}
	
	fclose( stu );
	return 0;
}
	
int
load()	//从文件读取数据
{
	FILE *stu;	//读取学生数据的流
	int i;	//返回已存学生账户的数量
	
	if( ( stu = fopen( "student_ID.dat", "r" ) ) == NULL )
		return 0;	//有些风险
	
	for( i = 0; !feof( stu ); i++ )
		fread( &student[ i ], sizeof( struct student ), 1, stu );
	
	fclose( stu );
	return ( i - 1 );
}

int 
input( int i )	//输入数据
{
	int w;	//判定循环 
	
	strcpy( student[ i ].time, "2011-7-14" );
	strcpy( student[ i ].computer, "007" );
	strcpy( student[ i ].fee, "20" );
	
	do
	{
		printf( "该学生的姓名为:" );
		getchar();
		scanf( "%s", student[ i ].name );
			
		printf( "\n该学生的性别为(男为m,女为f):" );
		getchar();
		scanf( "%c", &student[ i ].sex );
		
		printf( "\n该学生的学生的学号为(例如:4100121):" );
		getchar();
		scanf( "%s", student[ i ].num );
	} while( ( w = ensure_input( i, i ) ) );
	
	return 0;
}

int
ensure_input( int i, int n )	//保证输入健壮性,其实把测试代码再细分,重用性更高 
{
	int j, k;	//循环变量
	int flag = 0;	//决定循环是否继续 
	
	if( student[ i ].sex != 'm' && student[ i ].sex != 'f' )
	{
		printf( "\n输入错误,性别应为男m,女f \n请重新输入!\n\n" );
		flag = 1;
	}
		
	for( j = 0; student[ i ].num[ j ] != '\0'; j++ )
		if( student[ i ].num[ j ] > '9' )
		{
			printf( "\n输入错误,学号应该由0~9这些数字组成。\n请重新输入!\n\n" );
			flag = 1;
			break;
		}
	
	if( flag != 1 )
		for( k = 0; k < n; k++ )
			if( k != i )
				if( strcmp( student[ k ].num, student[ i ].num ) == 0 )
				{
					printf( "\n这个学生账户已存在。\n请重新输入!\n\n" );
					flag = 1;
					return flag;
				}
	
	return flag;
}

int
print_head()	//输出表头
{
	system( "cls" );
	
	printf( "学号     姓名             性别  上次登录时间和计算机  余额\n" );
	return 0;
}

int 
print_record( int i )	//输出记录数据
{
	printf( "%-7s  %-15s  %c     %-9s     %-3s     %s\n", student[ i ].num, student[ i ].name, student[ i ].sex, student[ i ].time, student[ i ].computer, student[ i ].fee );
	return 0;
}

int
after_print()	//输入后查看输入情况或回主菜单
{
	char w;	//判定变量
	int flag = 0;	//判定变量

	system( "cls" );

	printf( "------------------------\n" );
	printf( "|    操作成功!!!    |\n" );
	printf( "|    现在想干什么呢?  |\n" );
	printf( "------------------------" );
	printf( "\n\n1.浏览所有学生信息\t2.返回主菜单\t3.返回管理菜单: [ ]\b\b" );

	getchar();
	scanf( "%c", &w );
	
	while( w < '1' || w > '3' )	
	{	
		printf( "\n输入错误!\n" );
		printf( "按任意键重新输入\n" );
		system( "pause" );

		system( "cls" );

		printf( "------------------------\n" );
		printf( "|    操作成功!!!    |\n" );
		printf( "|    现在想干什么呢?  |\n" );
		printf( "------------------------" );
		printf( "\n\n1.浏览所有学生信息\t2.返回主菜单\t3.返回管理菜单: [ ]\b\b" );
		
		getchar();
		scanf( "%c", &w );	//还是防不住连续回车! 
		
		if( iscntrl( w ) )
			getchar();
	} 
	
	if( w =='1' )
		browse();
	else if( w == '2' )
		menu();
	else if( w == '3' )
		syscom();
		
	return 0;
}


你可能感兴趣的:(struct,null,search,System,input,menu)