内含文件流操作
需要把文件创建在根源代码一个文件目录下
文件名是carxinxi.txt
eve.txt
mima.txt
#include<iostream>
#include<cstdio>
#include<cstring>
#include<windows.h>
#include<fstream>
#include<time.h>
using namespace std;
void welcome();
void chushihua();
void denglu();
void xiugaimima();
void show();
void carData ();
void back();
int carshumu();
void cartianjia();
void shanchucar();
void npc();
const string currentDateTime();
void jiaona();
int days(int x,int y,int z);
int nianshu(int n);
void jishiqi();
int chazhao();
struct xinxi
{
char plate_num[20];
char brand[20];
char model[20];
float capacity;
char colour[20];
double rate_per_day;
char date[20];
};
xinxi rent[1000];//车辆结构体数组
int main ()
{
//主程序 (以下)
chushihua();//进入页面logo
denglu(); //登录
welcome();//核心程序
}
void denglu()
{
string a,c;
ifstream ifs;
ifs.open("mima.txt",ios::in);//读取密码
ifs>>c;
ifs.close();
cout<<"\n ******\t\t\t\t 请登录... ";
Sleep(2000);
system("cls");
cout<<"\t\t\t*******************************************" <<endl;
cout<< "\n ******\t\t\t\t | 请输入密码:";
cin>>a;
if (a==c)
{
cout<<"*** 登录成功...";
Sleep(1000);
system("cls");
}
else
{
cout<<"*** 密码错误,请重新登录";
Sleep(1000);
system("cls");
denglu();
}
}
void chushihua()
{
ifstream ifs;
ifs.open("eve.txt",ios::in);
char buf[2000]={0};
string Lines = "";
if (ifs)
{
while (ifs.good ())
{
string TempLine;
getline (ifs , TempLine);
TempLine += "\n";
Lines += TempLine;
}
cout << Lines;
}
ifs.close();
Sleep(2000);
system("cls");
}
int days(int x,int y,int z) //x:年 y:月 z:日
{
int i,j=0,k;
//计时器子程序
for(i=1;i<=y-1;i++)
{
switch(i)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:k=31;break;
case 4:case 6:case 9:case 11:k=30;break;
case 2:
{
if((x%100!=0&&x%4==0)||(x%100==0&&x%400==0))
k=29;
else
k=28;
}
break;
}
j+=k;
}
j+=z;
return j;
}
int nianshu(int n)
{ //计时器子程序2
int m=0;
if((n%100!=0&&n%4==0)||(n%100==0&&n%400==0))
{
m=366;
}
else
{
m=365;
}
return m;
}
void jishiqi()//计时器
{
int a,b,c,d,e,f,s=0,t,j;
printf("\n\t请输入起租与还车日期(年 月 日 以空格隔开,两个日期也以空格隔开):\n");
cout<<"\t";
scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f);
//两个日期算出共用的天数
s=s-days(a,b,c);
for(t=a;t<d-1;t++)
{
s+=nianshu(t);
}
s=s+days(d,e,f);
j=chazhao(); //查找出租用该车的日租金
printf("累计租车%d天\n",s);
cout<<endl<<"应缴纳租金:"<<j*s<<"元" ;
}
int chazhao()
{
char plate[10];
int car2;
ifstream ifs;
ifs.open("carxinxi.txt");
cout << "\n\t | \t\t输入车牌号(大写字母): "<<endl;
cout<< "\t | \t\t\t\t\t\t\t";
cin >> ws;
cin.getline(plate,10);
int x=carshumu();
for(int i = 0; i < carshumu(); i++)
{
if (strcmp(plate,rent[i].plate_num) == 0)
{
car2= rent[i].rate_per_day;
ifs.close();
return car2;
}
}
ifs.close();
cout<<"车牌号输入有误,请重新输入" ;
chazhao();
}
void xiugaimima()
{
string a;
cout<<" 请输入新密码 (不含空格) :";
cin>>a;
ofstream ofs;
ofs.open("mima.txt",ios::trunc);
ofs<<a;
ofs.close();
system("cls");
cout<<" 修改成功...";
Sleep(1000);
system("cls");
}
int carshumu()
{
int car0 = 0;
ifstream ifs("carxinxi.txt");
string wa;
while (getline(ifs, wa))
{
if (!wa.empty())
car0++;
}
ifs.close();
return car0;
}
void carData ()
{
ifstream ifs;
ifs.open("carxinxi.txt");
int carNum = 0;
while(!ifs.eof())
{
ifs.getline(rent[carNum].plate_num,20,' ');
ifs.getline(rent[carNum].brand,20,' ');
ifs.getline(rent[carNum].model,20,' ');
ifs >> rent[carNum].capacity;
ifs.ignore();
ifs.getline(rent[carNum].colour,20,' ');
ifs.getline(rent[carNum].date,20,' ');
ifs >> rent[carNum].rate_per_day;
ifs.ignore();
carNum++;
ifs>>ws;
}
ifs.close();
}
void show()
{
cout<<"\n\n";
cout << endl;
carData ();
cout << "\t | 车牌号\t品牌\t 型号\t排量\t 颜色 \t 每天租金\t 租车日期\t" << endl;
cout << "\t | =======================================================================================================================" << endl;
for (int i = 0; i < carshumu(); i++)
{
cout << "\t | " << rent[i].plate_num << "\t" << rent[i].brand << "\t " << rent[i].model << "\t " << rent[i].capacity << "\t "
<< rent[i].colour << "\t\t " << rent[i].rate_per_day
<< "\t\t "<< rent[i].date<<"\t"<<endl;
}
cout << "\n\n";
}
void back(){
int back0;
cout<<"输入1返回主菜单:";
cin>>back0;
if(back0==1)
{ system("cls");
welcome();
}
else back();
}
void newCarData()
{
ofstream ofs;
ofs.open("carxinxi.txt" , fstream::app);
ofs<<endl;
int newCar = carshumu() + 1;
cout << "\n\n";
cout << "\n\t |\t\t\t\t\t 请输入一下数据: "<< endl;
cout << "\n\t |\t\t\t\t\t 车牌号 : ";
cin >> ws;
cin.getline(rent[newCar].plate_num,10);
ofs << rent[newCar].plate_num;
ofs << " ";
cout << "\t |\t\t\t\t\t 品牌 : ";
cin.getline(rent[newCar].brand,20);
ofs << rent[newCar].brand;
ofs << " ";
cout << "\t |\t\t\t\t\t 型号 : ";
cin.getline(rent[newCar].model,20);
ofs << rent[newCar].model;
ofs << " ";
cout << "\t |\t\t\t\t\t 排量 : ";
cin >> rent[newCar].capacity;
ofs << rent[newCar].capacity;
ofs << " ";
cout << "\t |\t\t\t\t\t 颜色 : ";
cin >> ws;
cin.getline(rent[newCar].colour,20);
ofs << rent[newCar].colour;
ofs << " ";
cout << "\t |\t\t\t\t\t 租车日期 : ";
cin >> rent[newCar].date;
ofs << rent[newCar].date;
ofs << " ";
cout << "\t |\t\t\t\t\t 每天的租金: ";
cin >> rent[newCar].rate_per_day;
ofs << rent[newCar].rate_per_day;
ofs << " ";
ofs.close();
cout<<" "<<endl<<" 添加成功...";
Sleep(1000);
system("cls");
carData();
welcome();
}
void shanchucar()
{
char plate[10];
ofstream ofs;
ofs.open("temp.txt");
show();
cout << "\n\n";
cout << "\t | \t\t输入要删除的车牌号(大写字母): "<<endl;
cout<< "\t | \t\t\t\t\t\t\t";
cin >> ws;
cin.getline(plate,10);
int x=carshumu();
for(int i = 0; i < carshumu(); i++)
{
if (strcmp(plate,rent[i].plate_num) != 0)
{
ofs << rent[i].plate_num;
ofs << " ";
ofs << rent[i].brand;
ofs << " ";
ofs << rent[i].model;
ofs << " ";
ofs << rent[i].capacity;
ofs << " ";
ofs << rent[i].colour;
ofs << " ";
ofs << rent[i].date;
ofs << " ";
ofs << rent[i].rate_per_day;
ofs << " ";
if(i != carshumu())
{
ofs<<endl;
}
}
}
ofs.close();
remove("carxinxi.txt");
rename("temp.txt","carxinxi.txt");
system("cls");
cout<<" "<<endl<<" 已删除车辆信息...";
Sleep(1000);
system("cls");
carData();
welcome();
}
void npc()
{
cout<<endl;
cout<<"\n\t |\t\t\t1. 最低租金时间是7天";
cout<<"\n\t |\t\t\t2. 所有车辆已缴纳保险";
cout<<"\n\t |\t\t\t3. 万一发生意外,所有费用由司机支付";
cout<<"\n\t |\t\t\t4. 准备身份证明物品";
cout<<"\n\t |\t\t\t5. 如果迟到的话,额外的费用";
cout<<"\n\t |\t\t\t6. 安全驾驶!";
cout<<"\n\n\n\n\t |\t\t\t 按任意键继续 :)";
cout<<"\n\t |\t\t\t ";
//租车说明或者系统说明可自己更改
getchar();getchar();
back();
}
const string currentDateTime() //获取系统时间,方便缴纳租金时填写还车日期
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%d/%m/%Y 时间: %X", &tstruct);
return buf;
}
void jiaona()
{
show();
cout<<"\t"<<"当前时间 日期:"<<currentDateTime();
jishiqi();
cout<<"\n\t";
back();
}
void welcome()
{
int x;
cout<<"\n **\t\t\t\t\t\t\t 1. <农业车辆信息>";
cout<<"\n **\t\t\t\t\t\t\t 2. <添加车辆>";
cout<<"\n **\t\t\t\t\t\t\t 3. <删除车辆>";
cout<<"\n **\t\t\t\t\t\t\t 4. <修改密码>";
cout<<"\n **\t\t\t\t\t\t\t 5. <系统说明>";
cout<<"\n **\t\t\t\t\t\t\t 6. <缴纳租金>";
cout<<"\n **\t\t\t\t\t\t\t 7. <退出>"<<endl<<"\n";
cout<<"\n **\t\t\t\t\t\t\t INPUT :";
cin>>x;
if (x == 7)//退出
{
cout << "退出系统,欢迎下次使用" << endl;
system("pause");
return ;
}
else if(x==1)
{
system("cls");
show();
back();
}
else if (x==2)
{
system("cls");
newCarData();
}
else if(x==3)
{
system("cls");
shanchucar();
}
else if(x==4)
{
system("cls");
xiugaimima();
welcome();
}
else if(x==5)
{
system("cls");
npc();
}
else if(x==6)
{
system("cls");
jiaona();
}
}