假设电信计费标准:固定电话长途话费0.02元/秒,固定电话本地话费0.06元/分,无线电话长途话费1.00元/分,无线电话本地话费0.60元/分,无线电话接听话费0.50元/分。
源数据文件中存放:电话号码,电信服务类别,通话时间(秒)。
生成固定长途电话文件:长途电话号码和通话时间。
生成固定本地电话文件:本地电话号码和通话时间。
生成无线长途电话文件:长途电话号码和通话时间。
生成无线本地电话文件:本地电话号码和通话时间。
生成无线接听电话文件:接听电话号码和通话时间。
生成统计电信费用文件:电话号码、累计电信费用。
直接上代码
Oneperson.h
#ifndef ONE_PERSON
#define ONE_PERSON
#include <string>
using namespace std;
class Oneperson {
public:
Oneperson(string nums);
int getdistancefix();
int getlocalfix();
int getdistancemobile();
int getlocalmobile();
int getmobilereceive();
string getnums();
int adddistancefix(int n);
int addlocalfix(int n);
int adddistancemobile(int n);
int addlocalmobile(int n);
int addmobilereceive(int n);
private:
string nums;
int distance_fix;
int local_fix;
int distance_mobile;
int local_mobile;
int mobile_receive;
};
#endif
Oneperson.cpp
#include "Oneperson.h"
#include <string>
using namespace std;
Oneperson::Oneperson(string nums)
: distance_fix(0), local_fix(0), distance_mobile(0), local_mobile(0), mobile_receive(0){
this->nums = nums;
}
int Oneperson::getdistancefix() {
return distance_fix;
}
int Oneperson::getlocalfix() {
return local_fix;
}
int Oneperson::getdistancemobile() {
return distance_mobile;
}
int Oneperson::getlocalmobile() {
return local_mobile;
}
int Oneperson::getmobilereceive() {
return mobile_receive;
}
string Oneperson::getnums() {
return nums;
}
int Oneperson::adddistancefix(int n) {
distance_fix += n;
return distance_fix;
}
int Oneperson::addlocalfix(int n) {
local_fix += n;
return local_fix;
}
int Oneperson::adddistancemobile(int n) {
distance_mobile += n;
return distance_mobile;
}
int Oneperson::addlocalmobile(int n) {
local_mobile += n;
return local_mobile;
}
int Oneperson::addmobilereceive(int n) {
mobile_receive += n;
return mobile_receive;
}
Record.h
#ifndef RECORD_H
#define RECORD_H
#include <string>
#include <vector>
#include "Oneperson.h"
using namespace std;
class Record {
public:
Record();
void save_by_kinds();
private:
Record(const Record& a) = delete;
vector<Oneperson> message;
};
#endif
Record.cpp
#include "Record.h"
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
Record::Record() : message(vector<Oneperson>()){
ifstream fin;
fin.open("record.txt");
string nums;
string type;
int time;
while (1) {
if (!getline(fin, nums, ' ')) {
break;
}
getline(fin, type, ' ');
fin >> time;
cout << type << endl;
cout << time;
auto it = message.begin();
for (it; it != message.end(); it++) {
if (it->getnums() == nums) {
break;
}
}
if (it == message.end()) {
message.push_back(Oneperson(nums));
it = message.end() - 1;
}
if (type == "固定长途电话") {
it->adddistancefix(time);
}
else if(type == "固定本地电话") {
it->addlocalfix(time);
}
else if(type == "无线长途电话") {
it->adddistancemobile(time);
}
else if(type == "无线本地电话") {
it->addlocalmobile(time);
}
else if(type == "无线接听电话") {
it->addmobilereceive(time);
}
}
fin.close();
}
void Record::save_by_kinds() {
ofstream fout;
fout.open("固定长途电话.txt");
for (auto it = message.begin(); it != message.end(); it++) {
if (it->getdistancefix() == 0) {
continue;
}
else {
fout << it->getnums() << "\t" << to_string(it->getdistancefix());
}
}
fout.close();
fout.open("固定本地电话.txt");
for (auto it = message.begin(); it != message.end(); it++) {
if (it->getlocalfix() == 0) {
continue;
}
else {
fout << it->getnums() << "\t" << to_string(it->getlocalfix());
}
}
fout.close();
fout.open("无线长途电话.txt");
for (auto it = message.begin(); it != message.end(); it++) {
if (it->getdistancemobile() == 0) {
continue;
}
else {
fout << it->getnums() << "\t" << to_string(it->getdistancemobile());
}
}
fout.close();
fout.open("无线本地电话.txt");
for (auto it = message.begin(); it != message.end(); it++) {
if (it->getlocalmobile() == 0) {
continue;
}
else {
fout << it->getnums() << "\t" << to_string(it->getlocalmobile());
}
}
fout.close();
fout.open("无线接听电话.txt");
for (auto it = message.begin(); it != message.end(); it++) {
if (it->getmobilereceive() == 0) {
continue;
}
else {
fout << it->getnums() << "\t" << to_string(it->getmobilereceive());
}
}
fout.close();
fout.open("电信费用.txt");
for (auto it = message.begin(); it != message.end(); it++) {
float total = (it->getdistancefix() / 60 * 0.02 + (it->getdistancefix() % 60 > 0 ? 1 : 0) * 0.02);
total += (it->getlocalfix() / 60 * 0.06 + (it->getlocalfix() % 60 > 0 ? 1 : 0) * 0.06);
total += (it->getdistancemobile() / 60 * 1 + (it->getdistancemobile() % 60 > 0 ? 1 : 0) * 1);
total += (it->getlocalmobile() / 60 * 0.6 + (it->getlocalmobile() % 60 > 0 ? 1 : 0) * 0.6);
total += (it->getmobilereceive() / 60 * 0.5 + (it->getmobilereceive() % 60 > 0 ? 1 : 0) * 0.5);
fout << it->getnums() << "\t" << to_string(total);
}
fout.close();
}
main.cpp
#include "Record.h"
int main() {
Record test;
test.save_by_kinds();
return 1;
}
创建一个record.txt文件
内容格式为
17734795025 固定长途电话 10
等等 多复制几条
改一改其中的值。