这是学习C语言以来的第一个项目,花了我2周时间,前期基本上是在网上参考别人写的代码,然后自己尝试着创建链表,但是毕竟是第一个写的项目,还是挺累的,写完这个项目,从此有了信心写下面和以后所有的项目,自己还是能够干软件这行的。写的时候很辛苦,希望有人看的时候,能发现我的一些小东西吧。
项目资源地址http://download.csdn.net/detail/xx5595480/9158725
代码:
/*
* C语言学生信息管理系统(动态链表版)
* 作者:zhangyonghang
* 时间:2015年
* 功能:增添新键数据、修改删除数据、查询统计数据
* 平台:linux
*/
#include "ssss.h"
int menu_guanliyuan()
{
int a=0;
printf("*---------------------------*\n");
printf("* *\n");
printf("* \033[34;7m学-生-管-理-系-统\033[0m *\n");
printf("* *\n");
printf("* \033[31;1m1.管理员登录\033[0m *\n");
printf("* 2.教师登录 *\n");
printf("* 3.学生登录 *\n");
printf("* 4.退出系统 *\n");
printf("* *\n");
printf("*---------------------------*\n");
printf("请输入功能前的序号登录\n");
while(1){
scanf("%d",&a);
switch (a)
{
case 1:
menu_print1();
menu();
break;
case 2:
menu_print2();
menu();
break;
case 3:
menu_print3();
menu2();
break;
case 4:
break;
default:
printf("数字错误请重新输入\n");
break;
}
if(a==4)
return 0;
setbuf(stdin,NULL);
}
}
struct Student {
char num[10]; /*学号*/
char name[20]; /*姓名*/
char sex[10]; /*性别*/
int age; /*年龄*/
char phone[12];/*电话*/
char qq[12]; /*QQ号*/
float cscore; /*C语言成绩*/
float escore; /*英语成绩*/
struct Student *next;
};
char filename[30];//全局变量,用来保存要打开的文件名字
/*生成链表*/
struct Student *Creat(int n) {
void menu_print_in(void);
struct Student *head;
struct Student *p1, *p2;
system("clear");int i=0;
for(i=1;i<n+1;i++) {
p1 = (struct Student*)malloc(LEN);
menu_print_in();
scanf("%s%s%s%d%s%s%f%f",p1->num,p1->name,p1->sex,
&p1->age,p1->phone,p1->qq,&p1->cscore,&p1->escore);
p1->next = NULL;
if(i==1) {
head = p2 = p1;
}
else {
p2->next = p1;
p2 = p1;
}
}
return(head);
}
/*数据存盘(wb只写)*/
void WriteData_wb(struct Student *head) {
FILE *fp;
struct Student *p;
if((fp = fopen(filename, "wb"))==NULL)
printf("\a error! Can not open the file!");
p = head;
while(p!=NULL) {
if(fwrite(p,LEN,1,fp)!=1) {
printf("写入数据出错\n");
fclose(fp);
return;
}
p=p->next;
}
fclose(fp);
}
/*数据存盘(ab追加)*/
void WriteData_ab(struct Student *head) {
FILE *fp;
struct Student *p;
if((fp = fopen(filename, "ab"))==NULL)
printf("\a error! Can not open the file!");
p = head;
while(p!=NULL) {
if(fwrite(p,LEN,1,fp)!=1) {
printf("写入数据出错\n");
fclose(fp);
return;
}
p=p->next;
}
fclose(fp);
}
/*读取数据*/
/*读取数据文件保存到链表中 ,返回指向此链表头指针*/
struct Student *ReadData(void) {
struct Student *head = NULL;
struct Student *p1, *p2;//s = p1;p = p2;
FILE *fp;
if((fp=fopen(filename,"rb+"))==NULL)
{
printf("打开文件出错\n");
exit(0);
}
while(!feof(fp)) {
if((p1=(struct Student*)malloc(LEN))==NULL){
printf("内存申请出错\n");
fclose(fp);
exit(0);
}
if(fread(p1,LEN,1,fp)!=1){
free(p1);
break;
}
if(head==NULL)
head=p2=p1;
else{
p2->next=p1;
p2=p1;
}
}
fclose(fp);
return (head);
}
/*【1】全量查询*/
void Print_inquire_all(void) {
void menu_print_out(void);
struct Student *pt;
pt = ReadData();
menu_print_out();
do {
printf("%-10s%6s%8s%4d%13s%11s %4.1f %4.1f %4.1f %4.1f\n",
pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->qq,pt->cscore,
pt->escore,(pt->cscore+pt->escore)/2,pt->cscore+pt->escore);
pt = pt->next;
}w