gpt4 book ai didi

c - C : Addition and Printing简介

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:14 25 4
gpt4 key购买 nike

我正在尝试制作一个可以执行各种算术函数的基本计算器,从加法开始!现在我已经弄清楚了它的基本逻辑,但我不确定如何获取两个输入并将其打印出来!

#include <stdio.h>

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. */
;
}
}

最佳答案

使用 scanf 语句获取输入,就像您获取数学运算符的方式一样。switch case 语句可以很好地实现计算器。

scanf(" %d",&op1);

关于c - C : Addition and Printing简介,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16054574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com