我有一个程序可以计算不同日期和时间的不同费率的电话费用。费率如下:
周一至周五上午8:00至下午6:00之间进行的任何通话,收费标准为每分钟0.40美元。
在星期一至星期五上午8:00之前或下午6:00之后进行的任何通话,收取的费用为每分钟0.25美元。
周六或周日的任何通话费用为每分钟0.15美元。
任何持续时间跨越多个费率区域的通话都会根据相应费率区域的实际分钟收费。
我的代码为每个日志提供了正确的值,除了最后一个,应该是6.50美元,而不是4.50美元。
代码如下:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;
string makeTimeIntoInt(string line[]) {
string temp = "";
string time = line[1];
for (char a : time) {
if (a == ':')
continue;
else
temp += a;
}
return temp;
}
string nextDay(string day){
if(day == "Mo") return "Tu";
else if(day == "Tu") return "We";
else if(day == "We") return "Th";
else if(day == "Th") return "Fr";
else if(day == "Fr") return "Sa";
else if(day == "Sa") return "Su";
else if(day == "Su") return "Mo";
return day;
}
int convertSixtySecondsToHour(int time){
if(time == 060) time = 100;
else if(time == 160) time = 200;
else if(time == 260) time = 300;
else if(time == 360) time = 400;
else if(time == 460) time = 500;
else if(time == 560) time = 600;
else if(time == 660) time = 700;
else if(time == 760) time = 800;
else if(time == 860) time = 900;
else if(time == 960) time = 1000;
else if(time == 1060) time = 1100;
else if(time == 1160) time = 1200;
else if(time == 1260) time = 1300;
else if(time == 1360) time = 1400;
else if(time == 1460) time = 1500;
else if(time == 1560) time = 1600;
else if(time == 1660) time = 1700;
else if(time == 1760) time = 1800;
else if(time == 1860) time = 1900;
else if(time == 1960) time = 2000;
else if(time == 2060) time = 2100;
else if(time == 2160) time = 2200;
else if(time == 2260) time = 2300;
else if(time == 2360) time = 000;
return time;
}
float calculateCost(string line[]) {
int time = stoi(makeTimeIntoInt(line));
int duration = stoi(line[2]);
string day = line[0];
float totalCost = 0;
for(int i = 1; i <= duration; i++){
if (line[0] == "Mo" || line[0] == "Tu" || line[0] == "We" ||
line[0] == "Th" || line[0] == "Fr") {
if (time < 800 || time > 1800) {
totalCost += 0.25;
} else
totalCost += 0.4;
}
else if (line[0] == "Sa" || line[0] == "Su") {
totalCost += 0.15;
}
time++;
time = convertSixtySecondsToHour(time);
if(time == 000){
day = nextDay(day);
}
}
return totalCost;
}
int main() {
ifstream inputFile;
string line;
int cnt = 0;
inputFile.open("/Users/wonder-intern/Documents/C++ Workspace/PhoneRecords/PhoneRecords/calls_history.txt"); //change this to the path of the txt file
string arr[18];
string line1[3], line2[3], line3[3], line4[3], line5[3], line6[3];
float costLine1, costLine2, costLine3, costLine4, costLine5, costLine6;
if (!inputFile.is_open()) {
cout << "file could not be opened\n\n";
return 0;
}
while (getline(inputFile, line) && cnt < 18) {
istringstream split(line);
split >> arr[cnt];
cnt++;
split >> arr[cnt];
cnt++;
split >> arr[cnt];
cnt++;
}
inputFile.close();
line1[0] = arr[0];
line1[1] = arr[1];
line1[2] = arr[2];
line2[0] = arr[3];
line2[1] = arr[4];
line2[2] = arr[5];
line3[0] = arr[6];
line3[1] = arr[7];
line3[2] = arr[8];
line4[0] = arr[9];
line4[1] = arr[10];
line4[2] = arr[11];
line5[0] = arr[12];
line5[1] = arr[13];
line5[2] = arr[14];
line6[0] = arr[15];
line6[1] = arr[16];
line6[2] = arr[17];
costLine1 = calculateCost(line1);
costLine2 = calculateCost(line2);
costLine3 = calculateCost(line3);
costLine4 = calculateCost(line4);
costLine5 = calculateCost(line5);
costLine6 = calculateCost(line6);
cout << "Day Time Duration Cost\n\n";
cout << line1[0] << " " << line1[1] << " " << line1[2] << " $" << costLine1
<< "\n";
cout << line2[0] << " " << line2[1] << " " << line2[2] << " $" << costLine2
<< "\n";
cout << line3[0] << " " << line3[1] << " " << line3[2] << " $" << costLine3
<< "\n";
cout << line4[0] << " " << line4[1] << " " << line4[2] << " $" << costLine4
<< "\n";
cout << line5[0] << " " << line5[1] << " " << line5[2] << " $" << costLine5
<< "\n";
cout << line6[0] << " " << line6[1] << " " << line6[2] << " $" << costLine6
<< "\n";
cout << "Total: $"
<< costLine1 + costLine2 + costLine3 + costLine4 + costLine5 + costLine6
<< "\n\n";
return 0;
}
可在此处找到带有通话记录的文本文件:https://drive.google.com/open?id=0Bysk6_x4B46uQko4dEpMNXZtY2M
答案 0 :(得分:1)
两个问题:
1)060
实际上是八进制,十进制为48.因此,删除所有前导零。
2)内部calculateCost
更改:
if (line[0] == "Mo" || line[0] == "Tu" || line[0] == "We" ||
line[0] == "Th" || line[0] == "Fr")
为:
if (day == "Mo" || day == "Tu" || day == "We" ||
day == "Th" || day == "Fr")
和
else if (line[0] == "Sa" || line[0] == "Su")
到
else if (day == "Sa" || day == "Su")
因为当您致电day = nextDay(day);
时,您显然会更改day
而不是line[0]
。您收到4.5
因为line[0]
仍然是"Su"
。只有day
更改为"Mo"
。