这是本人做的第一个实践项目,用的知识都是书上有的,没有很多很复杂的知识
同时因为没有学习 C语言 图形方面的知识,所以界面做得很丑,暂时先这样吧
源文件:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "student.h" void main() { int num_order; char c; num_order = msg();//输出相关信息,获取相关操作 while( num_order!=1 && num_order!=2 && num_order!=3) { printf("请重新输入正确参数:\n"); scanf("%d",&num_order); } switch( num_order ) { case 1: from_file(0); break;//查询 case 2: input_(); break;//录入 case 3: from_file(1); break;//修改 } c = getchar();//用于接受上一个回车符 printf("\n是否执行其他操作?\n是/Y、否/N\n"); c = getchar(); while( c == 89 || c == 121 ) { printf("请输入相关参数\n"); scanf("%d",&num_order); switch( num_order ) { case 1: from_file(0); break;//查询 case 2: input_(); break;//录入 case 3: from_file(1); break;//修改 } c = getchar(); printf("\n是否执行其他操作?\n是/Y、否/N\n"); c = getchar(); } }
Student.h 头文件:
#include <stdio.h> #include <string.h> #include <stdlib.h> int msg();//信息提示窗口 void input_();//录入相关信息 void save_files( struct Student_info *p );//保存内容至文件 void print_( struct Student_info *p, int n );//输出内容到屏幕 void from_file( int flag );//读取文件内容 void change_info( struct Student_info *p, int loop );//修改相关内容 void sort_info(struct Student_info *p);//排序 struct Student_info { char name[10]; int id; int age; char adress[30]; }; int num_stud; int msg()//信息提示窗口 { int n; printf("\t\t\t\t学生管理系统\n\n"); printf("\t\t 请输入你想执行的操作:\n"); printf("\t\t ╭════════════════╮ \n"); printf("\t\t║1.学生信息查询 ║\n"); printf("\t\t║2.学生信息录入 ║\n"); printf("\t\t║3.学生信息修改 ║\n"); printf("\t\t╰═════════════════╯\n"); scanf("%d",&n); return(n); } void input_()//录入相关信息 { int i; struct Student_info *p,*into_save; system("cls"); printf("学生信息录入系统:\n"); printf("请输入想要录入信息的学生人数\n"); scanf("%d",&num_stud); if((into_save = p = ( struct Student_info *)malloc( num_stud * sizeof( struct Student_info ))) == NULL ) printf("创建空间失败!\n"); else printf("依次输入学生学号 姓名 年龄 地址\n"); for( i = 0; i < num_stud ; i++, p++ ) { scanf( "%d %s %d %s", &p->id, p->name, &p->age, p->adress); } save_files( into_save ); } void save_files( struct Student_info *p )//内容保存 { int i; FILE *fp; if( (fp = fopen( "stud.dat", "ab" )) != NULL ) { for( i = 0; i < num_stud; i++, p++ ) { fwrite( p , sizeof(struct Student_info), 1, fp); } fclose( fp ); printf( "信息录入完成!\n" ); } else if( (fp = fopen( "stud.dat", "wb" )) == NULL ) { printf( "文件创建失败!\n"); exit(0); } else { for( i = 0; i < num_stud; i++, p++ ) { fwrite( p , sizeof(struct Student_info), 1, fp); } fclose( fp ); printf( "信息录入完成!\n" ); } } void print_( struct Student_info *p, int n )//输出屏幕 { int i; printf( "学号\t姓名\t年龄\t地址\n\n"); for( i = 0; i < n ; i++, p++ ) { printf( "%-4d %7s %6d %8s\n", p->id, p->name, p->age, p->adress); } printf("\n"); } void from_file( int flag )//读取文件内容 { FILE *fp; struct Student_info temp; struct Student_info *p,*into_print,*first; int i = 0; int loop = 0; system("cls"); printf("\t学生信息查询 \n"); if((fp = fopen("stud.dat","rb")) == NULL) { printf( "文件信息不存在\n" ); exit(0); } while( !feof(fp) ) { fread( &temp, sizeof(struct Student_info), 1, fp); loop++; } loop-=1;//获取文件中内容个数 rewind(fp); first = into_print = p = (struct Student_info *)malloc(loop*sizeof(struct Student_info)); for( i = 0; i < loop; i++, p++ ) { fread( p, sizeof(struct Student_info), 1 ,fp); } print_( into_print, loop ); if( flag == 0 ) { free( first ); first = into_print = p = NULL; } else { change_info( first, loop ); } fclose(fp); } void change_info( struct Student_info *p, int loop ) { int n,i; struct Student_info *t1,*t2; FILE *fp; printf("请输入要修改学生信息的学号:\n"); scanf("%d",&n); t2 = t1 = p; for( i = 0; i < loop; i ++, t1++ ) { if( t1->id == n) break; } scanf( "%d %s %d %s", &t1->id, t1->name, &t1->age, t1->adress); if( ( fp = fopen( "stud.dat", "wb") ) == NULL ) { printf("文件写入错误!\n"); } else { for( i = 0; i < loop; i ++, p++ ) { fwrite( p, sizeof(struct Student_info), 1, fp ); } } fclose(fp); printf("修改成功!\n"); free(t2); }
运行界面:
1.首页
2.录入界面
3.查询界面
4.修改界面
注意看学号 102 的信息 已经改变
附上文件的链接:
链接:http://pan.baidu.com/s/1sjjSpy9 密码:kowr