#include <stdio.h>
#include <string.h>
typedef int SElemType // 栈的元素类型
#define STACK_INIT_SIZE 10 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
/*
*顺序栈的结构体
* */
typedef struct SqStack
{
SElemType *base // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top // 栈顶指针
int stacksize // 当前已分配的存储空间,以元素为单位
}SqStack
/*
*构造一个栈
* */
int InitStack(SqStack *S)
{
// 为栈底分配一个指定大小的存储空间
(*S).base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType))
if( !(*S).base )
exit(0) // 存储分配失败
(*S).top = (*S).base // 栈底与栈顶相同表示一个空栈
(*S).stacksize = STACK_INIT_SIZE
return 1
}
/*
*获取栈顶元素
* */
int GetTop(SqStack S,SElemType *e)
{
if(S.top > S.base)
{
*e = *(S.top-1) // 栈顶指针的下一个位置为栈顶元素
return 1
}
else
return 0
}
/*
*入栈(压栈)
* */
int Push(SqStack *S, SElemType e)
{
if((*S).top - (*S).base >= (*S).stacksize) // 栈满,追加存储空间
{
(*S).base = (SElemType *)realloc((*S).base,
((*S).stacksize + STACKINCREMENT) * sizeof(SElemType))
if( !(*S).base )
exit(0) // 存储分配失败
(*S).top = (*S).base+(*S).stacksize
(*S).stacksize += STACKINCREMENT
}
*((*S).top)++=e
// 这个等式的++ * 优先级相同,但是它们的运算方式,是自右向左
return 1
}
/*
*出栈
* */
int Pop(SqStack *S,SElemType *e)
{
if((*S).top == (*S).base)
return 0
*e = *--(*S).top
// 这个等式的++ * 优先级相同,但是它们的运算方式,是自右向左
return 1
}
/*
*判断优先级
* */
SElemType Precede(SElemType t1,SElemType t2)
{
SElemType f
switch(t2)
{
case '+':
case '-':
if(t1=='('||t1=='#')
f='<'
else
f='>'
break
case '*':
case '/':
if(t1=='*'||t1=='/'||t1==')')
f='>'
else
f='<'
break
case '(':
if(t1==')')
{
printf(ERROR1
)
exit(0)
}
else
f='<'
break
case ')':
switch(t1)
{
case '(':
f='='
break
case '#':
printf(ERROR2
)
exit(0)
default:
f='>'
}
break
case '#':
switch(t1)
{
case '#':
f='='
break
case '(':
printf(ERROR3
)
exit(0)
default:
f='>'
}
}
return f
}
/*
*搜索运算符
* */
int In(SElemType c)
{
switch(c)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'#':return 1
default:return 0
}
}
/*
*运算
* */
SElemType Operate(SElemType a,SElemType theta,SElemType b)
{
SElemType c
a=a-48 //ASCII值转化为对应的十进制值
b=b-48 //ASCII值转化为对应的十进制值
switch(theta)
{
case'+':
c=a+b+48
break
case'-':
c=a-b+48
break
case'*':
c=a*b+48
break
case'/':c=a/b+48
}
return c
}
/*
*比较运算符优先级
* */
SElemType EvaluateExpression()
{
SqStack OPTR,OPND
SElemType a,b,c,x,theta
InitStack(&OPTR)
Push(&OPTR,'#')
InitStack(&OPND)
c=getchar()
GetTop(OPTR,&x)
while(c!='#'||x!='#')
{
if(In(c)) // 是7种运算符之一
switch(Precede(x,c))
{
case'<':
Push(&OPTR,c) // 栈顶元素优先权低
c=getchar()
break
case'=':
Pop(&OPTR,&x) // 脱括号并接收下一字符
c=getchar()
break
case'>':
Pop(&OPTR,&theta) // 退栈并将运算结果入栈
Pop(&OPND,&b)
Pop(&OPND,&a)
Push(&OPND,Operate(a,theta,b))
break
}
else if(c>='0'&&c<='9') // c是操作数
{
Push(&OPND,c)
c=getchar()
}
else // c是非法字符
{
printf(非法字符!!
)
exit(0)
}
GetTop(OPTR,&x)
}
GetTop(OPND,&x)
return x
}
int main()
{
printf(请输入算术表达式,并以#结束)
printf("%d",EvaluateExpression())
return 0
}
#include<stdio.h>#include<stdlib.h>
#define MaxSize 99
void translate(char str[],char exp[]) /*将算术表达式转换成后缀表达式*/
{
struct
{
char data[MaxSize]
int top /*top为栈顶*/
}op/*定义一个含data和top的结构体*/
char ch
int i = 0,t = 0
op.top = -1
ch = str[i] /*将str的每一个数转换成ch*/
i++
while(ch != '\0') /*ch对应不同的符号的时候对应的转换情况*/
{
switch(ch)
{
case '(': /*当是(的时候,将此括号存入栈op*/
op.top++op.data[op.top]=ch
break
case ')':
while(op.data[op.top] != '(') /*括号内的转换优先级最高,故先提取表达式*/
{
exp[t]=op.data[op.top]
op.top--
t++
}
op.top--
break
case '+':
case '-':
while(op.top != -1&&op.data[op.top] != '(')
{
exp[t] = op.data[op.top]
op.top--
t++
}
op.top++ /*恢复可插入位置*/
op.data[op.top] = ch
break
case '*':
case '/':
while(op.top == '/'||op.top == '*') /*优先级*/
{
exp[t] = op.data[op.top]
op.top--
t++
}
op.top++
op.data[op.top] = ch
break
case ' ': /*忽略空格,排除误操作*/
break
default:
while(ch >= '0'&&ch <= '9')
{
exp[t] = cht++
ch = str[i]i++
}
i--
exp[t] = '#' /*分隔操作数,为了美观,也为了以后好分隔操作数,呵呵*/
t++
}
ch = str[i]
i++
}
while(op.top != -1) /*得到剩下的部分*/
{
exp[t] = op.data[op.top]
t++
op.top--
}
exp[t] = '\0'/*表达式结束*/
}
float cal_value(char exp[])
{
struct
{
float data[MaxSize]
int top
}st /*操作数栈*/
float d
char ch
int t = 0
st.top = -1
ch = exp[t]
t++
while(ch != '\0')
{
switch(ch) /*运算主体*/
{
case '+':
st.data[st.top-1] = st.data[st.top-1]+st.data[st.top]
st.top--
break
case '-':
st.data[st.top-1] = st.data[st.top-1]-st.data[st.top]
st.top--
break
case '*':
st.data[st.top-1] = st.data[st.top-1]*st.data[st.top]
st.top--
break
case '/':
if(st.data[st.top] != 0)
st.data[st.top-1]=st.data[st.top-1]/st.data[st.top]
else
{
printf("\n\t除0是错误的")
}
st.top--
break
default:
d=0
while(ch >= '0'&&ch <= '9') /*从后缀表达式中获取操作数,#作用在此体现*/
{
d = 10*d+ch-'0'
ch = exp[t]
t++
}
st.top++
st.data[st.top] = d
}
ch = exp[t]
t++
}
return st.data[st.top]
}
int main() /*可以提到前面去*/
{
char str[MaxSize],exp[MaxSize] /*str为算术表达式,exps为后缀表达式*/
printf("请输入一个求值表达式\n")
printf("表达式:")
gets(str) /*输入一个算术表达式*/
printf("原表达式是:%s\n",str)
translate(str,exp) /*将算术表达式转换成后追表达式*/
printf("后缀表达式:%s\n",exp)
printf("计算结果:%g\n",cal_value(exp))/*通过后缀表达式来求值*/
system("pause")
return 0
}
理论上来说增加括号只会增加编译的时间 并且对运行的时间是没有差别的
也就是增加了把你从c/c++代码变成exe的时间,但是不会影响exe运行的时间
而且 增加一两个括号 对于计算机编译时间的影响是微乎其微的
计算机一秒计算数量以亿算 怎么会差这一个括号呢
关于代码运行效率 更应该注意的是算法,而不是这些语句 那个才能从本质上提高程序运行效率
多行的代码 运行效率就不一定比单行的要低
作为源代码重要的一是算法 二是可读性 增加括号和增加有必要的换行不但不会降低运行效率,反而会使代码更加容易理解 减少歧义的产生