修改郝斌老师的学生信息管理系统

初学C语言,看了郝斌老师的视频,获益良多,以下是根据郝斌老师的思路写出的代码,仅用来巩固一下所学到的知识,没什么实际意义。

再看《C Primer Plus》,发现国外的人就是不一样,写出来的代码比谭的书中的代码优美N倍。


ps:下面的程序是经过多次改动,和原来郝斌相差巨大了....



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>					//调用Windows系统函数,其他平台下不可用
#define SHOWINFO 0
#define INSER 1
#define Search 2
#define CLEAR 3
#define CLS 4
#define QUIT 5

typedef struct student
{
	char name[20] ;
	float sore ;
	int age ;
}*ST ;

int size = sizeof(struct student) ;
FILE * fp ;

void showinfo(void) ;
int  choice(void) ;
void get_choice(int) ;
void IO(void) ;				//把学生信息写入到结构数组,再调用fp_write写入文件
void output(ST, int) ;  	//此函数功能是输入学生信息到屏幕
void fp_write(ST, int) ;	//把结构数组中的数据写入到文件中储存
void fp_read(void) ;		//读取文件中记录的学生信息
void search(void) ;			//按一定条件查找文件中的学生信息
void get_s_choice(ST, int) ;
void name_find(ST) ;
void sore_find(ST) ;
void age_find(ST) ;
void Open_File(void) ;
void fclear(void) ;			//删除文件函数
void jh_print(void) ; 		 //太多***看起来不舒服

int main(void)
{
	int sel;
    showinfo() ;

	while(1)
    {
        sel = choice() ;
        get_choice(sel) ;
    }

	return 0 ;
}

void showinfo()
{
    jh_print() ;
    printf("*                欢迎使用学生成绩管理系统            *\n") ;
    printf("*                按0显示数据库的学生信息             *\n") ;
    printf("*                按1开始录入的学生信息               *\n") ;
    printf("*                按2查找文件中的学生信息             *\n") ;
    printf("*                按3清空文件中存在的信息             *\n") ;
	printf("*                按4清屏                             *\n") ;
	printf("*                按5退出学生成绩管理系统             *\n") ;
    jh_print() ;
}

int choice()
{
	int i ;
	char ch ;

	while( scanf("%d",&i) != 1 )		//此语句功能是剔除用户错误的输入,下面也有类似的不再注释
	{
		while( (ch = getchar() ) != '\n' ) ;
			 ; //注意这个是空语句,下面不再提醒
		printf("输入错误,请重新输入:\n",i) ;
	}

	return i ;
}

void get_choice(int sel)
{
	switch(sel)
	{
		case SHOWINFO :
			fp_read() ;			//输入0则调用此函数
			break ;

		case INSER :
			IO() ;				//输入1则调用此函数
			break ;

		case Search :
			search() ;
			break ;

        case CLEAR :
            fclear() ;
            break ;

		case CLS :
			system("cls") ;
			break ;

		case QUIT :
			printf("GoodBye!\n") ;  // 输入4直接退出程序
			Sleep(2000) ;
			system("start http://wiyi.org") ;
			exit(1) ;
			break ;
		default :
			break ;
	}
}

void IO(void)
{
	ST pst = NULL ;
    int i,len ;
	char ch ;

    printf("您需要录入多少个学生信息?\nnum = ") ;
	while( (scanf("%d",&len)) != 1 || len <=0 || len > 10 )
	{
		while((ch = getchar()) != '\n')
			putchar(ch) ;
		printf("%c是一个错误输入,请重新输入(1-10)\nlen = ") ;
	}
	pst = (ST)malloc((sizeof(struct student)) * len) ;
	if( pst == NULL)
	{
		printf("内存分配失败!\n") ;
		exit(-1) ;
	}

	for(i=0;i<len;++i)
	{

	    while( getchar() == '\n' )   //剔除换行符影响fgets读取信息
            break ;

		printf("请输入第%d个学生的姓名:\nname = ",i+1) ;
		fgets(pst[i].name,19,stdin) ;
		if( (pst[i].name[strlen(pst[i].name) - 1] ) == '\n')
            pst[i].name[strlen(pst[i].name) - 1] = '\0' ; //剔除fgets获取输入流中的换行符

		printf("请输入第%d个学生的成绩:\nsore = ",i+1) ;
		while( ( scanf("%f",&pst[i].sore) ) != 1 ||
				 pst[i].sore < 0 ||
				 pst[i].sore > 150)
		{
			while( getchar() != '\n' )
				;
			printf("输入错误,分数不能大于150,不能小于0\nsore = ") ;
		}

		printf("请输入第%d个学生的年龄:\nage = ",i+1) ;
		while( ( scanf("%d",&pst[i].age) ) != 1 || pst[i].age < 10)
		{
			while( getchar() != '\n' )
				;
			printf("输入错误,年龄不能小于10\nage = ") ;
		}
	}

	output(pst,len) ;		//调用输出函数把结构数组的数据输出到屏幕
	fp_write(pst,len) ;		//把结构数组的数据写入到文件中
	free(pst) ;				//释放内存
	pst = NULL ;	//我不是野指针~
}

void output(ST pst, int len)
{
	int i ;
	jh_print() ;
	for(i=0;i<len;++i)
	{
		printf("name: %s     ",pst[i].name) ;
		printf("sore: %.2f     ",pst[i].sore) ;
		printf("age : %-5d\n",pst[i].age ) ;
	}
	jh_print() ;
}

void fp_write(ST pst, int len)
{
	int i;

	Open_File() ;

	for(i=0;i<len;++i)
	{
		fwrite(&pst[i],size,1,fp) ;  //写入文件,用for循环控制次数
	}
	fclose(fp) ;
}

void fp_read()
{
	struct student f_read ;

	if ( ( fp = fopen("date.dat","rb") ) == NULL )
	{
		printf("打开文件出错,请检查是否存在数据文件date.dat\n") ;
		exit(1) ;
	}

	jh_print() ;
	while( ( fread(&f_read,size,1,fp) ) != 0)
	{
		printf("name: %s     ",   f_read.name) ;
		printf("sore: %.2f     ", f_read.sore) ;
		printf("age : %-5d\n",    f_read.age) ;
	}
	jh_print() ;

    fclose(fp) ;
}

void search()
{
	struct student f_search ;
	int choice ;

	printf("1)按姓名查询      2)按成绩查询      3)按年龄查询\n") ;
	while( (scanf("%d",&choice)) != 1 || choice > 3 || choice <= 0)
	{
		while( getchar() != '\n' )
            ;
		printf("输入错误,请输入1、2、3\n") ;
	}

	get_s_choice(&f_search,choice) ;

}

void get_s_choice(ST pst, int choice)
{
	switch(choice)
	{
        case 1 :
            name_find(pst) ;
            break ;

		case 2 :
            sore_find(pst) ;
            break ;

		case 3 :
            age_find(pst) ;
            break ;

		default :
			break ;
	}

}

void name_find(ST pst)
{
	Open_File() ;
	char sname[20] ;
	printf("请输入要查找的名字\nname = ") ;

	while(getchar() == '\n')
		break ;

	fgets(sname,19,stdin) ;
	if( sname[strlen(sname) - 1] == '\n' )
		sname[strlen(sname) - 1] = '\0' ;

	while( (fread(pst,size,1,fp)) != 0 )
	{
		if(!strcmp(sname,pst->name))
		{
			jh_print() ;
			printf("name: %s     ",   pst->name) ;
			printf("sore: %.2f     ", pst->sore) ;
			printf("age : %-5d\n",    pst->age) ;
			jh_print() ;
			break ;
		}
	}
	fclose(fp) ;
}

void sore_find(ST pst)
{
	int select ;

    Open_File() ;

	printf("1. 59分以下     2. 60-79\n3. 80-99        4. 100分以上\n") ;
	while( (scanf("%d",&select)) != 1 || select <=0 || select > 4  )
	{
		if(select <= 0 || select > 4)
			printf("请输入1、2、3、4中的一个数字\n") ;
		else
		{
			while( getchar() != '\n')
				;
			printf("输入错误,请重新输入\n") ;
		}
	}

	if(select == 1)
	{
		jh_print() ;
		while( (fread(pst,size,1,fp)) != 0 )
		{
			if(pst->sore < 60)
			{
				printf("name: %s     ",   pst->name) ;
				printf("sore: %.2f     ", pst->sore) ;
				printf("age : %-5d\n",    pst->age) ;
			}
		}
		jh_print() ;
	}

	else if(select == 2)
	{
		jh_print() ;
		while( (fread(pst,size,1,fp)) != 0 )
		{
			if(pst->sore >=60 && pst->sore < 80)
			{
				printf("name: %s     ",   pst->name) ;
				printf("sore: %.2f     ", pst->sore) ;
				printf("age : %-5d\n",    pst->age) ;
			}
		}
		jh_print() ;
	}

	else if(select == 3)
	{
		jh_print() ;
		while( (fread(pst,size,1,fp)) != 0 )
		{
			if(pst->sore >= 80 && pst->sore < 100)
			{
				printf("name: %s     ",   pst->name) ;
				printf("sore: %.2f     ", pst->sore) ;
				printf("age : %-5d\n",    pst->age) ;
			}
		}
		jh_print() ;
	}

	else
	{
		jh_print() ;
		while( (fread(pst,size,1,fp)) != 0 )
		{
			if(pst->sore >= 100)
			{
				printf("name: %s     ",   pst->name) ;
				printf("sore: %.2f     ", pst->sore) ;
				printf("age : %-5d\n",    pst->age) ;
			}
		}
		jh_print() ;
	}
	fclose(fp) ;
}

void age_find(ST pst)
{
	int s_age ;

	Open_File() ;

	printf("请输入要查找的年龄\nage = ") ;
	while( scanf("%d",&s_age) != 1 || s_age <= 0 ||s_age >= 30 )
	{
		if( s_age >= 30 || s_age <= 0)
			printf("不要试图测试本程序的功能……\n") ;
		else
		{
			while( getchar() != '\n' )
				;
			printf("输入错误,请重新输入\n") ;
		}
	}

	jh_print() ;
	while( (fread(pst,size,1,fp)) != 0 )
	{
		if ( pst->age == s_age )
		{
			printf("name: %s     ",   pst->name) ;
			printf("sore: %.2f     ", pst->sore) ;
			printf("age : %-5d\n",    pst->age) ;
		}
	}
	jh_print() ;

	fclose(fp) ;
}

void fclear(void)
{
	int result ;
	char choice ;

	printf("操作不可逆,确认要删除文件?(y/n)\n") ;
	scanf(" %c",&choice) ;
	if(choice == 'y' || choice == 'Y')
	{
		result = remove("date.dat") ;
		if(result == 0)
			printf("清空成功,请继续操作\n") ;
		else
			printf("清空失败,请检查是否有其他程序正在使用date.dat\n") ;
	}
	else if(choice == 'n' || choice == 'N')
		printf("请继续操作\n") ;
	else
		printf("输入错误!中止此函数\n") ;

}

void Open_File(void)
{
	if ( ( fp = fopen("date.dat","a+b") ) == NULL )
	{
		printf("Can't open this file.\n") ;
		exit(1) ;
	}
}

void jh_print(void)
{
	printf("******************************************************\n") ;
}
 


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值