2016-07-27 45 views
-7

错误的值我有一个计算与不同的日期和时间不同速率的手机通话费用的程序。费率如下:为什么被计算

  • 至周五上午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

+2

您是否尝试过调试此代码? – meJustAndrew

+2

这个'if(time == 060)'实际上是'if(time == 48)'。 – DimChtz

+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"

+0

这就是我正在寻找的,非常感谢你! –