2016-03-29 18 views
-2

Programlama için yeni bir programım ve bu benim tasarladığım ilk program. Bu bir hesap makinesidir ve bir bölümde kullanıcıdan başka bir miktar girmek isteyip istemediğini ve eğer evet dediyse, x girişini, giriş operatörünü, giriş y'ini tekrarlayın ve hepsini tamamlama sürecini girmesini istiyorum. Tek sorun şu ki, işlemi bir kereden fazla tekrarlarsanız ve bitirirseniz ... işlev kendiliğinden (önceki arayan) geri döner ve istemediğim gibi ana değere dönmez. Şimdiye kadar #pragma once, #define'u denedim ve fazladan bir fonksiyon kullanıyorum ama çözelti hala aşağıya iniyor.Önceki işlev yerine int main() öğesine dönün?

Bunu açıklamakta zorlanıyorum, işte bu yüzden bütün programı burada görebilirsiniz. Ben "stdafx.h" ve <iostream> içeren bir başlık dosyası yapılmış

// Simple Calculator v3.cpp : Defines the entry point for the console application. 
// 

#include "tots.h" 

double userInput() 

{ 
    using namespace std; 
    //Prompt the user to enter a digit 
    cout << " Please enter a digit: " << endl; 
    cout << " "; 

    //Declare a variable and assign the user's input to that variable 
    double input; 
    cin >> input; 

    //Return the digit the user entered 
    return input; 
} 

int userOperatorInput() 

{ 
    using namespace std; 
    //Prompt the user to enter an operator 
    cout << " Please enter the desired mathematical operation: " 
     << "+ = 1; - = 2, * = 3,/= 4 " << endl; 
    cout << " "; 

    //Declare a variable and assign the user's input to that variable 
    int op; 
    cin >> op; 

    //Return the operator the user entered; 
    return op; 
} 

int userFinished() 

{ 
    using namespace std; 
    /*Ask the user whether he/she has entered the function he/she desires or if they have not 
    yet finished*/ 
    cout << " Have you finished the function? " 
     << "yes = 1; no = 0" << endl; 
    cout << " "; 

    //Declare a variable and assign the user's input to that variable 
    int userfinished; 
    cin >> userfinished; 

    //Return the user's input 
    return userfinished; 
} 

double calculateSolution(double input1, int op, double input2) 

{ 
    //Determine which operator the user entered and calculate the solution accordingly 
    if (op == 1) 
     return input1 + input2; 
    else if (op == 2) 
     return input1 - input2; 
    else if (op == 3) 
     return input1 * input2; 
    else if (op == 4) 
     return input1/input2; 

    else return -1; 
} 

double moreCalculus(double solution) 

{ 
    double xtrainput1 = solution; 
    int xtraop = userOperatorInput(); 
    double xtrainput2 = userInput(); 

    double xtrasolution = calculateSolution(xtrainput1, xtraop, xtrainput2); 

    bool xtrafinished = userFinished(); 

    if (xtrafinished == 0) 
     moreCalculus(xtrasolution); 
    else 
    { 
     #ifndef RETURN 
     #define RETURN 

     return xtrasolution; 

     #endif 
    } 
} 

void displaySolution(double input1, int op, double input2, double solution) 

{ 
    using namespace std; 
    cout << " " << input1 << " " << op << " " << input2 << " = " << solution << endl; 
} 

void displayXtraSolution(double xtrasolution) 

{ 
    using namespace std; 
    cout << setprecision(20); 
    cout << " Solution: " << xtrasolution; 
} 

void end() 

{ 
    using namespace std; 

    int x; 
    cin >> x; 
} 

int main() 

{ 
    //Input from the user 
    double input1 = userInput(); 

    //Operation user requires 
    int op  = userOperatorInput(); 

    //Second input from user 
    double input2 = userInput(); 

    /*Declare and assign a boolean that informs whether the user has finished 
     the operation or not*/ 
    bool finished = userFinished(); 

    //Calculate the solution 
    double solution = calculateSolution(input1, op, input2); 

    //If the user has finished, display the solution 
    /*If the user hasn't finished, assign the required inputs from the user and calculate the 
     solution*/ 
    if (finished == 1) 
    { 
     displaySolution(input1, op, input2, solution); 
    } 

    else 
    { 
     double xtrasolution = moreCalculus(solution); 

     displayXtraSolution(xtrasolution); 
    } 

    /*Finally, display some information on screen and ask whether the user requires to calculate 
     any more operations or if he/she wants to terminate the application*/ 

Not: Ben bahsediyorum.

+3

Eğer düşünürsen '# Pragma once' nasılsa yardımcı olacağını, o zaman * gerçekten * geri sizi C++ öğrenmek için her ne kaynak gitmek gerekir. Demek istediğin, istediğin bir döngüdür. – Biffen

+0

gibi ileri geri zıplama [BASIC] (https://en.wikipedia.org/wiki/BASIC) [* spaghetti code *] (https: //en.wikipedia) yarattığı gibi nefret dolu bir dildir. .org/wiki/Spaghetti_code) takip etmek, anlamak ve sürdürmek imkansızdır. –

cevap

1

loops numaralı telefona bakmanız gerekiyor, eğer kodunuz durmaktan vazgeçiyorsa, sadece siz onu terk etmelisiniz.

0

kullanım do..while döngü

int main(){ 
    do{ 
    //your code section 
    cout<<"Do you want to continue(Y/N):"; 
    cin>>ch; 
    }while(ch=='Y'||ch=='y'); 
    } 
İlgili konular