| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 849 人关注过本帖
标题:小弟刚写的一个不程序,没有修改,请大家完善指教
只看楼主 加入收藏
卷入内核
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2007-7-2
收藏
 问题点数:0 回复次数:1 
小弟刚写的一个不程序,没有修改,请大家完善指教

/*这是一个简单的整数四则运算小程序,在DOS下输入运算式以#号结束,能计算简单的整数运算。
制作人:刘仁长;
日期:2007年7月20日;
*/
#include <stdio.h>
#include <malloc.h>
//#include <ctype.h>
#include <math.h>

#define FALSE 0
#define TRUE 1
//运算符集合
char ops[11]={'#','+','-','*','/','(',')','[',']','{','}'};
char str[50]={0};
//数字栈
struct node
{
int i;
struct node *next;
}operate;
struct node *num; //数字栈顶指针
//符号栈
struct oper
{
char n;
struct oper *next;
}operand;
struct oper *stra;//符号栈顶指针
//数字入栈操作
int Push(struct node *top,int x)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)return(FALSE);
temp->i=x;
temp->next=top;
num=temp;
return(TRUE);
}
//数字出栈操作
int Pop(struct node *top,int *x)
{
struct node *temp;
temp=top->next;
if(temp==NULL)
return(FALSE);
num=top->next;
*x=top->i;
free(top);
return (TRUE);
}
//符号入栈操作
int Push1(struct oper *top,char x)
{
struct oper *temp;
temp=(struct oper *)malloc(sizeof(struct oper));
if(temp==NULL)return(FALSE);
temp->n=x;
temp->next=top;
stra=temp;
return(TRUE);
}
//符号出栈操作
int Pop1(struct oper *top,char *x)
{
struct oper *temp;
temp=top->next;
if(temp==NULL)
return(FALSE);
stra=top->next;
*x=top->n;
free(top);
return (TRUE);
}
//判别数字与符号算法
int IN(char ch)
{
int i;
for(i=0;i<11;i++)
if(ch==ops[i])
return TRUE;//如果是操作符,返回1
return (FALSE);
}

//优先算法
char pro(char a,char b)
{
int temp0=-1,temp1=-1;
if((a=='#')&(b=='#'))
return ('=');
switch(a)
{
case '+':
case '-':
temp0=1;break;
case '*':
case '/':
temp0=2;break;
case '{':
case '}':
temp0=3;break;
case '[':
case ']':
temp0=4;break;
case '(':
case ')':
temp0=5;break;
default: break;
}
switch(b)
{
case '+':
case '-':
temp1=1;break;
case '*':
case '/':
temp1=2;break;
case '{':
case '}':
temp1=3;break;
case '[':
case ']':
temp1=4;break;
case '(':
case ')':
temp1=5;break;
default: break;
}
if(temp0==temp1)
{
if((temp0>2)&&(a!=b))
return ('=');
else
return ('>');
}
else if(temp0>temp1)
{
if(a=='('||a=='['||a=='{')
return('<');
else
return ('>');
}
else
if(b==')'||b==']'||b=='}')
return ('>');
else
return ('<');
}
//匹配函数
int match(char x,char n)
{
if(abs(n-x)<3)
return 1;
return 0;
}
//检查符号匹配
int ExpEv(void)
{
struct oper S;
int i;
char ch;
stra=&S;
stra->next=NULL;
for(i=0;str[i]!='#';i++)
{
switch(str[i])
{
case'{':
case'[':
case'(':
Push1(stra,str[i]);
break;
case'}':
case']':
case')':
if(stra->next==NULL)
{
printf("右括号多余!\n");
return FALSE;
}
else
{
ch=stra->n;
if(match(ch,str[i]))
Pop1(stra,&ch);
else
{
printf("对应的左右括号不同类或未输入结束#号!\n");
return FALSE;
}
}
}
}
if(stra->next!=NULL)
{
printf("括号不匹配!\n");
return FALSE;
}
return TRUE;
}
//四则运算函数
int acc(int a,char op,int b)
{
int i=0;
switch(op)
{
case'+':i=a+b;break;
case'-':i=a-b;break;
case'*':i=a*b;break;
case'/':i=a/b;break;
default:break;
}
return i;
}
//运算式分析
int account(void)
{
int temp,a=0,b=0,v=0;
char op;
char *s=str;
struct node operate;
struct oper operand;
num=&operate;
stra=&operand;
num->next=NULL;
stra->next=NULL;
Push1(stra,'#');
while(*s!='\0')
{
if(!IN(*s))
{
temp=*s-'0';
s+=1;
while(!IN(*s)) //如果是操作数,不是操作符
{
temp=temp*10+*s-'0'; //转为十进制数
s+=1;
}
Push(num,temp); //压入数字栈
}
else //如果是操作符

switch(pro(stra->n,*s)) //检查优先级别
{
case'<':Push1(stra,*s);s+=1;break;
case'=':Pop1(stra,&op);s+=1;break;
case'>':Pop1(stra,&op);
Pop(num,&b);
Pop(num,&a);
v=acc(a,op,b);
Push(num,v);
break;
}
}
Pop(num,&v);
return (v);
}
int main()
{
int i;
printf("请输入四则运算式并于#号结束:\n");
gets(str);
while(ExpEv())
{
i=account();
printf("答案是%3d\n",i);
break;
}
return (1);
}
//程序完
程序有点小小的问题就是没有检查括号的顺序问题,有时间改了我再修改下

[此贴子已经被作者于2007-7-27 12:21:41编辑过]

搜索更多相关主题的帖子: node include struct 
2007-07-26 12:33
wingyip
Rank: 1
等 级:新手上路
威 望:2
帖 子:119
专家分:0
注 册:2007-7-16
收藏
得分:0 
高手啊 栈啊

2007-07-27 09:57
快速回复:小弟刚写的一个不程序,没有修改,请大家完善指教
数据加载中...
 
   



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

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