2015-12-14 92 views
-4

我嘗試開發C++程序,快速排序。在int main(),第一菜單:快速排序C++,代碼實現int main()

1. 100 elements 
2. 500 elements 
3. 1000 elements. 
4. Quit 

用戶後選擇或輸入1/2/3,那麼用戶需要選擇使用:

3. Pivot: First Element 
4. Pivot: Random element. 

的問題是我的代碼是因爲我太久沒有讓它變得簡單,我不知道該怎麼做。我使用相同的方法。

對於排序的100個元素:

負載數據>>打印未排序>> >>快速排序打印排序。這是用於支點 的第一個元素。

然後,對於pivot pivot元素,我需要再次加載相同的100 元素數據,並再次做同樣的事情,加載數據>> print unsorted >> quicksort >> print sorted。

它也是500和1000個元素。看到我這樣的代碼非常累。

int main(){ 

int ch,ch2; 
while(1) 
{ 
    cout<<endl<<endl; 
    cout << "\n QUICK SORT(RANDOM NUMBER)\n";  
    cout << " ---------------------------------\n"; 
    cout << "\n 1.100 elements\n 2.500 elements\n 3.1000 elements\n 4.Quit\n"; 
    cout<< " ---------------------------------"<<endl; 
    cout<<" Enter your choice : "; 
    cin>>ch; 
    cout<<endl; 
    switch(ch) 
    { 

     case 1 : cout << "\n 3.Pivot : First Element \n 4.Pivot: Random element\n"; 
       cout<< " ---------------------------------"<<endl; 
       cout<<" Enter your choice : "; 
       cin>>ch2; 
       cout<<endl; 
       if(ch2==3){  
            ifstream file; 
            file.open("100.txt"); 
            if(!file) { 
             cout<<" Error opening file. " << endl; 
            } 
            int input1[100]; 

             for(int i = 0; i < 100; i++) 
            { file>>input1[i]; 
            }        
             file.close(); 

              cout << " UNSORTED DATA (100 elements) \n"; 
              cout<< " ------------------------------------------------"<<endl; 
              print(input1,99);   


              //Quick sort Pivot: first element         
              cout << " SORTED DATA (100 elements) Pivot: First Element: \n"; 
              cout<< " ------------------------------------------------"<<endl; 
              quicksort1(input1, 0, 99);      
              cout<<endl; 
              print(input1,99); 
              cout << " Number of comparison: \n"; 
              cout << " Number of moves required: \n"; 
              cout<<endl; 
              break; 
              } 
     else{  
       ifstream file2; 
       file2.open("100.txt"); 
       if(!file2) { 
        cout<<" Error opening file. " << endl; 
       } 
       int input2[100]; 

        for(int i = 0; i < 100; i++) 
       { file2>>input2[i]; 
       }        
        file2.close(); 
        cout << " UNSORTED DATA (100 elements) \n"; 
              cout<< " ------------------------------------------------"<<endl; 
              print(input2,99);   

       //Quick sort Pivot: random element      
       cout << " SORTED DATA (100 elements) Pivot: Random Element: \n"; 
       cout << " ------------------------------------------------"<<endl; 
       print(input2,99); 
       quicksort2(input2, 0, 99); 
       cout<<endl; 
       print(input2,99); 
       cout << " Number of comparison: \n"; 
       cout << " Number of moves required: \n"; 

       cin.get(); 
       //if windows suddenly close 
       break;   
       }        

        break; 
     case 2 :cout << "\n 3.Pivot : First Element \n 4.Pivot: Random element\n"; 
       cout<< " ---------------------------------"<<endl; 
       cout<<" Enter your choice : "; 
       cin>>ch2; 
       cout<<endl; 
       if(ch2==3){  
            ifstream file; 
            file.open("500.txt"); 
            if(!file) { 
             cout<<" Error opening file. " << endl; 
            } 
            int input1[500]; 

             for(int i = 0; i < 500; i++) 
            { file>>input1[i]; 
            }        
             file.close(); 

              cout << " UNSORTED DATA (500 elements) \n"; 
              cout<< " ------------------------------------------------"<<endl; 
              print(input1,499);   


              //Quick sort Pivot: first element         
              cout << " SORTED DATA (500 elements) Pivot: First Element: \n"; 
              cout<< " ------------------------------------------------"<<endl; 
              quicksort1(input1, 0, 499);      
              cout<<endl; 
              print(input1,499); 
              cout << " Number of comparison: \n"; 
              cout << " Number of moves required: \n"; 
              cout<<endl; 
              break; 
              } 
     else{  
       ifstream file2; 
       file2.open("500.txt"); 
       if(!file2) { 
        cout<<" Error opening file. " << endl; 
       } 
       int input2[500]; 

        for(int i = 0; i < 500; i++) 
       { file2>>input2[i]; 
       }        
        file2.close(); 
        cout << " UNSORTED DATA (500 elements) \n"; 
              cout<< " ------------------------------------------------"<<endl; 
              print(input2,499);   

       //Quick sort Pivot: random element      
       cout << " SORTED DATA (100 elements) Pivot: Random Element: \n"; 
       cout << " ------------------------------------------------"<<endl; 
       print(input2,499); 
       quicksort2(input2, 0, 499); 
       cout<<endl; 
       print(input2,499); 
       cout << " Number of comparison: \n"; 
       cout << " Number of moves required: \n"; 

       cin.get(); 
       //if windows suddenly close 
       break;   
       }        

        break; 
     case 3 : 
       break; 
     case 4 : 
       break;    
    } 
+0

嘗試使用函數和一些變量,使其更清潔,生病舉個例子,等一下。 – ivan

回答

1

我會建議你採取先以乾重的方式在2個變量:

1號輸入

2-支點的選擇

cout<<endl<<endl; 
    cout << "\n QUICK SORT(RANDOM NUMBER)\n";  
    cout << " ---------------------------------\n"; 
    cout << "\n 1.100 elements\n 2.500 elements\n 3.1000 elements\n 4.Quit\n"; 
    cout<< " ---------------------------------"<<endl; 
    cout<<" Enter your choice : "; 
    cin>>ch; 
    cout<<endl; 


    cout << "\n 3.Pivot : First Element \n 4.Pivot: Random element\n"; 
    cout<< " ---------------------------------"<<endl; 
    cout<<" Enter your choice : "; 
    cin>>ch2; 
    cout<<endl; 

    string inputSize; 
    switch(ch) 
    { 
    case 1: inputSize="100"; 
    ... 
    } 
    string pivotSelection; 
    switch(ch2) 
    { 
    case 1: pivotSelection="FirstElement"; 
    ... 
    } 

然後重寫你的塊的代碼來使用這些變量,而不是硬編碼一切。 例如不是file.open("500.txt");可以有file.open(inputSize + ".txt");

但是用這種方法,你必須注意到,在C++中,不能創建可變大小的堆棧分配的數組。例如,而不是int inputs[sizeOfInput];你需要int * inputs = malloc(sizeof(int) * sizeOfInput);

+0

我已經做了你上面寫的代碼,但我在pivotSelection節有問題,input1尚未聲明。 – doraemon

+0

我已經做了你上面寫的代碼,但我在pivotSelection部分有問題,input1尚未聲明。但是你編寫的DRY方法代碼可以幫助我剪切相同的代碼,並且我只需要創建一個新的void函數來調用其相應的方法 – doraemon

+0

我提供的是您需要採取的方法。我沒有爲你寫作業。當然你需要聲明必要的變量。 – ForeverStudent