#include<stdio.h>
#include<stdlib.h>拿肢
#include<string.h>
double part_take1(char a[],int b,int c)
double multiply(char h[])
void part_take2(char d[],char *e,int f,int g)
int main(void)
{
int i
int j
int k
int length
char ch2[100]
char express[100]
char reset
double part1
double result=0
k=-1
puts("请输入计算式")
gets(express)
length=strlen(express)
for(i=0i<strlen(express)i++)
{
if(express[i]=='+'||express[i]=='-')
{
part_take2(express,ch2,k+1,i-1)
part1=multiply(ch2)
if(k==-1)
{
result=result+part1
}
if(express[k]=='+'&&k>0)
{
result=result+part1
}
if(express[k]=='-'&&k肆洞>0)
{
result=result-part1
}
ch2[0]='\0'
k=i
}
}
part_take2(express,ch2,k+1,length-1)
part1=multiply(ch2)
if(k==-1||express[k]=='+')
{
result=result+part1
}
else
{
result=result-part1
}
printf("%f",result)
system("pause")
return 0
}
double part_take1(char a[],int b,int c)
{
char ch1[100]
int j
int h
double result
for(h=b,j=0h<=ch++,j++)
{
ch1[j]=a[h]
}
result=atof(ch1)
return(result)
}
double multiply(char h[])
{
int k=-1
int i
double part=1
double num
int s=strlen(h)
for(i=0i<si++)
{
if(h[i]=='*'裂敏枯||h[i]=='/')
{
num=part_take1(h,k+1,i-1)
if(k<0)
{
part=part*num
}
if(h[k]=='*'&&k>0)
{
part=part*num
}
if(h[k]=='/'&&k>0)
{
part=part/num
}
k=i
}
}
num=part_take1(h,k+1,s-1)
if(h[k]=='*'||k==-1)
{
part=part*num
}
else
{
part=part/num
}
return(part)
}
void part_take2(char d[],char *e,int f,int g)
{
int i
int j
for(i=f,j=0i<=gi++,j++)
{
e[j]=d[i]
}
e[j]='\0'
}
如题,代码如下,欢迎探讨!!![code=C/C++][/code]
/* 表达式的后缀表示及其求值 */
#include <stdio.h>
typedef int ElemType/* 考虑到char型运算时的隐形提升会溢出,此处定义为int,代价仅孝颤肆浪费了内存空间 */
#define MAXNUM 16
struct stack
{
ElemType data[MAXNUM]
int top
}
void StackInit(struct stack *stack)
{
int i = 0
for(i <MAXNUMi++)
{
stack->data[i] = 0
}
stack->top = 0/* 栈底部从索引0处开始 */
}
void StackPush(struct stack *stack,ElemType c)
{
if(MAXNUM == stack->top) /* 栈中元素最多有MAXNUM - 1个 */
{
printf("The stack is full ")
return
}
stack->data[stack->top++] = c
}
ElemType StackPop(struct stack *stack)
{
if(0 == stack->top)
{
printf("The stack is empty ")/* 当栈空时,巧轿若返回0是错误的 */
return 0
}
return stack->data[--stack->top]
}
void PostfixEvaluation(struct stack *stack)
{
return/* 这个函数非常简单了,所有的麻烦都已经解决了,我实在不想完成它 */
}
int ChangeToPostfix(char *str)
{
int i = 0,flag = 0/* flag 用来标记连续数字的出现,没想到好点的办法 */
int c,ch
struct stack ch_stack
struct stack op_stack
StackInit(&ch_stack)
StackInit(&op_stack)
while( != (c = *(str + i))) /* 此处需注意的是:如果是静态的字符串,以为结束条件,如果是用户输入的,则以 ’为结束条件 */
{
if((* == c) || (/ == c) || (( == c))
{
flag = 0
StackPush(&op_stack,c)
}
else if() == c)
{
flag = 0
while(( != (c = StackPop(&op_stack)))
{
StackPush(&ch_stack,c)
}
if(0 == op_stack.top)
{
printf("the ( hasnt found when the ) come in! ")
return -1
}
}
else if((+ == c)|| (- == c))
{
flag = 0
/* +和-优先级低,运算符栈中的((如果存在)后的所有运算符需推出 */
if(0 != op_stack.top) /* 你可以不在此处添加top的检查,那样,你可以发现 StackPop错误返回的0被拾取了 */
{ /* 被逼无奈,只得在此检查top值,无法寄希望于StackPop了 */
while(( != (ch = StackPop(&op_stack)))
{
StackPush(&ch_stack,ch)
if(0 == op_stack.top)
{
break
}
}
}
StackPush(&op_stack,c)
}
else if((c >= 0) &&(c <= 9)) /* 对于表达式中2位或多位连续洞凯的数字,需特殊处理 */
{
if(0 == flag)
{
StackPush(&ch_stack,(c - 0))
flag = 1
}
else
{
StackPush(&ch_stack,10 * StackPop(&ch_stack) + (c - 0))
}
}
i++
}
while(0 != op_stack.top) /* 表达式处理结束,将运算符栈中的所有运算符推出并压入字符栈 */
{
StackPush(&ch_stack,StackPop(&op_stack))
}
PostfixEvaluation(&ch_stack)/* 该函数放在此处可能较为欠妥,可是,只要完成了任务不就OK了么,难道你会在乎? */
/*just test */
for(i = 0i <ch_stack.topi++)
{
if(+ == ch_stack.data[i])
{
printf("+..")
}
else if(- == ch_stack.data[i])
{
printf("-..")
}
else if(* == ch_stack.data[i])
{
printf("*..")
}
else if(/ == ch_stack.data[i])
{
printf("/..")
}
else
{
printf("%d..",ch_stack.data[i])
}
}
return 0
}
int main(void)
{
char str[] = "12 + 34 * 435 - 5 / 1"
/* just test */
printf("The result should be : ")
printf("12 34 435 * + 5 1 / - [= 8] ")
if(-1 == ChangeToPostfix(str))
{
printf("ChangeToPostfix() error ")
return 1
}
return 0
}
支持 加减乘除猛罩括号负数开根乘方#include<stdio.h>
#include<math.h>
#include<malloc.h>
double jisuan(char a[])
{
int i=1,j,k,m,cnt=0,t1=0,t2=0,t3=0
char nibo[50],zhan2[50]
double x,n,l,z=0,zhan3[50]
typedef struct
{
double d1
int d2
}dd
typedef struct
{
dd data[50]
int top
}zhan1
zhan1 *shu
shu=(zhan1 *)malloc(sizeof(zhan1))
shu->top=0
while(a[i]!='\0')
{
if(a[i]>='0'&&a[i]<='9')
{
z=0
j=i+1
while(a[j]>='0'&&a[j]<='9')
{j++}
j--
for(k=ik<=jk++)
{
z=z*10+a[k]-'0'
}
j=j+1
x=z
if(a[j]=='.')
{
l=1
i=j+1
j=i+1
while(a[j]>='0'&&a[j]<='9')
{j++}
j--
for(k=ik<=jk++)
{
n=pow(0.1,l)
l=l+1
x=x+n*(a[k]-'0')
}
i=j+1
}
else i=j
shu->data[++shu->top].d1=x
shu->data[shu->top].d2=++cnt
nibo[++t1]='0'+shu->data[shu->top].d2
nibo[t1+1]='\0'
}
else if(a[i]=='(')
{
zhan2[++t2]=a[i]
i++
}
else if(a[i]==')')
{
j=t2
while(zhan2[j]!='(')
{
nibo[++t1]=zhan2[j]
nibo[t1+1]='\0'
j--
}
t2=j-1
i++
}
else if(a[i]=='+'弊盯)
{
while(t2>0&&zhan2[t2]!='(')
{
nibo[++t1]=zhan2[t2]
nibo[t1+1]='\0'
t2--
}
zhan2[++t2]=a[i]
i++
}
else if(a[i]=='-')
{
if(a[i-1]=='$')
{
a[0]='0'
i=0
}
else if(a[i-1]=='(')
{
a[i-1]='0'
a[i-2]='('
i=i-2
t2--
}
else
{
while(t2>0&&zhan2[t2]!='(')
{
nibo[++t1]=zhan2[t2]
nibo[t1+1]='\0'
t2--
}
zhan2[++t2]=a[i]
i++
}
}
else if(a[i]=='*'||a[i]=='/')
{
while(zhan2[t2]=='*'||zhan2[t2]=='/'||zhan2[t2]=='^'||zhan2[t2]=='#'枝卜闹)
{
nibo[++t1]=zhan2[t2]
nibo[t1+1]='\0'
t2--
}
zhan2[++t2]=a[i]
i++
}
else if(a[i]=='^'||a[i]=='#')
{
while(zhan2[t2]=='^'||zhan2[t2]=='#')
{
nibo[++t1]=zhan2[t2]
nibo[t1+1]='\0'
t2--
}
zhan2[++t2]=a[i]
i++
}
}
while(t2>0)
{
nibo[++t1]=zhan2[t2]
nibo[t1+1]='\0'
t2--
}
j=1t3=0
while(j<=t1)
{
if(nibo[j]>='0'&&nibo[j]!='^'&&nibo[j]!='#')//
{
for(i=1i<=shu->topi++)
{
if((int)(nibo[j]-'0')==shu->data[i].d2)
{
m=i
break
}
}
zhan3[++t3]=shu->data[m].d1
}
else if(nibo[j]=='+')
{
zhan3[t3-1]=zhan3[t3-1]+zhan3[t3]
t3--
}
else if(nibo[j]=='-')
{
zhan3[t3-1]=zhan3[t3-1]-zhan3[t3]
t3--
}
else if(nibo[j]=='*')
{
zhan3[t3-1]=zhan3[t3-1]*zhan3[t3]
t3--
}
else if(nibo[j]=='/')
{
zhan3[t3-1]=zhan3[t3-1]/zhan3[t3]
t3--
}
else if(nibo[j]=='^')
{
zhan3[t3-1]=pow(zhan3[t3-1],zhan3[t3])
t3--
}
else if(nibo[j]=='#')
{
zhan3[t3]=sqrt(zhan3[t3])
}
j++
}
return zhan3[t3]
}
void main()
{
for()
{
char x,a[50]
double jieguo
int i=0
a[0]='$'
printf("#表示开方,^表示乘方(支持负数)\n")
printf("请输入表达式,退出请输入q:\n\n")
scanf("%c",&x)
if(x=='q') break
while(x!='\n')
{
a[++i]=x
scanf("%c",&x)
}
a[i+1]='\0'
jieguo=jisuan(a)
printf("\n")
printf("结果为:%lf",jieguo)
printf("\n\n\n\n\n")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)