0

立派な紳士がgoto文は悪いと言っていましたが、ここでそれを使用できない方法がわかりません。

int main()
{   
   using namespace std;
   int x;
   int y;
   int z;
   int a;
   int b;
   Calc: //How can i get back here, without using goto?
   {
   cout << "To begin, type a number" << endl;
   cin >> x;
   cout << "Excellent!" << endl;
   cout << "Now you need to type the second number" << endl;
   cin >> y;
   cout << "Excellent!" << endl;
   cout << "Now, what do you want to do with these numbers?" << endl;
   cout << "Alt. 1 +" << endl;
   cout << "Alt. 2 -" << endl;
   cout << "Alt. 3 *" << endl;
   cout << "Alt. 4 /" << endl;
   cin >> a;

       if (a == 1) {
    z = add(x, y);
   }

   if (a == 2) {
    z = sub(x, y);
   }

   if (a == 3) {
    z = mul(x, y);
   }

       if (a == 4) {
    z = dis(x, y);
   }
}

cout << "The answer to your math question is ";
cout << z << endl;
cout << "Do you want to enter another question?" << endl;
cout << "Type 1 for yes" << endl;
cout << "Type 0 for no" << endl;
cin >> b;

    if (b == 1) {
    goto Calc;
}
cout << "Happy trails!" << endl;
return 0;
}

ご覧のとおり、電卓です。また、必要に応じて、ユーザーが操作(+-* /)を選択できるようにするためのより良い方法(存在する場合)を提案できますか?ヘッダーファイルは管理下にあります。たくさんのご意見をお詫び申し上げますcout

4

7 に答える 7

8

do構造体に/ whileloopを使用して、クリーンアップされ、適切にフォーマットされたバージョンを次に示します。

using namespace std;

int main()
{   
    int x, y, z, a, b;

    do {
        cout << "To begin, type a number" << endl;
        cin >> x;
        cout << "Excellent!" << endl;
        cout << "Now you need to type the second number" << endl;
        cin >> y;
        cout << "Excellent!" << endl;
        cout << "Now, what do you want to do with these numbers?" << endl;
        cout << "Alt. 1 +" << endl;
        cout << "Alt. 2 -" << endl;
        cout << "Alt. 3 *" << endl;
        cout << "Alt. 4 /" << endl;
        cin >> a;
        if (a == 1) {
            z = add(x, y);
        }
        else if (a == 2) {
            z = sub(x, y);
        }
        else if (a == 3) {
            z = mul(x, y);
        }
        else if (a == 4) {
            z = dis(x, y);
        }
        cout << "The answer to your math question is ";
        cout << z << endl;
        cout << "Do you want to enter another question?" << endl;
        cout << "Type 1 for yes" << endl;
        cout << "Type 0 for no" << endl;
        cin >> b;
    } while (b != 0);
    cout << "Happy trails!" << endl;
    return 0;
}
于 2013-03-01T15:22:48.037 に答える
4

えーと、適切なループ構造を使用するwhileなどfor

于 2013-03-01T15:20:29.273 に答える
1

この場合の「より一般的に受け入れられている」アプローチはdo {...です} while(b==1);が、コンパイルされた結果はおそらく同じです。

于 2013-03-01T15:23:15.247 に答える
0

コードで「goto」を簡単に回避できます。それを関数に分割するだけです:

using namespace std;

void question () {
  cout << "To begin, type a number" << endl;
  cin >> x;

  // put rest of the code here
}

int main () {
  int ask = 1;
  while ( ask == 1 ) {
    question();
    cout << "Do you want to enter another question?" << endl;
    cout << "Type 1 for yes" << endl;
    cout << "Type 0 for no" << endl;
    cin >> ask;
  }

  return 0;
}

編集:コメントに記載されているように、do-whileを使用する方が実際にはより良いオプションです。

于 2013-03-01T15:22:51.020 に答える
0
  • goto実行がどこから来てどこに行くのかを追跡するのが難しくなります。

  • gotospagetti-codeを使用する場所を厳しく制限しない限り、spagetti-codeを推奨します(たとえば、クリーンアップブロックにのみ使用すると主張できますが、RAIIが存在する場合はそのような議論は意味がありません)。

  • gotoを使用してループをシミュレートしています。代わりにループを作成しないのはなぜですか?

  • それはあいまいであり、したがって、他の人があなたのコードを利用しにくくします。

  • gotoオブジェクトの存続期間を追跡することがより困難になります。

于 2013-03-01T15:27:38.587 に答える
0

実際の質問に対する簡単な答え:いいえ、gotoこのコードでは使用しないでください。その必要はありません。

の使用は、goto「コードがより明確またはより安全になる場合」である必要があります。「コードをより明確にする」の典型的な例は、ネストされたループの複数のレイヤーがあり、特定の状況ではすべてのネストレベルを残す必要があり、「ループを終了しますか」を追加するとコードがより複雑になる場合です。「より安全にする」の例は、関数がロックを保持し、ファイルなどを開き、早期に戻る必要がある場合です。ただし、「goto exit_now;」を使用して、ファイルを閉じるか、ロックを解除する必要もあります。どのロックやファイルなどが保持されているかを覚えてから実行するよりも安全ですreturn;

これ:

if (a == 1) {
    z = add(x, y);
}
if (a == 2) {
    z = sub(x, y);
}
if (a == 3) {
    z = mul(x, y);
}
if (a == 4) {
    z = dis(x, y);
}

'switch'を使用する必要がある典型的なケースです:

switch(a)
{
   case 1:
     z = add(x, y);
     break;
   case 2:
     z = sub(x, y);
     break;
  ....
}

コードをより明確にします-a値を変更するかどうか、そしておそらく別のifステートメントが実行可能になるかどうかについての混乱もありません。

于 2013-03-01T15:31:34.963 に答える
0

goto自動的に悪いわけではありません。読めないコードは悪いです。'goto'のようなあいまいなプログラミング構造が必要な場合は、通常、コードの記述が不十分であるか、プログラムの設計に欠陥があることを意味します。

解決策は、ほとんどの場合、より多くの関数です。例えば:

bool run_program();
int  prompt_user_begin();
int  prompt_user_again();
int  prompt_operation_type();
bool prompt_continue();


int main()
{
  while(run_program())
  {}

  cout << "Happy trails!" << endl;
  return 0; 
}


bool run_program()
{
  int first;
  int second;
  int operation_type;
  int result;

  first  = prompt_user_begin();
  cout << "Excellent!" << endl;

  second = prompt_user_again();
  cout << "Excellent!" << endl;

  operation_type = prompt_operation_type();

  switch(operation_type)
  {
    case 1: result = add(first, second); break;
    case 2: result = sub(first, second); break;
    case 3: result = mul(first, second); break;
    case 4: result = div(first, second); break;
  }

  cout << "The answer to your math question is ";
  cout << result << endl;

  return prompt_continue();
}

int prompt_user_begin ()
{
  int x;
  cout << "To begin, type a number" << endl;
  cin >> x;

  return x;
}

int prompt_user_again ()
{
  int x;
  cout << "Now you need to type the second number" << endl;
  cin >> x;
  return x;
}

int prompt_operation_type ()
{
  int x;
  cout << "Now, what do you want to do with these numbers?" << endl;
  cout << "Alt. 1 +" << endl;
  cout << "Alt. 2 -" << endl;
  cout << "Alt. 3 *" << endl;
  cout << "Alt. 4 /" << endl;
  cin >> x;
  return x;
}

bool prompt_continue ()
{
  int x;
  cout << "Do you want to enter another question?" << endl;
  cout << "Type 1 for yes" << endl;
  cout << "Type 0 for no" << endl;
  cin >> x;
  return x==1;
}
于 2013-03-01T15:44:02.843 に答える