主题:[活动]第77次编程比赛题目
yj1221 [专家分:20] 发布于 2008-11-21 14:25:00
编写一个程序可以计算表达式值的程序。
例如: 输入:3+6*8 输出:51
要求: 1. 程序能够进行小数运算
2. 表达式可以包含 ( ) [ ] { } 数字(0~9) 运算符(+ - * / ^) 小数点
3. 当用户输入有错时,给出出错位置,提示用户重新输入
比赛结束时间:11月28日(周五) 晚上10:00
网上有这题目的解法,不过,希望大家踊跃参加,提交自己写的程序。期待很好的代码出现!
比赛已经结束了,从提交的代码来看,最厉害的,相信大家都看得出来了。我会尽快给出比赛结果。希望这次比赛对大家有所收获。
最后更新于:2008-11-28 22:13:00
回复列表 (共29个回复)
沙发
yj1221 [专家分:20] 发布于 2008-11-21 14:27:00
test
板凳
liuwenhan [专家分:20] 发布于 2008-11-21 18:16:00
可能存在许多问题,power函数可能有问题,帮我运行一下,我用的是fedora,thanks.
#include<iostream>
#include<cctype>
#include<cmath>
using namespace std;
void highlever(const char*);
double result(double a,double b,char c);
bool done(const char*,int);
double suanfa(const char*);
int top=-1;//for lever
int top1=-1;//for sign
int top2=-1;//for number
double number[100];//store number
char sign[20];//store sign
int i=-1;//piont to the shizi array
int lever[4];//store the lever
int mark;//the current lever
int main()
{
char shizi[100];
double finalnumber;
cin>>shizi;
finalnumber=suanfa(shizi);
cout<<"the final result is"<<finalnumber;
}
void highlever(const char* shizi)
{
double a,b;
if(mark<=lever[top])
{
if(top==-1)
{
lever[++top]=mark;
sign[++top1]=shizi[i];
return;
}
else
{
a=number[top2--];
b=number[top2];
number[top2]=result(a,b,sign[top1--]);
highlever(shizi);
}
}
else lever[++top]=mark;
}
3 楼
liuwenhan [专家分:20] 发布于 2008-11-21 18:17:00
double suanfa(const char* shizi)
{
int flag;//mark :behind the number isn't '{','[','('
for(++i;i<100;++i)
{
if(shizi[i]=='\n') break;
if(isalpha(shizi[i]))
{
cout<<"The position of i is character";
exit(0);
}
else if(isdigit(shizi[i]))
{
number[top2]=number[top2]*10+double(shizi[i]+48);
flag=1;
}
else if(shizi[i]=='.')
{
if(!done(shizi,flag))
continue;
flag=1;
}
else
{
top2++;
if(shizi[i]=='{' || shizi[i]=='(' || shizi[i]=='[')
{
if(flag==1)
{
cout<<"The position of i is wrong";
exit(0);
}
sign[++top1]=shizi[i];
number[++top2]=suanfa(shizi);
}
else if(shizi[i]=='^')
{
mark=3;
highlever(shizi);
}
else if(shizi[i]=='*' || shizi[i]=='/')
{
mark=2;
highlever(shizi);
}
else if(shizi[i]=='+' || shizi[i]=='-')
{
mark=1;
highlever(shizi);
}
else if(shizi[i]=='}' || shizi[i]==')' || shizi[i]==']')
if(sign[top--]==shizi[i]);
else
{
cout<<"the position of i is wrong";
exit(0);
}
else
{
cout<<"the position of i is wrong";
exit(0);
}
flag=0;
}
}
}
4 楼
liuwenhan [专家分:20] 发布于 2008-11-21 18:18:00
bool done(const char* shizi,int b)
{
int j=0;
if(b!=1)
{
cout<<"The position of i is wrong";
exit(0);
}
else if(!isalpha(shizi[i]))
{
cout<<"The position of i is wrong";
exit(0);
}
else
for(i++;i<100;i++)
{
if(shizi[i]=='\n') return true;
if(isdigit(shizi[i]))
{
j++;
number[top2]=double(shizi[i]+48)/power(10,j)+number[top2];
}
else
{
i--;
return false;
}
}
}
double result(double a,double b,char c)
{
switch(c)
{
case '^':
return power(a,b);
case '*':
return a*b;
case '/':
return a/b;
case '+':
return a+b;
case '-':
return a-b;
}
}
5 楼
liuwenhan [专家分:20] 发布于 2008-11-21 18:35:00
change:
#include<iostream>
#include<cctype>
#include<cmath>
#include<stdio.h>
bool done(const char* shizi,int b)
{
double j=0;
if(b!=1)
{
cout<<"The position of i is wrong";
exit(0);
}
else if(!isalpha(shizi[i]))
{
cout<<"The position of i is wrong";
exit(0);
}
else
for(i++;i<100;i++)
{
if(shizi[i]=='\n') return true;
if(isdigit(shizi[i]))
{
j++;
number[top2]=double(shizi[i]+48)/pow(10.0,j)+number[top2];
}
else
{
i--;
return false;
}
}
}
double result(double a,double b,char c)
{
switch(c)
{
case '^':
return pow(a,b);
case '*':
return a*b;
case '/':
return a/b;
case '+':
return a+b;
case '-':
return a-b;
}
}
6 楼
liuwenhan [专家分:20] 发布于 2008-11-21 19:46:00
I change it and I don't know the problem .Maybe you can help me!Thanks
#include<iostream>
#include<cctype>
#include<cmath>
#include<stdio.h>
using namespace std;
void highlever(const char*);
double result(double a,double b,char c);
bool done(const char*,int);
void suanfa(const char*);
int top=-1;//for lever
int top1=0;//for sign
int top2=0;//for number
double number[100];//store number
char sign[20];//store sign
static int i=-1;//piont to the shizi array
int lever[4];//store the lever
int mark;//the current lever
int main()
{
char shizi[100];
double finalnumber;
cin.unsetf(ios::skipws);
cin>>shizi;
suanfa(shizi);
cout<<"the final result is"<<number[0];
}
void highlever(const char* shizi)
{
double a,b;
if(top1==-1 || lever[top]==0)
{
if(mark==0)
top--;
else
{
lever[++top]=mark;
sign[++top1]=shizi[i];
cout<<shizi[i];
return;
}
}
else
if(mark<=lever[top])
{
a=number[top2--];
b=number[top2];
number[top2]=result(a,b,sign[top1--]);
highlever(shizi);
}
else
{
lever[++top]=mark;
sign[++top1]=shizi[i];
}
}
7 楼
liuwenhan [专家分:20] 发布于 2008-11-21 19:47:00
void highlever(const char* shizi)
{
double a,b;
if(top1==-1 || lever[top]==0)
{
if(mark==0)
top--;
else
{
lever[++top]=mark;
sign[++top1]=shizi[i];
return;
}
}
else
if(mark<=lever[top])
{
a=number[top2--];
b=number[top2];
number[top2]=result(a,b,sign[top1--]);
highlever(shizi);
}
else
{
lever[++top]=mark;
sign[++top1]=shizi[i];
}
}
8 楼
liuwenhan [专家分:20] 发布于 2008-11-21 19:47:00
void suanfa(const char* shizi)
{
int flag;//mark :behind the number isn't '{','[','('
for(i=i+1;i<100;i++)
{
if(shizi[i]=='\0')
{
mark=-1;
highlever(shizi);
}
if(isalpha(shizi[i]))
{
cout<<"The position of "<<i+1<<" is character";
exit(0);
}
else if(isdigit(shizi[i]))
{
number[top2]=number[top2]*10+double(shizi[i]+48);
flag=1;
}
else if(shizi[i]=='.')
{
if(!done(shizi,flag))
continue;
flag=1;
}
else
{
top2++;
if(shizi[i]=='{' || shizi[i]=='(' || shizi[i]=='[')
{
if(flag==1)
{
cout<<"The position of "<<i+1<<" is wrong";
exit(0);
}
sign[++top1]=shizi[i];
suanfa(shizi);
mark=0;
lever[++top]=mark;
}
else if(shizi[i]=='^')
{
mark=3;
highlever(shizi);
}
else if(shizi[i]=='*' || shizi[i]=='/')
{
mark=2;
highlever(shizi);
}
else if(shizi[i]=='+' || shizi[i]=='-')
{
mark=1;
highlever(shizi);
}
else if(shizi[i]=='}' || shizi[i]==')' || shizi[i]==']')
if(sign[top--]==shizi[i])
{
mark=0;
highlever(shizi);
}
else
{
cout<<"The position of "<<i+1<<" is wrong";
exit(0);
}
else
{
cout<<"The position of "<<i+1<<" is wrong";
//exit(0);
}
flag=0;
}
}
}
9 楼
liuwenhan [专家分:20] 发布于 2008-11-21 19:48:00
bool done(const char* shizi,int b)
{
double j=0;
if(b!=1)
{
cout<<"The position of "<<i+1<<" is wrong";
exit(0);
}
else if(isalpha(shizi[++i]))
{
cout<<"The position of "<<i+1<<" is wrong";
exit(0);
}
else
for(i;i<100;i++)
{
if(shizi[i]=='\n') return true;
if(isdigit(shizi[i]))
{
j++;
number[top2]=double(shizi[i]+48)/pow(10.0,j)+number[top2];
}
else
{
i--;
return false;
}
}
}
double result(double a,double b,char c)
{
switch(c)
{
case '^':
return pow(a,b);
case '*':
return a*b;
case '/':
return a/b;
case '+':
return a+b;
case '-':
return a-b;
}
}
10 楼
yingshuai16 [专家分:10] 发布于 2008-11-21 22:49:00
#define MAXSIZE 20
typedef struct _STACK
{
int elems[MAXSIZE];
int top;
}STACK;
int Push(STACK *stk,intx)
{
if((stk->top+1)>=MAXSIZE)
{
printf("Stack is overflow!");
}
stk->top=stk->top+1;
stk->elems[stk->top]=x;
}
int Pop(STACK *stk)
{
if (stk->tip<0)
{
printf("stack is empty!\n");
return;
}
stk->top-=1;
}
intTop(STACK *stk)
{
return stk->elens[stk->top];
}
main()
{
if(输入是数字)
push(数字);
if(不是)
push(符号)
比较下一个符号
优先级低就
pop(数字)
pop(数字)
记算结果
否则
push(符号)
}
没有空详细写,时间要到了,呵呵。
我来回复