C语言实现——————“学生成绩管理系统”

话不多说,代码附上:

#include
#include
#include
struct Student
{
	char num[20];                             //学号
	char name[20];                              //姓名
	char sex[5];                                //性别
	char address[80];                           //家庭住址
	struct Student *next;                      //动态链表连接下一个  int *p 与上面对比 方便思考      
};
struct Student *creat(struct Student *head0)                          //定义了一个返回值为结构体类型的指针变量
{
	struct Student *head1, *p1, *p2, *t0, *t1;
	int m, n = 1, h;
	FILE *fp;
	printf("请选择:");
	printf("1.继续上次输入:    2.重新输入:");
	scanf("%d",&h);
	if (h==1)
	{
		if(head0 == NULL)
		{
			printf("\n\n上次无学生输入!!!\n");
			return NULL;
		}
		t0 = head0;
		while (t0 != NULL)
		{
//			printf("进入head循环"); 
//			printf("学生学号:%s\t学生姓名:%s\t学生性别:%s\t学生家庭地址:%s\n", t->num, t->name, t->sex, t->address);
			t1 = t0;
			t0 = t0->next;
		}
		p1 = p2 = (struct Student*)malloc(1 * sizeof(struct Student));
		if (p1 == NULL)
		{
			printf("内存开辟失败!\n");
			return 0;
		}
		t1->next = p2;
		printf("请输入学生学号:");
		scanf("%s", p2->num);
		printf("请输入学生姓名:");
		scanf("%s", p2->name);
		printf("请输入学生性别:");
		scanf("%s", p2->sex);
		printf("请输入学生家庭地址:");
		scanf("%s", p2->address);
		printf("结束请按    '0'    继续请按     '1'    :");   //判断是否继续输入的标准
		scanf("%d", &m);
		printf(" t->name    %s\n", t1->next->name);
		while (m != 0)
		{
			printf("--------------------------已输入了    %d      名学生的信息-----------------------\n", n);
			n = n + 1;
			p1 = (struct Student*)malloc(1 * sizeof(struct Student));
			if (p1 == NULL)
			{
				printf("内存开辟失败!\n");
				return 0;
			}
			p2->next = p1;
			p2 = p1;
			printf("请输入学生学号:");
			scanf("%s", (*p1).num);
			printf("请输入学生姓名:");
			scanf("%s", p1->name);
			printf("请输入学生性别:");
			scanf("%s", p1->sex);
			printf("请输入学生家庭地址:");
			scanf("%s", p1->address);
			printf("结束请按   '0'    继续请按    '1'    :");
			scanf("%d", &m);
		}
		p1->next = NULL;    //使链表最后一位的next指向为空
		printf("--------------------------共输入了    %d      名学生的信息-----------------------\n", n);
		return(head0);     // 返回头
		}
	if (h==2)
	{
	head1 = p1 = p2 = (struct Student*)malloc(1 * sizeof(struct Student));
	if (p1 == NULL)
	{
		printf("内存开辟失败!\n");
		return 0;
	}
	printf("请输入学生学号:");
	scanf("%s", p1->num);
	printf("请输入学生姓名:");
	scanf("%s", p1->name);
	printf("请输入学生性别:");
	scanf("%s", p1->sex);
	printf("请输入学生家庭地址:");
	scanf("%s", p1->address);
	printf("结束请按    '0'    继续请按     '1'    :");   //判断是否继续输入的标准
	scanf("%d", &m);
	while (m != 0)
	{
		printf("--------------------------已输入了    %d      名学生的信息-----------------------\n", n);
		n = n + 1;
		p1 = (struct Student*)malloc(1 * sizeof(struct Student));
		if (p1 == NULL)
		{
			printf("内存开辟失败!\n");
			return 0;
		}
		p2->next = p1;
		p2 = p1;
		printf("请输入学生学号:");
		scanf("%s", (*p1).num);
		printf("请输入学生姓名:");
		scanf("%s", p1->name);
		printf("请输入学生性别:");
		scanf("%s", p1->sex);
		printf("请输入学生家庭地址:");
		scanf("%s", p1->address);
		printf("结束请按   '0'    继续请按    '1'    :");
		scanf("%d", &m);
	}
	p2->next = NULL;    //使链表最后一位的next指向为空
	printf("--------------------------共输入了    %d      名学生的信息-----------------------\n", n);
	head0 = head1;
	return(head0);     // 返回头
	}
}
void find(struct Student *find1)   // 查询信息
{
	if(find1 == NULL) 
	{
		printf("\n\n\n无学生信息!!!\n"); 
		return ;
	}
	struct Student *p, *p1;
	int f, o = 0, e = 0;
	char a[20], x[20];
	p = p1 = find1;
	printf("1.按学号查询    2.按姓名查询\n");
	printf("请选择查询方式:");
	scanf("%d", &f);
	if (f == 1)
	{
		printf("请输入学生学号:");
		scanf("%s", a);
		do
		{
			if (strcmp(a, p1->num) == 0)
			{
				o = 1;
				printf("已找到的学生信息如下:\n");
				printf("学号:%s\n姓名:%s\n性别:%s\n家庭地址:%s\n", p1->num, p1->name, p1->sex, p1->address); break;
			}
			p1 = p1->next;
		} while (p1 != NULL);
		if (o == 0) printf("未找到该同学\n ");
	}
	if (f == 2)
	{
		printf("请输入学生姓名:");
		scanf("%s", x);
		do
		{
			if (strcmp(x, p->name) == 0)
			{
				e = 1;
				printf("已找到的学生信息如下:\n");
				printf("学号:%s\n姓名:%s\n性别:%s\n家庭地址:%s\n", p->num, p->name, p->sex, p->address); break;
			}
			p = p->next;
		} while (p != NULL);
		if (e == 0) printf("未找到该同学\n");
	}
}
struct Student *del(struct Student *del1)      //删除程序
{
	if(del1 == NULL) 
	{
		printf("\n\n\n无学生信息!!!\n"); 
		return 0;
	}
	struct Student *p, *p1, *p2, *p3;
	int a, d = 0, e = 0;
	char b[20], c[20];
	printf("可根据以下方式进行删除:\n");
	printf("1.学号     2.姓名\n请选择:");
	scanf("%d", &a);
	if (a == 1)
	{
		p = p1 = del1;
		printf("请输入学生学号:");
		scanf("%s", b);
		do
		{
			if (strcmp(b, p->num) == 0 || p == del1)
			{
				d = 1;
				del1 = del1->next;
				printf("已删除!!!\n"); break;
			}
			if (strcmp(b, p->num) == 0)
			{
				d = 1;
				p1->next = p->next;
				printf("已删除!!!\n"); break;
			}
			p1 = p;
			p = p->next;
		} while (p != NULL);
		if (d == 0) printf("未找到该同学\n");
	}
	if (a == 2)
	{
		p = p1 = del1;
		printf("请输入学生姓名:");
		scanf("%s", c);
		do
		{
			if (strcmp(c, p->name) == 0 || p == del1)
			{
				e = 1;
				del1 = del1->next;
				printf("已删除!!!\n"); break;
			}
			if (strcmp(c, p->name) == 0)
			{
				e = 1;
				p1->next = p->next;
				printf("已删除!!!\n"); break;
			}
			p1 = p;
			p = p->next;
		} while (p != NULL);
		if (e == 0) printf("未找到该同学\n");
	}
	if(del1 == NULL) printf("\n\n\n学生信息已删光!!!\n");
	return (del1);
}
struct Student *change(struct Student *change1)     //修改程序
{
	if(change1 == NULL) 
	{
		printf("\n\n\n无学生信息!!!\n"); 
		return NULL;
	}
	struct Student *p, *p1;
	int f, i, j, o = 0, q = 0;
	char a[20], x[20];
	p = p1 = change1;
	printf("可查找的方式如下:");
	printf("1.按学号    2.按姓名\n");
	printf("请选择查找方式:");
	scanf("%d", &f);
	if (f == 1)
	{
		printf("请输入学生学号:");
		scanf("%s", a);
		while (p1 != NULL)
		{
			if (strcmp(a, p1->num) == 0)
			{
				o = 1;
				printf("已找到的学生信息如下:\n");
				printf("学号:%s\n姓名:%s\n性别:%s\n家庭地址:%s\n", p1->num, p1->name, p1->sex, p1->address);
				printf("可修改的选项如下:");
				printf("1.学号    2.姓名    3.性别    4.家庭地址    \n");
				printf("请输入选择的选项:");
				scanf("%d", &i);
				switch (i)
				{
				case 1:printf("请输入修改后的学号:"); scanf("%s", (*p1).num); printf("已修改!!!\n"); break;
				case 2:printf("请输入修改后的姓名:"); scanf("%s", (*p1).name); printf("已修改!!!\n"); break;
				case 3:printf("请输入修改后的性别:"); scanf("%s", (*p1).sex); printf("已修改!!!\n"); break;
				case 4:printf("请输入修改后的家庭地址:"); scanf("%s", (*p1).address);  printf("已修改!!!\n"); break;
				default:printf("输入有误,请重新输入:\n"); break;
				}
			}
			p1 = p1->next;
		}
		if (o == 0) printf("未找到该同学\n ");
	}
	if (f == 2)
	{
		printf("请输入学生姓名:");
		scanf("%s", x);
		do
		{
			if (strcmp(x, p->name) == 0)
			{
				q = 1;
				printf("已找到的学生信息如下:\n");
				printf("学号:%s\n姓名:%s\n性别:%s\n家庭地址:%s\n", p->num, p->name, p->sex, p->address);
				printf("可修改的选项如下:");
				printf("1.学号    2.姓名    3.性别    4.家庭地址    \n");
				scanf("%d", &i);
				switch (i)
				{
				case 1:printf("请输入修改后的学号:"); scanf("%s", (*p1).num); printf("已修改!!!\n"); break;
				case 2:printf("请输入修改后的姓名:"); scanf("%s", (*p1).name); printf("已修改!!!\n"); break;
				case 3:printf("请输入修改后的性别:"); scanf("%s", (*p1).sex); printf("已修改!!!\n"); break;
				case 4:printf("请输入修改后的家庭地址:"); scanf("%s", (*p1).address);  printf("已修改!!!\n"); break;
				default:printf("输入有误,请重新输入:\n"); break;
				}
			}
			p = p->next;
		} while (p != NULL);
		if (q == 0) printf("未找到该同学\n");
	}


	return (change1);
}
void put(struct Student *put1)   //输出信息
{
	if(put1 == NULL) 
	{
		printf("\n\n\n无学生信息!!!\n"); 
		return ;
	}
	struct Student *t;
	t = put1;
	do
	{
		printf("学生学号:%s\t学生姓名:%s\t学生性别:%s\t学生家庭地址:%s\n", t->num, t->name, t->sex, t->address);
		t = t->next;
	} while (t != NULL);
	//printf("输出无问题"); 
	printf("\n\n\n");
}
void write(struct Student *p0)       //写入文件
{
	int m = 0, n = 0;
	//	printf("\n本次无新学生信息存储!!!\n"); 
	FILE *fp;
	if(p0 == NULL) 
	{
		printf("\n\n\n存储信息为空!!!\n");
		if ((fp = fopen("stu.dat", "wb+")) == NULL)
		{
			printf("\n\n\n删除存储文件失败!!!\n");
			exit(0);
		}
		return; 
	}
	if ((fp = fopen("stu.dat", "wb+")) == NULL)
	{
		printf("\n\n\n文件打开失败!!!\n");
		return;
	}
	do
	{
		if ((fwrite(p0, sizeof(struct Student), 1, fp) != 1)) printf("\n\n\n文件写入失败!!!\n");
		else n++;
		p0 = p0->next;
		m++;
	} while (p0 != NULL );
	printf("\n\n\n应写入%d名学生信息,已写入%d名学生的信息!!!\n", m, n);
	fclose(fp);
}
struct Student *read()   //    读取链表
{
//	printf("\n本次读取学生信息!!!\n"); 
	struct Student  *p0, *p1, *head;
	FILE *fp;
	if ((fp = fopen("stu.dat", "rb+")) == NULL)
	{
		printf("\n\n\n文件打开失败!!!\n");
		return NULL;
	}
	rewind(fp);
    head = p0 = p1 = (struct Student*)malloc(1 * sizeof(struct Student));
	if (p1 == NULL)
	{
		printf("内存开辟失败!\n");
		return 0;
	}
	do 
	{
		//p0=p2->next;
		p1->next = p0;
		p1 = p0;
		if ((fread(p1, sizeof(struct Student), 1, fp) != 1))
		{
			 printf("\n\n\n文件为空,请先存入信息再读取!!!\n\n\n");
			 return NULL;
		}
//		else
//		{
//			printf("学生学号:%s\t学生姓名:%s\t学生性别:%s\t学生家庭地址:%s\n", p2->num, p2->name, p2->sex, p2->address);
//		}
		p0 = (struct Student*)malloc(1 * sizeof(struct Student));
		if (p0 == NULL)
		{
			printf("内存开辟失败!\n");
			return 0;
		}
	} while (p1->next != NULL  );
	p1->next = NULL;
	free(p0);
	fclose(fp);
	//put(head);
	return head;
}
int choose(struct Student *p1)   //功能菜单选择
{
	struct Student *head;
	int i;
	p1 = NULL;
	head = NULL;
	printf("\n\n\t        ****************欢迎使用学生信息管理系统**************\n\n");
	printf("\n\t\t*----------------|可选择的操作类型如下|--------------*\n\t\t*\t\t\t\t\t\t     *\n");
	printf("\t\t*\t1.录入学生信息    \t2.查询学生信息\t     *\n\t\t*\t\t\t\t\t\t     *\n\t\t*\t3.删除学生信息  \t4.修改学生信息\t     *\n\t\t*\t\t\t\t\t\t     *\n\t\t*\t5.输出学生信息      \t6.结束管理系统\t     *\n\t\t*\t\t\t\t\t\t     *\n");
	printf("\t\t*----------------------------------------------------*");
	printf("\n\t\t\t        请输入所选择的操作:");
	scanf("%d", &i);
	printf("\t\t\t      ----------------------\n");
	switch (i)
	{
	case 1:printf("现在开始输入:\n"); head = read(); head = creat(head);write(head); break;
	case 2:printf("现在开始查询:\n"); head = read(); find(head); break;
	case 3:printf("现在开始删除:\n"); head = read(); head = del(head); write(head); break;
	case 4:printf("现在开始修改:\n"); head = read(); head = change(head); write(head); break;
	case 5:printf("现在开始输出:\n"); head = read(); put(head); break;
	case 6:printf("结束系统!!!   \n"); return 0;
	default:printf("输入有误,请重新输入:\n"); break;
	}
	return 1;
}
int main()
{
	struct Student *p1 = NULL;
	while(choose(p1))
	{
		//choose(); // 进行功能的选择
    	//read();           //读取链表  
		//put(head);       // 将链表输出
	} 
	return 0;
}




由于程序用到了文件的存取,所以第一次编译会存在文件打开失败的情况,可以屏蔽错误信息输出,或者进行第二次编译即可。整个课程设计是用C语言写的,里面还包括的文件的读取部分,代码可能不够精简,思想也不够完善,里面的注释部分是我调试的时候使用过的,如果有需要的朋友可以参考一下,同时如果有什么好的建议希望大家可以发到评论里,我们共同进步!!!谢谢!!!

你可能感兴趣的:(C)