| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1611 人关注过本帖
标题:请各位高手帮忙,写一个四则运算的算法
只看楼主 加入收藏
bud
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2004-6-2
收藏
 问题点数:0 回复次数:8 
请各位高手帮忙,写一个四则运算的算法

请各位高手帮忙,写一个四则运算的算法,最好是C程序。例如可以根据字符串 1+2+3+4+5*6+((7+8)/5) 求出结果的程序或算法。急!

搜索更多相关主题的帖子: 算法 运算 
2004-06-02 21:10
jiely
Rank: 1
等 级:新手上路
帖 子:106
专家分:0
注 册:2004-5-15
收藏
得分:0 
好象数据结构书上有的~~~用栈来存储~

我的爱好:C和数据库!正在学VC++ 我的QQ:345895839,有共同爱好的加我!期待着与你共同进步!
2004-06-02 21:17
C_Builder
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2004-6-2
收藏
得分:0 

楼上的,你用的是什么出版社出版的书呀?把书名和出版社名写出来给大家呀!

___________________________________________

I'm Programer !


2004-06-03 19:47
crazy
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2004-5-8
收藏
得分:0 
最好查查编译原理的书,有关编译器章节得好象有!
2004-06-03 23:21
jellen
Rank: 1
等 级:新手上路
威 望:1
帖 子:107
专家分:0
注 册:2004-5-3
收藏
得分:0 

帖一个我n年前写的程序,基本上满足楼主的条件(在TC2.0里面通过):

程序代码:
#include<stdio.h>
#include<stdlib.h>

struct stack_node { int data; struct stack_node *next; }; typedef struct stack_node stack_list; typedef stack_list *link;

link operator=NULL; link operand=NULL;

link push(link stack, int value) { link new_node; new_node=(link)malloc(sizeof(stack_list)); if(!new_node) { printf(\"Overflow!\n\"); return NULL; } new_node->data=value; new_node->next=stack; stack=new_node; return stack; }

link pop(link stack, int *value) { link top; if(stack!=NULL) { top=stack; stack=stack->next; *value=top->data; free(top); return stack; } else *value=-1; }

int empty(link stack) { if(stack==NULL) return 1; else return 0; }

int isoperator(char op) { switch(op) { case '+': case '-': case '*': case '/': return 1; default: return 0; } }

int priority(char op) { switch(op) { case ')': return 3; case '*': case '/': return 2; case '+': case '-': return 1; case '(': return -1; default: return 0; } }

int get_value(int op, int operand1, int operand2) { switch((char)op) { case '*': return(operand2*operand1); case '/': return(operand2/operand1); case '-': return(operand2-operand1); case '+': return(operand2+operand1); } }

void main() { char exp[100]; int op=0; int operand1=0; int operand2=0; int result=0; int pos=0;

printf(\"Please input the expression:\n\"); gets(exp);

while(exp[pos]!='\0'&&exp[pos]!='\n') { if(exp[pos]==' ') { pos++; continue; } if(exp[pos]=='(') operator=push(operator,exp[pos]); else if(exp[pos]==')') { operator=pop(operator,&op); while(op!='(') { operand=pop(operand,&operand1); operand=pop(operand,&operand2); operand=push(operand,get_value(op,operand1,operand2)); operator=pop(operator,&op); } } else if(isoperator(exp[pos])) { while(priority(exp[pos])<=priority(operator->data)&&!empty(operator)) { operator=pop(operator, &op); operand=pop(operand, &operand1); operand=pop(operand, &operand2); operand=push(operand,get_value(op,operand1,operand2)); } operator=push(operator,exp[pos]); } else operand=push(operand,exp[pos]-48); pos++; } while(!empty(operator)) { operator=pop(operator,&op); operand=pop(operand,&operand1); operand=pop(operand,&operand2); operand=push(operand,get_value(op,operand1,operand2)); } operand=pop(operand,&result); printf(\"The result of expression [%s] is: %d\n\",exp,result); }


再见,理想!
2004-06-17 21:54
chengstone
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:562
专家分:226
注 册:2004-4-3
收藏
得分:0 

楼上的写的不错 正是最典型的栈操作

http://bbs.bc-cn.net/bbs/dispbbs.asp?boardID=39&ID=442&page=1

楼主若是想看计数器及四则混合运算方面的 就看上面的连接把


qq:69558139
2004-07-04 15:04
网络游侠
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2004-9-27
收藏
得分:0 
写一个计算器不就解决问题了吗

我追求! 我需要! 我感受! 你是我的一切!
2004-10-15 09:28
icehacker99
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2005-5-7
收藏
得分:0 
这么没有注释啊。

2005-05-10 22:20
冰雪
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-7-3
收藏
得分:0 
支持,希望来点注释就好了,这样像我这样的菜鸟就能看懂了
2005-07-03 16:34
快速回复:请各位高手帮忙,写一个四则运算的算法
数据加载中...
 
   



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

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