我正在试着制作一个基本的计算器,它可以执行各种算术功能,从添加开始!现在我已经掌握了它的基本逻辑,但我不确定如何接受两个输入并将其打印出来!C语言简介:添加和打印

#include

int main()

{

char mychar;

int a;

int op1;

int op2;

printf("Welcome to Andrew Hu's calculator program!\n"); //Greeting

while(1)

{ printf("Enter a mathematical operation to perform:\n");

scanf("%c", &mychar);

if(mychar == '+') //Valid Operators

a = 1;

else

a = 0;

if(a == 0) //Operator Checker, error if invalid

printf("\nError, not a valid operator\n");

else if(a == 1){

printf("%c\n", &mychar),

printf("Enter OP1:\n"),

/* not sure what to put here to echo the character as a decimal*/

printf("Enter OP2:\n"),

/* not sure what to put here to echo the character as a decimal either*/

printf("Result of %d %c %d = %d\n", op1, mychar, op2, (op1 + op2))

/* this last line I'm not too sure of. I'm trying to print out the expression

which is op1 + op2 = the sum of both. */

;

}

}

+0

你首先要做的是找出不同的输入方法。最常用的(初学者)是'scanf'。然后,您必须阅读['scanf'手册](http://pubs.opengroup.org/onlinepubs/009695399/functions/scanf.html)。当您了解如何使用scanf时,开始编码_only_。 –

+1

@Andrew Hu - 不要忘记接受答案,已经解决了你的问题/问题:) –

更多推荐