void browseFlightInfo(struct Flight *flights, int numFlights) {
printf("班次信息列表:\n");
for (int i = 0; i < numFlights; i++) {
代码不完整,我猜是遍历所有的航班吧
不知道你这个问题是否已经解决, 如果还没有解决的话:设计要求:设计一个通讯录管理系统,可以实现相关信息的添加,查询,修改,删除功能。
信息添加包括名字,性别,年龄,电话号码,地址等信息。
工具:dev-c++
代码如下:
#include <iostream>
#include<string>
#define MAX 100
using namespace std;
struct person
{
string name;//姓名
string sex;//性别
int age;//年龄
string phone;//电话号码
string addr;//地址
};
struct addressbooks
{
struct person people[MAX];
int size=0;
};
//添加联系人
void addperson(addressbooks *p)
{
//判断通讯录是否满了没有
if(p->size==MAX)
{
cout<<"通讯录已满,无法添加联系人"<<endl;
return;
}
else
{
//添加联系人
string name;
cout<<"请输入姓名:"<<endl;
cin>>name;
p->people[p->size].name=name;
string sex;
cout<<"请输入性别(男/女):"<<endl;
cin>>sex;
p->people[p->size].sex=sex;
int age;
cout<<"请输入年龄:"<<endl;
cin>>age;
p->people[p->size].age=age;
string phone;
cout<<"请输入电话号码:"<<endl;
cin>>phone;
p->people[p->size].phone=phone;
string addr;
cout<<"请输入地址:"<<endl;
cin>>addr;
p->people[p->size].addr=addr;
p->size++;
cout<<"信息添加成功"<<endl;
system("pause");//请按任意键继续
system("cls");//清屏操作
}
}
//显示联系人
void showperson(addressbooks *p)
{
//判断通讯录中人数是否为0
if(p->size==0)
cout<<"当前记录为空"<<endl;
else
{
for(int i=0;i<p->size;i++)
{
cout<<"姓名: "<<p->people[i].name<<"\t";
cout<<"性别: "<<p->people[i].sex<<"\t";
cout<<"年龄: "<<p->people[i].age<<"\t";
cout<<"电话号码: "<<p->people[i].phone<<"\t";
cout<<"地址: "<<p->people[i].addr<<"\t";
cout<<""<<endl;
}
}
system("pause");
system("cls");
}
//判断此人是否存在
int exist(addressbooks *p,string name)
{
for(int i=0;i<p->size;i++)
{
if(p->people[i].name==name)
return i;
}
return -1;
}
//删除联系人
void deleteperson(addressbooks *p)
{
string name;
cout<<"请输入你要删除的联系人:"<<"";
cin>>name;
int ret=exist(p,name);
if(ret!=-1)
{
//查到此人了
for(int i=ret;i<p->size;i++)
p->people[i]=p->people[i+1];
p->size--;
cout<<"删除成功"<<endl;
}
else
cout<<"----没有找到此人----"<<endl;
system("pause");
system("cls");
}
//查找联系人
void findperson(addressbooks *p)
{
string name;
cout<<"请输入你要查找的联系人:"<<"";
cin>>name;
int ret=exist(p,name);
if(ret!=-1)
{
cout<<"姓名: "<<p->people[ret].name<<"\t";
cout<<"性别: "<<p->people[ret].sex<<"\t";
cout<<"年龄: "<<p->people[ret].age<<"\t";
cout<<"电话号码: "<<p->people[ret].phone<<"\t";
cout<<"地址: "<<p->people[ret].addr<<"\t";
cout<<""<<endl;
}
else
cout<<"无法找到该联系人"<<endl;
system("pause");
system("cls");
}
//修改联系人
void changepeople(addressbooks *p)
{
string name;
cout<<"请输入你要修改的联系人:"<<"";
cin>>name;
int ret=exist(p,name);
if(ret!=-1)
{
string sex;
int age;
string phone;
string addr;
cout<<"请输入姓名 性别 年龄 电话 地址等信息"<<endl;
cin>>name>>sex>>age>>phone>>addr;
p->people[ret].name=name;
p->people[ret].age=age;
p->people[ret].phone=phone;
p->people[ret].sex=sex;
p->people[ret].addr=addr;
}
else
cout<<"没有该联系人,无法修改"<<endl;
system("pause");
system("cls");
}
//清空通讯录
void clearpeople(addressbooks *p)
{
p->size=0;
cout<<"清空通讯录成功"<<endl;
system("pause");
system("cls");
}
//菜单
void showmenu()
{
cout<<"************************"<<endl;
cout<<"*****1.添加联系人******"<<endl;
cout<<"*****2.显示联系人******"<<endl;
cout<<"*****3.删除联系人******"<<endl;
cout<<"*****4.查找联系人******"<<endl;
cout<<"*****5.修改联系人******"<<endl;
cout<<"*****6.清空联系人******"<<endl;
cout<<"*****0.退出通讯录******"<<endl;
cout<<"************************"<<endl;
}
//主函数
int main()
{
addressbooks abs;
int select=0;
while(true)
{
showmenu();
cout<<"请输入你的选择(0-6):";
cin>>select;
switch(select)
{
case 1://1.添加联系人
addperson(&abs);
break;
case 2://2.显示联系人
showperson(&abs);
break;
case 3://3.删除联系人
deleteperson(&abs);
break;
case 4://4.查找联系人
findperson(&abs);
break;
case 5://5.修改联系人
changepeople(&abs);
break;
case 6://清空联系人
clearpeople(&abs);
break;
case 0:
{
cout<<"------欢迎下次使用-----"<<endl;
system("pause");
system("cls");
break;
return 0;}
default:
break;
}
}
return 0;
}
运行效果:
我可以为您提供一个机票管理系统的C++代码,包括以下功能:
以下是一个简单的示例代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Flight {
string flightNumber;
string departure;
string destination;
string date;
float price;
int seatCount;
};
class TicketManagementSystem {
public:
void addFlight(const Flight& flight) {
flights.push_back(flight);
}
Flight searchFlight(const string& departure, const string& destination, const string& date) {
for (const auto& flight : flights) {
if (flight.departure == departure && flight.destination == destination && flight.date == date) {
return flight;
}
}
throw runtime_error("No flights found");
}
void reserveTicket(const string& flightNumber, int seatCount) {
for (auto& flight : flights) {
if (flight.flightNumber == flightNumber) {
if (flight.seatCount >= seatCount) {
flight.seatCount -= seatCount;
} else {
throw runtime_error("Not enough seats available");
}
return;
}
}
throw runtime_error("Flight not found");
}
void cancelTicket(const string& flightNumber, int seatCount) {
for (auto& flight : flights) {
if (flight.flightNumber == flightNumber) {
flight.seatCount += seatCount;
return;
}
}
throw runtime_error("Flight not found");
}
void updatePrice(const string& flightNumber, float price) {
for (auto& flight : flights) {
if (flight.flightNumber == flightNumber) {
flight.price = price;
return;
}
}
throw runtime_error("Flight not found");
}
void updateSeatCount(const string& flightNumber, int seatCount) {
for (auto& flight : flights) {
if (flight.flightNumber == flightNumber) {
flight.seatCount = seatCount;
return;
}
}
throw runtime_error("Flight not found");
}
private:
vector<Flight> flights;
};
int main() {
TicketManagementSystem ticketManagementSystem;
// 添加航班
Flight flight1{"F001", "Shanghai", "Beijing", "2022-01-01", 1000.0, 200};
ticketManagementSystem.addFlight(flight1);
Flight flight2{"F002", "Shanghai", "Guangzhou", "2022-01-01", 800.0, 150};
ticketManagementSystem.addFlight(flight2);
// 查询特定航班的票价和座位数量
try {
Flight result = ticketManagementSystem.searchFlight("Shanghai", "Beijing", "2022-01-01");
cout << "Flight " << result.flightNumber << ": Price - " << result.price << ", Seats - " << result.seatCount << endl;
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
// 预订和取消机票
try {
ticketManagementSystem.reserveTicket("F001", 2);
cout << "Ticket reserved" << endl;
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
try {
ticketManagementSystem.cancelTicket("F001", 1);
cout << "Ticket canceled" << endl;
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
// 更新票价和座位数量
try {
ticketManagementSystem.updatePrice("F001", 1200.0);
cout << "Price updated" << endl;
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
try {
ticketManagementSystem.updateSeatCount("F001", 180);
cout << "Seat count updated" << endl;
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}
请注意,这只是一个简单的示例代码,并未实现输入验证、持久化数据存储等复杂功能。如果有具体需求,请提供更多信息或要求,以便我们可以进一步完善代码。