[UVA][模擬] 187 - Transaction Processing
Transaction Processing
Transaction Processing |
You have been called upon to write a program which performs one of the initial steps in posting transactions to a general ledger. The central principle of double-entry bookkeeping is that the sum of all debits must equal the sum of all credits. This is true for each transaction. For the purposes of your program, positive numbers represent debits and negative numbers represent credits. That is, 2.00 is a two dollar debit, and -2.00 is a two dollar credit. The purpose of your program is to check that each transaction balances, and to report it if it doesn't.
Input
Input data to your program will come in two sections. The first section is a list of up to 100 accounts in the general ledger. It consists of lines in the format:
nnnxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
where nnn is a three-digit account number and xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx is a 1-30 character account name string. This section is terminated by a record starting with 000, which is not used as an account number.
The second section of the input data consists of 15-character records, one per line in the format
sssnnnxxxxxxxxx
where sss is a three-digit sequence number, nnn is a three-digit account number, and xxxxxxxxx is a nine-digit amount in dollars and cents (without the decimal point). Each of these records is one entry of a transaction. A transaction consists of between two and ten entries with identical sequence numbers. Each transaction will be contiguous within the input data. This section of input data is terminated by a record which has a sequence number of 000.
Output
Nothing is to be printed for transactions which balance. For transactions which do not balance, an exception report is to be printed in the form:
*** Transaction sss is out of balance *** nnn xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx vvvvvvv.vv nnn xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx vvvvvvv.vv . . . 999 Out of Balance vvvvvvv.vv
where nnn is an account number, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx is the corresponding account name, and vvvvvvv.vv is the amount. Print a space between the above fields. The entries should be listed in the order that they were received in the input. The last entry in the report is one you will create to make the transaction balance, using the special account number 999 (the suspense account). Print a blank line after each exception report.
Sample input
111Cash 121Accounts Receivable 211Accounts Payable 241Sales Tax Payable 401Sales 555Office Supplies 000No such account 100111 11795 100121 -11795 101121 105 101241 -7 101401 -100 102211 -70000 102555 40000 103111 -40000 103555 40000 000000 0
Sample output
*** Transaction 101 is out of balance *** 121 Accounts Receivable 1.05 241 Sales Tax Payable -0.07 401 Sales -1.00 999 Out of Balance 0.02 *** Transaction 102 is out of balance *** 211 Accounts Payable -700.00 555 Office Supplies 400.00 999 Out of Balance 300.00
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
struct D {
int cost;
int it;
};
int main() {
string line, name;
string item[1000];
int num;
while(getline(cin, line)) {
name = line.substr(3);
num = (line[2]-'0')+(line[1]-'0')*10+(line[0]-'0')*100;
if(!num) break;
item[num] = name;
}
vector<D> cust[1000];
int cost, it;
while(cin >> line >> cost) {
num = (line[2]-'0')+(line[1]-'0')*10+(line[0]-'0')*100;
it = (line[5]-'0')+(line[4]-'0')*10+(line[3]-'0')*100;
if(num == 0) break;
D ele;
ele.cost = cost;
ele.it = it;
cust[num].push_back(ele);
}
int i, j;
for(i = 0; i < 1000; i++) {
int r[1000] = {}, sum = 0;
for(vector<D>::iterator it = cust[i].begin();
it != cust[i].end(); it++) {
r[it->it] += it->cost;
sum += it->cost;
}
if(sum) {
printf("*** Transaction %03d is out of balance ***\n",i);
for(j = 0; j < 1000; j++) {
if(r[j]) {
printf("%03d %-30s %10.2lf\n", j, item[j].c_str(), r[j]/100.0);
}
}
printf("999 Out of Balance %10.2lf\n", -sum/100.0);
puts("");
}
}
return 0;
}