📄 main_prog.cpp
字号:
////////////////////////////////////////////////////////
//Function:使用前面定义的类完成顺序表的有关操作:
// 如插入表项、删除表项、查找、更新、输入表项等
//Program:main_prog.cpp
////////////////////////////////////////////////////////
#include "student.h"
#include <conio.h>
void ShowSelection()
{
//显示功能菜单方便用户选择
cout<<setw(60)<<"******==请输入单个字母选择以下选项,并按回车键==******"<<endl;
cout<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"| 打开数据文件-----------(O or o) |"<<endl;
cout<<"| 在顺序表尾部插入-------(I) |"<<endl;
cout<<"| 在顺序表指定位置插入---(i) |"<<endl;
cout<<"| 按姓名删除元素---------(D) |"<<endl;
cout<<"| 按位置号删除的元素-----(d) |"<<endl;
cout<<"| 更新特定的数据元素-----(U) |"<<endl;
cout<<"| 更新指定位置的元素-----(u) |"<<endl;
cout<<"| 按籍贯统计人数---------(T) |"<<endl;
cout<<"| 统计学生人数-----------(t) |"<<endl;
cout<<"| 按姓名查找特定的数据---(F or f) |"<<endl;
cout<<"| 输出顺序表的数据-------(P or p) |"<<endl;
cout<<"| 保存数据---------------(S or s) |"<<endl;
cout<<"| 退出-------------------(X or x) |"<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"请选择:";
}
int main()
{
SeqList<student> a;
student s;
char choice;
ShowSelection();//显示菜单
cin>>choice;
int insert_pos;//插入位置
int delete_pos;//删除位置
int update_pos;//更新元素的位置
while((choice!='X')&&(choice!='x'))
{
switch(choice)
{
case 'o'://open the data file
case 'O':
a.ReadFromFile();
//define the format
cout<<"记录号"<<setw(8)<<"学号"<<setw(10)<<"姓名"<<setw(8)<<"性别"
<<setw(20)<<"籍贯"<<setw(12)<<"出生年月"
<<setw(6)<<" 成绩"<<endl;
cout.fill('=');
cout<<setw(80)<<"="<<endl;
cout.fill(' ');
a.print(print_all_record);
break;
case 'I'://在表的尾部插入
s.Input_data();
a.Insert(s);
break;
case 'i'://在指定位置插入
s.Input_data();
cout<<"请输入插入位置:";
cin>>insert_pos;
a.Insert(s,insert_pos);
break;
case 'D'://删除用户指定的表项
char del_name[12];
cout<<"请输入单个表项的相应数据:"<<endl;
cout<<"请输入姓名:";
cin>>del_name;
s.setName(del_name);
a.Remove(s);
break;
case 'd'://根据指定的位置进行删除操作
cout<<"请输入单个表项的相应数据:"<<endl;
cout<<"请输入待删除元素的位置:";
cin>>delete_pos;
a.Remove(delete_pos);
break;
case 'U'://根据学生姓名更新其成绩
char in_name[12];
int score;
cout<<"请输入单个表项的相应数据:"<<endl;
cout<<"请输入姓名:";
cin>>in_name;
cout<<"请输入新成绩:";
cin>>score;
s.setName(in_name);
s.set_score(score);
a.Update(s);
break;
case 'u'://根据指定位置更新表项的姓名
int new_score;
cout<<"请输入待更新记录的位置:";
cin>>update_pos;
cout<<"---请输入新的记录值---"<<endl;
cout<<"请输入成绩:";
cin>>new_score;
s.set_score(new_score);
a.Update(s,update_pos);
break;
case 'T'://按籍贯统计人数
char address[20];
cout<<"请输入籍贯信息:";
cin>>address;
s.set_Address(address);
cout<<"籍贯为"<<address<<"的共有:"<<a.Total(s)<<"人"<<endl;
break;
case 't':
cout<<"学生人数为: "<<a.Length()<<endl;;
break;
case 'F':
case 'f'://根据姓名查找学生的数据
char f_name[12];
cout<<"请输入单个表项的相应数据:"<<endl;
cout<<"请输入姓名:";
cin>>f_name;
s.setName(f_name);
int pos;
pos=a.Find(s);
if(pos!=-1)a.print(pos);
else cout<<"无此条记录"<<endl;
break;
case 'P':
case 'p'://输出顺序表表项的内容
//define the format
cout<<"记录号"<<setw(8)<<"学号"<<setw(10)<<"姓名"<<setw(8)<<"性别"
<<setw(20)<<"籍贯"<<setw(12)<<"出生年月"
<<setw(6)<<" 成绩"<<endl;
cout.fill('=');
cout<<setw(80)<<"="<<endl;
cout.fill(' ');
a.print(print_all_record);
break;
case 's':
case 'S'://已完成
a.SaveToFile();
break;
default://缺省处理
cout<<"未提供此项功能!"<<endl;
break;
}
ShowSelection();
cin>>choice;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -