2016-07-26 67 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; 
}