| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 473 人关注过本帖
标题:转自“数据结构与算法”的帖子大家指点一下,谢谢了。
只看楼主 加入收藏
phh
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-4-22
收藏
 问题点数:0 回复次数:1 
转自“数据结构与算法”的帖子大家指点一下,谢谢了。
这是转自“数据结构与算法”的帖子,在那里求助了好久~~~ 但是没有人帮忙~~~
这个程序不识别 负 号 那位能帮帮忙指点一下把这个程序改一下,能识别 负 号 怎样区别 负 号 与 减号 的区别?
我想了好久,就是想不明白怎么办~~~
而且要是 运算中有括号 怎么办?
是不是在用户输入数据的时候,如果系统遇到左括号,那么就记下这个括号所在数组的地址,依次类推,用if 嵌套来进行判断哪两个括号是一对的?请高手指点一下~~~~ 但是具体怎么实现呢~~~~
大家 高手 们 帮帮忙
#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);
}

搜索更多相关主题的帖子: 数据结构 算法 帖子 
2006-04-22 14:59
phh
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-4-22
收藏
得分:0 
。。。。。。
没有人么~~
2006-04-22 16:15
快速回复:转自“数据结构与算法”的帖子大家指点一下,谢谢了。
数据加载中...
 
   



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

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