uva424Integer Inquiry

题目:Integer Inquiry


题意:高精度整数加法;


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

const int N = 105;
int t = 0;
char s[N];

class begin {

public:

	int len, s[N];
	begin() {  memset(s, 0, sizeof(s)); len = 1; }
	begin operator = (char * s1);
	begin operator += (const begin &x);

} b[N];

begin begin::operator = ( char *s1) {

	len = strlen(s1);
	for (int i = 0; i < len; i++) {
		
		s[i] = s1[len - 1 - i] - '0';
	}

	return *this;
}

begin begin::operator += (const begin &x)  {

	int c = 0, i;
	int l = (len > x.len)? len : x.len;
	for( i = 0; c || i < l; i++) {
		
		c = x.s[i] + s[i] + c;
		s[i] = c % 10;
		c = c / 10; 
		
	}
	len = i ;
	
	return *this;
}


int main() {

	while (scanf("%s",s) ) {

		
		if(strcmp(s,"0") == 0)
			break;

		b[t++] = s;

		memset(s, 0 ,sizeof(s));

	}
	for (int i = 1; i < t; i++) {

		b[0] += b[i];
	}
	for (int j = b[0].len - 1; j >= 0; j--)
	
		printf("%d",b[0].s[j]);
	printf("\n");

	return 0;
}


你可能感兴趣的:(uva424Integer Inquiry)