summaryrefslogtreecommitdiffstats
path: root/mat/euler/13/dodaj.c
blob: d1fbb598a6c9cb18a063c7495e92ce13e5322c36 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int dodaj (const char * c, char * d, long int a, long int b) {
	unsigned long long int carry = 0; // carry je pri seštevanju vedno pozitiven
	long int diff = b-a;
	unsigned long long int dodajmo_tole = 0;
	unsigned long long int temu = 0;
	for (b = b-1; b >= 0; b--) {
		if (b-diff >= 0) {
			dodajmo_tole = c[b-diff] - '0';
		} else {
			dodajmo_tole = 0;
		}
		temu = d[b] - '0';
		dodajmo_tole = dodajmo_tole + carry;
		carry = 0;
		if (temu + dodajmo_tole > 9) {
			dodajmo_tole = (temu + dodajmo_tole) - 10;
			temu = 0;
			carry++;
		}
		d[b] = temu + dodajmo_tole + '0';
	}
	return 0;
}
#ifndef DISABLE_LIB_TESTS
int main (int argc, char ** argv) {
	char c[] = "37107287533902102798797998220837590246510135740250";
	char d[] = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000";
	int a = strlen(c);
	int b = strlen(d);
	dodaj(c, d, a, b);
	fprintf(stdout, "test: %s\n", d);
	return 0;
}
#endif