Solução: Calculando - OBI (Delson Rosario)

From AdonaiMedrado.Pro.Br
Jump to: navigation, search
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int numero(char *num){
	int i=0,result=0;
	while(isdigit(num[i])){ 
		result = result*10 + (num[i]-48);
		i++;
	}
	return result;
}
 
main(){
	int i,j,result,nteste=0;
	char signal,*expressao;
	scanf("%d",&i);
	while (i>0){
		nteste++;
		expressao = malloc(2*i*sizeof(int));
		scanf("%s",expressao);
		result = numero(&expressao[0]);
		for (j=1;j<strlen(expressao);){
			if (expressao[j] == '+') result = result + (numero(&expressao[j+1]));
			else result = result - (numero(&expressao[j+1]));
			j++;
			while(isdigit(expressao[j])) j++;
		}
		printf("Teste %d\n%d\n",nteste,result);
		scanf("%d",&i);
	}
}