View Code of Problem 94

#include<stdio.h>
#include<string.h>
#define MAX 1000000+10
#define Min(a,b) a<b?a:b
char s[MAX];
int stack[MAX];
int n;
char operater(int ch,int s,int e){
	int sum=stack[s]-'0';
	for(int i=s+1;i<e;i++){
		switch(ch){
			case '+'-'0':
				sum+=stack[i];
				break;
			case '-'-'0':
				sum-=stack[i];
				break;
			case '*'-'0':
				sum*=stack[i];
				break;
			case '/'-'0':
				sum/=stack[i];
				break;
		}
	}
	return sum;
}
int  out(int e){
	int i=e-1;
	while(1){
		if(stack[i]=='('-'0')break;
		i--;
	}
	stack[i]=operater(stack[i+1],i+2,e);
	return i;
}
void work(){
	int i=0,flag=0,m=0;
	char ch;
	for(int j=0;j<n;j++){
		if(s[j]!=' '){
			if(s[j]=='(')stack[i++]=s[j]-'0';
			else if(s[j]==')')stack[i++]=s[j]-'0';
			else stack[i++]=s[j]-'0';
		}
		if(stack[i]==')'-'0')i=out(i);
	}
	printf("%d\n",stack[0]);
	return ;
}
int main(){
	while(1){
		gets(s)
		n=strlen(s);
		work();
	}
	return 0;
}
/*
Main.c: In function 'work':
Main.c:39:7: warning: unused variable 'ch' [-Wunused-variable]
  char ch;
       ^
Main.c:38:17: warning: unused variable 'm' [-Wunused-variable]
  int i=0,flag=0,m=0;
                 ^
Main.c:38:10: warning: unused variable 'flag' [-Wunused-variable]
  int i=0,flag=0,m=0;
          ^
Main.c: In function 'main':
Main.c:53:3: warning: 'gets' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
   gets(s)
   ^
Main.c:54:3: error: expected ';' before 'n'
   n=strlen(s);
   ^
*/

Double click to view unformatted code.


Back to problem 94