Answer to Question #318038 in C++ for Nobody

Question #318038

The XYZ Telephone Company’s charges file, contains records for each call made by its subscribers during a month. Each record on a file contains the subscriber’s name and phone number, the phone number called, the distance from Shah Alam of the number called (in kilometres) and duration of the call in seconds.

Design an algorithm that will read the Tidy Phones charges file and produce a telephone charges report, as follows:

 

The cost of each call is calculated based on table 1.

Distance from Metroville

Cost($)/minute

Less than 25 km

0.35

25 <= km < 75

0.65

75 <= km < 300

1.00

300 <= km <=1000

2.00

Greater than 1000 km

3.00

 

Write C++ complete programs for the above problem. You program must use control structure and user defined function.

 


1
Expert's answer
2022-03-27T09:05:28-0400

File records.txt:

Alice 123-4567 987-9988 50 100
Bob   987-9988 123-4567 50 200
Charly 222-3456 123-4567 500 600
Alice 123-456 222-3456 500 200


#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <map>
using namespace std;


struct Subscriber {
    string name;
    string phone;
    int duration;
    double charge;
    Subscriber(string n="", string ph="", int dur=0, double ch=0) {
        name = n;
        phone = ph;
        duration = dur;
        charge = ch;
    }
};


double calcCost(int distance, int minutes) {
    double cost;


    if (distance < 25) {
        cost = 0.35;
    }
    else if (distance < 75) {
        cost = 0.65;
    }
    else if (distance < 300) {
        cost = 1.0;
    }
    else if (distance < 1000) {
        cost = 2.0;
    }
    else {
        cost = 3.0;
    }
    return cost*minutes;
}


void createReport(map<string, Subscriber>& report, char* file) {
    string name;
    string phone;
    string called;
    int dist;
    int sec, min;
    double cost;


    ifstream ifs(file);


    while (ifs) {
        ifs >> name >> phone >> called >> dist >> sec;
        min = (sec + 59) / 60;
        cost = calcCost(dist, min);


        if (report.count(name) == 0) {
            report[name] = Subscriber(name, phone, min, cost);
        }
        else {
            report[name].duration += min;
            report[name].charge += cost;
        }
    }
}


void printReport(map<string, Subscriber>& report) {
    cout << "Name        phone     min     charge" << endl;


    for (auto it= report.begin(); it!=report.end(); it++) {
        cout << setw(10) << it->second.name << setw(10) << it->second.phone 
             << setw(5) << it->second.duration 
             << setw(10) << fixed << setprecision(2) << it->second.charge << endl;
    }
}


int main() {
    map<string, Subscriber> report;

    createReport(report, "records.txt");
    printReport(report);

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog