uva 187 Transaction Processing(simulation)

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 <iostream>
#include <cstdio>
#include <vector>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 1100;
string account_name[maxn];
struct transaction{
    vector<int> account;
    vector<double> dc;
    double sum;
}t[maxn];

void initial(){
    for(int i = 0;i < maxn; i++){
        account_name[i].clear();
        t[i].account.clear();
        t[i].dc.clear();
        t[i].sum = 0;
    }
}

void readcase(){
    string line;
    while(getline(cin , line) && !(line[0] == '0' && line[1] == '0' && line[2] == '0')){
        if(line.length() != 0){
            string nnn = line.substr(0 , 3) , account = line.substr(3 , line.length()-3);
            stringstream ss;
            ss << nnn;
            int n;
            ss >> n;
            account_name[n] = account;
        }
    }
    while(getline(cin , line) && !(line[0] == '0' && line[1] == '0' && line[2] == '0')){
        if(line.length() != 0){
            string sss = line.substr(0 , 3) , nnn = line.substr(3 , 3) , xxx = line.substr(6 , line.length()-6);
            stringstream s1 , s2 , s3;
            s1 << sss;
            s2 << nnn;
            s3 << xxx;
            int n , s;
            double x;
            s1 >> s;
            s2 >> n;
            s3 >> x;
            t[s].account.push_back(n);
            t[s].dc.push_back(x);
            t[s].sum += x;
        }
    }
}

void computing(){
    for(int i = 0; i < 1000; i++){
        if(fabs(t[i].sum-0.0) > 0.0){
            printf("*** Transaction %03d is out of balance ***\n" , i);
            for(int j = 0; j < t[i].account.size(); j++){
                printf("%03d %-30s %10.2lf\n", t[i].account[j] , account_name[t[i].account[j]].c_str() , t[i].dc[j]/100);
            }
            printf("999 Out of Balance                 %10.2lf\n" , fabs(t[i].sum-0.0)/100);
            printf("\n");
        }
    }
}

int main(){
    initial();
    readcase();
    computing();
    return 0;
}
/*
*** 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
nnn xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx vvvvvvv.vv
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值