⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 task_deeds.cpp

📁 学生成绩管理。 Win32(dos)字符界面。可实现读入、简单计算、统计、输出等操作
💻 CPP
📖 第 1 页 / 共 3 页
字号:

		case 1: cout<<"\n按名字排序结果:"<<endl;
				PrintFormHead(headOfCourse);
				PrintToScreen_Name(headOfStudent);
				break;

		case 2: cout<<"\n按学号排序结果:"<<endl;
				PrintFormHead(headOfCourse);
				PrintToScreen_Number(headOfStudent);
				break;

		case 3: temp=headOfCourse;
				if (temp==NULL)
				{
					cout<<char(7);
					cout<<"\n未输入任何课程信息!不能按课程成绩排序!"<<endl;
					return;
				}
				cout<<"\n已输入的课程有:"<<endl;
				while (temp)
				{
					cout<<temp->courseName<<' ';
					temp=temp->next;
				}
				cout<<"\n请输入需要排序的课程名称,退出请输入 * "<<endl;
				do
				{
					cout<<"请输入:";
					cin>>coursename;
					if (coursename[0]=='*') return;
					temp=headOfCourse;
					while (temp)
					{
						if (strcmp(temp->courseName,coursename)==0)
							break;
						temp=temp->next;
					}
					if (temp==NULL)
					{
						cout<<char(7);
						cout<<"\n该门课程不存在!请重新输入需要排序的课程名称!"<<endl
							<<"结束排序请输入 * "<<endl;
					}
					else
						break;
				}while (1);
				if (headOfStudent->pNameLc)
					Tree(headOfStudent->pNameLc,headOfStudent,coursename,Sort_Score);
				if (headOfStudent->pNameRc)
					Tree(headOfStudent->pNameRc,headOfStudent,coursename,Sort_Score);
				cout<<"\n按"<<coursename<<"成绩排序的结果:"<<endl;
				PrintFormHead(headOfCourse);
				PrintToScreen_Score(headOfStudent);
				break;

		case 0:return;

	}
}

void Tree(student *&head,student *&order_head,char coursename[],void (*function)(student *&pStudent,student *&order_head,char coursename[]))
{
	if (head->pNameLc)
		Tree(head->pNameLc,order_head,coursename,(*function));

	(*function)(head,order_head,coursename);

	if (head->pNameRc)
		Tree(head->pNameRc,order_head,coursename,(*function));
}

void Sort_Score(student *&pStudent,student *&order_head,char coursename[])
{
	courseOfStudent *temp1=pStudent->courses,*temp2=order_head->courses;
	while (temp1)
	{
		if (strcmp(temp1->courseName,coursename)==0)
			break;
		temp1=temp1->next;
		temp2=temp2->next;
	}
	if (temp1->score>=temp2->score)
	{
		if (order_head->pScoreRc)
			Sort_Score(pStudent,order_head->pScoreRc,coursename);
		else
			order_head->pScoreRc=pStudent;
	}
	else
	{
		if (order_head->pScoreLc)
			Sort_Score(pStudent,order_head->pScoreLc,coursename);
		else
			order_head->pScoreLc=pStudent;
	}
}

void CalculateGPA(student *&headOfStudent,courselist *&headOfCourse)
{
	short i;
	char s[25];
	student *pStudent,*code;
	while (1)
	{
		cout<<" 1. 输入学生的名字"<<endl
			<<" 2. 输入学生的学号"<<endl
			<<" 0. 返回上一级菜单"<<endl
			<<"请选择: ";
		cin>>i;
		while (cin.fail() || (i>2 || i<0))
		{
			cout<<char(7);
			cout<<"\n输入错误 !"<<endl
				<<"输入菜单中您要选择的项目前的序号即可,即0-2的数字"<<endl
				<<"请选择: ";
			cin.clear();
			cin.ignore(100,'\n');
			cin>>i;
		}
		switch (i)
		{
			case 1: do
					{
						cout<<"\n请输入要查询的学生的名字,退出请输入 * "<<endl;
						cin>>s;
						if (s[0]=='*') break;
						pStudent=SearchByName(headOfStudent,s,code);
						if (pStudent==NULL)
						{
							cout<<char(7);
							cout<<"\n没有该学生的纪录!"<<endl;
							continue;
						}
						CalcGPA(pStudent,headOfCourse);
						cout<<endl<<pStudent->studentName<<"的平均绩点为 "
							<<setiosflags(ios::fixed)<<setprecision(4)<<pStudent->studentGPA<<endl;
					}while (1);
					break;
				
			case 2: do
					{
						cout<<"\n请输入要查询的学生的学号,退出请输入 * "<<endl;
						cin>>s;
						if (s[0]=='*') break;
						pStudent=SearchByNumber(headOfStudent,s,code);
						if (pStudent==NULL)
						{
							cout<<char(7);
							cout<<"\n没有该学生的纪录!"<<endl;
							continue;
						}
						CalcGPA(pStudent,headOfCourse);
						cout<<endl<<pStudent->studentName<<"的平均绩点为 "
							<<setiosflags(ios::fixed)<<setprecision(4)<<pStudent->studentGPA<<endl;
					}while (1);
					break;

			case 0: return;
		}			
	}
}

void CalcGPA(student *&pStudent,courselist *&headOfCourse)
{
	courselist *temp=headOfCourse;
	courseOfStudent *p=pStudent->courses;
	float sum_credits=0.0f,sum_score=0.0f;
	while (p)
	{
		if (p->score>=0)
		{
			sum_credits+=temp->credits;
			sum_score+=ScoreToGPA(p->score)*temp->credits;
		}
		p=p->next;
		temp=temp->next;
	}
	if (sum_credits)
		pStudent->studentGPA=sum_score/sum_credits;
	else
		pStudent->studentGPA=0.0f;
}

float ScoreToGPA(float score)
{
	if (score>=90) return 4.0f;
	if (score>=85) return 3.7f;
	if (score>=82) return 3.3f;
	if (score>=78) return 3.0f;
	if (score>=75) return 2.7f;
	if (score>=72) return 2.3f;
	if (score>=68) return 2.0f;
	if (score>=64) return 1.5f;
	if (score>=60) return 1.0f;
	return 0.0f;
}

void Statistic(student *&head,courselist *&headOfCourse)
{
	char coursename[20];
	courselist *temp=headOfCourse;
	if (temp==NULL)
	{
		cout<<char(7);
		cout<<"\n未输入任何课程信息!不能进行统计!"<<endl;
		return;
	}
	cout<<"\n已输入的课程有:"<<endl;
	while (temp)
	{
		cout<<temp->courseName<<' ';
		temp=temp->next;
	}
	cout<<"\n请输入要统计的课程名称:";
	while (cin>>coursename)
	{
		temp=headOfCourse;
		while (temp)
		{
			if (strcmp(temp->courseName,coursename)==0)
				break;
			temp=temp->next;
		}
		if (temp==NULL)
		{
			cout<<char(7);
			cout<<"\n该门课程不存在!请重新输入需要排序的课程名称!"<<endl
				<<"\n已经输入的课程有:"<<endl;
			temp=headOfCourse;
			while (temp)
			{
				cout<<temp->courseName<<' ';
				temp=temp->next;
			}
			cout<<"\n请输入:";
		}
		else
			break;
	}

	short count[5]={0};
	Stat(head,count,coursename);

	cout<<"\n <"<<coursename<<">  统计结果:"<<endl;
	short i,j,max=0;
	double height[5],k;
	for (i=0;i<5;i++)
	{
		height[i]=count[i];
		if (max<count[i]) max=count[i];
	}
	if (max>0)
	{
		for (i=0;i<5;i++)
			height[i]*=8.0/max;
	}
	else
	{
		for (i=0;i<5;i++)
			height[i]=0.0f;
	}
	max=8;
	cout<<endl<<"人数"<<endl<<"┃"<<endl;
	for (i=max+1;i>0;i--)
	{
		cout<<"┃";
		for (j=0;j<5;j++)
		{
			cout<<"    ";
			if (i>height[j] && i<=height[j]+1)
			{
				cout<<setw(4)<<count[j];
			}
			else
			if (i<=height[j])
			{
				k=floor(height[j]);
				if (i<k)
				{
					cout<<"██";
					continue;
				}
				if (height[j]-k==0)
				{
					cout<<"██";
				}
				else
				if (height[j]-k<=0.125)
				{
					cout<<"▁▁";
				}
				else
				if (height[j]-k<=0.25)
				{
					cout<<"▂▂";
				}
				else
				if (height[j]-k<=0.375)
				{
					cout<<"▃▃";
				}
				else
				if (height[j]-k<=0.5)
				{
					cout<<"▄▄";
				}
				else
				if (height[j]-k<=0.625)
				{
					cout<<"▅▅";
				}
				else
				if (height[j]-k<=0.75)
				{
					cout<<"▆▆";
				}
				else
				if (height[j]-k<=0.875)
				{
					cout<<"▇▇";
				}
				else
				{
					cout<<"██";
				}
			}
			else
			{
				cout<<"    ";
			}

		}
		cout<<endl;
	}
	cout<<"┗";
	for (i=0;i<22;i++)
		cout<<"━";
	cout<<"分数段"<<endl;
	cout<<"     100-90   90-80   80-70   70-60   60以下 "<<endl;
}

void Stat(student *&head,short count[],char coursename[])
{
	if (head->pNameLc)
		Stat(head->pNameLc,count,coursename);

	courseOfStudent *temp=head->courses;
	while (temp)
	{
		if (strcmp(temp->courseName,coursename)==0)
		{
			if (temp->score>=90) count[0]++;
			else
			if (temp->score>=80) count[1]++;
			else
			if (temp->score>=70) count[2]++;
			else
			if (temp->score>=60) count[3]++;
			else
			if (temp->score>=0) count[4]++;
			break;
		}
		temp=temp->next;
	}

	if (head->pNameRc)
		Stat(head->pNameRc,count,coursename);
}

void PrintToScreen_Name(student *&head)
{
	if (head->pNameLc)
	{
		PrintToScreen_Name(head->pNameLc);
	}

	cout<<setw(10)<<head->studentName
		<<setw(10)<<head->studentNumber;
	courseOfStudent *p=head->courses;
	while (p)
	{
		if (p->score>=0)
			cout<<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<p->score;
		else
			cout<<"--        ";
		p=p->next;
	}
	cout<<endl;

	if (head->pNameRc)
	{
		PrintToScreen_Name(head->pNameRc);
	}
}

void PrintToScreen_Number(student *&head)
{
	if (head->pNumberLc)
	{
		PrintToScreen_Number(head->pNumberLc);
	}

	cout<<setw(10)<<head->studentName
		<<setw(10)<<head->studentNumber;
	courseOfStudent *p=head->courses;
	while (p)
	{
		if (p->score>=0)
			cout<<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<p->score;
		else
			cout<<"--        ";
		p=p->next;
	}
	cout<<endl;

	if (head->pNumberRc)
	{
		PrintToScreen_Number(head->pNumberRc);
	}
}

void PrintToScreen_Score(student *&head)
{
	if (head->pScoreRc)
	{
		PrintToScreen_Score(head->pScoreRc);
	}

	cout<<setw(10)<<head->studentName
		<<setw(10)<<head->studentNumber;
	courseOfStudent *p=head->courses;
	while (p)
	{
		if (p->score>=0)
			cout<<setw(10)<<setiosflags(ios::fixed)<<setprecision(2)<<p->score;
		else
			cout<<"--        ";
		p=p->next;
	}
	cout<<endl;

	if (head->pScoreLc)
	{
		PrintToScreen_Score(head->pScoreLc);
	}
	head->pScoreLc=NULL;
	head->pScoreRc=NULL;
}

void PrintSingleStudent(student *&temp)
{
	cout<<"\n姓 名:"<<temp->studentName<<endl
		<<"学 号:"<<temp->studentNumber<<endl;
	courseOfStudent *p=temp->courses;
	bool flag=true;
	while (p)
	{
		if (p->score>0)
		{
			if (flag)
				cout<<"各科成绩:"<<endl;
			cout<<setw(10)<<setiosflags(ios::left)<<p->courseName
				<<setiosflags(ios::fixed)<<setprecision(2)<<p->score<<endl;
			flag=false;
		}
		p=p->next;
	}
	if (flag)
		cout<<char(7)<<"该生的成绩还未录入!"<<endl;
}

void PrintFormHead(courselist *courses)
{
	cout<<"\n姓  名    学  号    ";
	while (courses)
	{
		cout<<setw(10)<<setiosflags(ios::left)<<courses->courseName;
		courses=courses->next;
	}
	cout<<endl;
}

void Free_Student(student *&head)
{
	if (head->pNameLc)
		Free_Student(head->pNameLc);
	if (head->pNameRc)
		Free_Student(head->pNameRc);
	delete head;
}

void Free_Courselist(courselist *&head)
{
	courselist *temp;
	while (head)
	{
		temp=head;
		head=head->next;
		delete temp;
	}
}

void Exit(student *&headOfStudent,courselist *&headOfCourse)
{
	short i;
	cout<<" 1. 退出"<<endl
		<<" 2. 显示菜单"<<endl
		<<"请选择: ";
	cin>>i;
	while (cin.fail() || (i!=1 && i!=2))
	{
		cout<<char(7);
		cout<<"\n输入错误 !"<<endl
			<<"输入菜单中您要选择的项目前的序号即可,即输入 1 或 2"<<endl
			<<"请选择: ";
		cin.clear();
		cin.ignore(100,'\n');
		cin>>i;
	}
	if (i==1)
	{
		char filename[30];
		fileOut(headOfStudent,headOfCourse,filename);
		if (headOfStudent)
			Free_Student(headOfStudent);
		if (headOfCourse)
			Free_Courselist(headOfCourse);
		if (filename[0]) 
			StoreSucceed(filename);
		GoodBye();
		exit(0);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -