学生信息管理系统
問題描述
某班開設有英語、數學、程序設計三門課程,該班同學不多于50人。編寫具有下列功能的菜單驅動的程序。
(1)添加學生信息
(2)顯示所有學生信息
(3)刪除學生信息 讓操作者輸入要刪除學生的名字,將其刪除
(4)查詢學生信息 讓操作者輸入要查詢學生的名字,顯示其信息或提示無
(5)保存當前輸入數據
(6)載入先前數據
(0)退出系統
程序代碼
#include<bits/stdc++.h>
using namespace std;
#define max_len 51 //根據問題描述,最多50個學生
struct Student
{
double english;
double math;
double program;
double sum;
char name[20];
bool isexist; //用該bool型變量來標記學生是否被刪除
} list_student[max_len]; //學生列表
int pos = 0; //指向學生列表的位置
int show_Control()
{
cout<<"請選擇你要的功能:
";
cout<<" 1---添加學生信息
";
cout<<" 2---顯示所有學生信息
";
cout<<" 3---刪除學生信息
";
cout<<" 4---查詢單個學生信息
";
cout<<" 5---保存當前學生信息到文件中去
";
cout<<" 6---載入先前保存進度
";
cout<<" 0---退出系統
";
int n;
cin>>n;
return n;
} //展示操作菜單并返回用戶選擇的數字
void add_student()
{
cout<<"請輸入學生的名字 ";
cin>>list_student[pos].name;
cout<<"請輸入學生的英語成績 ";
cin>>list_student[pos].english;
cout<<"請輸入學生的數學成績 ";
cin>>list_student[pos].math;
cout<<"請輸入學生的程序設計成績 ";
cin>>list_student[pos].program;
list_student[pos].sum = list_student[pos].english + list_student[pos].math + list_student[pos].program;
list_student[pos].isexist = true;
pos++; //指針加一
cout<<endl;
} //添加學生信息
void sortbysum()
{
for(int i=0;i<pos;i++)
for(int j=0;j<pos-i;j++)
{
if(list_student[i].isexist && list_student[j].isexist && list_student[j].sum>list_student[j+1].sum)
{
struct Student tmp = list_student[i];
list_student[i] = list_student[j];
list_student[j] = tmp;
}
}
}//冒泡排序,根據學生的總成績進行排序
void show_all__information()
{
cout<<"你是否想對學生的總成績進行排序后輸出?(y/n)";
char tmp[10];
cin>>tmp;
if(tmp[0]=='y') sortbysum();
cout<<"學生姓名 英語成績 數學成績 程序設計成績 總成績
";
for(int i=0;i<pos;i++)
{
if(list_student[i].isexist)
{
cout<<list_student[i].name << " ";
cout<<list_student[i].english << " ";
cout<<list_student[i].math << " ";
cout<<list_student[i].program << " ";
cout<<list_student[i].sum << "
";
}
}
cout<<endl;
}//展示學生信息,提供選項是否要排序
void delete_student()
{
cout<<"請輸入你要刪除的學生的名字
";
char student_name[20];
cin>>student_name;
for(int i=0;i<pos;i++)
{
if(strcmp(student_name,list_student[i].name)==0 && list_student[i].isexist) //如果找到指定的學生
{
list_student[i].isexist = false; //置為false就是刪除
cout<<"刪除成功!
";
cout<<endl;
return;
}
}
cout<<"查無該學生信息
";
cout<<endl;
}//刪除某個學生
void get_student()
{
cout<<"請輸入你要查詢的學生的名字
";
char student_name[20];
cin>>student_name;
for(int i=0;i<pos;i++)
{
if(strcmp(student_name,list_student[i].name)==0) //如果找到指定的學生
{
if(!list_student[i].isexist)
{
cout<<"查無該學生信息
";
cout<<endl;
return;
}
else
{
cout<<"這位學生的英語成績是: " << list_student[i].english << endl;
cout<<"這位學生的數學成績是: " << list_student[i].math << endl;
cout<<"這位學生的程序設計成績是: " << list_student[i].program << endl;
cout<<"這位學生的總成績是: " << list_student[i].sum<< endl;
cout<<endl;
return;
}
}
}
cout<<"查無該學生信息
";
cout<<endl;
}//查詢單個學生信息
void savedate()
{
FILE *fp;
fp = fopen("data.txt","w");
for(int i=0;i<pos;i++)
{
if(list_student[i].isexist)
{
fprintf(fp,"%s %.2lf %.2lf %.2lf %.2lf
",list_student[i].name,list_student[i].english,list_student[i].math,list_student[i].program,list_student[i].sum);
printf("寫入成功!
");
}
}
fclose(fp);
}//保存當前輸入數據
void loaddata()
{
FILE *fp;
if((fp=fopen("data.txt","r"))==NULL)//打開輸入文件
{
cout<<"無法打開該文件
";
return;
}
char tmp[100];
int line_length = 0;
while(fgets(tmp,100,fp)!=0) line_length++; //統計行數,也是統計學生個數
rewind(fp); //將光標移回文件開頭
for(int i=0;i<line_length;i++)
{
fscanf(fp,"%s%lf%lf%lf%lf",&list_student[i].name,&list_student[i].english,&list_student[i].math,&list_student[i].program,&list_student[i].sum);
list_student[i].isexist = true;
}
pos = line_length;
fclose(fp);
}//載入先前數據
int main()
{
int choice = show_Control();
while(choice!=0)
{
switch(choice)
{
case(0):
{
cout<<"是否要保存并退出(y/n)";
char tmp[5];
cin>>tmp;
if(tmp[0]=='y')
savedate();
return 0;
}//退出
case(1): add_student();break; //添加學生信息
case(2): show_all__information();break; //展示所有學生信息
case(3): delete_student();break; //刪除學生信息
case(4): get_student();break; //獲取單個學生信息
case(5): savedate();break; //保存當前所有學生信息
case(6): loaddata();break; //載入先前數據
}
choice = show_Control(); //更新用戶選項
if(choice == 0)
{
cout<<"是否要保存并退出(y/n)";
char tmp[5];
cin>>tmp;
if(tmp[0]=='y')
savedate();
return 0;
}
}
return 0;
}
測試用例
張三 154.00 15.00 59.00 228.00
李四 489.00 15.00 49.00 553.00
老王 156.00 155.00 156.00 467.00
李子 156.00 114.00 236.00 506.00
結果展示
總結
- 上一篇: 好的网站建设对于我们来说意味着什么
- 下一篇: 微分销系统开发多少钱 微分销系统开发所需