回忆炭功能

发布于 2025-01-31 05:41:09 字数 620 浏览 1 评论 0原文

我是编程的新手,并且在C中进行了一个简单的机器人,该机器人具有一个计算器功能。我在此代码中遇到了一些麻烦:

char operation = get_char("Insert the mathematical operation: ");
float x = get_float("X: ");
float y = get_float("Y: ");
if (operation == 43)
{
    float result = (x + y);
    int rresult = round(result);
    printf("X + Y = %i\n", rresult);
    string confirmation = get_string("Need to do something more? ");
    if (strcmp(confirmation, "Y") == 0)
    {
        return operation;
    } 

如您所见,该计算器向用户询问char(*, /, +或 - [它在代码的其他部分中定义的所有内容,我不会发布它在这里简要介绍]定义数学操作,然后在进行计算并打印结果后,询问用户是否要进行更多计算。一件代码,要求炭和浮子,并做所有事情。 另外,我正在使用CS50 IDE。

I'm very new to programming, and I'm doing a simple bot in C, that has a calculator function. I'm having some trouble in this piece of code:

char operation = get_char("Insert the mathematical operation: ");
float x = get_float("X: ");
float y = get_float("Y: ");
if (operation == 43)
{
    float result = (x + y);
    int rresult = round(result);
    printf("X + Y = %i\n", rresult);
    string confirmation = get_string("Need to do something more? ");
    if (strcmp(confirmation, "Y") == 0)
    {
        return operation;
    } 

As you can see, this calculator ask the user for a char (*, /, + or - [its everything defined in the other parts of the code, I will not post it here just to be brief] that defines the math operation and then, after doing the calculation and printing the result, asks the user if he wants to do more calculations. If the answer is "Y" (yes), I want to restart this piece of code, asking for the char and the floats, and doing everything. I want to know the simplest way to do this, without making the code looks bad designed.
Also, I'm using CS50 IDE.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2025-02-07 05:41:09

我没有CS50,因此,C。

#include <stdio.h>
#include <math.h>

char get_char(char * msg) {char c; printf("%s", msg); scanf(" %c", &c); return c;}
float get_float(char * msg) {float f; printf("%s", msg); scanf(" %f", &f); return f;}

int main() {
    char confirmation = 'n';
    do {
        char operation = get_char("Insert the mathematical operation: ");
        float x = get_float("X: ");
        float y = get_float("Y: ");
        if (operation == '+') {
            float result = (x + y);
            printf("Result in float: %f\n", result);
            int rresult = round(result);
            printf("X + Y = %i\n", rresult);
        }
        confirmation = get_char("Need to do something more [y/n]? ");
    } while (confirmation == 'y');
    return 0;
}
$ gcc -Wall dowhile.c
$ ./a.out            
Insert the mathematical operation: +
X: 0.11
Y: 0.88
Result in float: 0.990000
X + Y = 1
Need to do something more [y/n]? y
Insert the mathematical operation: +
X: 0.1
Y: 0.1
Result in float: 0.200000
X + Y = 0
Need to do something more [y/n]? n
$ 

I don't have CS50, therefore, plain C. The construct you might want to use is a do-while loop.

#include <stdio.h>
#include <math.h>

char get_char(char * msg) {char c; printf("%s", msg); scanf(" %c", &c); return c;}
float get_float(char * msg) {float f; printf("%s", msg); scanf(" %f", &f); return f;}

int main() {
    char confirmation = 'n';
    do {
        char operation = get_char("Insert the mathematical operation: ");
        float x = get_float("X: ");
        float y = get_float("Y: ");
        if (operation == '+') {
            float result = (x + y);
            printf("Result in float: %f\n", result);
            int rresult = round(result);
            printf("X + Y = %i\n", rresult);
        }
        confirmation = get_char("Need to do something more [y/n]? ");
    } while (confirmation == 'y');
    return 0;
}
$ gcc -Wall dowhile.c
$ ./a.out            
Insert the mathematical operation: +
X: 0.11
Y: 0.88
Result in float: 0.990000
X + Y = 1
Need to do something more [y/n]? y
Insert the mathematical operation: +
X: 0.1
Y: 0.1
Result in float: 0.200000
X + Y = 0
Need to do something more [y/n]? n
$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文