【发布时间】:2017-07-27 21:23:54
【问题描述】:
我正在尝试编写一个程序,该程序将根据电话的开始/结束时间以及通话时间来计算通话费用。一切正常,除了它试图实际显示每次通话的费用和最后的总费用。
每项费用都列为 6.9531e-308 美元。有similar issue 的人通过将有问题的变量初始化为零来解决他们的问题,但是当我这样做时,程序只是开始将每个成本列为 0 美元。
我计算总额的方式似乎也不起作用,因为当成本为 6.9531e-308 美元时,总额为 7.64841e-307 美元,而不是 4.17186e-307 美元。它要么将其中一个成本乘以 11,要么认为需要将 11 个成本相加。
代码如下:
#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
using namespace std;
ifstream fin("call_history.txt"); //read from a file
//int main
int main() {
string temp;
double total = 0.0;
while (getline(fin, temp)) {
istringstream s(temp);
string token[3];
int i = 0;
while(getline(s, token[i], ' ')) { //split the string by spaces and store into token[0], token[1], etc.
i++;
}
//token[0] stores the day of week
//token[1] stores the time (i.e "14:32")
//token[2] stores duration
istringstream s2(token[1]);
string time_values[2];
i = 0;
while(getline(s2, time_values[i], ':')) { //split token[1] by ':'
i++;
}
//time_values[0] stores hour
//time_values[1] stores minute
//rename variables for readibility
string day = token[0];
int hr = atoi(time_values[0].c_str()); //atoi to convert string to integer
int minute = atoi(time_values[1].c_str());
int duration = atoi(token[2].c_str());
double cost=0; //accumulator
int start_time = (hr*60)+minute;
int end_time = start_time+duration;
int startzone, endzone;
if (day == "Mo" || day == "Tu" || day == "We" || day == "Th")
{
day == "weekday";
}
if (day == "Sa" || day == "Su")
{
day == "weekend";
}
//assigning each starting time to a zone
if ((day == "weekday" || day == "Fr") && (start_time >= 480 && start_time < 1080))
{
startzone = 1; //call starts on hours
}
if ((day == "weekday" || day == "Fr") && (start_time <= 480))
{
startzone = 2; //call starts before 8 am
}
if ((day == "weekday" || day == "Fr") && (start_time >= 1080))
{
startzone = 3; //call starts after 6 pm
}
if (day == "weekend")
{
startzone = 4; //call starts on the weekend
}
//assigning each ending time to a zone
//this program will handle at most 3 zone changes in one call.
if (day == "weekday" && (end_time >= 480 && end_time <= 1080))
{
endzone = 1; //call ends on-hours the same day
}
if (day == "weekday" && (end_time >= 1080 && end_time <= 1920))
{
endzone = 2; //call ends after 6 pm on the same day, before 8 am on the next day
}
if (day == "weekday" && (end_time > 1920 && end_time <= 2520))
{
endzone = 3; //call ends on-hours on the next day
}
if (day == "weekday" && (end_time > 2520))
{
endzone = 4; //call ends after 6 pm the next day
}
if (day == "weekend" && (end_time <= 2880))
{
endzone = 5; //call starts and ends on the weekend
}
if (day == "Fr" && (end_time >= 1440 && end_time <= 2880))
{
endzone = 6; //call goes from weekday to weekend
}
if (day == "Su" && (end_time >= 1440 && end_time <= 1920))
{
endzone = 7; //call goes from weekend to weekday off-hours
}
//Cost calculations
//CALL STARTS ON HOURS
if (startzone == 1 && endzone == 1) //call is entirely within on-hours
{
cost = duration*0.4;
}
if (startzone == 1 && endzone == 2) //call starts on-hours and ends before 8 am the next day
{
cost = ((1080-start_time)*0.4) + ((end_time-1080)*0.25);
}
if (startzone == 1 && endzone == 3) //call starts on-hours and ends on-hours the next day
{
cost = ((1080-start_time)*0.4) + (840*0.25) + ((end_time-1920)*0.4);
}
if (startzone == 1 && endzone == 4) //call starts on-hours and ends after 6 pm the next day
{
cost = ((1080-start_time)*0.4) + (840*0.25) + (600*0.4) + ((end_time-2520)*0.25);
}
if ((startzone == 1 && endzone == 6)) //call starts on hours friday and ends on the weekend
{
cost = ((1080-start_time)*0.4) + ((360)*0.25) + ((end_time-1440)*0.15);
}
//CALL STARTS OFF HOURS
if (startzone == 2 && endzone == 2) //call starts before 8 am and ends before 8 AM the next day
{
cost = ((480-start_time)*0.25) + (600*0.4) + ((end_time-1440)*0.25);
}
if (startzone == 2 && endzone == 3) //call starts before 8 am and ends on-hours the next day
{
cost = ((480-start_time)*0.25) + ((end_time-1920)*0.25);
}
if (startzone == 2 && endzone == 6) //call starts before 8 AM friday and ends on the weekend
{
cost = ((480 - start_time)*0.25) + (600*0.4) + (360*0.25) + ((end_time-1440)*0.15);
}
if (startzone == 3 && endzone ==6) //call starts after 6 PM friday and ends on the weekend
{
cost = ((1440-start_time)*0.25) + ((end_time-1440)*0.15);
}
if ((startzone == 3 && endzone == 2) || (startzone == 2 && end_time <= 480)) //call is entirely within off-hours
{
cost = duration*0.25;
}
if ((startzone == 3 && endzone == 3)) //call starts after 6 pm and ends on-hours the next day
{
cost = ((1920-start_time)*0.25) + ((end_time-1920)*0.4);
}
if ((startzone == 3 && endzone == 4)) //call starts after 6 pm and ends after 6 pm the next day
{
cost = ((1920-start_time)*0.25) + (600*0.4) + ((end_time-2520)*0.25);
}
//CALL STARTS ON WEEKEND
if (startzone == 4 && endzone == 5) //call is entirely within weekends
{
cost = duration*0.15;
}
if (startzone == 4 && endzone == 7) //call starts on sunday and ends before 8 am monday
{
cost = ((1440-start_time)*0.15) + ((end_time-1440)*0.25);
}
cout << setw(4)<< endl;
cout << day << " " << hr << ":" << minute << " " << duration << " $" << cost << "\n";
total += cost;
}
cout << setw(4) << endl;
cout << "\tTotal $" << total << "\n";
}
【问题讨论】:
-
1.我不希望 cost 显示为 0,我希望它显示为通话费用,由适当的公式 2 计算得出。如何降低值?
-
是否有可能在成本计算下没有一个 if 语句永远不会被调用,所以
cost总是0这意味着total总是0?您是否尝试过调试您的代码? -
调试器。使用调试器。调试器将帮助您在观察变量中的值时分别执行每个语句。请使用调试会话的结果编辑您的帖子。
-
您能否编辑问题以添加输入示例、预期计算(如何计算成本)以及程序给您的输出?
-
您发布的代码是输出零还是非常小的正数?您需要一次只关注一个问题,而不是代码的不同版本会做什么。另外,是的,调试器。