2016-07-26 89 views
-2

我是C++的初学者,创建了一个包含计算出的通话费率的输入文件。我能够分别计算每次通话的成本;然而,我不知道如何...如何添加循环输入文件的结果?

  1. 把每个电话的结果,并总计在一起。

  2. 准确计算从白天到夜间/周日到周末的通话费用。

这是我到目前为止。任何帮助深表感谢。谢谢!

Call_History.txt

(Day/Time/Duration/Cost) 

Mo 1330 16 $6.40 

Mo 815 35 $14.00 

Tu 750 20 $3.00 

We 1745 30 $12.00 

Th 800 45 $18.00 

Su 2350 30 $4.50 

代码

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 

using namespace std; 

const double DAYTIME = 0.40; 
const double NIGHT = 0.25; 
const double WEEKEND = 0.15; 

int main() 
{ 
    ifstream fin; 
    fin.open("Call_History.txt"); 

    string day; 
    int time; 
    int duration; 
    int dayOfWeek; 
    double cost; 
    double total; 

    // Set the numeric output formatting. 
    cout << fixed << showpoint << setprecision(2); 

    cout << "Day Time Duration Cost\n" << endl; 

    while (fin >> day >> time >> duration) 
    { 
     if (day == "Mo") 
     { 
      dayOfWeek = 1; 
     } 
     else if (day == "Tu") 
     { 
      dayOfWeek = 2; 
     } 
     else if (day == "We") 
     { 
      dayOfWeek = 3; 
     } 
     else if (day == "Th") 
     { 
      dayOfWeek = 4; 
     } 
     else if (day == "Fr") 
     { 
      dayOfWeek = 5; 
     } 
     else if (day == "Sa") 
     { 
      dayOfWeek = 6; 
     } 
     else if (day == "Su") 
     { 
      dayOfWeek = 7; 
     } 

     // Determine cost of call based on rate schedule. 
     if ((time >= 800) && (time <= 1800) && (dayOfWeek <= 5)) 
     { 
      cost = duration * DAYTIME; 
     } 
     else if ((time < 800) && (time > 1800) && (dayOfWeek <= 5)) 
     { 
      cost = duration * NIGHT; 
     } 
     else 
     { 
      cost = duration * WEEKEND; 
     } 
     cout << day << " " << time << " " << duration << " $" << cost << endl; 

    } 

    cout << "\nTotal $" << endl; 

    return 0; 
} 
+0

题外话:你有每行四个输入或五个标记('莫1330 $ 16 6.40')和三个标记读取('鳍>>天> >时间>>持续时间)。阅读价格可能会让你觉得合适,所以我会在进一步深入之前弄清楚,并且不得不做大量的重新编写以适应它。 – user4581301

+0

也是关键主题:你可以将dayOfWeek变量改为isWeekday变量,从而将你的if语句从7减少到2.即如果day ==“Mo”| “屠” ... – user3605508

回答

0
  1. 创建总和变数,如double sum = 0.0;
  2. 后,你读的输入线cost,加它的总和为:sum += cost;
  3. 打印总数时,打印总和:cout << "\nTotal $" << sum << endl;
0

通话记录包括价格,所以您列出的程序在第一行之后将不起作用。但是,如果修改Call_History.txt这样,它会工作

Mo 1330 16 

Mo 815 35 

Tu 750 20 

We 1745 30 

Th 800 45 

Su 2350 30 

除了一个简单的建议,总结一下你在total变量获取成本,你也应该考虑重构你的程序转化为简单的功能,例如, dayOfWeek和成本因素的计算只是乞求被放置在单独的功能

#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 
#include <map> 

using namespace std; 

//function to wrap the day lookup 
int getDayOfWeek(string const &day) { 
    //create a constant map to make lookup more convenient 
    static const map<string, int> dayOfWeek = { { "Mo", 1 },{ "Tu", 2 },{ "We", 3 },{ "Th", 4 },{ "Fr", 5 },{ "Sa", 6 },{ "Su", 7 } }; 
    auto dayIterator = dayOfWeek.find(day); 
    if (dayIterator == dayOfWeek.cend()) { 
     //Couldn't find a match for a specified day 
     return 0; 
    } 
    return dayIterator->second; 
} 

double getCostMultiplier(int time, int dayOfWeek) { 
    const double DAYTIME = 0.40; 
    const double NIGHT = 0.25; 
    const double WEEKEND = 0.15; 

    //evaluate day of week to make comparisons simplier and easier to read 
    if (dayOfWeek > 5) { 
     return WEEKEND; 
    } 
    else if ((time >= 800) && (time <= 1800)) 
    { 
     return DAYTIME; 
    } 
    else 
    { 
     return NIGHT; 
    } 

    //default case so that if the conditions would change and there would be other cases this function would return something meaningful 
    return WEEKEND; 
} 

int main() 
{ 
    ifstream fin; 
    fin.open("Call_History.txt"); 

    string day; 
    int time = 0; 
    int duration = 0; 
    int dayOfWeek = 0; 
    double cost = 0; 
    double total = 0; 

    // Set the numeric output formatting. 
    cout << fixed << showpoint << setprecision(2); 

    cout << "Day Time Duration Cost\n" << endl; 

    while (fin >> day >> time >> duration) 
    { 
     dayOfWeek = getDayOfWeek(day); 
     cost = duration * getCostMultiplier(time, dayOfWeek); 
     cout << day << " " << time << " " << duration << " $" << cost << endl; 

     //add cost to total 
     total += cost; 
    } 

    cout << "\nTotal $" << total << endl; 

    return 0; 
}