解释这段代码int main() { int choice; while (true) { showMenu(); cout << "请选择操作:"; cin >> choice; switch (choice) { case 1: insertStudent(); break; case 2: deleteStudent(); break; case 3: modifyStudent(); break; case 4: sortByScore(); break; case 5: calculateAverageScore(); break; case 6: countFailedStudents(); break; case 7: sortByAverageScore(); break; case 8: printScoreTable(); break; case 9: saveToFile(); break; case 10: loadFromFile(); break; case 0: return 0; default: cout << "无效的选择!\n";
时间: 2024-02-10 21:34:52 浏览: 206
这段代码是一个简单的学生成绩管理系统的主函数。它使用一个无限循环,不断显示菜单并等待用户输入选项。当用户选择一个选项时,会调用相应的函数来进行相应的操作。
具体来说,这个系统支持以下操作:
1. 插入学生信息
2. 删除学生信息
3. 修改学生信息
4. 按成绩排序
5. 计算平均成绩
6. 统计不及格学生
7. 按平均成绩排序
8. 打印成绩表格
9. 保存到文件
10. 从文件加载
当用户选择退出时,主函数返回0,程序结束。如果用户输入了无效的选项,主函数会输出一个错误信息并重新显示菜单等待用户输入。
相关问题
C++编写代码,使控制台运行之后可以返回菜单界面
当然,我可以帮你编写一段代码来实现这个功能。以下是一个简单的示例:
```cpp
#include <iostream>
void showMenu() {
std::cout << "=== 菜单 ===" << std::endl;
std::cout << "1. 选项1" << std::endl;
std::cout << "2. 选项2" << std::endl;
std::cout << "3. 选项3" << std::endl;
std::cout << "4. 退出" << std::endl;
}
int main() {
bool running = true;
while (running) {
showMenu();
int choice;
std::cout << "请选择一个选项: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "你选择了选项1" << std::endl;
// 执行选项1对应的操作
break;
case 2:
std::cout << "你选择了选项2" << std::endl;
// 执行选项2对应的操作
break;
case 3:
std::cout << "你选择了选项3" << std::endl;
// 执行选项3对应的操作
break;
case 4:
std::cout << "程序退出" << std::endl;
running = false;
break;
default:
std::cout << "无效的选项,请重新选择" << std::endl;
break;
}
// 清空输入缓冲区
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return 0;
}
```
这段代码使用了一个 `while` 循环来实现菜单的循环展示和用户选择。用户可以通过输入选项的数字来进行选择,然后根据选择执行相应的操作。当用户选择退出时,`running` 变量被设置为 `false`,循环结束,程序退出。
飞机订票系统程序设计c++代码
飞机订票系统的C++代码设计需要考虑以下几个方面:
1. 用户管理:设计用户信息类,包括用户ID、密码、姓名、联系方式等信息,实现用户注册、登录、修改密码等功能。
2. 航班信息管理:设计航班信息类,包括航班号、起点、终点、出发时间、到达时间、价格等信息,实现添加航班、删除航班、查询航班等功能。
3. 订单管理:设计订单类,包括订单号、用户ID、航班号、起点、终点、出发时间、到达时间、价格等信息,实现用户下单、查询订单等功能。
4. 界面设计:设计界面类,包括用户登录界面、主菜单界面、航班信息管理界面、订单管理界面等,实现用户友好的操作界面。
下面是一个简单的飞机订票系统程序设计C++代码示例:
```
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
class User
{
private:
string UserID; //用户ID
string Password; //密码
string Name; //姓名
string Tel; //联系电话
public:
void Register(); //用户注册
bool Login(); //用户登录
void ChangePwd(); //修改密码
};
void User::Register()
{
cout<<"请输入您的姓名:";
cin>>Name;
cout<<"请输入您的联系电话:";
cin>>Tel;
cout<<"请输入您的ID:";
cin>>UserID;
cout<<"请输入您的密码:";
cin>>Password;
cout<<"注册成功!"<<endl;
}
bool User::Login()
{
string userid, pwd;
cout<<"请输入您的ID:";
cin>>userid;
cout<<"请输入您的密码:";
cin>>pwd;
if(userid==UserID && pwd==Password)
return true;
else
return false;
}
void User::ChangePwd()
{
string oldpwd, newpwd;
cout<<"请输入旧密码:";
cin>>oldpwd;
if(oldpwd!=Password)
{
cout<<"旧密码输入错误!"<<endl;
return;
}
cout<<"请输入新密码:";
cin>>newpwd;
Password=newpwd;
cout<<"修改成功!"<<endl;
}
class Flight
{
private:
string FlightNo; //航班号
string Start; //起点
string Destination; //终点
string DepartTime; //出发时间
string ArriveTime; //到达时间
float Price; //价格
public:
void AddFlight(); //添加航班
void DeleteFlight(); //删除航班
void QueryFlight(); //查询航班
};
void Flight::AddFlight()
{
cout<<"请输入航班号:";
cin>>FlightNo;
cout<<"请输入起点:";
cin>>Start;
cout<<"请输入终点:";
cin>>Destination;
cout<<"请输入出发时间(格式为hh:mm):";
cin>>DepartTime;
cout<<"请输入到达时间(格式为hh:mm):";
cin>>ArriveTime;
cout<<"请输入价格:";
cin>>Price;
}
void Flight::DeleteFlight()
{
FlightNo="";
}
void Flight::QueryFlight()
{
cout<<"航班号:"<<FlightNo<<"\t起点:"<<Start<<"\t终点:"<<Destination<<"\t出发时间:"<<DepartTime<<"\t到达时间:"<<ArriveTime<<"\t价格:"<<Price<<endl;
}
class Order
{
private:
string OrderNo; //订单号
string UserID; //用户ID
string FlightNo; //航班号
string Start; //起点
string Destination; //终点
string DepartTime; //出发时间
string ArriveTime; //到达时间
float Price; //价格
public:
void BookTicket(); //订票
void QueryOrder(); //查询订单
};
void Order::BookTicket()
{
cout<<"请输入航班号:";
cin>>FlightNo;
cout<<"请输入起点:";
cin>>Start;
cout<<"请输入终点:";
cin>>Destination;
cout<<"请输入出发时间(格式为hh:mm):";
cin>>DepartTime;
cout<<"请输入到达时间(格式为hh:mm):";
cin>>ArriveTime;
cout<<"请输入价格:";
cin>>Price;
}
void Order::QueryOrder()
{
cout<<"订单号:"<<OrderNo<<"\t用户ID:"<<UserID<<"\t航班号:"<<FlightNo<<"\t起点:"<<Start<<"\t终点:"<<Destination<<"\t出发时间:"<<DepartTime<<"\t到达时间:"<<ArriveTime<<"\t价格:"<<Price<<endl;
}
class Menu
{
public:
void ShowMenu(); //显示主菜单
void ShowUserMenu(); //显示用户菜单
void ShowAdminMenu(); //显示管理员菜单
void Run(); //程序运行
};
void Menu::ShowMenu()
{
cout<<"***************************"<<endl;
cout<<"* 1. 用户登录 "<<endl;
cout<<"* 2. 用户注册 "<<endl;
cout<<"* 3. 退出系统 "<<endl;
cout<<"***************************"<<endl;
}
void Menu::ShowUserMenu()
{
cout<<"***************************"<<endl;
cout<<"* 1. 查询航班信息 "<<endl;
cout<<"* 2. 订票 "<<endl;
cout<<"* 3. 查询订单 "<<endl;
cout<<"* 4. 修改密码 "<<endl;
cout<<"* 5. 退出登录 "<<endl;
cout<<"***************************"<<endl;
}
void Menu::ShowAdminMenu()
{
cout<<"***************************"<<endl;
cout<<"* 1. 添加航班信息 "<<endl;
cout<<"* 2. 删除航班信息 "<<endl;
cout<<"* 3. 查询航班信息 "<<endl;
cout<<"* 4. 查询订单信息 "<<endl;
cout<<"* 5. 修改密码 "<<endl;
cout<<"* 6. 退出登录 "<<endl;
cout<<"***************************"<<endl;
}
void Menu::Run()
{
while(true)
{
ShowMenu();
int choice1=0, choice2=0, choice3=0;
cin>>choice1;
if(choice1==1)
{
User u1;
if(u1.Login())
{
cout<<"登录成功!"<<endl;
if(u1.IsAdmin()) //管理员菜单
{
while(true)
{
ShowAdminMenu();
cin>>choice3;
if(choice3==1)
{
Flight f1;
f1.AddFlight();
}
else if(choice3==2)
{
Flight f1;
f1.DeleteFlight();
}
else if(choice3==3)
{
Flight f1;
f1.QueryFlight();
}
else if(choice3==4)
{
Order o1;
o1.QueryOrder();
}
else if(choice3==5)
u1.ChangePwd();
else if(choice3==6)
break;
else
cout<<"无效的输入,请重新输入!"<<endl;
}
}
else //用户菜单
{
while(true)
{
ShowUserMenu();
cin>>choice2;
if(choice2==1)
{
Flight f1;
f1.QueryFlight();
}
else if(choice2==2)
{
Order o1;
o1.BookTicket();
}
else if(choice2==3)
{
Order o1;
o1.QueryOrder();
}
else if(choice2==4)
u1.ChangePwd();
else if(choice2==5)
break;
else
cout<<"无效的输入,请重新输入!"<<endl;
}
}
}
else
cout<<"登录失败,请检查用户名和密码!"<<endl;
}
else if(choice1==2)
{
User u1;
u1.Register();
}
else if(choice1==3)
break;
else
cout<<"无效的输入,请重新输入!"<<endl;
}
}
int main()
{
Menu m1;
m1.Run();
return 0;
}
```
阅读全文
相关推荐
















