读书人

关于最基础的计算器。该怎么解决

发布时间: 2012-02-09 18:22:27 作者: rapoo

关于最基础的计算器。。。
我知道计算器是个几乎烂掉的问题。。。看了很多高手回复说自己去找网上一堆一堆的教程。。。

但我只需要开始用scanf输入数字,然后回车,getche输入运算符号,回车,循环一直到输入=的时候停止并显示运算结果就可以了。。。
因为有回车,运算出来的效果应该是:
4.0
+
3.0
-
1.0
=
6.0

网上的教程我找了,好多都太复杂,我是刚刚入门的,看不懂。

我自己试着写了,但写到一半卡住。。。请各位帮忙指点一下!

我自己的大概思路是:

输入数字 i (最开始的数字)
输入符号 op
while (op 不等于'=')
输入数字a
计算
输入符号
结束 while
输入=显示结果b



#include <stdio.h>
#include <conio.h>

int main(){

char op;
double i,a,b;
printf("please enter numbers to begin the calculation:\n");

scanf("%0.2f\n",i);
op=getche();

while(op !='='){
scanf("%0.2f\n",a)
b=i(到这里卡住了,我想的是b是运算结果,但没有具体的运算符号(当运算符号是op=getche();的时候),我怎么写才能让它计算?)






[解决办法]
没有仔细看你的代码,但是我按照你的思路写了一个

#include <stdio.h >
#include <conio.h >

int main(){

char op;
double a,b; a为左操作数, b为右操作数
printf("please enter numbers to begin the calculation:\n");

scanf("%0.2f\n",a);
op=getchar();

while(op != '= '){
scanf("%0.2f\n",b)
seitch(op) //计算结果并存储在a中,将a作为新的左操作数
{
case:'+';a+=b;
case:'-';a-=b;
case:'*';a*=b;
case:'\';a/=b;
}
op=getchar();
}
printf("The result is %f",a);
[解决办法]
只写了个大概
#include <stdio.h>
#include <conio.h>
#include <math.h>

int main()
{
int nResult = 0, nTemp = 0;
char OP = 0;
char Continue;

printf("Input Number\n");
scanf("%d", &nResult);

do
{
while(1)
{
printf("\nInput OP\n");
OP = getche();

if(OP == '+' || OP == '-' || OP == '*' || OP == '/')
break;
}

printf("\nInput Another Number\n");
scanf("%d", &nTemp);

switch(OP)
{
case '+':
nResult += nTemp;
break;

case '-':
nResult -= nTemp;
break;

case '*':
nResult *= nTemp;
break;

case '/':
nResult /= nTemp;
break;

default:
break;
}

printf("%d\n", nResult);
printf("Continue ? (Y/N)");
Continue = getche();;

}while(Continue == 'Y' || Continue == 'y');

return 0;
}

读书人网 >C++

热点推荐