用c语言编写计算器

Python029

用c语言编写计算器,第1张

#include <stdio.h>

struct s_node

{

int 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()

}

1

首先,得从网上下载并安装c++,然后从桌面找到并打开它。

2

四则运算+ - * / 可以用if语句或switch语句实现。两者均用于判断。

if语句运用较广,switch语句运用较为简单方便。

END

用if语句

1

下面先介绍用if语句实现四则运算的方法。

2

#include "stdafx.h"

int main(int argc, char* argv[])

{

float x

float y

char r

printf("请输入如 1+2 的格式\n")

scanf("%f%c%f",&x,&r,&y)

if(r=='+') printf("x+y=%f\n",x+y)

else if(r=='-') printf("x-y=%f\n",x-y)

else if(r=='*') printf("x*y=%f\n",x*y)

else if(r=='/') printf("x/y=%f\n",x/y)

else  printf("input error\n")

return 0

}

3

输入完毕,进行【全部重建】,然后就可以运行了。

在运行框输入你想要进行的运算,例如想计算 2*5 的值,你只需在运行框直接输入 2*5 ,然后按【enter】键,结果就出来啦。

4

因为每次进行计算都要重新运行,很麻烦,我们可以加入循环语句。

5

#include "stdafx.h"

int main(int argc, char* argv[])

{

float x

float y

char r

printf("请输入如 1+2 的格式\n")

scanf("%f%c%f",&x,&r,&y)

for()

{

if(r=='+') printf("x+y=%f\n",x+y)

else if(r=='-') printf("x-y=%f\n",x-y)

else if(r=='*') printf("x*y=%f\n",x*y)

else if(r=='/') printf("x/y=%f\n",x/y)

else  printf("input error\n")

printf("请输入如 1+2 的格式\n")

scanf("%f%c%f",&x,&r,&y)

}

return 0

}

6

加入循环之后,我们不用退出再次运行,我们可以在运行框连续输入,非常方便。

END

用switch语句

1

这次是使用switch语句,可达到同样的目的。

2

#include "stdafx.h"

int main(int argc, char* argv[])

{

float x

float y

char r

printf("请输入如 1+2 的格式\n")

scanf("%f%c%f",&x,&r,&y)

switch(r)

{

case '+': printf("x+y=%f\n",x+y)break

case '-': printf("x-y=%f\n",x-y)break

case '*': printf("x*y=%f\n",x*y)break

case '/': printf("x/y=%f\n",x/y)break

default:printf("input error\n")

}

return 0

}

3

输入表达式完毕后,进行【全部重建】,然后就可以运行了。

在运行框输入你想要进行的运算,例如想计算 8/7 的值,你只需在运行框直接输入 8/7 ,然后按【enter】键,结果就出来啦。

4

switch语句也可如if语句一般,加入循环,可进行连续输入。

5

#include "stdafx.h"

int main(int argc, char* argv[])

{

float x

float y

char r

printf("请输入如 1+2 的格式\n")

scanf("%f%c%f",&x,&r,&y)

for()

{

switch(r)

{

case '+': printf("x+y=%f\n",x+y)break

case '-': printf("x-y=%f\n",x-y)break

case '*': printf("x*y=%f\n",x*y)break

case '/': printf("x/y=%f\n",x/y)break

default:printf("input error\n")

}

printf("请输入如 1+2 的格式\n")

scanf("%f%c%f",&x,&r,&y)

}

return 0

}

END