旅游景点信息管理系统程序设计 设计结构体数组
发布网友
发布时间:2022-05-02 13:46
我来回答
共1个回答
热心网友
时间:2022-06-20 08:05
#include<iostream>
#include<string>
#include<conio.h>
#include<fstream>
#define filepath "JDInfo.txt"
#define max 1000
using namespace std;
typedef struct JDInfo
{
string jdid;
string jdname;
string cityname;
float money;
}JDInfo;
static int count;
static JDInfo jdinfo[max];
int load_info()
{
count = 0;
ifstream ifs(filepath, ios::in);
if(ifs.good())
{
while(!ifs.eof())
{
ifs >> jdinfo[count].jdid;
ifs >> jdinfo[count].jdname;
ifs >> jdinfo[count].cityname;
ifs >> jdinfo[count++].money;
}
ifs.close();
return count;
}
else
return 0;
}
bool write_info()
{
cout << "请输入景点编号:";
cin >> jdinfo[count].jdid;
cout << "\n请输入景点名称:";
cin >> jdinfo[count].jdname;
cout << "\n请输入旅游地名称:";
cin >> jdinfo[count].cityname;
cout << "\n请输入所需费用:";
cin >> jdinfo[count].money;
ofstream ofs(filepath, ios::app);
if(ofs.good())
{
ofs << jdinfo[count].jdid << endl;
ofs << jdinfo[count].jdname << endl;
ofs << jdinfo[count].cityname << endl;
ofs << jdinfo[count++].money << endl;
cout << "输入记录成功!请按任意键继续..." << endl;
getch();
ofs.close();
return true;
}
else
return false;
}
void read_info()
{
int i = 0;
system("cls");
for( ; i < count; i++)
{
cout << "第" << i + 1 << "个景点数据:" << endl;
cout << "景点编号:" << jdinfo[i].jdid << endl;
cout << "景点名称:" << jdinfo[i].jdname << endl;
cout << "旅游地名称:" << jdinfo[i].cityname << endl;
cout << "所需费用:" << jdinfo[i].money << endl;
}
system("pause");
}
float calc_money(string n)
{
float smoney = 0.0;
int i = 0;
for( ; i < count; i++)
{
if(jdinfo[i].cityname == n)
smoney += jdinfo[i].money;
}
return smoney;
}
int main()
{
load_info();
string tempname;
while(true)
{
cout << "请选择所需功能:" << endl;
cout << "1.输入数据" << endl;
cout << "2.显示所有信息" << endl;
cout << "3.输入旅游地查询并计算总所需费用" << endl;
cout << "4.退出系统" << endl;
i: switch(getch())
{
case '1': write_info(); break;
case '2': read_info(); break;
case '3':
cout << "请输入旅游地名称:";
cin >> tempname;
cout << "\r去 " << tempname << " 旅游,所需要总费用为:" << calc_money(tempname) << endl;
break;
case '4': return 0; break;
default: cout << "\r输入选择错误,请重新输入:";goto i;
}
}
}
给你写了一个,你去运行看下,有什么不懂的再问我吧。