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

📄 test.cpp

📁 增加学生信息、删除学生信息、修改学生信息 查询时可实现按姓名查询、按学号查询; 能对用户指定的任意课程名
💻 CPP
字号:
#include <iostream>
#include "linkstudent.h"
#include "student.h"
#include "liststudent.h"
#include "mainmenu.h"
#include "findmenu.h"
const static NAME = 40;
const static SNO = 20;
using namespace std;

void main()
{	
	//创建学生信息
	//	Student stu[6] = {
	//		Student("cd","1000",23,'m'),
	//			Student("ab","1002",25,'m'),
	//			Student("jk","1004",24,'m'),
	//			Student("baby","1005",22,'f'),
	//			Student("gf","1007",23,'f'),
	//			Student("sakura","1006",22,'f')
	//	};
	//添加学生成绩
	//	stu[0].set_pfm(80,90,56);
	//	stu[1].set_pfm(75,68,63);
	//	stu[2].set_pfm(90,98,86);
	//	stu[3].set_pfm(76,80,84);
	//	stu[4].set_pfm(60,56,38);
	//	stu[5].set_pfm(78,88,98);
	
	Liststudent list;//学生链表
	
	//	Linkstudent link_stu;   //
	
	
	//添加学生信息到链表
	//	for(int i = 0; i < 5; i++)
	//	list.insert(stu[i]);
	//	list.append(stu[5]);
	
	list.read_stuInfo(); //从studentInfo.txt文件中读取数据,并将其加入到list链表
	//进入主菜单循环
	while(true )
	{
		m_menu();//显示主菜单
		char option; //主菜单选项
		cout << "Enter your choice(q to quit):";
		cin >> option;
		if (option == 'q')
			break;			//退出主程序
		switch(option)
		{
			////////////////////当选查看学生信息的操作////////////////
		case '1':	
			{	
				while(true)
				{	
					mfindstu();
					char op;
					cout << "Enter your choice(q to 上一层):";
					cin >> op;
					if('q' == op)
						break;
					cin.get();
					if(op == '1')
						list.show_all();
					else if('2' == op)
					{	
						char na[NAME];
						cout << "\n请输入待查学生姓名(quit to quit):";
						cin.getline(na,NAME);
						
						while(strcmp(na,"quit"))
						{
							Linkstudent * t = list.find_n(na);
							if(t != NULL)
								t->stu.showall();	
							else cout<<" \nthere isn't the student!"<<endl;
							cin.clear();
							cin.sync();
							cout << "请输入待查学生姓名(quit to quit):" << endl;
							cin>>na;
							cin.get();
						}//end while
						
					}//end if
					else if('3' == op)
					{
						char snu[20];
						cout << "请输入待查学生学号:";
						cin>>snu;
						cin.get();
						while(strcmp(snu,"quit"))
						{
							Linkstudent * t = list.find_sn(snu);
							if(t != NULL)
								t->stu.showall();
							
							else cout<<" there isn't the student!"<<endl;
							cin.clear();
							cin.sync();
							cout << "Enter quit to quit:";
							cin>>snu;
							cin.get();
						}
					}//endif			
				} //end while
			}   //end case 1
			
			break;								
			/////////////////////修改学生信息的操作////////////////////////////	    
		case '2':
			{
				cin.get();
				char modify_sn[SNO];
				list.show_all();
				cout << endl << endl;
				cout << "请输入所修改学生学号:";
				
				cin.getline(modify_sn,SNO);
				Linkstudent *modify_st = list.find_sn( modify_sn );
				if(modify_st)
				{
					
					cout << "所修改学生信息为:" << endl;
					modify_st->stu.showall();
					cout << endl << endl;
					cout << "请选择所修改信息:" << endl << endl;
					cout << "1 学号\t2 姓名\t3 性别\t4 年龄\t5 语文\t6 数学\t7 英语"<<endl<<endl;
					cout << "(q to quit)!:";
					
					while(true)
					{
						char modify_op;
						cin >> modify_op;
						
						if('q' == modify_op)
							break;
						//while(cin && cin.get() != '\n')
						//continue;
						
						switch(modify_op)
						{
						case '1':
							{
								
								char modi_sn[SNO];
								cin.get();
								cout << "请输入新学号:";
								cin.getline(modi_sn,SNO);
								modify_st->stu.set_sn(modi_sn);
								cout << "修改后学生信息为:\n";
								modify_st->stu.showall();
								cout << endl << endl;
								
							}break;
							
						case '2':
							{
								char modi_n[NAME];
								cin.get();
								cout << "请输入新姓名:";
								cin.getline(modi_n,NAME);
								modify_st->stu.set_name(modi_n);
								cout <<endl;
								cout << "修改后学生信息为:\n";
								modify_st->stu.showall();
								cout << endl;
								
							}break;
						case '3':
							{
								
								
								while(true)
								{
									cin.get();
									cout << "请输入新性别(f or m):";
									char modi_s;
									cin >> modi_s;
									if(modi_s == 'f' || modi_s == 'm')
									{
										cout << endl;
										modify_st->stu.set_sex(modi_s);
										cout << "修改后学生信息为:\n";
										modify_st->stu.showall();
										cout << endl << endl;
										break;
									}
									
								}//end while
								
							}break; //end case '3'
						case '4':
							{
								while(true)
								{
									cin.get();
									cout << "请输入新年龄(2 to 98):";
									int modi_a;
									cin >> modi_a;
									if(modi_a >= 2 && modi_a <=98)
									{
										modify_st->stu.set_age(modi_a);
										modify_st->stu.showall();
										break;
									}
								}// end while
							}break;//end case '4'
						case '5':
							{
								while(true)
								{
									cin.get();
									cout << "请输入新语文成绩(0 to 150):";
									int modi_y;
									cin >> modi_y;
									if(modi_y >= 0 && modi_y <=150)
									{
										modify_st->stu.set_chinese(modi_y);
										modify_st->stu.showall();
										break;
									}
								}// end while
							}break;//end case '5'
						case '6':
							{
								while(true)
								{
									cin.get();
									cout << "请输入新数学成绩(0 to 150):";
									int modi_m;
									cin >> modi_m;
									if(modi_m >= 0 && modi_m <=150)
									{
										modify_st->stu.set_math(modi_m);
										modify_st->stu.showall();
										break;
									}
								}// end while
							}break;//end case '6'
						case '7':
							{
								while(true)
								{
									cin.get();
									cout << "请输入新英语成绩(0 to 150):";
									int modi_e;
									cin >> modi_e;
									if(modi_e >= 0 && modi_e <=150)
									{
										modify_st->stu.set_english(modi_e);
										modify_st->stu.showall();
										break;
									}
								}// end while
							}break;//end case '7'
						}//end switch
						cout << "请选择所修改信息:" << endl;
						cout << "1 学号\t2 姓名\t3 性别\t4 年龄\t5 语文\t6 数学\t7 英语"<<endl;
						cout << "(q to quit)!:";
						
					}// end while
				}//end if
				}//end case 2
				break;
				
				/////////////////////////添加学生信息的操作////////////////////////////
			case '3':
				{ 
					char insert_sn[SNO];
					char insert_n[NAME];
					int   insert_a;
					char   insert_s;
					int	  insert_y = 0;
					int   insert_m = 0;
					int   insert_e = 0;
					while(cin && cin.get() != '\n')
						cin.get();
					cout << "请输入学号:" ;
					cin.getline(insert_sn,SNO);
					
					while( list.find_sn(insert_sn) )
					{
						cout << "该学号已存在,请重新输入:";
						cin.getline(insert_sn,SNO);
					}
					
					cout << "请输入姓名:";
					cin.clear();
					cin.sync();
					cin.getline(insert_n,NAME);
					cout << "请输入年龄:";
					cin >> insert_a;
					
					while(insert_a < 2 || insert_a > 98)
					{
						cout << "该年龄无效,请重新输入:";
						cin.clear();
						cin.sync();
						cin >> insert_a;
					}
					cout << "请输入性别(M or F):";
					cin >> insert_s;
					while(insert_s != 'F' && insert_s != 'M')
					{
						cout << "再仔细想一下你的性别吧(M or F):";
						cin.clear();
						cin.sync();
						cin >> insert_s;
					}
					char answer;
					cout << "要输入分数么?(Y or N)";
					cin >> answer;
					while(true)
					{
						if( answer == 'y')
						{
							cin.clear();
							cin.sync();
							cout << "请输入语文分数(0~150):";
							cin >> insert_y;
							while(insert_y < 0  || insert_y > 150)
							{
								
								cout << "分数不对,请重新输入!(0~150):";
								cin.clear();
								cin.sync();
								cin >> insert_y;
							}
							
							cout << "请输入数学分数(0~150):";
							cin >> insert_m;
							while(insert_m < 0  || insert_m > 150)
							{
								cout << "分数不对,请重新输入!(0~150):";
								cin.clear();
								cin.sync();
								cin >> insert_m;
							}
							
							cout << "请输入英语分数(0~150):";
							cin >> insert_e;
							
							while(insert_e < 0  || insert_e > 150)
								
							{
								
								
								cout << "分数不对,请重新输入!(0~150):";
								cin.clear();
								cin.sync();
								cin >> insert_e;
								
							}
							break;
						}//endif
						else if( answer == 'n')
						{
							cin.clear();
							cin.sync();
							break;
						}
						cout << "y or n:";
						cin.clear();
						cin.sync();
						cin >> answer;
					}//end while
					Student insert_stu(insert_n,insert_sn,insert_a,insert_s,insert_y,insert_m,insert_e);
					list.insert(insert_stu);
					cout << "所添加学生信息是:" << endl;
					insert_stu.showall();	
				}  //end case 3
				
				break;
				////////////////////////删除学生信息的操作////////////////////////////
			case '4':
				{
					cin.get();
					int dele_op;
					cout << "1 按学号删除:" << endl;
					cout << "2 按姓名删除:" << endl;
					cin >> dele_op;
					if(dele_op == 1)//按照学号删除学生信息
					{
						char dele_sn[SNO];
						cout << "请输入待删学号:";
						cin.get();
						cin.getline(dele_sn,SNO);
						Linkstudent * t = list.find_sn(dele_sn);
						if( t )
						{
							cout << "该学生信息为:"<<endl;
							t->stu.showall();
							char dele_an;
							cout << "确定要删除么?(y or n)";
							cin >> dele_an;
							while(true)
							{
								if(dele_an == 'y')
								{
									list.remvoe_sn(dele_sn);
									break;
								}
								else if(dele_an == 'n')
									break;
								else
								{
									cout << "确定要删除么?(y or n)";
									cin >> dele_an;
								}
								
							}//endwhile
							
							
						}//endif
						else
							cout << "没有该学生!"<<endl;
						
					}//endif
					if(2 == dele_op)// 按照姓名删除学生信息
					{
						char dele_n[NAME];
						cout << "请输入待删姓名:";
						cin.get();
						cin.getline(dele_n,NAME);
						Linkstudent * t = list.find_n(dele_n);
						if( t )
						{
							cout << "该学生信息为:"<<endl;
							t->stu.showall();
							char dele_an;
							cout << "确定要删除么?(y or n)";
							cin >> dele_an;
							while(true)
							{
								if(dele_an == 'y')
								{
									list.remove_n(dele_n);
									break;
								}
								else if(dele_an == 'n')
									break;
								else
								{
									cout << "确定要删除么?(y or n)";
									cin >> dele_an;
								}
								
							}//endwhile
							
						}//endif
						else
							cout << "没有该学生!" << endl;							
					}//endif
					
					
				}//end case 4
				break;
				////////////////////////////成绩统计////////////////////////////
			case '5':
				{
					cin.get();
					int inall_op;
					int inall_begin;
					int inall_end;
					cout << "请选择要统计的课程:" << endl;
					cout << "1 语文\t2 数学\t3 英语"<<endl<<endl;
					cin>>inall_op;
					cout << "请输入要统计的分数段:" << endl;
					cin>>inall_begin>>inall_end;
					switch(inall_op)
					{ 
					case 1:
						{     
							Linkstudent *x=list.get_first ();
							Linkstudent *temp = x->next;
							int i;
							while(temp)
							{
								i=temp->stu.get_chinese();
								//cout<<endl<<i<<endl;
								if(inall_begin<=i && i<=inall_end)
									list.find_chinese(i);   
								temp=temp->next ;
							}
						}
						break;
					case 2:	         
						{      
							Linkstudent *x=list.get_first ();
							Linkstudent *temp = x->next;
							int i;
							while(temp)
							{
								i=temp->stu.get_math();
								//cout<<endl<<i<<endl;
								if(inall_begin<=i && i<=inall_end)
									list.find_math(i);   
								temp=temp->next ;
							}
						}
						break;
					case 3:
						{      
							Linkstudent *x=list.get_first ();
							Linkstudent *temp = x->next;
							int i;
							while(temp)
							{
								i=temp->stu.get_math();
								//cout<<endl<<i<<endl;
								if(inall_begin<=i && i<=inall_end)
									list.find_math(i);   
								temp=temp->next ;
							}
						}
						break;	
					}
					break;
					////////////////////////////排序////////////////////////////////
					case '6':	
						{
							cin.get();
							int order_op;
							int order_sort;
							cout << "请选择要排序的课程:" << endl;
							cout << "1 语文\t2 数学\t3 英语\t4 总分"<<endl<<endl;
							cin>>order_op;
							cout << "请选择排序的方式:" << endl;
							cout << "1 升序\t2 降序"<<endl<<endl;
							cin>>order_sort;
							if(order_sort==1)
							{
								list.sort_ascending(order_op);//冒泡法升序排序
							}
							else
								list.sort_descending(order_op);//冒泡法降序排序
							list.show_all();//显示排序后的结果
						}
						break;
					case '7':
						{
							list.save_stuInfo();//保存数据到文件	
						}
						break;	
					default:
						break;
				}		
}		
}
}

⌨️ 快捷键说明

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