2016-04-21 2 views
1

私はC++を学んでおり、簡単な電卓プログラムを作成しました。これに関連して、私はいくつかの質問があります:まず、私はそれをきれいにしたり、それを改善するために何かできますか?次に、このコードでは、gotoステートメントを使用してプログラムの先頭に戻ります。私はgotoが "スパゲッティ"コードを作成できるので、多くのプログラマに見られていると聞いています。 gotoを使わないために何かできることがありますか?最後に、私はインターネットからのコード行をコピペしました: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)のようなラムダと表明しました。

+1

lambdasはC++で読みやすいものを作ることができないと確信しています –

+0

母音はやや公正です - おそらく "ペタニック"はここで "読みやすい"よりも正しい言葉です – fish2000

+1

おそらく、私はディスカッションが英語言語SE –

関連する問題