用C语言编写一个简易计算器可实现加减乘除,连加连减,连乖连除.
用C语言编写一个简易计算器可实现加减乘除,连加连减,连乖连除.
#include<stdio.h>
#include"stdlib.h"
#define N 10
void fun()
{
int a,b,t;
char c;
scanf("%d",&a);
scanf(" %c",&c);
scanf("%d",&b);
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%')
{
if(c=='+') t=a+b;
if(c=='-') t=a-b;
if(c=='*') t=a*b;
if(c=='%') t=a%b;
if(c=='/')
{
if(b==0)
{
printf("0不能做除数\n");
}
else
t=a/b;
}
printf("%d%c%d=%d\n",a,c,b,t);
}
else printf("运算符号错误!\n");
}
void main()
{
int i;
char quit;
for(i=0;i<N;i++)
{
fun();
printf("是否继续(y/n)?\n");
scanf(" %c", &quit);
if(quit=='Y'||quit=='y') {
system("cls"); //清屏函数 头文件为stdlib.h
continue;
}
if(quit=='N'||quit=='n') break;
}
}#include <stdio.h>
int jia(int a,int b)
{
int s;
s=a+b;
return s;
}
int jian(int a,int b)
{
int s;
s=a-b;
return s;
}
int cheng(int a,int b)
{
int s;
s=a*b;
return s;
}
int chu(int a,int b)
{
int s;
s=a/b;
return s;
}
void main()
{
int a,b,x,y,i;
char c;
printf("请输入运算式:\n");
scanf("%d%c%d",&a,&c,&b);
x=a;
y=b;
for(i=0;;i++)
{
if(c=='+')x=jia(x,y);
else if(c=='-')x=jian(x,y);
else if(c=='*')x=cheng(x,y);
else if(c=='/')x=chu(x,y);
c=getchar();
if(c=='=')
{
printf("%d\n",x);
break;
}
scanf("%d",&y);
}
}