2016-04-21 4 views
1

Je suis en train d'apprendre C++, et j'ai créé un programme simple de calculatrice. En ce qui concerne cela, j'ai quelques questions: Premièrement, y a-t-il quelque chose que je puisse faire pour le nettoyer ou le rendre meilleur? Deuxièmement, dans ce code, j'utilise l'instruction goto pour revenir au début du programme. J'ai entendu que goto est méprisé par de nombreux programmeurs, car il peut créer du code "spaghetti". Y at-il quelque chose que je pourrais faire pour ne pas utiliser goto, mais plutôt un llop de quelque sorte? Enfin, j'ai coppied une ligne de code sur Internet:Comment utiliser std :: transform en C++?

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

Quelqu'un peut-il m'expliquer ce que cela fait? Je comprends la transform et

::toupper 

mais pas le .begin() et .end()

Voici mon code complet, et je vous remercie de l'aide:

#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

Avez-vous lu la [documentation] (http://fr.cppreference.com/w/cpp/algorithm/transform)? Si oui, qu'est-ce qui ne vous est pas clair? Il explique comment les itérateurs 'begin' et' end' sont utilisés. –

+0

La lecture de tout tutoriel sur C++ peut vous aider à apprendre les boucles 'while' et' for'. Les '.begin()' et '.end()' sont pour les itérateurs, et seront probablement couverts dans tout bon tutoriel. – Kupiakos

+0

cochez celui-ci http://www.cprogramming.com/tutorial/stl/iterators.html – user3159253

Répondre

0

Les méthodes begin et end sont de la API de l'itérateur STL C++.

L'exemple copié peut être plus lisible (et moins optimale) exprimé avec lambdas comme celui-ci (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" 

... la plupart des fonctions dans l'utilisation de la bibliothèque C++ <algorithm> itérateurs.

+1

Je ne suis pas sûr que lambdas puisse rendre quelque chose de plus lisible en C++ –

+0

Haha, c'est un peu juste - peut-être que "pédant" est plus juste que "lisible" ici – fish2000

+1

Peut-être, mais je pense que cette discussion pourrait être plus langue SE –