gpt4 book ai didi

c - 在用户不退出的情况下,在 C 中查找两个数字的总和、差等

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

我需要找到用户输入的两个数字的总和、差异等。但是程序首先显示菜单,然后要求用户输入这些数字并进行操作。之后,它应该询问用户是否要再次计算一些东西,以及他们是否想要使用旧数字还是新数字。

我完全是 C 语言的初学者,所以我需要一些帮助。我在案例中遇到错误:

cfinal.c:49:14: warning: multi-character character constant [-Wmultichar]

还有这个:

cfinal.c:49:14: warning: overflow converting case value to switch condition type (21075 to 83) [-Wswitch]

有问题,我想不通。第二个问题是,我需要询问用户是否想再次重复,如果他们选择是,他们是想用新数字还是以前计算中使用的数字来做。那是我什至没有开始的事情,我不知道如何开始。谢谢。

我到目前为止的代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main (void)
{
char choice; //Define choice character type
int first_number, second_number, firstSquare, secondSquare, firstQube, secondQube, cont; //define intigers
printf("Select operation you would like to do by typing shortcut:\n");
printf("SU - sum\n");
printf("DI - difference\n");
printf("MU - multiplication\n");
printf("RA - ratio\n");
printf("SS - Sum of Squares\n");
printf("SQ - Sum of Qubes\n");
printf("RS - Sum of Squares\n");
printf("RQ - Sum of Qubes\n");
choice = (char)toupper(getchar());
getchar ();
printf("Enter the first number: ");
scanf("%d", &first_number);
getchar ();
printf("Enter the second number:");
scanf("%d", &second_number);
getchar ();
firstSquare = first_number * first_number;
secondSquare = second_number * second_number;
firstQube = first_number * first_number * first_number;
secondQube = second_number * second_number * second_number;
printf("\n");
printf("Result is");
switch(choice)
{
case 'SU':
printf("%d + %d = %d", first_number, second_number, first_number + second_number);
break;
case 'DI':
printf("%d - %d = %d", first_number, second_number, first_number - second_number);
break;
case 'MU':
printf("%d * %d = %d", first_number, second_number, first_number * second_number);
break;
case 'SS':
printf("%d * %d + %d * %d = %d", first_number, first_number, second_number, second_number);
break;
case 'SQ':
printf("%d * %d * %d + %d * %d * %d = %d", first_number, first_number, first_number, second_number, second_number, second_number, firstQube + secondQube);
break;
case 'RS':
printf("(%d * %d) / (%d * %d) = %d", first_number, first_number, second_number, second_number, firstSquare / secondSquare);
break;
case 'RQ':
printf("(%d * %d * %d) / (%d * %d * %d) = %f", first_number, first_number, first_number, second_number, second_number, second_number, firstQube / secondQube);
break;
case 'RA':
if (second_number == 0)
fprintf(stderr, "The divisor can't be 0");
else
{
printf("%d / %d = %f", first_number, second_number, (double)first_number / second_number);
}
break;
default :
fprintf(stderr, "Invalid entry");
break;
}
printf("\n");
for (;;)
{
printf("Continue (Type Y for yes or N for no)? ");
cont = toupper(getchar());
getchar ();
if (cont == 'Y')
return main(); // the difference.
else if (cont == 'N')
return EXIT_SUCCESS;
}
}

最佳答案

getchar 返回单个字符。如果您想使用字符串“xy”、“d99”等,则必须读取一个字符串。 How to read a line from the console in C?

其次你不能打开你需要做的字符串

if(strcmp(input,"SU")==0)
{
....
}
else if (strcmp(input, "DQ") == 0)
{
}
...
else
{
... error

关于c - 在用户不退出的情况下,在 C 中查找两个数字的总和、差等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47316456/

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