想请问一下,用c语言编一个程序输入任意表达式可以计算结果

要考虑符号优先极
(非常紧急)
用swich语句和栈来实现

大一时候写的,你看行不

#include<stdio.h>
#include<math.h>
#include<ctype.h>
#include<conio.h>
/*#include"stack.c"
#include"box.c"*/

#define MAXLEN1 100 /* 定义栈1的最大长度 */
#define MAXLEN2 100 /* 定义栈2的最大长度 */
#define NULL 0
#define OK 1
#define ERROR 0 /*定义出错信息 */
#define INFEASIBLE -1
#define ZEROOVERFLOW -2
#define OPTRERROR -3
#define SYERROR -4
#define POINTERROR -5
#define NOINPUT -6

typedef int Status;

enum boolean{FALSE,TRUE};

struct S1{
int top;
char element[MAXLEN1];
}; /*定义字符栈1的节点 */
typedef struct S1 *STACK1;

struct S2{
int top;
double element[MAXLEN2];
}; /*定义实数栈2的节点 */
typedef struct S2 *STACK2;*/

void main()
{
void setbkgnd();
void setsur();
void initespn(char ex[MAXLEN1]);
Status EXPRESSION(char e[MAXLEN1],double *p);/*求值函数,返回正确或错误信息,若正确,用p返回结果*/
void printresult(int con,double result);
char corewin(int lefttopx,int lefttopy);

char yorn;
double result; /*计算的结果*/
double *rp;
int count=1; /*标记进入循环体的次数*/
rp=&result;

do{
char espn[MAXLEN1]={\n,NULL};
Status expr;

yorn=NULL;

if(count==1) /*若是首次进入*/
{
setbkgnd();
setsur(); /*作初始画面*/
}

setbkgnd();

count++;

initespn(espn);

gotoxy(3,11);

expr=EXPRESSION(espn,rp);

printresult(expr,result);

yorn=corewin(3,13); /*提示继续或退出的窗口操作*/

}while(yorn!=e&&yorn!=E);

}

void setbkgnd()
{
void box1( int startx, int starty, int width, int high); /*画一个矩形线框*/

int j;
window(1,1,80,25);
textbackground(BLUE);
textcolor(WHITE);
clrscr();
box1(1,1,80,24);

gotoxy(2,2);
printf(" This is a program for calculating.\n");
gotoxy(2,3);
printf(" You are allowed to use these operators:\n");
gotoxy(2,4);
printf(" + - * / sin cos tan ln lg arcsin arccos arctan sqrt exp\n");
gotoxy(1,5);
putch(198);
for(j=1;j<79;j++)
putch(205);
putch(181);
gotoxy(1,12);
putch(195);
for(j=1;j<79;j++)
putch(0xc4);
putch(180);

gotoxy(1,14);
putch(198);
for(j=1;j<79;j++)
putch(205);
putch(181);
gotoxy(2,15);
printf("%cDirectories:",249);
gotoxy(3,16);
printf("%cinput the expression correctly,and the result will be given followed by the",250);
gotoxy(3,17);
printf(" postfix notation.");
gotoxy(3,18);
printf("%cif you dont input correctly,the information of the error will be given.",250);
gotoxy(3,19);
printf("%cpress c for continue or e for exit.",250);
gotoxy(3,20);
printf("%cpress Enter to confirm your choice.",250);
gotoxy(3,21);
printf("%cyou can change your choice before pressing Enter.",250);
gotoxy(3,22);

gotoxy(3,23);

}

void box1( int startx, int starty, int width, int high) /*画一个矩形线框*/
{
int i;
gotoxy(startx,starty);
putch(0xda); /*画-|*/
for(i=startx+1;i<width;i++)
putch(0xc4); /*画-*/
putch(0xbf);
for(i=starty+1;i<high;i++)
{
gotoxy(startx,i);putch(0xb3); /*画|*/
gotoxy(width,i);putch(0xb3); /*画|*/
}
gotoxy(startx,high);
putch(0xc0); /*画|-*/
for(i=startx+1;i<width;i++)
putch(0xc4); /*画-*/
putch(0xd9); /*画-|*/
}

void setsur()
{
void box2(int startx, int starty, int width, int high); /*画一个双矩形线框*/

window(21,8,60,16);
textbackground(LIGHTGRAY);
clrscr();
textcolor(WHITE);
box2(2,1,39,9);
textcolor(BLACK);
gotoxy(10,3);
cprintf("Expression Operating");
gotoxy(14,4);
cprintf("Version 1.1 ");
gotoxy(13,6);
cprintf("LKP&ZZC ID_27");
gotoxy(10,7);
cprintf("0003104_HIT 2002.5");
window(1,1,80,25);
gotoxy(2,6);
getch();
}

void box2( int startx, int starty, int width, int high) /*画一个双矩形线框*/
{
int i;
gotoxy(startx,starty);
putch(201); /*画|-*/
for(i=startx+1;i<width;i++)
putch(205); /*画-*/
putch(187);
for(i=starty+1;i<high;i++)
{
gotoxy(startx,i);putch(186); /*画|*/
gotoxy(width,i);putch(186); /*画|*/
}
gotoxy(startx,high);
putch(200); /*画|_*/
for(i=startx+1;i<width;i++)
putch(205); /* 画- */
putch(188); /*画_|*/
}

void initespn(char ex[MAXLEN1])
{
int i=1;

textcolor(YELLOW);
gotoxy(2,6);
cprintf("%cinput the expression:",249); /*提示输入表达式*/
gotoxy(3,7);

do
{
scanf("%c",&ex[i]);
i++;
}while(ex[i-1]!=\n); /*将表达式保存于字符数组中*/
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-01-04
1.用字符组的形式先读入表达式。
2.先把各个字母和数字摘出来,放入各个变量名中。
3.符号的话一个字符的好说,两个字符的,判断一下。找出所有的符号,排列一下优先级。
4.把符号和他对应要运算的数据传给函数(自己做)
void oper(char *p,要运算的数据);
比如如果p对应的是‘+’号,他的要运算的数据就是前后两个数。
4.特殊的符号(比如 ? : )特殊对待。

补充:原来你说的是这个啊,课本上的题目.<数据结构教程> (蔡子经 施伯乐 编)上有完整的题目和解释过程,18页左右,自己找吧.
相似回答