【问题标题】:is there a way to overwrite the variable in the code below有没有办法覆盖下面代码中的变量
【发布时间】:2019-02-27 21:41:57
【问题描述】:
#include <stdio.h>
#include <string>

int main(){
    float a,b,z,u,r,k;
    char f,q,m;
    system("color B");
repeat:
    printf("Unesite broj a:  ");
    scanf("%f",&a);
repeatm:
    printf("Unesite broj b:  ");
    scanf("%f",&b);
    printf("Unesite funkciju (+,-,*,/):  ");
finput:
    scanf("%s",&f);
    if(f=='+'){
        goto zbroj;  
       }else{
        if(f=='-'){
            goto razlika;
        }else{
            if(f=='*'){
                goto umnozak;
            }else{
                if(f=='/'){
                    goto kolicnik;
                }else{
                    goto _newf;
                }
            }
        }
    }
//input of numbers and a check if the value of 'f' is one of the characters +,-,* or /
zbroj:
    z=a+b;
    printf("-------------------------------------\n");
    printf("Zbroj ta dva broja je: ");
    printf("%f \n",z);
    goto _new;
razlika:
    r=a-b;
    printf("-------------------------------------\n");
    printf("Razlika ta dva broja je: ");
    printf("%f \n",r);
    goto _new;
umnozak:
    u=a*b;
    printf("-------------------------------------\n");
    printf("Umnozak ta dva broja je: ");
    printf("%f \n",u);
    goto _new;
kolicnik:
   if(b==0){
        system("cls");
        printf("Ne mozete dijeliti s nulom \n");
        goto repeatB;
    }else{
 k=a/b;
    printf("-------------------------------------\n");
    printf("Kolicnik ta dva broja je: ");
    printf("%f \n",k);
    goto _new;
    }
//prints results
_newf:
    printf("Nevazeca funkcija, upisite ponovo:  ");
    goto finput;
//loop if 'f' isnt one of +,-,*,/
_new:
    printf("-------------------------------------\n");
    printf("Novi racun?(y/n)  ");
    scanf("%s",&q);
    system("cls");
    goto repeat1;
//asks if i want to exit or go back to start
repeat1:
    if(q=='y'){
        system("cls");
        goto memory;
    }else{
        if(q=='n'){
            goto exit;
        }else{
            printf("Novi racun? \n");
            printf("Molimo upisite y ili n:  ");
            scanf("%s", &q);
            goto repeat1;
        }
    }
//loops back to start
memory:
    printf("Zelite li iskoristits peredhodni rezultat kao broj a?(y/n):  ");
    scanf("%s", &m);
//asks if i want the previous result as the number 'a'
repeat2:
    if(m=='y'){
        system("cls");
        goto printfa;
    }else{
        if(m=='n'){
            system("cls");
            goto repeat;
        }else{
            printf("Zelite li iskoristits peredhodni rezultat kao broj a? \n");
            printf("Molimo upisite y ili n:  ");
            scanf("%s", &m);
            goto repeat2;
        }
    }
//checks if variable 'm' is either y or n
printfa:
    if(f=='+'){
        a=z;     
    }else{
        if(f=='-'){
            a=r;
        }else{
            if(f=='*'){
                a=u;
            }else{
                if(f=='/'){
                    a=k;       
                }
            }
        }
    }
//this should(?) overwrite the variable 'a'
//why isnt 'a' overwritten with the value of 'z', 'r', 'u' or 'k' here?
    printf("Broj a je:  %f \n", a); 
//here the value of 'a' stays the same as it was before
    goto repeatm;
repeatB:
    printf("Unesite broj b:  ");
    scanf("%f",&b);
    goto kolicnik;
exit:
    system("cls");
    system("pause");
    return(0);
}

有没有办法用“z”、“r”、“u”或“k”的值之一覆盖变量“a”(无需手动输入)。此代码的结果只是 a + (或 -,*,/) new b (我再次手动输入,但变量 a 与第一个输入保持相同)。请不要介意它只是说“输入a”,“输入b”,“结果是..”的外语,如果代码混乱也很抱歉..

【问题讨论】:

  • A) 这不是 C++ 代码。它可能在 C++ 编译器中编译,但这不是 C++ 代码应有的样子。 B) 请忘记goto 的存在。请改用循环和函数。一旦你开始进入真正的高级主题,你可以重新访问goto
  • 另外请考虑 elseif 而不是越来越多地嵌套您的子句。我认为您可能很难找到鼓励您编写包含 goto 的代码的人。
  • Spaghetti code。你不仅很难找到鼓励这种编码的人,而且如果你的程序是这样编写的,你将很难找到愿意提供帮助的人。没有人会尝试解开意大利面——相反,重新设计您的代码以使用适当的结构化编程结构(forwhiledo-while,将代码分离为函数等)。看来您以特别的方式编写程序(我需要执行“x”,因此创建一个名为“x”的代码块并去那里)。不要这样编程。
  • 如果有人给了你那个代码,那他们就是一个希望你失败的邪恶。如果你写了那段代码,我强烈推荐upgrading your C++ reference library
  • @TimPavic 但这是我设法让这件事发挥作用的最简单方法。 -- 这相当于说“我把地毯下的所有东西都扫干净了” .房间可能看起来很干净,但事实并非如此。你很幸运有人拿走了你的代码,花时间解开它,因此代码的流程更容易理解。同样,不要以我提到的方式编写程序,即如果您需要执行一项任务,请为该任务创建一个goto。那么当您需要退出该任务时会发生什么?创建另一个goto 去其他地方。然后一切都变得一团糟。

标签: stdio


【解决方案1】:

我只能间接回答这个问题,因为直到我稍微清理了代码之后才开始研究实际问题 - 但现在我没有得到错误。所以,答案是:清理代码,它可能会按预期工作。

我一直将结果保存在不同的变量中,尽管程序中不需要像现在这样拥有多个 result 变量。程序的主要流程应该和你原来的一样,但现在更容易阅读了:

#include <iostream>
#include <string>

// helper functions
bool yes_or_no(const std::string& question) {
    char answer;
    do {
        std::cout << question << " (y/n)  ";
        std::cin >> answer;
    } while(answer != 'y' && answer != 'n');
    return answer == 'y';
}

void line() { std::cout << "-------------------------------------\n"; }

void display_result(const std::string& heading, float value) {
    std::cout << heading << " ta dva broja je: " << value << "\n";
}

int main() {
    float op_a, op_b, add_res = 0, mul_res = 0, sub_res = 0, div_res = 0;
    char oper;
    bool save_op_a = false;
    system("color B");

    while(true) {
        system("cls");
        if(save_op_a == false) {
            std::cout << "Unesite broj a:  ";
            std::cin >> op_a;
        } else
            save_op_a = false;

        std::cout << "Unesite broj b:  ";
        std::cin >> op_b;

        while(true) {
            std::cout << "Unesite funkciju (+,-,*,/):  ";
            std::cin >> oper;
            line();
            if(oper == '+') {
                add_res = op_a + op_b;
                display_result("Zbroj", add_res);
                break;
            } else if(oper == '-') {
                sub_res = op_a - op_b;
                display_result("Razlika", sub_res);
                break;
            } else if(oper == '*') {
                mul_res = op_a * op_b;
                display_result("Umnozak", mul_res);
                break;
            } else if(oper == '/') {
                if(op_b == 0.) {
                    std::cout << "Ne mozete dijeliti s nulom\n";
                    continue;
                }
                div_res = op_a / op_b;
                display_result("Kolicnik", div_res);
                break;
            }
        }

        line();

        if(yes_or_no("Novi racun?")) {
            system("cls");
            // asks if i want the previous result as the number 'op_a'
            if(yes_or_no("Zelite li iskoristits peredhodni rezultat kao broj a?")) {
                // this should(?) overwrite the variable 'op_a'
                // why isnt 'op_a' overwritten with the value here?
                system("cls");
                if(oper == '+') {
                    op_a = add_res;
                } else if(oper == '-') {
                    op_a = sub_res;
                } else if(oper == '*') {
                    op_a = mul_res;
                } else if(oper == '/') {
                    op_a = div_res;
                }
                // here the value of 'op_a' stays the same as it was before
                std::cout << "Broj a je:  " << op_a << "\n";
                save_op_a = true;
            }
        } else
            break; // exit
    }

    system("cls");
    system("pause");
    return (0);
}

【讨论】:

  • holy.. 我不希望有人将我的整个代码翻译成 iostream。谢谢,非常喜欢。我会在剩下的晚上尝试学习它是如何工作的(直到现在我才对 stdio.h 有经验)。谢谢。
  • @TimPavic 您应该赞扬 OP 将您的代码从 goto 混乱中解开的事实。这比简单地使用或不使用iostream 更有价值。
  • Tim,不客气,@PaulMcKenzie 是正确的,使用 iostreams 只是一个不费吹灰之力的小改动。 goto 丛林要花更多时间才能通过,而且 bug 可能就在某个地方消失了。大概……
猜你喜欢
  • 2010-10-15
  • 2013-06-24
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
相关资源
最近更新 更多