2015-02-24 53 views
0

如何擺脫goto聲明在這個程序中,並結束flip:>>>如何擺脫goto並翻轉下面的程序

int main() 
{ 
    int array[10]; 
    int sum = 0; 
    int desending; 
    int mul = 1; 

    float avg = 10; 

    int option; 

    do 
    { 
flip1: 
     cout << "\n\t\t------------------------------------------------" << endl; 
     cout << "\t\tlllllllll   Menue   lllllllllll" << endl; 
     cout << "\t\tlllllllll 1. Array Basics   lllllllllll" << endl; 
     cout << "\t\tlllllllll 2. Searching Array  lllllllllll" << endl; 
     cout << "\t\tlllllllll 3. Sorting Array  lllllllllll" << endl; 
     cout << "\t\tlllllllll 4. Exit     lllllllllll" << endl; 
     cout << "\t\t------------------------------------------------\n" << endl; 
     cout << "Please select an option" << endl; 
     cin >> option; 
     system("cls"); 
flip2: 
     if (option == 1) 
     { 
      int h = 1; 
      int i; 
      for (i = 0; i < 10; i++) 
      { 

       cout << h << " Enter the Numbers "; 

       cin >> array[i]; 

       sum = sum + array[i]; 

       mul = mul*array[i]; 

       h++; 
      } 
      system("cls"); 

      for (int j = 0; j < 10; j++) 
      { 

       cout << "Given numbers" << j << " = " << array[j] << "\n"; 


      } 
      avg = sum/avg; 
      int max = array[0]; 
      int min = array[0]; 


      for (int i = 0; i < 10; i++) 
      { 
       if (array[i] > max) 
       { 
        max = array[i]; 
       } 
       else if (array[i] < min) 
       { 
        min = array[i]; 
       } 

      } 
      cout << "\nSum of the Numbers= 4" << sum << endl; 
      cout << "\nProduct of the Numbers is = " << mul << endl; 
      cout << "\nThe Avg of Numbers is  = " << avg << endl; 
      cout << "\nmaximum of the Numbers is = " << max << endl; 
      cout << "\nminimum of the Numbers is = " << min << endl; 
     } 
     else if (option == 2) 
     { 
      int array[10], o, index = -1; 
      cout << "enter the elements of array" << endl; 
      for (o = 0; o < 10; o++) 
      { 
       cin >> array[o]; 

      } 
      system("cls"); 
      int p; 
      cout << "enter value to find" << endl; 
      cin >> p; 
      for (int c = 0; c < 10; c++) 
      { 
       if (array[c] == p) 

        index = c; 
      } 
      system("cls"); 
      if (index == -1) 
      { 
       cout << "no value found" << endl; 
      } 
      else 
       cout << "Value found at\t" << index << endl; 

     } 
     else if (option == 3) 
     { 
      system("cls"); 
      int a[10], r, t, temp; 
      cout << "Enter the array elements: " << endl; 
      for (r = 0; r<10; ++r) 
       cin >> a[r]; 
      system("cls"); 
      for (r = 0; r<10; ++r) 
      for (t = 0; t<9; t++) 
      if (a[r]<a[t]) 
      { 
       temp = a[r]; 
       a[r] = a[t]; 
       a[t] = temp; 
      } 

      cout << "Array after sorting: " << endl; 
      for (r = 0; r<10; r++) 
       cout << a[r] << "\t "; 

     } 

     cout << "\nplease select an option\n" << endl; 
     cout << "press m for main menue" << endl; 
     cout << "press r for repeat" << endl; 
     cout << "press e for exit" << endl; 
     char q; 
     cin >> q; 
     if (q == 'm') 
     { 
      system("cls"); 
      goto flip1; 
     } 
     if (q == 'r') 
     { 
      system("cls"); 
      goto flip2; 
     } 
     if (q == 'e') 
     { 
      return 0; 
     } 
    } while (option != 4); 


    return 0; 
} 
+1

將你的代碼分成小函數,每個都做一件事 – 2015-02-24 04:44:00

+0

lol,這是一個基本程序的端口嗎? – 2015-02-24 04:49:11

回答

1

您可以參考下面的代碼。

進入循環之前。

cout << "\n\t\t------------------------------------------------" << endl; 
cout << "\t\tlllllllll   Menue   lllllllllll" << endl; 
cout << "\t\tlllllllll 1. Array Basics   lllllllllll" << endl; 
cout << "\t\tlllllllll 2. Searching Array  lllllllllll" << endl; 
cout << "\t\tlllllllll 3. Sorting Array  lllllllllll" << endl; 
cout << "\t\tlllllllll 4. Exit     lllllllllll" << endl; 
cout << "\t\t------------------------------------------------\n" << endl; 
cout << "Please select an option" << endl; 
cin >> option; 
system("cls");  

do { 
    if (option == 1) 
    { 
     int h = 1; 
     int i; 
     for (i = 0; i < 10; i++) 
     { 
      /* All text as same */ 

底:變化

if (q == 'm') 
{ 
    system("cls"); 
    goto flip1; 
} 
if (q == 'r') 
{ 
    system("cls"); 
    goto flip2; 
} 
if (q == 'e') 
{ 
    return 0; 
} 

要。

if (q == 'e') 
{ 
    return 0; 
} 
system("cls"); 
if (q == 'm') 
{ 
    cout << "\n\t\t------------------------------------------------" << endl; 
    cout << "\t\tlllllllll   Menue   lllllllllll" << endl; 
    cout << "\t\tlllllllll 1. Array Basics   lllllllllll" << endl; 
    cout << "\t\tlllllllll 2. Searching Array  lllllllllll" << endl; 
    cout << "\t\tlllllllll 3. Sorting Array  lllllllllll" << endl; 
    cout << "\t\tlllllllll 4. Exit     lllllllllll" << endl; 
    cout << "\t\t------------------------------------------------\n" << endl; 
    cout << "Please select an option" << endl; 
    cin >> option; 
    system("cls"); 
} 

PS:您還可以創建一個打印菜單,從用戶獲取輸入並返回相同的函數。

1

如果將代碼劃分爲多個粘性功能塊,可以將它們放入各自的功能中,並且功能可以簡單得多。這裏是我的建議:

#include <iostream> 
#include <cstdlib> 

using namespace std; 

int getOption1() 
{ 
    int option; 

    cout << "\n\t\t------------------------------------------------" << endl; 
    cout << "\t\tlllllllll   Menue   lllllllllll" << endl; 
    cout << "\t\tlllllllll 1. Array Basics   lllllllllll" << endl; 
    cout << "\t\tlllllllll 2. Searching Array  lllllllllll" << endl; 
    cout << "\t\tlllllllll 3. Sorting Array  lllllllllll" << endl; 
    cout << "\t\tlllllllll 4. Exit     lllllllllll" << endl; 
    cout << "\t\t------------------------------------------------\n" << endl; 
    cout << "Please select an option" << endl; 

    cin >> option; 
    system("cls"); 

    return option; 
} 

char getOption2() 
{ 
    char option; 

    cout << "\nplease select an option\n" << endl; 
    cout << "press m for main menue" << endl; 
    cout << "press r for repeat" << endl; 
    cout << "press e for exit" << endl; 
    cin >> option; 
    return option; 
} 

void doArrayBasics() 
{ 
    int array[10]; 
    int sum = 0; 
    float avg = 10; 
    int mul = 1; 

    int h = 1; 
    int i; 
    for (i = 0; i < 10; i++) 
    { 

     cout << h << " Enter the Numbers "; 

     cin >> array[i]; 

     sum = sum + array[i]; 

     mul = mul*array[i]; 

     h++; 
    } 
    system("cls"); 

    for (int j = 0; j < 10; j++) 
    { 

     cout << "Given numbers" << j << " = " << array[j] << "\n"; 


    } 
    avg = sum/avg; 
    int max = array[0]; 
    int min = array[0]; 


    for (int i = 0; i < 10; i++) 
    { 
     if (array[i] > max) 
     { 
     max = array[i]; 
     } 
     else if (array[i] < min) 
     { 
     min = array[i]; 
     } 

    } 
    cout << "\nSum of the Numbers= 4" << sum << endl; 
    cout << "\nProduct of the Numbers is = " << mul << endl; 
    cout << "\nThe Avg of Numbers is  = " << avg << endl; 
    cout << "\nmaximum of the Numbers is = " << max << endl; 
    cout << "\nminimum of the Numbers is = " << min << endl; 
} 

void doArraySearching() 
{ 
    int array[10]; 
    int o; 
    int index = -1; 

    cout << "enter the elements of array" << endl; 
    for (o = 0; o < 10; o++) 
    { 
     cin >> array[o]; 

    } 
    system("cls"); 
    int p; 
    cout << "enter value to find" << endl; 
    cin >> p; 
    for (int c = 0; c < 10; c++) 
    { 
     if (array[c] == p) 
     index = c; 
    } 
    system("cls"); 
    if (index == -1) 
    { 
     cout << "no value found" << endl; 
    } 
    else 
     cout << "Value found at\t" << index << endl; 

} 

void doArraySorting() 
{ 
    system("cls"); 
    int a[10]; 
    int r; 
    int t; 
    int temp; 

    cout << "Enter the array elements: " << endl; 
    for (r = 0; r<10; ++r) 
     cin >> a[r]; 
    system("cls"); 
    for (r = 0; r<10; ++r) 
     for (t = 0; t<9; t++) 
     if (a[r]<a[t]) 
     { 
      temp = a[r]; 
      a[r] = a[t]; 
      a[t] = temp; 
     } 

    cout << "Array after sorting: " << endl; 
    for (r = 0; r<10; r++) 
     cout << a[r] << "\t "; 

} 

int main() 
{ 
    int option1; 
    char option2 = 'r'; 

    do 
    { 
     if (option2 == 'r') 
     { 
     option1 = getOption1(); 
     } 

     switch (option1) 
     { 
     case 1: 
      doArrayBasics(); 
      break; 

     case 2: 
      doArraySearching(); 
      break; 

     case 3: 
      doArraySorting(); 
      break; 

     case 4: 
      break; 

     default: 
      cout << "Invalid option " << option1 << endl; 
     } 

     if (option1 != 4) 
     { 
     option2 = getOption2(); 
     } 

    } while (option1 != 4 && option2 != 'e'); 

    return 0; 
} 
1

您可以使用內循環,以清除所有的goto。

刪除flip1:,將flip2:更改爲do {。循環應該開始那樣:

do 
{ 
    cout << "\n\t\t------------------------------------------------" << endl; 
    cout << "\t\tlllllllll   Menue   lllllllllll" << endl; 
    cout << "\t\tlllllllll 1. Array Basics   lllllllllll" << endl; 
    cout << "\t\tlllllllll 2. Searching Array  lllllllllll" << endl; 
    cout << "\t\tlllllllll 3. Sorting Array  lllllllllll" << endl; 
    cout << "\t\tlllllllll 4. Exit     lllllllllll" << endl; 
    cout << "\t\t------------------------------------------------\n" << endl; 
    cout << "Please select an option" << endl; 
    cin >> option; 
    system("cls"); 
    do 
    { 
     if (option == 1) 
     { 

在循環,改變的末尾:

if (q == 'm') 
{ 
    system("cls"); 
    goto flip1; 
} 
if (q == 'r') 
{ 
    system("cls"); 
    goto flip2; 
} 
if (q == 'e') 
{ 
    return 0; 
} 

到:

if (q == 'e') 
    { 
     return 0; 
    } 
    system("cls"); 
} while (q == 'r'); 

打破你的代碼的功能也將使其更具可讀性但不是強制刪除gotos。

0
caveat: the following code not tested 

int main() 
{ 
    int array[10]; 
    int sum = 0; 
    int desending; 
    int mul = 1; 

    float avg = 10; 

    int option; 
    int done = 0; 
    int looping = 1; 

    while(!done) 
    { 

     cout << "\n\t\t------------------------------------------------" << endl; 
     cout << "\t\tlllllllll   Menue   lllllllllll" << endl; 
     cout << "\t\tlllllllll 1. Array Basics   lllllllllll" << endl; 
     cout << "\t\tlllllllll 2. Searching Array  lllllllllll" << endl; 
     cout << "\t\tlllllllll 3. Sorting Array  lllllllllll" << endl; 
     cout << "\t\tlllllllll 4. Exit     lllllllllll" << endl; 
     cout << "\t\t------------------------------------------------\n" << endl; 
     cout << "Please select an option" << endl; 
     cin >> option; 
     system("cls"); 

     while(looping) 
     { 
      looping = 0; 

      switch(option) 
      { 
       case 1: 
        int h = 1; 
        int i; 
        for (i = 0; i < 10; i++) 
        { 

         cout << h << " Enter the Numbers "; 

         cin >> array[i]; 

         sum = sum + array[i]; 

         mul = mul*array[i]; 

         h++; 
        } 
        system("cls"); 

        for (int j = 0; j < 10; j++) 
        { 

         cout << "Given numbers" << j << " = " << array[j] << "\n"; 


        } 
        avg = sum/avg; 
        int max = array[0]; 
        int min = array[0]; 


        for (int i = 0; i < 10; i++) 
        { 
         if (array[i] > max) 
         { 
          max = array[i]; 
         } 
         else if (array[i] < min) 
         { 
          min = array[i]; 
         } 

        } 
        cout << "\nSum of the Numbers= 4" << sum << endl; 
        cout << "\nProduct of the Numbers is = " << mul << endl; 
        cout << "\nThe Avg of Numbers is  = " << avg << endl; 
        cout << "\nmaximum of the Numbers is = " << max << endl; 
        cout << "\nminimum of the Numbers is = " << min << endl; 
        break; 

       case 2:   
        int array[10], o, index = -1; 
        cout << "enter the elements of array" << endl; 
        for (o = 0; o < 10; o++) 
        { 
         cin >> array[o]; 

        } 

        system("cls"); 
        int p; 
        cout << "enter value to find" << endl; 
        cin >> p; 

        for (int c = 0; c < 10; c++) 
        { 
         if (array[c] == p) 

          index = c; 
        } 

        system("cls"); 
        if (index == -1) 
        { 
         cout << "no value found" << endl; 
        } 

        else 
         cout << "Value found at\t" << index << endl; 
        break; 

       case 3: 
        system("cls"); 
        int a[10], r, t, temp; 
        cout << "Enter the array elements: " << endl; 

        for (r = 0; r<10; ++r) 
         cin >> a[r]; 

        system("cls"); 

        for (r = 0; r<10; ++r) 
        { 
         for (t = 0; t<9; t++) 
         { 
          if (a[r]<a[t]) 
          { 
           temp = a[r]; 
           a[r] = a[t]; 
           a[t] = temp; 
          } 
         } 
        } 

        cout << "Array after sorting: " << endl; 

        for (r = 0; r<10; r++) 
         cout << a[r] << "\t "; 
        break; 

       case 4: 
        done = 1; 
        break; 

       default:   
        cout << "\nplease select an option\n" << endl; 
        cout << "press m for main menue" << endl; 
        cout << "press r for repeat" << endl; 
        cout << "press e for exit" << endl; 
        char q; 

        cin >> q; 

        switch(q) 
        { 
         case 'm': 
          system("cls"); 
          // looping already set = 0 
          break; 

         case 'r': 
          system("cls"); 
          looping = 1; 
          break; 

         case 'e': 
          done = 1; 
          break; 

         default: 
          break; 
        } // end switch 

        break; 
      } // end switch 
     } // end while looping 
    } // end while not done 



    return 0; 
} // end function: main