View Code of Problem 1

#include <stdio.h>
int main() {
    int a = 0;
    int b = 0;
	int minusA = 0;
	int minusB = 0;
	char c;
	char target = 'a';
	while((c = getchar()) != EOF){
		if (c == '\n') {
			if (minusA) {
				a = -a;
			}
			if (minusB) {
				b = -b;
			}
			printf("%d\n",a + b);
			a = 0;
			b = 0;
			minusA = 0;
			minusB = 0;
			target = 'a';
		} else {
			if (c == ' ') {
				target = 'b';
			} else if (c == '-') {
				if (target == 'a') {
					minusA = 1;
				} else {
					minusB = 1;
				}
			} else if (target == 'a'){
				a = a * 10 + (c - '0');
			} else {
			    b = b * 10 + (c - '0');
			}
		}
	}
	
	if (minusA) {
		a = -a;
	}
	if (minusB) {
		b = -b;
	}
	printf("%d",a + b);
	return 0;
}

Double click to view unformatted code.


Back to problem 1