如何添加循环输入文件的结果? 我是C++初学者,创建了一个输入的文件,计算的调用率。我能够单独计算每个电话的费用;然而,我不知道如何

如何添加循环输入文件的结果? 我是C++初学者,创建了一个输入的文件,计算的调用率。我能够单独计算每个电话的费用;然而,我不知道如何,c++,if-statement,while-loop,ifstream,C++,If Statement,While Loop,Ifstream,将每次通话的结果汇总在一起 准确计算从白天到夜间/工作日到周末的通话成本 这就是我目前所拥有的。非常感谢您的帮助。谢谢大家! 调用_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> #inclu

将每次通话的结果汇总在一起

  • 准确计算从白天到夜间/工作日到周末的通话成本

  • 这就是我目前所拥有的。非常感谢您的帮助。谢谢大家!

    调用_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;
    }
    
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    常数双日=0.40;
    常数双夜=0.25;
    常数双周=0.15;
    int main()
    {
    流鳍;
    财务公开(“Call_History.txt”);
    弦日;
    整数时间;
    int持续时间;
    int dayOfWeek;
    双重成本;
    双倍总数;
    //设置数字输出格式。
    cout>持续时间)
    {
    如果(日=“月”)
    {
    dayOfWeek=1;
    }
    否则,如果(日期=“Tu”)
    {
    dayOfWeek=2;
    }
    否则,如果(日期=“我们”)
    {
    每周一天=3;
    }
    否则,如果(日=“第日”)
    {
    每周一天=4;
    }
    否则,如果(日期=“Fr”)
    {
    星期五=5天;
    }
    否则,如果(日期=“Sa”)
    {
    星期五=6天;
    }
    否则,如果(日期=“Su”)
    {
    每周一天=7;
    }
    //根据费率表确定通话成本。
    如果((时间>=800)&(时间)
    
  • 创建一个求和变量,例如
    double sum=0.0;
  • 从输入行读取
    cost
    后,将其添加到总和:
    sum+=cost;

  • 当您打印总数时,请打印总和:
    coutCall history include price,这样您列出的程序在第一行之后就不会工作了。但是,如果您像这样修改
    Call\u history.txt
    ,它会工作

    Mo 1330 16
    
    Mo 815  35
    
    Tu 750  20
    
    We 1745 30
    
    Th 800  45
    
    Su 2350 30
    

    除了一个简单的建议来总结你在Toe/<代码>变量中得到的所有成本,你还应该考虑将你的程序重构成简单的函数,例如“代码> DayOfWea/<代码>,而成本因子计算只需要放在单独的函数

    中。
    #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;
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    //函数来包装日期查找
    int getDayOfWeek(字符串常量和日期){
    //创建常量映射以使查找更方便
    静态常量映射dayOfWeek={{“Mo”,1},{“Tu”,2},{“We”,3},{“Th”,4},{“Fr”,5},{“Sa”,6},{“Su”,7};
    自动dayIterator=dayOfWeek.find(天);
    if(dayIterator==dayOfWeek.cend()){
    //找不到指定日期的匹配项
    返回0;
    }
    返回dayIterator->second;
    }
    双getCostMultiplier(整数时间,整数周数){
    常数双日=0.40;
    常数双夜=0.25;
    常数双周=0.15;
    //评估一周中的哪一天,使比较更简单、更容易阅读
    如果(星期五>5天){
    周末返回;
    }
    否则,如果((时间>=800)和((持续时间)
    {
    dayOfWeek=getDayOfWeek(天);
    成本=持续时间*获取成本乘数(时间,星期几);
    
    cout Off-topic:每行有四个输入或五个令牌(
    Mo 1330 16$6.40
    )和三个读取令牌(
    fin>>day>>time>>duration
    )。阅读价格可能会让你感到合适,所以我会在做得太过深入,不得不做大量的重写来适应它之前弄清楚。另外,离题:你可以将dayOfWeek变量更改为isWeekday变量,从而将if语句从7减少到2。即,if day==“Mo”|“Tu”。。。