| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1141 人关注过本帖
标题:[求助]:高手帮我编编嘛!急等!
只看楼主 加入收藏
lijunhua
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2005-4-17
收藏
 问题点数:0 回复次数:1 
[求助]:高手帮我编编嘛!急等!
算术表达式求值演示:
演示用算符优先法对算术表达式求值的过程.
以字符序列的形式从终端输入语法正确的,不含变量的整数表达式.实现对算术四则混合运算表达式的求值,并演示在求值中运算符栈,运算数栈,输入字符的变化过程.(是栈的应用)   用c语言编,是欺中作业啊!感激不尽了!
搜索更多相关主题的帖子: 求值 算术 演示 变量 
2005-04-26 22:04
涛声依旧
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2005-3-5
收藏
得分:0 

晕,我已经发过了,就是中缀表达式转化成后缀表达式在求值!!也不去看哈~!! #include<stdio.h> #include<stdlib.h> #define TRUE 1 #define FALSE 0 #define MAXNUM 100 typedef int DataType; struct SeqStack { DataType s[MAXNUM]; int t; }; typedef struct SeqStack *PSeqStack; PSeqStack createEmptyStack_seq() { PSeqStack pastack; pastack = (PSeqStack)malloc(sizeof(struct SeqStack)); if (pastack == NULL) printf("Out of space!!\n"); else pastack->t = -1; return pastack; } int isEmptyStack_seq(PSeqStack pastack) { return pastack->t == -1; } void push_seq(PSeqStack pastack, DataType x) { if (pastack->t >= MAXNUM - 1) printf("Overflow!\n"); else { pastack->t = pastack->t + 1; pastack->s[pastack->t] = x; } } void pop_seq(PSeqStack pastack) { if (pastack->t == -1) printf("Underflow!\n"); else pastack->t = pastack->t - 1; } DataType top_seq(PSeqStack pastack) { return pastack->s[pastack->t]; } int infixtoSuffix(const char * infix, char * suffix) { /*将中缀表达式转换为后缀表达式,顺利转换返回true,若转换过程中发现中缀表达式非法则返回false*/ int state_int = FALSE; /*state_int记录状态,等于true表示刚读入的是数字字符,等于false表示刚读入的不是数字字符, 设置这个变量是为了在每输出一个整数后输出一个空格,以免连续输出的两个整数混在一起。*/ char c, c2; PSeqStack ps = createEmptyStack_seq(); /*运算符栈*/ int i, j = 0; if (infix[0] == '\0') return FALSE; /*不允许出现空表达式*/ for (i = 0; infix[i] != '\0'; i++) { c = infix[i]; switch (c) { case ' ': case '\t': case '\n': if (state_int == TRUE) suffix[j++] = ' ';/*状态从true转换为false时输出一个空格*/ state_int = FALSE; break; /*遇到空格或制表符忽略*/ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': state_int = TRUE; suffix[j++] = c; /*遇到数字输出*/ break; case '(': if (state_int == TRUE) suffix[j++] = ' ';/*状态从true转换为false时输出一个空格*/ state_int = FALSE; push_seq(ps, c); /*遇到左括号,入栈*/ break; case ')': if (state_int == TRUE) suffix[j++] = ' ';/*状态从true转换为false时输出一个空格*/ state_int = FALSE; c2 = ')'; while (!isEmptyStack_seq(ps)) { c2 = top_seq(ps);/*取栈顶*/ pop_seq(ps); /*出栈*/ if (c2 == '(') { break; } suffix[j++] = c2; } if (c2 != '(') { free(ps); suffix[j++] = '\0'; return FALSE; } break; case '+': case '-': if (state_int == TRUE) suffix[j++] = ' '; state_int = FALSE; while(!isEmptyStack_seq(ps)) { c2 = top_seq(ps); if (c2 == '+' || c2 == '-' || c2 == '*' || c2 == '/') { pop_seq(ps); suffix[j++] = c2; } else if(c2=='(') break; } push_seq(ps, c); break; case '*': case '/': if (state_int == TRUE) suffix[j++] = ' '; state_int = FALSE; while (!isEmptyStack_seq(ps)) { c2 = top_seq(ps); if (c2 == '*' || c2 == '/') { pop_seq(ps); suffix[j++] = c2; } else if(c2=='+'||c2=='-'||c2=='(') break; } push_seq(ps, c); break; default: free(ps); suffix[j++] = '\0'; return FALSE; } } if (state_int == TRUE) suffix[j++] = ' '; while (!isEmptyStack_seq(ps)) { c2 = top_seq(ps); pop_seq(ps); if (c2 == '(') { free(ps); suffix[j++] = '\0'; return FALSE; } suffix[j++] = c2; } free(ps); suffix[j++] = '\0'; return TRUE; } int calculateSuffix(const char * suffix, int * presult) {

int state_int = FALSE; PSeqStack ps = createEmptyStack_seq(); int num = 0, num1, num2; int i; char c; for (i = 0; suffix[i] != '\0'; i++) { c = suffix[i]; switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (state_int == TRUE) num = num * 10 + c - '0'; else num = c - '0'; state_int = TRUE; break; case ' ': case'\t': case '\n': if (state_int == TRUE) { push_seq(ps, num); state_int = FALSE; } break; case '+': case '-': case '*': case '/': if (state_int == TRUE) { push_seq(ps, num); state_int = FALSE; } if (isEmptyStack_seq(ps)) { free(ps); return FALSE; } num2 = top_seq(ps); pop_seq(ps); if (isEmptyStack_seq(ps)) { free(ps); return FALSE; } num1 = top_seq(ps); pop_seq(ps); if (c == '+') push_seq(ps, num1 + num2); if (c == '-') push_seq(ps, num1 - num2); if (c == '*') push_seq(ps, num1 * num2); if (c == '/') push_seq(ps, num1 / num2); break; default: free(ps); return FALSE; } } *presult = top_seq(ps); pop_seq(ps); if (!isEmptyStack_seq(ps)) { free(ps); return FALSE; } free(ps); return TRUE; } void getline(char * line, int limit) { char c; int i = 0; while (i < limit - 1 && (c = getchar()) != EOF && c != '\n') line[i++] = c; line[i] = '\0'; } void main() { char c, infix[MAXNUM], suffix[MAXNUM]; int result; int flag = TRUE; while (flag == TRUE) { printf("Plese input an infix expression!\nInput:"); getline(infix, MAXNUM); if(infixtoSuffix(infix, suffix) == TRUE) printf("suffix:%s\n", suffix); else { printf("Invalid infix!\n"); printf("\nContinue? (y/n)"); scanf("%c", &c); if (c == 'n' || c == 'N') flag = FALSE; while (getchar() != '\n'); printf("\n"); continue; } if(calculateSuffix(suffix, &result) == TRUE) printf("Result:%d\n", result); else printf("Invalid suffix!\n"); printf("\nContinue? (y/n)"); scanf("%c", &c); if (c == 'n' || c == 'N') flag = FALSE; while (getchar() != '\n'); printf("\n"); } }

2005-04-26 22:28
快速回复:[求助]:高手帮我编编嘛!急等!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028754 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved