为什么计算的值错误
Why is the wrong value being calculated
我有一个程序,可以计算不同日期和时间不同费率的电话费用。费率如下:
-
周一至周五上午 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 openednn";
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 Costnn";
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
<< "nn";
return 0;
}
带有通话记录的文本文件可以在这里找到:https://drive.google.com/open?id=0Bysk6_x4B46uQko4dEpMNXZtY2M
两个问题:
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"
.仅对"Mo"
进行day
更改。
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- OpenCV - Python 断言错误:SAD 算法 - 立体相机视差图计算
- 如何在常量计算表达式中获取编译时错误?
- 计算阶乘的 C++17 倍表达式中的错误
- 为什么 2 个双精度值之间的差值计算错误?
- 我在c++中遇到了一个奇怪的错误,其中一个计算2个小整数加法的语句溢出到一个长值中
- 错误 C2064:术语的计算结果不是采用 3 个参数的函数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- C++ openmp 并行计算计算错误的结果
- 为什么按位运算符在使用与整数中提供的位数相同的位数计算可能的最大范围时会产生错误
- 术语不计算为函数采用 1 个参数错误?
- Travis CI 中奇怪的编译器错误不会在本地计算机上发生
- 点云库:计算筛选关键点 - 输入云错误
- 在线程错误 C2064 中:term 的计算结果不为 0 个参数的函数
- GetAsyncKeyState(VK_RETURN) 错误地计算为 true
- 错误 C2064:term 的计算结果不是采用 1 个参数的函数 - 关于线程的一些东西
- log2 计算在 CPP 中给出了错误的结果
- 错误计算RSI或错误在MetaTrader 4
- c++程序错误计算