学到知识:
1.getline(istream& is, string& str, char delim), 依次读取character,读到’\n’或者delim的时候停下来, 并将读取的字符串保存进srt.
// Method:
// 1.创建一客户类,用string存储Id, vector<pair<string, int> >存储对应date和amount,
// withdram --> amount < 0, 否则为deposit --> amount > 0. 再设置一个total表示最终交易结果
// 2.用vector<Customer>存储数据,依次读取这些数据,id相同则放进同一个类中.
// 3.根据id对vector<Customer>排序,然后输出结果即可.
//
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <utility>
#include <vector>
#include <sstream>
using namespace std;
int str2int(string str) {
int num = 0;
for (int i = 0; i < str.size(); i++) {
num = num * 10 + (str[i] - '0');
}
return num;
}
class Customer {
public:
Customer(string Id, string date, string act, string amount);
void setInfo(string date, string act, string amount);
string getId() const;
void print() const;
private:
string Id;
vector<pair<string, int> > info; // store date and amount
};
Customer::Customer(string Id, string date, string act, string amount) {
this->Id = Id;
int temp = str2int(amount);
if (act == "withdraw")
temp = 0 - temp;
info.push_back(make_pair(date, temp));
}
void Customer::setInfo(string date, string act, string amount) {
int temp = str2int(amount);
if (act == "withdraw")
temp = 0 - temp;
info.push_back(make_pair(date, temp));
}
string Customer::getId() const {
return Id;
}
void Customer::print() const {
int total = 0;
for (unsigned int i = 0; i < info.size(); i++) {
cout << Id << ',' << info[i].first << ',';
if (info[i].second < 0)
cout << "withdraw" << ',';
else
cout << "deposit" << ',';
cout << info[i].second << endl;
total += info[i].second;
}
cout << "Total:" << total << endl;
}
bool compare(Customer a, Customer b) {
return a.getId() < b.getId();
}
int main() {
string line;
vector<Customer> customer;
ifstream in;
in.open("demo.txt");
if (!in) {
cerr << "failed to open" << endl;
return 1;
}
getline(in, line);
while (!in.eof()) {
getline(in, line);
// ------------
// when read the end of the file, it would keep reading,
// even it is "" (void)
if (line == "") break;
//----------------------------------------------------------
// - using getline(istream& is, string &str, char delim) -
// - rea1 characters until meeting "delim" or "\n" -
//----------------------------------------------------------
stringstream sstr(line);
string temp;
getline(sstr, temp, ',');
string Id = temp;
getline(sstr, temp, ',');
string date = temp;
getline(sstr, temp, ',');
string act = temp;
getline(sstr, temp, ',');
string amount = temp;
int flag = 0;
for (unsigned int i = 0; i < customer.size(); i++) {
// if the customer already in the vector<Customer>
// then just add the information of the trede
if (Id == customer[i].getId()) {
flag = 1;
customer[i].setInfo(date, act, amount);
break;
}
}
// if the coustomer in not in the vector<Customer>
// then creat a new space for it
if (flag == 0) {
Customer t(Id, date, act, amount);
customer.push_back(t);
}
}
in.close();
stable_sort(customer.begin(), customer.end(), compare);
for (unsigned int i = 0; i < customer.size(); i++)
customer[i].print();
return 0;
}
Core part:
getline(in, line);
// ------------
// when read the end of the file, it would keep reading,
// even it is "" (void)
if (line == "") break;
//----------------------------------------------------------
// - using getline(istream& is, string &str, char delim) -
// - rea1 characters until meeting "delim" or "\n" -
//----------------------------------------------------------
stringstream sstr(line);
string temp;
getline(sstr, temp, ',');
string Id = temp;
getline(sstr, temp, ',');
string date = temp;
getline(sstr, temp, ',');
string act = temp;
getline(sstr, temp, ',');
string amount = temp;
The question:
Description
银行根据每个月的客户流水账单,需要打印每个月每个客户的交易情况。要求打印按照客户账号排序,每个客户按照其交易发生日期排序。
流水单(Log file)和输出要求见样例文件。
Sample Input
Copy sample input to clipboard
Id date Act Amount
6222010002,2017-08-01,deposit,1700
6222010005,2017-08-02,withdraw,2900
6222010004,2017-08-03,deposit,200
6222010002,2017-08-04,deposit,1300
6222010007,2017-08-06,withdraw,200
6222010000,2017-08-06,withdraw,1500
6222010002,2017-08-07,withdraw,2000
6222010006,2017-08-07,withdraw,200
6222010003,2017-08-08,deposit,2100
6222010005,2017-08-09,withdraw,1300
6222010006,2017-08-11,deposit,1300
6222010003,2017-08-11,deposit,2500
6222010001,2017-08-12,withdraw,1100
6222010005,2017-08-12,deposit,1900
6222010002,2017-08-14,deposit,2000
6222010009,2017-08-14,withdraw,700
6222010008,2017-08-16,withdraw,1000
6222010003,2017-08-17,withdraw,2000
6222010005,2017-08-17,deposit,2400
6222010006,2017-08-18,deposit,1100
6222010008,2017-08-18,deposit,100
6222010002,2017-08-19,withdraw,2600
6222010006,2017-08-19,deposit,1700
6222010008,2017-08-20,withdraw,200
6222010005,2017-08-21,withdraw,600
6222010000,2017-08-21,deposit,2200
6222010005,2017-08-22,withdraw,800
6222010002,2017-08-22,withdraw,2400
6222010004,2017-08-23,withdraw,2700
6222010006,2017-08-24,deposit,2500
6222010001,2017-08-25,withdraw,2700
6222010004,2017-08-26,withdraw,2300
6222010006,2017-08-28,deposit,2100
6222010008,2017-08-31,withdraw,2700
Sample Output
6222010000,2017-08-06,withdraw,1500
6222010000,2017-08-21,deposit,2200
Total:700
6222010001,2017-08-12,withdraw,1100
6222010001,2017-08-25,withdraw,2700
Total:-3800
6222010002,2017-08-01,deposit,1700
6222010002,2017-08-04,deposit,1300
6222010002,2017-08-07,withdraw,2000
6222010002,2017-08-14,deposit,2000
6222010002,2017-08-19,withdraw,2600
6222010002,2017-08-22,withdraw,2400
Total:-2000
6222010003,2017-08-08,deposit,2100
6222010003,2017-08-11,deposit,2500
6222010003,2017-08-17,withdraw,2000
Total:2600
6222010004,2017-08-03,deposit,200
6222010004,2017-08-23,withdraw,2700
6222010004,2017-08-26,withdraw,2300
Total:-4800
6222010005,2017-08-02,withdraw,2900
6222010005,2017-08-09,withdraw,1300
6222010005,2017-08-12,deposit,1900
6222010005,2017-08-17,deposit,2400
6222010005,2017-08-21,withdraw,600
6222010005,2017-08-22,withdraw,800
Total:-1300
6222010006,2017-08-07,withdraw,200
6222010006,2017-08-11,deposit,1300
6222010006,2017-08-18,deposit,1100
6222010006,2017-08-19,deposit,1700
6222010006,2017-08-24,deposit,2500
6222010006,2017-08-28,deposit,2100
Total:8500
6222010007,2017-08-06,withdraw,200
Total:-200
6222010008,2017-08-16,withdraw,1000
6222010008,2017-08-18,deposit,100
6222010008,2017-08-20,withdraw,200
6222010008,2017-08-31,withdraw,2700
Total:-3800
6222010009,2017-08-14,withdraw,700
Total:-700