Cod sursa(job #879510)

Utilizator bratiefanutBratie Fanut bratiefanut Data 15 februarie 2013 15:26:37
Problema A+B Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include<fstream>
#include<string.h>
#include<stdio.h>
#include<algorithm>

using namespace std;

ifstream f("adunare.in");
ofstream g("adunare.out");

char a[100],b[100],c[100];
int i,t,x,y;

int minim(int x, int y)
{
    if(x<=y)
    return x;
    return y;
}

void adunare(char a[100], char b[100], char c[100])
{
    reverse(a,a+strlen(a));
    reverse(b,b+strlen(b));
    t=0;
    y=minim(strlen(a),strlen(b));

    for(i=0;i<=y-1;i++)
    {
        x=(a[i]-'0')+(b[i]-'0')+t;
        c[i]=x%10+'0';
        t=x/10;
    }
    if((int)strlen(a)>y)
        for(i=y;i<=(int)strlen(a)-1;i++)
            c[i]=a[i];
    if((int)strlen(b)>y)
        for(i=y;i<=(int)strlen(b)-1;i++)
            c[i]=b[i];
    if(t)
    {
    c[y]=t+'0';
    y++; }

    reverse(c,c+strlen(c));
}

int main()
{
    f.getline(a,100);
    f.getline(b,100);

    adunare(a,b,c);

    g<<c;

    return 0;
}