B. 四则运算
运行时间<1000 ms 单次测试运行时间限制: 1000 ms 内存限制: 64 MB
总提交: 2次 通过: 1次
题目描述
给定一个四则运算表达式,包括 + - * / ( ),计算结果
程序输入说明
有N行输入,每行有一个四则运算表达式,不超过10个数字ai( 1 < = i < = 10 )。
为了简便定 0 < = ai < = 9
程序输出说明
有N行输出,每行一个运算结果
程序输入样例
可见格式 带空格和换行符的格式 带空格和换行符的格式说明
1+1
5/2
程序输出样例
Original Transformed 带空格和换行符的格式说明
2
2.5
1
#include <stdio.h>
#include <string.h>
#define maxsize 100
int f(char ch, char sh)
{
if(sh=='(') return 1;
if( (ch=='*' || ch=='/' || ch=='%')&&( sh=='+' || sh=='-') )
return 1;
else
return -1;
}
int Op(int a,char op,int b)
{
if(op == '+')
return a+b;
if(op == '-')
return a-b;
if(op == '*')
return a*b;
if(op == '/')
return a/(b*1.0);
}
int com(char exp[])
{
int i,a,b,c;
int stack[maxsize];
int top = -1;
char op;
for(i = 0;exp[i];i++)
{
if(exp[i]>='0' && exp[i]<='9')
stack[++top] = exp[i] - '0';
else
{
op = exp[i];
b = stack[top--];
a = stack[top--];
c = Op(a,op,b);
stack[++top] = c;
}
}
return stack[top];
}
int main()
{
int i = 0 , n , top = -1 ;
char exp[1000] , str[1000] , ch ;
while(scanf("%c", &ch) && ch != '#')
{
if(ch >= '0' && ch <='9')
exp[i++] = ch ;
else if(ch=='(')
str[++top] = ch ;
else if(ch==')')
{
while(top!=-1)
{
exp[i++] = str[top];
top--;
if(str[top]=='(')
{
top--;
break;
}
}
}
else
{
if(top == -1 || f(ch,str[top]) > 0 )
str[++top] = ch ;
else
{
while(top >=0 && f(ch,str[top]) < 0 )
{
exp[i++] = str[top--];
}
str[++top] = ch ;
}
}
}
while(top != -1)
{
exp[i++] = str[top--];
}
exp[i] = '\0';
printf("%c\n",exp[i]);
printf("%s\n", exp);
printf("%d\n",com(exp));
return 0;
}
2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 100
int f(char ch, char sh)
{
if(sh=='(') return 1;
if((ch=='*' || ch=='/' || ch=='%')&&(sh=='+' || sh=='-'))
return 1;
else
return -1;
}
double Op(double a,char op,double b)
{
if(op == '+')
return a+b;
if(op == '-')
return a-b;
if(op == '*')
return a*b;
if(op == '/')
//printf("%.1f",a/(b*1.0));
return a/b;
}
double com(char exp[])
{
int i;
double a,b,c;
double stack[maxsize];
int top = -1;
char op;
for(i = 0; exp[i]; i++)
{
if(exp[i]>='0' && exp[i]<='9')
{
stack[++top] = (double)(exp[i] - '0');
// stack[++top] = atof(exp[i]);
//printf("h: %f\n",(double)(exp[i] - '0'));
}
else
{
op = exp[i];
b = stack[top--];
a = stack[top--];
//printf("%d %d \n",a,b);
c = Op(a,op,b);
stack[++top] = c;
//printf("hello: %f\n",c);
}
}
return stack[top];
}
int main()
{
int i = 0, n, top = -1 ;
char exp[1000], str[1000], ch ;
//exp存放要转成的后缀表达式 str存放字符乘除括号
while(scanf("%c", &ch) && ch!='\n')
{
if(ch >= '0' && ch <='9')
exp[i++] = ch ;
else if(ch=='(')
str[++top] = ch ;
else if(ch==')')
{
while(top!=-1)
{
exp[i++] = str[top];
top--;
if(str[top]=='(')
{
top--;
break;
}
}
}
else
{
if(top == -1 || f(ch,str[top]) > 0)
str[++top] = ch ;
else
{
while(top >=0 && f(ch,str[top]) < 0)
{
exp[i++] = str[top--];
}
str[++top] = ch ;
}
}
}
while(top != -1)
{
exp[i++] = str[top--];
}
exp[i] = '\0';
//printf("%.2f\n",Op(5,'/',2));
//printf("%c\n",exp[i]);
//printf("%s\n", exp);
printf("%.2f\n",com(exp));
return 0;
}
//5-(6+7)*8+9/4# -96.75
// 5 6 7 + 8 * - 9 4 / +
3 输入多行
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 100
int f(char ch, char sh)
{
if(sh=='(') return 1;
if((ch=='*' || ch=='/' || ch=='%')&&(sh=='+' || sh=='-'))
return 1;
else
return -1;
}
double Op(double a,char op,double b)
{
if(op == '+')
return a+b;
if(op == '-')
return a-b;
if(op == '*')
return a*b;
if(op == '/')
//printf("%.1f",a/(b*1.0));
return a/b;
}
double com(char exp[])
{
int i;
double a,b,c;
double stack[maxsize];
int top = -1;
char op;
for(i = 0; exp[i]; i++)
{
if(exp[i]>='0' && exp[i]<='9')
{
stack[++top] = (double)(exp[i] - '0');
// stack[++top] = atof(exp[i]);
//printf("h: %f\n",(double)(exp[i] - '0'));
}
else
{
op = exp[i];
b = stack[top--];
a = stack[top--];
//printf("%d %d \n",a,b);
c = Op(a,op,b);
stack[++top] = c;
//printf("hello: %f\n",c);
}
}
return stack[top];
}
int main()
{
int i = 0, n, top = -1 ;
int k;
char exp[1000], str[1000] ;
char ch[1000];
//exp存放要转成的后缀表达式 str存放字符乘除括号
while(~scanf("%s", ch))
{
for(k=0; ch[k]; k++)
{
if(ch[k] >= '0' && ch[k] <='9')
exp[i++] = ch[k] ;
else if(ch[k]=='(')
str[++top] = ch[k] ;
else if(ch[k]==')')
{
while(top!=-1)
{
exp[i++] = str[top];
top--;
if(str[top]=='(')
{
top--;
break;
}
}
}
else
{
if(top == -1 || f(ch[k],str[top]) > 0)
str[++top] = ch[k] ;
else
{
while(top >=0 && f(ch[k],str[top]) < 0)
{
exp[i++] = str[top--];
}
str[++top] = ch[k] ;
}
}
}
while(top != -1)
{
exp[i++] = str[top--];
}
exp[i] = '\0';
//printf("%.2f\n",Op(5,'/',2));
//printf("%c\n",exp[i]);
//printf("%s\n", exp);
printf("%.1f\n",com(exp));
}
return 0;
}
//5-(6+7)*8+9/4# -96.75
// 5 6 7 + 8 * - 9 4 / +
最终版,但未通过
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define maxsize 100
int f(char ch, char sh)
{
if(sh=='(') return 1;
if((ch=='*' || ch=='/' || ch=='%')&&(sh=='+' || sh=='-'))
return 1;
else
return -1;
}
double Op(double a,char op,double b)
{
if(op == '+')
return a+b;
if(op == '-')
return a-b;
if(op == '*')
return a*b;
if(op == '/')
//printf("%.1f",a/(b*1.0));
return a/b;
}
double com(char exp[])
{
int i;
double a,b,c;
double stack[maxsize];
int top = -1;
char op;
for(i = 0; exp[i]; i++)
{
if(exp[i]>='0' && exp[i]<='9')
{
stack[++top] = (double)(exp[i] - '0');
// stack[++top] = atof(exp[i]);
//printf("h: %f\n",(double)(exp[i] - '0'));
}
else
{
op = exp[i];
b = stack[top--];
a = stack[top--];
//printf("%d %d \n",a,b);
c = Op(a,op,b);
stack[++top] = c;
//printf("hello: %f\n",c);
}
}
return stack[top];
}
int main()
{
int i = 0, n, top = -1 ;
int k;
char exp[1000], str[1000] ;
char ch[1000];
//exp存放要转成的后缀表达式 str存放字符乘除括号
while(~scanf("%s", ch))
{
for(k=0; ch[k]; k++)
{
if(ch[k] >= '0' && ch[k] <='9')
exp[i++] = ch[k] ;
else if(ch[k]=='(')
str[++top] = ch[k] ;
else if(ch[k]==')')
{
while(top!=-1)
{
exp[i++] = str[top];
top--;
if(str[top]=='(')
{
top--;
break;
}
}
}
else
{
if(top == -1 || f(ch[k],str[top]) > 0)
str[++top] = ch[k] ;
else
{
while(top >=0 && f(ch[k],str[top]) < 0)
{
exp[i++] = str[top--];
}
str[++top] = ch[k] ;
}
}
}
while(top != -1)
{
exp[i++] = str[top--];
}
exp[i] = '\0';
//printf("%.2f\n",Op(5,'/',2));
//printf("%c\n",exp[i]);
//printf("%s\n", exp);
if((com(exp)-floor(com(exp))) == 0)
printf("%d\n",(int)com(exp));
else
printf("%.1f\n",com(exp));
}
return 0;
}
//5-(6+7)*8+9/4# -96.75
// 5 6 7 + 8 * - 9 4 / +
AC
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define maxsize 100
int f(char ch, char sh)
{
if(sh=='(') return 1;
if((ch=='*' || ch=='/' || ch=='%')&&(sh=='+' || sh=='-'))
return 1;
else
return -1;
}
double Op(double a,char op,double b)
{
if(op == '+')
return a+b;
if(op == '-')
return a-b;
if(op == '*')
return a*b;
if(op == '/')
//printf("%.1f",a/(b*1.0));
return a/b;
}
double com(char exp[])
{
int i;
double a,b,c;
double stack[maxsize];
int top = -1;
char op;
for(i = 0; exp[i]; i++)
{
if(exp[i]>='0' && exp[i]<='9')
{
stack[++top] = (double)(exp[i] - '0');
// stack[++top] = atof(exp[i]);
//printf("h: %f\n",(double)(exp[i] - '0'));
}
else
{
op = exp[i];
b = stack[top--];
a = stack[top--];
//printf("%d %d \n",a,b);
c = Op(a,op,b);
stack[++top] = c;
//printf("hello: %f\n",c);
}
}
return stack[top];
}
int main()
{
int i = 0, n, top = -1 ;
int k;
char exp[1000], str[1000] ;
char ch[1000];
//exp存放要转成的后缀表达式 str存放字符乘除括号
while(~scanf("%s", ch))
{
for(k=0; ch[k]; k++)
{
if(ch[k] >= '0' && ch[k] <='9')
exp[i++] = ch[k] ;
else if(ch[k]=='(')
str[++top] = ch[k] ;
else if(ch[k]==')')
{
while(top!=-1)
{
exp[i++] = str[top];
top--;
if(str[top]=='(')
{
top--;
break;
}
}
}
else
{
if(top == -1 || f(ch[k],str[top]) > 0)
str[++top] = ch[k] ;
else
{
while(top >=0 && f(ch[k],str[top]) < 0)
{
exp[i++] = str[top--];
}
str[++top] = ch[k] ;
}
}
}
while(top != -1)
{
exp[i++] = str[top--];
}
exp[i] = '\0';
//printf("%.2f\n",Op(5,'/',2));
//printf("%c\n",exp[i]);
//printf("%s\n", exp);
printf("%g\n",com(exp));
}
return 0;
}
//5-(6+7)*8+9/4# -96.75
// 5 6 7 + 8 * - 9 4 / +
网友评论