Cod sursa(job #1252606)

Utilizator vdevVlad Ungureanu vdev Data 30 octombrie 2014 22:24:45
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.3 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main(int argc, const char * argv[]) {
    vector<char> final_result;
    vector<char> a, b;
    int a_int, b_int;
    bool reminder = false;
    
    fstream adunare_in("adunare.in", std::ios_base::in);
    fstream adunare_out("adunare.out", std::ios_base::out);
    
    adunare_in >> a_int;
    adunare_in >> b_int;
    while (a_int) {
        int x = a_int % 10;
        a_int /= 10;
        
        a.push_back('0'+x);
    }
    
    while (b_int) {
        int x = b_int % 10;
        b_int /= 10;
        
        b.push_back('0' + x);
    }
    
    vector<char>::iterator a_iterator = a.begin();
    vector<char>::iterator b_iterator = b.begin();
    
    while (a_iterator != a.end() && b_iterator != b.end()) {
        int result = (*a_iterator) - '0' + (*b_iterator) - '0';
        
        if (reminder ==  true) {
            result += 1;
            reminder = false;
        }
        if (result >= 10) {
            reminder = true;
            result = result % 10;
        }
        else {
            reminder = false;
        }
        cout << result % 10<< endl;
        final_result.push_back('0' + result % 10);
        a_iterator++;
        b_iterator++;
    }
    
    while (a_iterator != a.end()) {
        int result = *a_iterator - '0';
        if (reminder == true) {
            result += 1;
            reminder = false;
        }
        
        if (result >= 10) {
            reminder = true;
            result = result % 10;
        }
        else {
            reminder = false;
        }
        
        final_result.push_back('0' + result % 10);
        *a_iterator++;
    }
    
    while (b_iterator != b.end()) {
        int result = *b_iterator - '0';
        if (reminder == true) {
            result += 1;
            reminder = false;
        }
        
        if (result >= 10) {
            reminder = true;
            result = result % 10;
        }
        else {
            reminder = false;
        }
        
        final_result.push_back('0' + result % 10);
        *b_iterator++;
    }
    
    
    reverse(final_result.begin(), final_result.end());
    for(vector<char>::iterator it = final_result.begin(); it != final_result.end(); it++)
        adunare_out<<*it;
    adunare_out<<endl;

    return 0;
}