2016-04-21 23 views
1

Ich lerne gerade C++, und habe ein einfaches Taschenrechnerprogramm erstellt. Dazu habe ich ein paar Fragen: Erstens, kann ich irgendetwas tun, um es aufzuräumen oder besser zu machen? Zweitens verwende ich in diesem Code die goto-Anweisung, um zum Anfang des Programms zurückzukehren. Ich habe gehört, dass goto von vielen Programmierern herabgesehen wird, weil es "Spaghetti" -Code erstellen kann. Gibt es etwas, was ich tun könnte, um nicht goto zu verwenden, aber eher ein llop irgendeiner Art? Zuletzt habe ich eine Codezeile aus dem Internet gefunden:Wie verwende ich Std :: Transform in C++?

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

Kann mir jemand erklären, was das ist? Ich verstehe die transform und

::toupper 

aber nicht die .begin() und .end()

Hier ist mein vollständiger Code, und danke für die Hilfe:

#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

Haben Sie die [Dokumentation] (http://en.cppreference.com/w/cpp/algorithm/transform) gelesen? Wenn ja, was genau ist Ihnen unklar? Es erklärt, wie die Iteratoren "begin" und "end" verwendet werden. –

+0

Wenn Sie ein Tutorial zu C++ lesen, können Sie die While- und For-Schleifen lernen. Die '.begin()' und '.end()' sind für Iteratoren und werden wahrscheinlich in jedem guten Tutorial behandelt. – Kupiakos

+0

überprüfen Sie diese http://www.cprogramming.com/tutorial/stl/iterators.html – user3159253

Antwort

0

Die begin und end Methoden sind von der C++ STL-Iterator-API.

Das Beispiel Sie können mehr leserlich kopiert werden (und weniger optimal) ausgedrückt mit Lambda-Ausdrücke wie diese (running example on ideone):

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" 

... die meisten Funktionen in den <algorithm> Bibliothek Verwendung C++ Iteratoren.

+1

Ich bin mir nicht sicher, ob lambdas etwas besser lesbar machen kann in C++ –

+0

Haha, das ist etwas fair - vielleicht ist "pedantisch" eher ein richtiges Wort als "leserlich" – fish2000

+1

Vielleicht denke ich, dass die Diskussion mehr für das Englische sein könnte Sprache SE –