| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 578 人关注过本帖
标题:帮我改一下,要有小数运算
只看楼主 加入收藏
uhb
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-6-20
收藏
 问题点数:0 回复次数:4 
帮我改一下,要有小数运算
#include <stdio.h>
struct s_node
{
float data;
struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
link operator=NULL;
link operand=NULL;

link push(link stack,int value)
{
link newnode;

newnode=(link) malloc(sizeof(s_list));
if(!newnode)
{
printf("\nMemory allocation failure!!!");
return NULL;
}
newnode->data=value;
newnode->next=stack;
stack=newnode;
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 is_operator(char operator)
{
switch (operator)
{
case '+': case '-': case '*': case '/': return 1;
default:return 0;
}
}


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


int two_result(int operator,int operand1,int operand2)
{
switch(operator)
{
case '+':return(operand2+operand1);
case '-':return(operand2-operand1);
case '*':return(operand2*operand1);
case '/':return(operand2/operand1);
}
}

void main()
{
char expression[50];
int position=0;
int op=0;
int operand1=0;
int operand2=0;
int evaluate=0;

printf("\nPlease input the inorder expression:");
gets(expression);

while(expression[position]!='\0'&&expression[position]!='\n')
{
if(is_operator(expression[position]))
{
if(!empty(operator))
while(priority(expression[position])<= priority(operator->data)&&
!empty(operator))
{
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);
operator=pop(operator,&op);
operand=push(operand,two_result(op,operand1,operand2));
}
operator=push(operator,expression[position]);
}
else
operand=push(operand,expression[position]-48);
position++;
}
while(!empty(operator))
{
operator=pop(operator,&op);
operand=pop(operand,&operand1);
operand=pop(operand,&operand2);

operand=push(operand,two_result(op,operand1,operand2));
}
operand=pop(operand,&evaluate);
printf("The expression [%s] result is '%d' ",expression,evaluate);
getch();
}

搜索更多相关主题的帖子: 小数 运算 
2008-06-20 13:19
scl512
Rank: 1
来 自:福建
等 级:新手上路
帖 子:35
专家分:0
注 册:2008-6-20
收藏
得分:0 
结构这边还是不大懂得 在学习中!!呵呵!!~!~
2008-06-20 13:22
yyow3193
Rank: 1
来 自:三峡宜昌
等 级:新手上路
帖 子:165
专家分:0
注 册:2008-6-5
收藏
得分:0 
超出我目前了解的范围了
2008-06-20 13:45
coming
Rank: 1
等 级:新手上路
帖 子:244
专家分:0
注 册:2008-4-20
收藏
得分:0 
看见这么长的程序就得晚上静静的看,头晕...
2008-06-20 21:43
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
话说,变量名如果太长还是建议大小写交叉着写,这样看起来比较清晰……

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-06-20 21:56
快速回复:帮我改一下,要有小数运算
数据加载中...
 
   



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

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