Pagini recente » Cod sursa (job #800654) | Cod sursa (job #2558952) | Cod sursa (job #378550) | Cod sursa (job #2896236) | Cod sursa (job #1662108)
#include <stdio.h>
#include <ctype.h>
#define NR_CIFRE 30
void citire(char* nr) {
int n = 0;
do {
scanf("%c", &nr[n]);
} while (!isdigit(nr[n]));
do {
n++;
scanf("%c", &nr[n]);
} while (isdigit(nr[n]));
int i;
for (i = 0; i < n; i++) {
nr[i] -= '0';
}
for (i = 0; 2 * i < n; i++) {
//nr[i] ^= nr[n - i - 1] ^= nr[i] ^= nr[n - i - 1];
int aux = nr[i];
nr[i] = nr[n - i - 1];
nr[n - i - 1] = aux;
}
nr[n] = -1;
}
void afisare(char* nr) {
int i = 0;
while (nr[i] != -1) {
i++;
}
//assert(nr[i] == -1);
for (i--; i >= 0; i--) {
printf("%d", nr[i]);
}
}
void adunare(char* const a, char* const b, char* suma) {
//suma = a + b
int i = 0;
suma[i] = 0;
while (a[i] != -1 && b[i] != -1) {
suma[i] += a[i] + b[i];
if (suma[i] >= 10) {
suma[i + 1] = 1;
suma[i] -= 10;
} else {
suma[i + 1] = 0;
}
i++;
}
if (a[i] != -1) {
while (a[i] != -1) {
suma[i] += a[i];
if (suma[i] >= 10) {
suma[i + 1] = 1;
suma[i] -= 10;
} else {
suma[i + 1] = 0;
}
i++;
}
} else {
while (b[i] != -1) {
suma[i] += b[i];
if (suma[i] >= 10) {
suma[i + 1] = 1;
suma[i] -= 10;
} else {
suma[i + 1] = 0;
}
i++;
}
}
if (suma[i] == 0) {
suma[i] = -1;
} else {
suma[i + 1] = -1;
}
//suma[i + (suma[i] != 0)] = -1;
}
void scadere(char* const a, char* const b, char* diferenta) {
//assert(a >= b);
//diferenta = a - b
int i = 0;
diferenta[i] = 0;
while (a[i] != -1 && b[i] != -1) {
diferenta[i] += a[i] - b[i];
if (diferenta[i] < 0) {
diferenta[i + 1] = -1;
diferenta[i] += 10;
} else {
diferenta[i + 1] = 0;
}
i++;
}
while (a[i] != -1) {
diferenta[i] += a[i];
if (diferenta[i] < 0) {
diferenta[i + 1] = -1;
diferenta[i] += 10;
} else {
diferenta[i + 1] = 0;
}
i++;
}
while (i > 1 && diferenta[i - 1] == 0)
i--;
diferenta[i] = -1;
}
//nr1 = 12
//nr2 = 12
//char nr1[] = {4, 3, 2, 1, -1};
//char nr2[] = {7, 6, 6, -1};
//char sum[] = {1, 0, 9, 0};
char nr1[NR_CIFRE];
char nr2[NR_CIFRE];
char suma[NR_CIFRE];
char diferenta[NR_CIFRE];
int main() {
freopen("adunare.in", "r", stdin);
freopen("adunare.out", "w", stdout);
citire(nr1);
citire(nr2);
adunare(nr1, nr2, suma);
scadere(nr1, nr2, diferenta);
afisare(suma);
printf("\n");
afisare(diferenta);
return 0;
}