UVA Solution 187 - Transaction Processing - Solution in C++ | Volume 1 - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Sunday, April 30, 2017

UVA Solution 187 - Transaction Processing - Solution in C++ | Volume 1

UVA Solution 187 - Transaction Processing - Solution in C++ | Volume 1


UVA Online Judge Solution 187- Transaction Processing| Volume 1


Problem Name: Transaction Processing
Problem Number : UVA - 187
Online Judge : UVA Online Judge Solution
Volume: 1
Solution Language : C plus plus


UVA Solution 187 Code in CPP:

#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;
}

No comments:

Post a Comment