Record each customer's call and calculate each customer's monthly bill

Source: Internet
Author: User
Tags bool time and date

Topic Overview

The system records each customer's call and tells you each time period (00:00~01:00, 01:00~02:00, ...) Call charges. Requires that each customer's monthly bill be calculated.

Input

Each input contains a test case, and each test case consists of two parts: the call fee (cent) per minute for each time period and the call status for each customer.

The call cost is made up of 24 non-negative integers, representing the call cost for each hour within 24 hours.

The next n (<= 1000) line is the customer's call record. Each record consists of the following items: The customer's name, time and date (in Month:day:hour:minute format) and on-line ("on-line" or "off-line")

For each test case, the record is within the same one months. when sorted by time , all "on-line" records follow a "off-line" record. Any records that do not have a "off-line" match after any "on-line" record can be ignored; Similarly, records that do not have a "on-line" record before any "off-line" record can also be ignored. The test data guarantees that there is at least one "on-line" and "off-line" matching record, and that no client calls at the same time with two other customers. Time is made in 24 hours.

Output

For each test case, you need to print the monthly billing for each customer.

Customers are sorted alphabetically, and for each customer you need to print the following:

Line 1th: Customer name Bill corresponding month

Line 2nd: The start and end times of each call for the user, and the cost of the call

Last 1 lines: Total cost of the call

Ideas for solving problems

Because the call logs are unordered, we have the time for each customer to save the current call (on-line and off-line) with a vector; When all input is finished, the vector is sorted.

For each customer's call history match, find the call time and expense for each conversation, stored in another vector.

In order to facilitate indexing, the user's name and call status are saved in the map with the user's name as the primary key.

In the coding process, calculating the amount of each call is a complex place.

Problems encountered

First submitted, there are 2 points WA.

The problem is that if a customer has no (legal) call history for the month, no information is exported from that customer.

Source

#include <iostream> #include <iomanip> #include <fstream> #include <algorithm> #include &  
      
      
lt;string> #include <vector> #include <map> const int number_of_hours = 24;  
    struct Record {std::string time;  
BOOL Isonline;  
      
      
};  
    struct Call {std::string starttime;  
    Std::string Endtime;  
    int totaltime;  
int amount;  
      
      
};  
    struct Customer {std::vector<record> records;  
    Std::vector<call> callrecords;  
int TotalAmount;  
      
      
};  
int atoi (char digit0, char digit1) {return (Digit0-' 0 ') * + (Digit1-' 0 ');  
    int Gettotaltime (std::string time) {int day = Atoi (Time[3], time[4]);  
    int hour = Atoi (Time[6], time[7]);  
      
      
    int minute = Atoi (time[9], time[10]);  
Return day * hour + minute; int Gettotalminute (const STD::STRING&AMP;  
StartTime, const std::string& endtime) {return gettotaltime (endtime)-Gettotaltime (starttime); int Getcallamount (int* toll, const std::string& starttime, const std::string& endtime) {i  
    NT StartDay = atoi (Starttime[3], starttime[4]);  
    int endday = Atoi (Endtime[3], endtime[4]);  
    int starthour = Atoi (Starttime[6], starttime[7]);  
    int endhour = Atoi (Endtime[6], endtime[7]);  
    int startminute = Atoi (starttime[9], starttime[10]);  
      
      
    int endminute = Atoi (endtime[9], endtime[10]);  
    int callamount = 0;  
        for (int i = StartDay I <= Endday + + i) {int lowerbound = 0, upperbound = 23;  
        if (i = = startday) {lowerbound = Starthour;  
        } if (i = = endday) {upperbound = Endhour;  
        for (int j = lowerbound J <= Upperbound + + j) {Callamount + = toll[j] * 60; }} Callamount-=Toll[starthour] * startminute + toll[endhour] * (60-endminute);  
return callamount;  
    int Gettotalamount (const std::vector<call>& callrecords) {int totalamount = 0;  
    for (size_t i = 0; i < callrecords.size (); + + i) {totalamount + = Callrecords[i].amount;  
return totalamount;  
    } void Getcallrecords (int* toll, customer& Customer) {std::string previoustime;  
    BOOL Previousisonline = false; for (size_t i = 0; i < customer.records.size (); + + i) {if (!customer.records[i].isonline && Previ  
            Ousisonline) {call call;  
            Call.starttime = Previoustime;  
            Call.endtime = Customer.records[i].time;  
            Call.totaltime = Gettotalminute (Call.starttime, call.endtime);  
            Call.amount = Getcallamount (Toll, Call.starttime, call.endtime);  Customer.callRecords.push_back (call);
                  
            Previousisonline = false;  
            else if (customer.records[i].isonline) {previoustime = Customer.records[i].time;  
        Previousisonline = true;  
} Customer.totalamount = Gettotalamount (customer.callrecords); BOOL Comparerecords (const record& record1, const record& record2) {return record1.time &L T  
Record2.time;  
    int main () {using Std::cin;  
    Std::ifstream cin;  
    Cin.open ("Input.txt");  
    More wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/int toll[number_of_hours] = {0};  
    int n = 0;  
      
      
    Std::map<std::string, customer> customers;  
    Input for (int i = 0; i < number_of_hours + i) {cin >> toll[i];  
    CIN >> N;  
        for (int i = 0; i < n; + + i) {std::string name, time, Isonline; CIN >> Name >> time >> isonline;  
        Record record;  
        Record.time = time;  
        Record.isonline = (Isonline = = "on-line");  
    Customers[name].records.push_back (record); //Process for (Auto ITR = Customers.begin (); ITR!= customers.end (); + + ITR) {St  
        D::sort (Itr->second.records.begin (), Itr->second.records.end (), comparerecords);  
    Getcallrecords (toll, Itr->second);  
    } std::string Currentmonth;  for (Auto ITR = Customers.begin (); ITR!= customers.end (); + + ITR) {if (Itr->second.callrecords.size ()!=  
            0) {currentmonth = Itr->second.callrecords[0].starttime.substr (0, 2);  
        Break  
        }//output for (Auto ITR = Customers.begin (); ITR!= customers.end (); + + ITR) { if (itr->second.callrecords.size ()!= 0) {std::cout << itr->first << ""<< currentmonth << Std::endl; for (size_t i = 0; i < itr->second.callrecords.size (); + + i) {std::cout << Itr->second . CALLRECORDS[I].STARTTIME.SUBSTR (3, 8) << "" << itr->second.callrecords[i].endtime.substr (3, 8) < < "" << itr->second.callrecords[i].totaltime << "$" << s Td::fixed << std::setprecision (2) << (itr->second.callrecords[i].amount/100.0) <  
            < Std::endl;  
                Std::cout << "Total Amount: $" << std::fixed << std::setprecision (2) <<  
        (itr->second.totalamount/100.0) << Std::endl;  
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.