#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);
else
*value=-1;
int empty(link stack)
if(stack==NULL)
return 1;
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;
default: return 0;
void main()
int position=0;
int op=0;
int operand1=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);
operator=pop(operator,op);
operator=push(operator,expression[position]);
position++;
while(!empty(operator))
operand=pop(operand,evaluate);
printf("The expression [%s] result is '%d' ",expression,evaluate);
getch();
我们平时进行数学运算都是用计算器完成的,那么如何用C语言编写一个计算器呢?下面我给大家分享一下.
工具/材料
Dev C++
首先我们需要在Dev C++软件中创建一个C语言项目,项目类型选择控制台程序,如下图所示
此时此刻呢我们在项目下面新建C语言文件,如下图所示
然后我们在C文件中写入计算器逻辑代码,主要是让用户输入计算方式,然后程序自动计算,如下图所示
最后在弹出的界面中我们输入要计算的公式,程序就会自动计算,如下图所示
用C语言编写一个简单的可以进行加减乘除运算混合运算的计算器的方法:
#includestdio.h /*函数头:输入输出头文件*/
void main()/*空类型:主函数*/
int a,b,d; /*定义变量的数据类型为整型*/
char c;/*定义变量的数据类型为字符型*/
scanf("%d%c%d",a,c,b);/*输入四则运算式*/
switch(c) /*判断运算符号*/
case'+':d=a+b;break;/*进行加法运算*/
case'-':d=a-b;break;/*进行减法运算*/
case'*':d=a*b;break;/*进行乘法运算*/
case'/':d=a/b;break; /*进行除法运算*/
printf("%d%c%d=%d\n",a,c,b,d);/*输出结果*/
完整的源代码:
int a,b,d;/*定义变量的数据类型为整型*/
switch(c)/*判断运算符号*/
case'/':d=a/b;break;/*进行除法运算*/
#include
void
main()
float
a,b;
char
d;
do
printf("Please
enter
the
two
Numbers,
separated
by
Spaces:\n");
scanf("%f
%f",a,b);
select
operation
way:
(-,*,/,^,s,!)\n");
scanf("%s",d);
switch(d)
case'+':
printf("a+b=%f\n",a+b);
break;
case'-':
printf("a-b=%f\n",a-b);
case'*':
printf("a*b=%f\n",a*b);
case'/':
printf("a/b=%f\n",a/b);
default:
printf("input
error\n");
printf("Do
you
want
to
continue(Y/N
or
y/n)");
fflush(stdin);
while(toupper(getchar())=='Y');
可以运行,不知道满不满足你的要求,你自己可以试试
四则运算+ - * / 可以用if语句或switch语句实现.两者均用于判断.
if语句运用较广,switch语句运用较为简单方便.
END
用if语句
下面先介绍用if语句实现四则运算的方法.
#include "stdafx.h"
int main(int argc, char* argv[])
float x;
float y;
char r;
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");
输入完毕,进行【全部重建】,然后就可以运行了.
因为每次进行计算都要重新运行,很麻烦,我们可以加入循环语句.
for(;;)
加入循环之后,我们不用退出再次运行,我们可以在运行框连续输入,非常方便.
用switch语句
这次是使用switch语句,可达到同样的目的.
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");
输入表达式完毕后,进行【全部重建】,然后就可以运行了.
switch语句也可如if语句一般,加入循环,可进行连续输入.
C语言编写计算器
然后需要检测输入是否是正确的,检查是不是+ - * / %,今天这一节要用到switch函数,用来看operation变量是否别传入了正确的值.
switch(operation)
case '+':
printf........
具体的运算我们只需要再case之后的printf语句中设定和输出就可以了.
以上就是土嘎嘎小编为大家整理的c语言用函数制作计算器相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!