2016-04-21 39 views
1

我剛剛學習C++,並創建了一個簡單的計算器程序。針對這一點,我有幾個問題:首先,我能做些什麼來清理它,或使它變得更好?其次,在此代碼中,我使用goto語句返回到程序的開頭。我聽說goto被許多程序員看不起,因爲它可以創建「意大利麪條」代碼。有沒有什麼我可以做不使用goto,而是一種什麼樣的?最後,我coppied的代碼從互聯網上一行:如何在C++中使用std :: transform?

std::transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper); 

有人能向我解釋這是什麼呢?據我所知,transform

::toupper 

但不是.begin().end()

這裏是我完整的代碼,並感謝大家的幫助:

#include <iostream> 
#include <stdlib.h> 
#include <string> 
#include <cmath> 
#include <algorithm> 
using namespace std; 

void showMenu(); 
unsigned long facInput;             //Prototyping functions 
int getInput(); 
float additionGetInput(); 
float subtractionGetInput(); 
float multiplicationGetInput(); 
float divisionGetInput(); 
unsigned long long factorialInput(unsigned long long facInput); 
float hypotenuseFinder(); 



class prompt               //Class so I don't have to repeatedly type the prompt. 
{ 
public: 
    prompt() 
    { 
     inputPromptOne = "\nEnter float one: "; 
     inputPromptTwo = "\nEnter float two: "; 
    } 
    string inputPromptOne; 
    string inputPromptTwo; 
}; 

string returnToMain; 

prompt prompt; 




int main() 
{ 

    startOfProgram:              //Label for goto function. 
    system("CLS"); 
    showMenu(); 
    int userInput = getInput(); 

    switch(userInput) 
    { 
    case 1:                //Addition 
     system("CLS"); 
     cout << "You chose the Addition Calculator." << endl; 
     cout << "The sum is: " << additionGetInput() << endl; 
     cout << "\n\nEnter 'quit' to return to menu: "; 
     cin >> returnToMain; 
     transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper); 
     if(returnToMain == "QUIT") 
     { 
      goto startOfProgram; 
     } 

     break; 
    case 2:                 //Subtraction 
     system("CLS"); 
     cout << "You chose the Subtraction Calculator." << endl; 
     cout << "The difference is: " << subtractionGetInput() << endl; 
     cout << "\n\nEnter 'quit' to return to menu: "; 
     cin >> returnToMain; 
     transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper); 
     if(returnToMain == "QUIT") 
     { 
      goto startOfProgram; 
     } 
     break; 
    case 3:                 //Multiplication 
     system("CLS"); 
     cout << "You chose the Multiplication Calculator." << endl; 
     cout << "The product is: " << multiplicationGetInput() << endl; 
     cout << "\n\nEnter 'quit' to return to menu: "; 
     cin >> returnToMain; 
     transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper); 
     if(returnToMain == "QUIT") 
     { 
      goto startOfProgram; 
     } 
     break; 
    case 4:                 //Division 
     system("CLS"); 
     cout << "You chose the Division Calculator." << endl; 
     cout << "The quotient is: " << divisionGetInput() << endl; 
     cout << "\n\nEnter 'quit' to return to menu: "; 
     cin >> returnToMain; 
     transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper); 
     if(returnToMain == "QUIT") 
     { 
      goto startOfProgram; 
     } 
     break; 
    case 5: 
     system("CLS");              //Factorial Finder 
     cout << "You chose the Factorial Calculator." << endl; 
     cout << "Enter a number (MAX 65): "; 
     cin >> facInput; 
     cout << "The factorial is: " << factorialInput(facInput) << endl; 
     cout << "\n\nEnter 'quit' to return to menu: "; 
     cin >> returnToMain; 
     transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper); 
     if(returnToMain == "QUIT") 
     { 
      goto startOfProgram; 
     } 
     break; 
    case 6:                 //Pythagorean Theorem 
     system("CLS"); 
     cout << "You chose the Pythagorean Theorem Calculator." << endl; 
     cout << "Length of side c (hypotenuse): " << hypotenuseFinder() << endl; 
     cout << "\n\nEnter 'quit' to return to menu: "; 
     cin >> returnToMain; 
     transform(returnToMain.begin(), returnToMain.end(), returnToMain.begin(), ::toupper); 
     if(returnToMain == "QUIT") 
     { 
      goto startOfProgram; 
     } 
     break; 
    default: 
     cout << "That is an invalid function. Try again." << endl; 
     goto startOfProgram; 


    } 

} 
void showMenu() 
{ 
    cout << "1-Addition" << endl; 
    cout << "2-Subtraction" << endl; 
    cout << "3-Multiplication" << endl; 
    cout << "4-Division" << endl; 
    cout << "5-Factorial" << endl; 
    cout << "6-Pythagorean Theorem" << endl; 
    cout << "---------------------" << endl; 
} 
int getInput() 
{ 
    int userInput; 

    cout << "Choose a function: "; 
    cin >> userInput; 
    return userInput; 
} 
float additionGetInput() 
{ 
    float addInput1; 
    float addInput2; 

    cout << prompt.inputPromptOne; 
    cin >> addInput1; 
    cout << prompt.inputPromptTwo; 
    cin >> addInput2; 
    system("CLS"); 

    float sum = addInput1 + addInput2; 
    return sum; 
} 
float subtractionGetInput() 
{ 
    float subInput1; 
    float subInput2; 

    cout << prompt.inputPromptOne; 
    cin >> subInput1; 
    cout << prompt.inputPromptTwo; 
    cin >> subInput2; 
    system("CLS"); 

    float difference = subInput1 - subInput2; 
    return difference; 
} 
float multiplicationGetInput() 
{ 
    float mulInput1; 
    float mulInput2; 

    cout << prompt.inputPromptOne; 
    cin >> mulInput1; 
    cout << prompt.inputPromptTwo; 
    cin >> mulInput2; 
    system("CLS"); 

    float product = mulInput1 * mulInput2; 
    return product; 
} 
float divisionGetInput() 
{ 
    float divInput1; 
    float divInput2; 

    cout << prompt.inputPromptOne; 
    cin >> divInput1; 
    cout << prompt.inputPromptTwo; 
    cin >> divInput2; 
    system("CLS"); 

    float quotient = divInput1/divInput2; 
    return quotient; 
} 
unsigned long long factorialInput(unsigned long long facInput) 
{ 
    if(facInput==1) 
    {cout << "---------------------" << endl; 
     return 1; 
    } 
    else 
    { 
     return facInput*factorialInput(facInput-1); 
    } 
} 
float hypotenuseFinder() 
{ 
    float a; 
    float b; 

    cout << "Enter length of side a: "; 
    cin >> a; 
    cout << "\nEnter length of side b: "; 
    cin >> b; 

    float hypotenuse = sqrt(pow(a, 2) + pow(b, 2)); 
    return hypotenuse; 
} 
+3

您是否閱讀過[documentation](http://en.cppreference.com/w/cpp/algorithm/transform)?如果是這樣,你還不清楚什麼?它解釋瞭如何使用'begin'和'end'迭代器。 –

+0

閱讀關於C++的任何教程可能會幫助你學習'while'和'for'循環。 '.begin()'和'.end()'是用於迭代器的,很可能在任何好的教程中都會介紹。 – Kupiakos

+0

檢查這一個http://www.cprogramming.com/tutorial/stl/iterators.html – user3159253

回答

0

beginend方法是從C++ STL迭代器API。在<algorithm>庫中使用C++迭代器

std::string input = "Yo Dogg"; 
std::string output(input); 
std::transform(input.begin(), input.end(), 
       output.begin(), 
      [](auto character) { 
    return ::toupper(character); 
}); 
/// 'output' will contain "YO DOGG" 

...大部分功能:

複製可以更清晰(和較少最佳)的例子,像這樣(running example on ideone)lambda表達式表示。

+1

我不知道lambda表達式可以做任何事情,在C++中 –

+0

更清晰哈哈,這是有點公平的 - 也許「迂腐」比「清晰」更多的是正確的話這裏 – fish2000

+1

也許,但我認爲這種討論可能是多爲英語語言SE –

相關問題