c语言计算器函数 c语言计算器函数程序
用简单c语言编写计算器
#include"stdio.h"
专注于为中小企业提供成都网站建设、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业旅顺口免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了数千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
/*预处理命令*/
void
main()
/*主函数*/
{
double
a,b;
/*双精度实型变量说明*/
char
c,d;
/*变量说明*/
do
/*循环体*/
{
printf("input
a
(-*/)b\n");
/*输入提示*/
scanf("%lf%c%lf",a,c,b);
/*输入算术表达式*/
if(c=='
')
/*判断
*/
printf("=%0.2f",a
b);
/*输出a
b的值*/
else
if(c=='-')
/*判断-*/
printf("=%0.2f",a-b);
/*输出a-b的值*/
else
if(c=='*')
/*判断**/
printf("=%0.2f",a*b);
/*输出a*b的值*/
else
if(c=='/')
/*判断/*/
printf("=%0.3f",a/b);
/*输出a/b*/
else
/*不满足以上条件*/
printf("error");
/*输出错误*/
printf("\n\ninput\n");
/*输入\n*/
scanf("%c",d);
/*输入符号给d*/
}
/*循环体结束*/
while(d=='\n');
/*循环条件语句*/
}
C语言编写计算器
总算看懂了,一个只能两个数相加减乘除的计算器何必写的那么复杂,竟然还用了六个函数,下面我写一个功能一样的,更精简方便的,只要一个函数。
/*
Note:Your
choice
is
C
IDE
*/
/*一个具有两个数加减乘除功能的计算器*/
#include
"stdio.h"
void
main()
{
int
iFirNum,iSecNum,iResult;
char
ch,ch1;
printf("请输入表达式如
5+6=
然后按回车键:");
scanf("%d%c%d%c",iFirNum,ch,iSecNum,ch1);
switch(ch)
{
case
'+':
iResult=iFirNum+iSecNum;
printf("%d+%d=%d\n",iFirNum,iSecNum,iResult);
break;
case
'-':
iResult=iFirNum-iSecNum;
printf("%d-%d=%d\n",iFirNum,iSecNum,iResult);
break;
case
'*':
iResult=iFirNum*iSecNum;
printf("%d*%d=%d\n",iFirNum,iSecNum,iResult);
break;
case
'/':
iResult=iFirNum/iSecNum;
printf("%d/%d=%d\n",iFirNum,iSecNum,iResult);
break;
default:
printf("输入表达式错误或该计算器不具备
%ch
功能\n",ch);
}
}
编写函数实现简易计算器的功能(C语言)
#include
void
main()
{
float
a,b;
char
d;
do
{
printf("Please
enter
the
two
Numbers,
separated
by
Spaces:\n");
scanf("%f
%f",a,b);
printf("Please
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);
break;
case'*':
printf("a*b=%f\n",a*b);
break;
case'/':
printf("a/b=%f\n",a/b);
break;
default:
printf("input
error\n");
}
printf("Do
you
want
to
continue(Y/N
or
y/n)");
fflush(stdin);
}
while(toupper(getchar())=='Y');
}
可以运行,不知道满不满足你的要求,你自己可以试试
C语言函数做计算器的问题
在jisuanqi()已经输出,在main()又一次输出jisuanqi()的返回值a+b。可以修改如下:
#include
"stdio.h"
int
jisuanqi(int
a,char
c,
int
b)
{
switch(c)
{
case
'+':
printf("%d\n",a+b);
break;
case
'-':
printf("%d\n",a-b);
break;
case
'*':
printf("%d\n",a*b);
break;
case
'/':
printf("%d\n",a/b);
break;
}
return
0;
}
int
main(int
argc,
char*
argv[])
{
int
a,b;
char
c;
scanf("%d
%c
%d",a,c,b);
jisuanqi(a,c,b);
return
0;
}
用c语言编写计算器
#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();
}
本文标题:c语言计算器函数 c语言计算器函数程序
网页链接:http://hbruida.cn/article/dooscoj.html