学生学籍管理系统

时间:2015-04-03 13:36:04   收藏:0   阅读:104

                                         <span style="color:#ff0000;">   学生学籍管理系统</span>
       这个项目用了一天半的时间就完成了,一开始我没打算用链表来实现的,但是我自学链表也有一段时间了,想看看自己对链表掌握了多少,所以选择了用链表。第一次用链表写了一个系统,难免会有Bug,希望各位程序猿能指点一下。。。。。

/**********************************************
学生学籍管理系统
每个学生的信息包括:学号、姓名、性别、三门成绩
***********************************************/

#include <iostream>
#include<cstring>
#include <cstdio>
#include <Windows.h>
using namespace std;
const int N=10 ;  //N表示系统最多能记录多少个学生的信息
const int courseNum = 3;  //三门课程
const char courseTitle[courseNum][10] = { "数学", "英语", "体育" }; //三门课程名

struct Date
{
	// 定义年、月、日
	int yesr;
	int month;
	int day;
};

struct CStudent
{
	int id; // 学号,范围1至100
    char  name[10]; // 假设每个人的姓名长度不超过9个字符
	char sex; // 性别,'M'表示男,'F'表示女
	double score[courseNum]; // 每人有三门成绩
	double avescore;//平均成绩
	Date birthDate;
	CStudent *next;
}stud;

int choice;
int n=0;
int getChoice()
{
	system("cls");
	printf("                                当前学生人数:%d\n",n);
	printf("1. 添加学生信息\n");
	printf("2. 显示所有学生信息\n");
    printf("3. 按学号查询某个学生的信息\n");
    printf("4. 按姓名查询某个学生的信息\n");
    printf("5. 按姓名修改某个学生的信息\n");
	printf("6. 按学号删除某个学生的信息\n");
	printf("7. 退出系统\n");
	do
	{
		scanf("%d",&choice);
	} while (choice < 1 || choice > 7);
	return choice;
}

void getAge(const CStudent* s)
{
	SYSTEMTIME time;
	GetLocalTime(&time);
	int year = time.wYear;
	printf("%d\n",time.wYear-s->birthDate.yesr);
}

// 添加一个记录,若添加成功,则函数返回true;
// 否则(数组已满),则返回false
int a[100000];//判断学号是否重复
CStudent *p, *head, *q,*t;
void InPut()
{
	if (n==0)
	{
		head = new CStudent;
		head->next = NULL;
		p = head;
	}
	double sum = 0;
	q = new CStudent;
	cout << "学号(1~100):";
	scanf("%d",&q->id); // 思考:如何添加代码,判断输入学号不重复
	while (!a[q->id])
	{
		if (a[q->id] == 0)
		{
			a[q->id] = 1;
		}
		else
		{
		  scanf("%d",&q->id);
		}
	}
    printf("姓名:");
	 scanf("%s",&q->name);
	 printf("性别(M-男,F-女):");
	 cin.get();
	 scanf("%c",&q->sex);
	 printf("%d",courseNum);
	 printf("门课程成绩:\n");
	for (int i = 0; i < courseNum; i++)
	{
		printf("%s :",courseTitle[i]);
		 scanf("%lf",&q->score[i]);
		sum += q->score[i];
	}
	q->avescore = sum / 3.0;
	printf("请输入出生年月日:");
	scanf("%d %d %d",&q->birthDate.yesr,&q->birthDate.month,&q->birthDate.day);
	printf("\n");
	if (n)
	{
		head=p;
		t = p->next;
		for (int i = 0; i < n; ++i)
		{
			if (q->avescore>t->avescore&&t->next!=NULL)
			{
				head=t;
				t = t->next;
			}
			else if(q->avescore>t->avescore&&t->next==NULL)
			{
				t->next = q;
				t=q;
				t->next = NULL;
				break;
			}
			else
			{
				q->next=t;
				head->next=q;
			}
		}
	}
	else
	{
		head->next = q;
		head = q;
		head->next = NULL;
	}
	n++;  //记录人数+1
	printf("成功添加信息\n");
}
void Sort(CStudent* q)//修改数据后重新按平均分排序
{
	if (n)
	{
		head=p;
		t = p->next;
		for (int i = 0; i < n; ++i)
		{
			if (q->avescore>t->avescore&&t->next!=NULL)
			{
				head=t;
				t = t->next;
			}
			else if(q->avescore>t->avescore&&t->next==NULL)
			{
				t->next = q;
				t=q;
				t->next = NULL;
				break;
			}
			else
			{
				q->next=t;
				head->next=q;
			}
		}
	}
	else
	{
		head->next = q;
		head = q;
		head->next = NULL;
	}
	n++;  //记录人数+1
}
void ShowAllInfo(const CStudent* w)// 显示所有记录
{
	w = w->next;
	int h=0;
	while (w)
    { 
		printf("位置\t学号\t姓名\t性别\t平均分\t年龄\n");
		printf("%d\t",h++);
		printf("%d\t",w->id);
		printf("%s\t",w->name);
		printf("%c\t",w->sex);
		printf("%.2lf\t",w->avescore);
		 getAge(w);
		for (int i=0;i<courseNum;++i)
		{
			printf("%s: ",courseTitle[i]);
			printf("%.1lf\t",w->score[i]);
		}
		printf("\n");
		printf("----\\\n");
		printf("----/\n");
		w=w->next;
	}
}
void findById(const CStudent* w)
{
	int count = 0 ,num,flag=0;
	printf("请输入学生学号:\n");
	scanf("%d",&num);
	w=w->next;
	while (w)
	{
		if (w->id==num)
		{
			flag=1;
			printf("此学生的位置:");
			printf("%d\n",count);
			printf("查找成功!\n");
			break;
		}
		count++;
		w=w->next;
	}
	if (flag==0)
	{
		printf("查找失败!\n");
	}
}
void findByName(const CStudent* w)
{
	int count = 0 ,flag=0;
	char Name[10];
    printf("请输入想要找的姓名:\n");
	scanf("%s",&Name);
	printf("\n");
	w=w->next;
	while (w)
	{
		if (strcmp(w->name,Name)==0)
		{
			flag=1;
			printf("此名学生的位置:%d\n",count);
			break;
		}
		count++;
		w=w->next;
	}
	if (flag==0)
	{
		printf("找不到此名学生学号!\n");
	}
}
void modify(CStudent* w)
{
	int count = 0 ,flag=0;
	char Name[10];
	double sum=0;
    printf("请输入要修改的姓名:");
	scanf("%s",&Name);//修改第N条记录,为name
	cin.get();
	printf("\n");
		while (true&&w!=NULL)
		{
			head=w;
			w=w->next;
			if (strcmp(Name,w->name)==0)
			{
				if (w->next!=NULL)
				{
					head->next=w->next;
				}
				else
				{
					head->next=NULL;
				}
				q=w;
				printf("姓名:");
				scanf("%s",& q->name);
				cin.get();
				printf("性别(M-男,F-女):");
				scanf("%c",& q->sex);
				printf("%d",courseNum);
				printf("门课程成绩:\n");
				for (int i = 0; i < courseNum; i++)
				{
					printf("%s :",courseTitle[i]);
					scanf("%lf",& q->score[i]);
					sum += q->score[i];
				}
				q->avescore = sum / courseNum;
				printf("请输入出生年月日:");
				scanf("%d %d %d",&q->birthDate.yesr,&q->birthDate.month,&q->birthDate.day);
				printf("\n");
				flag=1;
				n--;
				Sort(q);
				printf("修改成功!\n");
				break;
			}
		}
		if (flag==0)
		{
			printf("修改失败!\n");
		}
}
void removeAt(CStudent* w)
{
	//删除第N条记录
	int N,L=0;
	CStudent *k,*H;
	printf("请输入要删除的学号:");
	scanf("%d",&N);
	printf("\n");
	k=p;
	w=w->next;
		while (w)
		{
			if (w->id==N)
			{
				H=w;
				if (w->next==NULL)
				{
					k->next=NULL;
				}
				else
				{
				  k->next=w->next;
				}
				printf("删除成功\n");
				L=1;
				delete H;
				n--;
				break;
			}
		}
		if (L==0)
		{
			printf("删除失败\n");
		}
}
int main()
{
	memset(a, 0, sizeof(a));
	while (true)
	{
		getChoice();
		switch (choice)
		{
		case 1:
			// 添加一个记录,若添加成功,则函数返回true;
			// 否则(数组已满),则返回false
			InPut(); break;
		case 2:
			// 显示所有记录
			ShowAllInfo(p); break;
		case 3:
			// 按学号查找,若记录中存在queryId,则返回该记录在数组中的位置
			// 否则返回-1
			findById(p); break;
		case 4:
			// 按姓名查找,若记录中存在queryName,则返回该记录在数组中的位置
			// 否则返回-1
			findByName(p); break;
		case 5:
			// 修改第p条记录
			modify(p); break;
		case 6:
			// 删除第p条记录
			removeAt(p); break;
		case 7: return 0;
		}
		system("pause");
	}
}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!