|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 dong50252409 于 2011-8-10 12:09 编辑
- /* Note:Your choice is C IDE */
- #include <stdio.h>
- #include <stdlib.h>
- #define LEN sizeof(struct student)
- struct student
- {
- char num[6], name[20], sex[3];//定义学号、姓名、性别
- float math, english, politics, chinese, sum;//定义数学、英语、**、语文、总分
- struct student *next;
- };
- //菜单
- void menu()
- {
- system("cls");//清屏
- printf("-------------------------------------------------------------------------------");
- printf("\n 学生数据管理系统\n");
- printf("\n [0]退出\n");
- printf("\n [1]创建学生数据\n");
- printf("\n [2]添加学生数据\n");
- printf("\n [3]删除学生数据\n");
- printf("\n [4]查询\n");
- printf("-------------------------------------------------------------------------------");
-
- }
- // 等待用户按回车后回到主菜单
- void to_menu()
- {
- char c1, c2;
- printf("\n\n\n按回车键返回主菜单...");
- scanf("%c%c",&c1,&c2);//第一个字符吸收上次的确认回车键
- menu();
- }
- //录入学生输入
- struct student *init(int n)
- {
- int i;
- struct student *head, *p, *s;
- for(i = 1;i <= n;i++)
- {
- if(i == 1)
- {
- printf("\n请输入第%d个学生信息:",i);
- p = (struct student *)malloc(LEN);//开辟存储单元
- printf("\n学号:");
- scanf("%s",&p->num);
- printf("\n姓名:");
- scanf("%s",&p->name);
- printf("\n性别:");
- scanf("%s",&p->sex);
- printf("\n数学成绩:");
- scanf("%f",&p->math);
- printf("\n英语成绩:");
- scanf("%f",&p->english);
- printf("\n**:");
- scanf("%f",&p->politics);
- printf("\n语文成绩:");
- scanf("%f",&p->chinese);
- p->sum = p->math + p->english + p->politics + p->chinese;
- printf("\n学号\t姓名\t性别\t数学\t英语\t**\t语文\t总分\n");
- printf("------------------------------------------------------------\n");
- printf("%s\t%s\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",p->num, p->name, p->sex, p->math, p->english, p->politics, p->chinese, p->sum);
- head = p;
- if(n == 1) p->next = NULL;
- }
-
- else
- {
- printf("\n请输入第%d个学生信息:",i);
- s = (struct student *)malloc(LEN);
- printf("\n学号:");
- scanf("%s",&s->num);
- printf("\n姓名:");
- scanf("%s",&s->name);
- printf("\n性别:");
- scanf("%s",&s->sex);
- printf("\n数学成绩:");
- scanf("%f",&s->math);
- printf("\n英语成绩:");
- scanf("%f",&s->english);
- printf("\n**:");
- scanf("%f",&s->politics);
- printf("\n语文成绩:");
- scanf("%f",&s->chinese);
- s->sum = s->math + s->english + s->politics + s->chinese;
- printf("\n学号\t姓名\t性别\t数学\t英语\t**\t语文\t总分\n");
- printf("------------------------------------------------------------\n");
- printf("%s\t%s\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",s->num, s->name, s->sex, s->math, s->english, s->politics, s->chinese, s->sum);
- p->next = s;
- p = s;
- s->next = NULL;
- }
- }
- return head;
- }
- //添加学生数据
- void insert(struct student *head)
- {
- struct student *p, *pi;
- char num[6];
- printf("\n请问要在哪个学生后面插入数据(输入学号):");
- scanf("%s",num);
- pi = (struct student *)malloc(LEN);
- p = head;
- printf("\n学号:");
- scanf("%s",&pi->num);
- printf("\n姓名:");
- scanf("%s",&pi->name);
- printf("\n性别:");
- scanf("%s",&pi->sex);
- printf("\n数学成绩:");
- scanf("%f",&pi->math);
- printf("\n英语成绩:");
- scanf("%f",&pi->english);
- printf("\n**:");
- scanf("%f",&pi->politics);
- printf("\n语文成绩:");
- scanf("%f",&pi->chinese);
- pi->sum = pi->math + pi->english + pi->politics + pi->chinese;
- if(head == NULL)
- {
- head = pi;
- pi->next = NULL;
- }
- else
- {
- while((p->num != num) && (p->next != NULL))
- {
- p = p->next;
- }
- if(p->next != NULL)
- {
- pi->next = p->next;
- p->next = pi;
- }
- else
- {
- p->next = pi;
- pi->next = NULL;
- }
- }
- }
- //删除学生数据
- struct student *del(struct student *head, char n[6])
- {
- struct student *p, *q;
- p = head;
- if(head = NULL)
- {
- printf("\n没有学生的资料要删除!");
- return head;
- }
- while(p->num != n && p->next != NULL)
- {
- q = p;
- p = p->next;
- }
- if(p->num == n)
- {
- if(p == head)
- {
- head = p->next;
- }
- else
- {
- q->next = p->next;
-
- }
- free(p);
- }
- else
- {
- printf("\n找不到相应的学生资料!");
- }
- return head;
- }
- //列表
- void list(struct student *head)
- {
- int i = 0;
- struct student *p;
- p = head;
- printf("\n学号\t姓名\t性别\t数学\t英语\t**\t语文\n");
- printf("----------------------------------------------------\n");
- while(p != NULL)
- {
- printf("%s\t%s\t%s\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",p->num, p->name, p->sex, p->math, p->english, p->politics, p->chinese, p->sum);
- p = p->next;
- }
- printf("----------------------------------------------------\n");
- }
- //保存
- void save(struct student *head)
- {
- FILE *fp;
- struct student *p;
- p = head;
- if((fp = fopen("stu_list","wb")) == NULL)
- {
- printf("\n不能打开指定文件!");
- to_menu();
- }
- while(p)
- {
- fwrite(p, sizeof(struct student), 1, fp);
- p = p->next;
- }
- fclose(fp);
-
- }
- //读取
- struct student *read()
- {
- FILE *fp;
- struct student *p;//工作指针
- struct student *last, *head;//最后一项指针
- head = (struct student*)malloc(LEN);
- last = head;
- if((fp = fopen("stu_list","rb")) == NULL)
- {
- printf("\n不能打开指定文件!");
- to_menu();
- }
- while(!feof(fp))
- {
- p = (struct student *)malloc(LEN);
- if(fread(p, sizeof(struct student), 1, fp) == 1)
- {
- last->next = p;
- last = last->next;
- }
- }
- fclose(fp);
- return head = head->next;
- }
- //主函数
- void main()
- {
- char fun;
- int n;
- struct student *head;
- menu();
- printf("\n请输入功能号[0-4]:");
- while((fun = tolower(getchar())) != '0')
- {
- switch(fun)
- {
- case '1':
- {
- printf("\n请输入班级总人数:");
- scanf("%d",&n);
- head = init(n);
- list(head);
- save(head);
- break;
- }
- case '2':
- {
- insert(head);
- list(head);
- save(head);
- break;
- }
- case '3':
- {
- char num[6];
- printf("\n请输入要删除学生的学号:");
- scanf("%s",num);
- head = del(head,num);
- list(head);
- save(head);
- break;
- }
- }
- }
- }
复制代码 不能加入清屏函数 一加入就报错 |
|