第3题:设计一个模拟电信计费程序
1.假设电信计费标准:固定电话长途话费0.02元/秒,固定电话本地话费0.06元/分,无线电话长途话费1.00元/分,无线电话本地话费0.60元/分,无线电话接听话费0.50元/分。
2.源数据文件中存放:电话号码,电信服务类别,通话时间(秒)。
3.生成固定长途电话文件:长途电话号码和通话时间。
4.生成固定本地电话文件:本地电话号码和通话时间。
5.生成无线长途电话文件:长途电话号码和通话时间。
6.生成无线本地电话文件:本地电话号码和通话时间。
7.生成无线接听电话文件:接听电话号码和通话时间。
8.生成统计电信费用文件:电话号码、累计电信费用。
.cpp文件
#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;
#include<iomanip>
#include<stdlib.h>
#include<fstream>
#include<Windows.h>
#include<math.h>
#include<cstring>
class Customer
{
private:
Customer* next;
string Pnumber;
public:
bool FLD;//固定长途
bool FL;//固定本地
bool WLD;//无线长途
bool WL;//无线本地
bool WA;//无线接听
int FLDS;//固定长途时间,s
int FLM,WLDM,WLM,WAM;
float Cost;int count;
public:
Customer()
{
FLD=0;FL=0;WLD=0;WL=0;WA=0;
FLDS=0;FLM=0;WLDM=0;WLM=0;WAM=0;Cost=0;
next=NULL;count=0;
Pnumber="无";
}
void setData(string ss)
{
count++;char a[100]={'\0'};
if(count==1)Pnumber=ss;
if(count%2==0)
{
if(!ss.compare("固定长途"))FLD=1;
if(!ss.compare("固定本地"))FL=1;
if(!ss.compare("无线长途"))WLD=1;
if(!ss.compare("无线本地"))WL=1;
if(!ss.compare("无线接听"))WA=1;
}
if((count%2)!=0&&count!=1)
{
int length=strlen(ss.c_str());
int sum=0;int temp=0;
for(int i=length-1;i>=0;i--)
{
strcpy_s(a,length+1,ss.c_str());
temp=a[i]-'0';
sum=temp*(pow(10.0,length-i-1))+sum;
}
if(FLD==1&&FLDS==0)FLDS=sum;
if(FL==1&&FLM==0)FLM=sum;
if(WLD==1&&WLDM==0)WLDM=sum;
if(WL==1&&WLM==0)WLM=sum;
if(WA==1&&WAM==0)WAM=sum;
}
Cost=FLDS*0.02+FLM*0.06+WLDM*1+WLM*0.6+WAM*0.5;
}
void SetNext(){next=new Customer;}
Customer* GetNext(){return next;}
string GetNumber(){return Pnumber;}
void Display()
{
cout<<FLDS<<" "<<FLM<<" "<<WLDM<<" "<<WLM<<" "<<WAM<<" ";
cout<<Cost<<endl;
}
};
class customerData
{
private:
string FileName;Customer* start;
Customer* first;
Customer* readStart;
Customer* readFirst;
public:
customerData(){
FileName="customerData.txt";start=NULL;first=start;
readStart=NULL;readFirst=readStart;
}
void setData()
{
start=new Customer();
first=start;
fstream f(FileName,ios::app);
string s="w";
string s2="结束";
cout<<"请输入顾客通话信息"<<endl;
while(1)
{
getline(cin,s);
if(s.compare(s2)==-1)
{
f<<s<<'\n';
int length=strlen(s.c_str());
char a[100]={'\0'};
strcpy_s(a,length+1,s.c_str());
int i=0;
int j=0;
while(j<length)
{
if(a[j]==32)i++;
j++;
}
char *ss=strtok(a," ");i--;
start->setData(ss);
while(i>=0)
{
ss=strtok(NULL," ");
start->setData(ss);
i--;
}
if(i<=0)
{
start->SetNext();
start=start->GetNext();
}
}
else
{f.close();break;}
}
}
int setData2()
{
readStart=new Customer();
readFirst=readStart;
fstream f(FileName,ios::in);
string s;
while(f.eof()==0)
{
getline(f,s);
int length=strlen(s.c_str());
if(length==0)break;
char a[100]={'\0'};
strcpy_s(a,length+1,s.c_str());
int i=0;
int j=0;
while(j<length)
{
if(a[j]==32)i++;
j++;
}
char* ss=strtok(a," ");
i--;
readStart->setData(ss);
while(i>=0)
{
ss=strtok(NULL," ");
readStart->setData(ss);
i--;
}
if(i<=0)
{
readStart->SetNext();
readStart=readStart->GetNext();
}
}
f.close();
if(readFirst==readStart) {cout<<"系统没有顾客的通话信息,请确保有顾客信息"<<endl;return 0;}
else return 1;
}
void readFile()//流对象不能复制
{
string s;
fstream f(FileName,ios::in);
while(!f.eof())
{getline(f,s);cout<<s<<endl;}
f.close();
}
void CreateFLD()//生成固定长途电话文件
{
ofstream file("FLDCustomer.txt",ios::trunc); file.close();
fstream fld("FLDCustomer.txt",ios::app);
Customer* write=readFirst;
string ss;
while(write->GetNext()!=NULL)
{
ss=write->GetNumber();
if(strlen(ss.c_str())==0)break;
if(write->FLD==1)
{
fld<<ss<<" "<<write->FLDS<<'\n';
write=write->GetNext();
}
else
{
write=write->GetNext();
}
}
fld.close();
}
void CreateFL()//生成固定长途电话文件
{
ofstream file("FLCustomer.txt",ios::trunc); file.close();
fstream fld("FLCustomer.txt",ios::app);
Customer* write=readFirst;
string ss;
while(write->GetNext()!=NULL)
{
ss=write->GetNumber();
if(ss.compare("")==0)break;
if(write->FL==1)
{
fld<<ss<<" "<<write->FLM<<'\n';
write=write->GetNext();
}
else
{
write=write->GetNext();
}
}
fld.close();
}
void CreateWLD()//生成固定长途电话文件
{
ofstream file("WLDCustomer.txt",ios::trunc); file.close();
fstream fld("WLDCustomer.txt",ios::app);
Customer* write=readFirst;
string ss;
while(write->GetNext()!=NULL)
{
ss=write->GetNumber();
if(ss.compare("")==0)break;
if(write->WLD==1)
{
fld<<ss<<" "<<write->WLDM<<'\n';
write=write->GetNext();
}
else
{
write=write->GetNext();
}
}
fld.close();
}
void CreateWL()//生成固定长途电话文件
{
ofstream file("WLCustomer.txt",ios::trunc); file.close();
fstream fld("WLCustomer.txt",ios::app);
Customer* write=readFirst;
string ss;
while(write->GetNext()!=NULL)
{
ss=write->GetNumber();
if(ss.compare("")==0)break;
if(write->WL==1)
{
fld<<ss<<" "<<write->WLM<<'\n';
write=write->GetNext();
}
else
{
write=write->GetNext();
}
}
fld.close();
}
void CreateWA()//生成固定长途电话文件
{
ofstream file("WACustomer.txt",ios::trunc); file.close();
fstream fld("WACustomer.txt",ios::app);
Customer* write=readFirst;
string ss;
while(write->GetNext()!=NULL)
{
ss=write->GetNumber();
if(ss.compare("")==0)break;
if(write->WA==1)
{
fld<<ss<<" "<<write->WAM<<'\n';
write=write->GetNext();
}
else
{
write=write->GetNext();
}
}
fld.close();
}
void CreateCost()
{
ofstream file("CustomerCost.txt",ios::trunc); file.close();
fstream fld("CustomerCost.txt",ios::app);
Customer* write=readFirst;
string ss;
while(write->GetNext()!=NULL)
{
ss=write->GetNumber();
if(ss.compare("")==0)break;
fld<<ss<<" "<<write->Cost<<'\n';
write=write->GetNext();
}
fld.close();
}
void display()
{
Customer* display=readFirst;
while(display->GetNext()!=NULL)
{
display->Display();
display=display->GetNext();
}
}
};
void nav()
{
cout<<" "<<"欢迎进入飞鸟电信收费管理系统"<<endl;
cout<<" "<<"请选择您所需要的服务"<<'\n';
cout<<" "<<"输入顾客信息输入1"<<'\n';
cout<<" 生成通话类型文件输入2"<<'\n';
cout<<" 查看顾客信息输入3"<<'\n';
cout<<" 退出即退出系统输入4"<<endl;
}
int main()
{
customerData d;
int service=10;
nav();
cin>>service;
while(service!=4)
{
if(service==1)
{
customerData c;
system("cls");
cin.ignore();
c.setData();
cout<<"输入信息完成"<<endl;
Sleep(1000);
system("cls");
nav();
cin>>service;
}
if(service==2)
{
if(d.setData2()==0)
{
Sleep(5000);
system("cls");
nav();
cin>>service;
}
else
{
d.CreateFLD();
d.CreateFL();
d.CreateWLD();
d.CreateWL();
d.CreateWA();
d.CreateCost();
cout<<"文件生成完毕,请至后台查看文件"<<endl;
Sleep(3000);
system("cls");
nav();
cin>>service;
}
}
if(service==3)
{
d.readFile();
string ss;cout<<"输入结束即返回菜单"<<endl;
cin>>ss;
if(strlen(ss.c_str())!=0)
{
system("cls");
nav();
cin>>service;
}
}
}
system("pause");
return 0;
}
.h文件
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: 在此处引用程序需要的其他头文件