5个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define LEN 10//输入数字不得超过10位
#define MAXSIZE 40//数字和运算符总个数不得超过40个
typedef struct
{
char data[MAXSIZE][LEN];//栈区为二维数组
int top;
}seqstack;
seqstack *initseqstack();
void push(seqstack *s, char *x);
char *pop(seqstack *s);
int optr(char *z);
int priority(char *a, char *b);
void process(char arrOne[][LEN], char arrTwo[][LEN]);
double calc(char *a, char *b, char *ch);
void express(char (*arrTwo)[LEN]);
seqstack *s = NULL;//声明全局变量
seqstack *initseqstack()//初始化栈顶指针
{
seqstack *s = (seqstack *) malloc (sizeof(seqstack));
s->top = -1;
return s;
}
void push(seqstack *s, char *x)//入栈
{
if (MAXSIZE-1 == s->top)
{
printf("overflow\n");
exit(0);
}
strcpy(s->data[++s->top], x);
}
char *pop(seqstack *s)//出栈
{
char *y = (char *) malloc (LEN * sizeof(char));
strcpy(y, s->data[s->top--]);
return y;
}
int optr(char *z)//判断是否为运算符,是返回TRUE
{
int i;
char *optr1[6] = {"+", "-", "*", "/", "(", ")"};
for (i=0; i<6; i++)
{
if (0 == strcmp(optr1[i], z))
{
return TRUE;
}
}
return FALSE;
}
int priority(char *a, char *b)//比较优先级
{
int i, j;
char *array[5] = {"(", "+", "-", "*", "/"};//优先级列表,由小到大
for (i=0; i<5; i++)
{
if (0 == strcmp(array[i], a))
{
break;
}
}
for (j=0; j<5; j++)
{
if (0 == strcmp(array[j], b))
{
break;
}
}
switch (i)
{
case 0: i = 0; break;
case 1:
case 2: i = 1; break;
case 3:
case 4: i = 2; break;
}
switch (j)
{
case 0: j = 0; break;
case 1:
case 2: j = 1; break;
case 3:
case 4: j = 2; break;
}
if (i >= j)
{
return TRUE;
}
else
{
return FALSE;
}
}
void process(char (*arrOne)[LEN], char (*arrTwo)[LEN])//将中缀式处理成后缀式(运算符入栈处理)
{
int i, j = 0;
s = initseqstack();//初始化
for (i=0; 0!=strcmp(arrOne[i], "\0"); i++)
{
if (1 < strlen(arrOne[i]) || (TRUE != optr(arrOne[i])))//arrOne[i][LEN]为数字时
{
strcpy(arrTwo[j++], arrOne[i]);//将数字拷贝到arrTwo
}
else if (0 == strcmp(arrOne[i], "("))
{
push(s, arrOne[i]);//入栈
}
else if (0 == strcmp(arrOne[i], ")"))
{
while ((-1 != s->top) && (0 != strcmp(s->data[s->top], "(")))
{
strcpy(arrTwo[j++], pop(s));//出栈
}
pop(s);//将"("弹出
}
else//此时arrOne的内容只可能是"+", "-", "*", "/"
{
if (-1 == s->top)
{
push(s, arrOne[i]);
}
else
{
while (-1 != s->top)
{
if (TRUE == priority(s->data[s->top], arrOne[i]))//与栈顶元素进行优先级比较
{
strcpy(arrTwo[j++], pop(s));
}
else
{
push(s, arrOne[i]);
break;
}
}
if (-1 == s->top)
{
push(s, arrOne[i]);
}
}
}
}
while (-1 != s->top)
{
strcpy(arrTwo[j++], pop(s));
}
}
double calc(char *a, char *b, char *ch)//运算,返回double类型
{
double num;
double numOne, numTwo;
numOne = atof(a);
numTwo = atof(b);//将字符串转化为double类型(atof是库函数)
switch(*ch)
{
case '+': num = numOne + numTwo; break;
case '-': num = numOne - numTwo; break;
case '*': num = numOne * numTwo; break;
case '/':
if ((0.000001 >= numTwo) && (-0.000001 <= numTwo))
{
printf("error!\n");
}
else
{
num = numOne / numTwo;
}
break;
default:
exit(0);
}
return num;
}
void express(char (*arrTwo)[LEN])//后缀式入栈运算
{
int i;
double num;
char result[LEN];
char numOne[LEN], numTwo[LEN];
s = initseqstack();//初始化
for (i=0; 0!=strcmp(arrTwo[i], "\0"); i++)
{
if (TRUE != optr(arrTwo[i]))//假如是数,则入栈
{
push(s, arrTwo[i]);
}
else
{
strcpy(numTwo, pop(s));
strcpy(numOne, pop(s));//将顶上两数字弹出
num = calc(numOne, numTwo, arrTwo[i]);//运算
gcvt(num, LEN, result);//将double转换成字符串(gcvt是库函数)
push(s, result);//入栈
}
}
printf(" %s\n\n", result);//输出最终结果
}
void Menu(void)
{
printf("注意:\n");
printf("1.本程序有一定误差,误差范围大约是-0.001到+0.001\n");
printf("2.本程序只能处理正确的表达形式.\n");
printf("3.避免计算结果越界.\n");
printf("4.输入方式:如输入表达式(-1+2)*3\n");
printf(" 输入:\n");
printf(" (空格-1空格+空格2空格)空格*空格3空格=回车\n");
}
void main(void)
{
int i = 0;
char arr[MAXSIZE][LEN]; //输入字符串
char arrOne[MAXSIZE][LEN] = { "\0" }; //保存中缀式
char arrTwo[MAXSIZE][LEN] = { "\0" }; //保存后缀式
Menu();
printf("\n\n\n请输入表达式:\n");
while (scanf("%s", arr[i]))
{
if (0 == strcmp(arr[i], "="))
{
break;
}
strcpy(arrOne[i], arr[i]);
i++;
}
process(arrOne, arrTwo); //调用
express(arrTwo); //调用
}
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define LEN 10//输入数字不得超过10位
#define MAXSIZE 40//数字和运算符总个数不得超过40个
typedef struct
{
char data[MAXSIZE][LEN];//栈区为二维数组
int top;
}seqstack;
seqstack *initseqstack();
void push(seqstack *s, char *x);
char *pop(seqstack *s);
int optr(char *z);
int priority(char *a, char *b);
void process(char arrOne[][LEN], char arrTwo[][LEN]);
double calc(char *a, char *b, char *ch);
void express(char (*arrTwo)[LEN]);
seqstack *s = NULL;//声明全局变量
seqstack *initseqstack()//初始化栈顶指针
{
seqstack *s = (seqstack *) malloc (sizeof(seqstack));
s->top = -1;
return s;
}
void push(seqstack *s, char *x)//入栈
{
if (MAXSIZE-1 == s->top)
{
printf("overflow\n");
exit(0);
}
strcpy(s->data[++s->top], x);
}
char *pop(seqstack *s)//出栈
{
char *y = (char *) malloc (LEN * sizeof(char));
strcpy(y, s->data[s->top--]);
return y;
}
int optr(char *z)//判断是否为运算符,是返回TRUE
{
int i;
char *optr1[6] = {"+", "-", "*", "/", "(", ")"};
for (i=0; i<6; i++)
{
if (0 == strcmp(optr1[i], z))
{
return TRUE;
}
}
return FALSE;
}
int priority(char *a, char *b)//比较优先级
{
int i, j;
char *array[5] = {"(", "+", "-", "*", "/"};//优先级列表,由小到大
for (i=0; i<5; i++)
{
if (0 == strcmp(array[i], a))
{
break;
}
}
for (j=0; j<5; j++)
{
if (0 == strcmp(array[j], b))
{
break;
}
}
switch (i)
{
case 0: i = 0; break;
case 1:
case 2: i = 1; break;
case 3:
case 4: i = 2; break;
}
switch (j)
{
case 0: j = 0; break;
case 1:
case 2: j = 1; break;
case 3:
case 4: j = 2; break;
}
if (i >= j)
{
return TRUE;
}
else
{
return FALSE;
}
}
void process(char (*arrOne)[LEN], char (*arrTwo)[LEN])//将中缀式处理成后缀式(运算符入栈处理)
{
int i, j = 0;
s = initseqstack();//初始化
for (i=0; 0!=strcmp(arrOne[i], "\0"); i++)
{
if (1 < strlen(arrOne[i]) || (TRUE != optr(arrOne[i])))//arrOne[i][LEN]为数字时
{
strcpy(arrTwo[j++], arrOne[i]);//将数字拷贝到arrTwo
}
else if (0 == strcmp(arrOne[i], "("))
{
push(s, arrOne[i]);//入栈
}
else if (0 == strcmp(arrOne[i], ")"))
{
while ((-1 != s->top) && (0 != strcmp(s->data[s->top], "(")))
{
strcpy(arrTwo[j++], pop(s));//出栈
}
pop(s);//将"("弹出
}
else//此时arrOne的内容只可能是"+", "-", "*", "/"
{
if (-1 == s->top)
{
push(s, arrOne[i]);
}
else
{
while (-1 != s->top)
{
if (TRUE == priority(s->data[s->top], arrOne[i]))//与栈顶元素进行优先级比较
{
strcpy(arrTwo[j++], pop(s));
}
else
{
push(s, arrOne[i]);
break;
}
}
if (-1 == s->top)
{
push(s, arrOne[i]);
}
}
}
}
while (-1 != s->top)
{
strcpy(arrTwo[j++], pop(s));
}
}
double calc(char *a, char *b, char *ch)//运算,返回double类型
{
double num;
double numOne, numTwo;
numOne = atof(a);
numTwo = atof(b);//将字符串转化为double类型(atof是库函数)
switch(*ch)
{
case '+': num = numOne + numTwo; break;
case '-': num = numOne - numTwo; break;
case '*': num = numOne * numTwo; break;
case '/':
if ((0.000001 >= numTwo) && (-0.000001 <= numTwo))
{
printf("error!\n");
}
else
{
num = numOne / numTwo;
}
break;
default:
exit(0);
}
return num;
}
void express(char (*arrTwo)[LEN])//后缀式入栈运算
{
int i;
double num;
char result[LEN];
char numOne[LEN], numTwo[LEN];
s = initseqstack();//初始化
for (i=0; 0!=strcmp(arrTwo[i], "\0"); i++)
{
if (TRUE != optr(arrTwo[i]))//假如是数,则入栈
{
push(s, arrTwo[i]);
}
else
{
strcpy(numTwo, pop(s));
strcpy(numOne, pop(s));//将顶上两数字弹出
num = calc(numOne, numTwo, arrTwo[i]);//运算
gcvt(num, LEN, result);//将double转换成字符串(gcvt是库函数)
push(s, result);//入栈
}
}
printf(" %s\n\n", result);//输出最终结果
}
void Menu(void)
{
printf("注意:\n");
printf("1.本程序有一定误差,误差范围大约是-0.001到+0.001\n");
printf("2.本程序只能处理正确的表达形式.\n");
printf("3.避免计算结果越界.\n");
printf("4.输入方式:如输入表达式(-1+2)*3\n");
printf(" 输入:\n");
printf(" (空格-1空格+空格2空格)空格*空格3空格=回车\n");
}
void main(void)
{
int i = 0;
char arr[MAXSIZE][LEN]; //输入字符串
char arrOne[MAXSIZE][LEN] = { "\0" }; //保存中缀式
char arrTwo[MAXSIZE][LEN] = { "\0" }; //保存后缀式
Menu();
printf("\n\n\n请输入表达式:\n");
while (scanf("%s", arr[i]))
{
if (0 == strcmp(arr[i], "="))
{
break;
}
strcpy(arrOne[i], arr[i]);
i++;
}
process(arrOne, arrTwo); //调用
express(arrTwo); //调用
}
展开全部
#include<stdio.h>
void main()
{
printf("%d\n",3*(7-2));
}
void main()
{
printf("%d\n",3*(7-2));
}
追问
要用栈的方法来实现求值。谢谢了
追答
不好意思,我没看到。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include"stdio.h"
void main()
{int a=3,b=7,c=2;
int d;
d=3*(7-2);
printf("%d\n",d);
}
void main()
{int a=3,b=7,c=2;
int d;
d=3*(7-2);
printf("%d\n",d);
}
追问
要用栈的方法来实现求值。谢谢了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
main()
{
int x;//define a variable;
x=3*(7-2);//Expression
printf("x=%d\n",x);//output
return;
}
main()
{
int x;//define a variable;
x=3*(7-2);//Expression
printf("x=%d\n",x);//output
return;
}
追问
要用栈的方法来实现求值。谢谢了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我不需要你给分,你可以看下这个资料就行了
表达式运算,我空间里。
中缀转后缀
http://hi.baidu.com/ccocwwh/blog/item/875af00252c529ff08fa938e.html
表达式运算,我空间里。
中缀转后缀
http://hi.baidu.com/ccocwwh/blog/item/875af00252c529ff08fa938e.html
参考资料: http://hi.baidu.com/ccocwwh/blog/item/875af00252c529ff08fa938e.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询