计算器的C语言代码.就进行简单的加减乘除的运算,可以循环的,谢谢大神了!
发布网友
发布时间:2022-04-30 19:35
我来回答
共2个回答
热心网友
时间:2023-10-09 19:42
#include <stdio.h>
char chooseOprate;
void input(char choose){
float result,numA,numB;
printf("请输入第一个操作数:");
scanf("%2f",&numA);
printf("请输入第二个操作数:");
scanf("%2f",&numB);
printf("请输入操作符(1.+、2.-、3.*、4./):");
scanf("%c",&choose);
chooseOprate = choose;
if (choose == '1')) {
result = numA + numB;
printf("numA + numB = %2f",result);
}else if (choose=='2') {
result = numA - numB;
printf("numA - numB = %2f",result);
}else if (choose == '3'){
result = numA * numB;
printf("numA * numB =%2f ",result);
}else if (choose == '4') {
result = numA / numB;
printf("numA / numB = %2f",result);
}else{
printf("输入错误,请重新输入!\n");
printf(" ");
input(choose);
}
}
inputAnswer(){
char answer;
input(chooseOprate);
printf(" ");
printf("是否继续?(Y/N)");
scanf("%c",answer);
if (answer == 'y' || answer =='Y') {
input(chooseOprate);
inputAnswer();
printf("是否继续?(Y/N)");
}else if(answer == 'n' || answer == 'N')){
printf("程序结束!");
}else{
printf("输入错误,请重新输入!\n");
printf("\n");
inputAnswer();
}
printf("是否继续?(Y/N)");
}
void main() {
inputAnswer();
}
热心网友
时间:2023-10-09 19:42
int main()
{
char exp[8];
printf("enter exp:\n");
scanf("%s",exp); //输入类似于1+1 2*5这样的式子,一次一个
switch(exp[1]){
case '+':
printf("%d\n",exp[0]-'0'+exp[2]-'0'); break;
case '-':
printf("%d\n",exp[0]-exp[2]); break;
case '*':
printf("%d\n",(exp[0]-'0')*(exp[2]-'0')); break;
case '/':
printf("%f\n",(exp[0]-'0')*1.0/(exp[2]-'0')*1.0); //1.0是为了转化成浮点数好得到小数结果
};
return 0;
}