Skripta za kalkulator

Skripta za kalkulator

offline
  • Programer
  • Pridružio: 23 Maj 2012
  • Poruke: 4577

Evo, muči me još jedno pitanje - naime pronašao sam na internetu skriptu za kalkulator. Zanima me šta je sve ovde netačno? (Mislim da ima negde oko dvadesetak grešci), i kako to da ispravim?

//Calculator Script //Ameobea //December 2010 #include <iostream> #include <string> //Functions that perform the basic mathematical functions.  They require two double-type numbers to preform the operations on.  double add(double first, double second) {    double temp=first+second;    return(temp); } double subtract(double first, double second) {    double temp=first-second;    return(temp); } double divide(double first, double second) {    double temp=first/second;    return(temp); } double multiply(double first, double second) {    double temp=first*second;    return(temp); } //This function is used if no operation sign is found in the input string.  It will simply display the number inputted.  double do_nothing(double first) {    double temp=first;    return(temp); } //gets the location of the operator in the string for use in the proactive debugger in the function get_second_number.  get_operator_location(string input_string_a) {    if(input_string.find("+")>0) {       return(input_string.find("+")+1);    }    else if(input_string.find("-")>0) {       return(input_string.find("-")+1);    }    else if(input_string.find("*")>0) {       return(input_string.find("*")+1);    }    else if(input_string.find("/")>0) {       return(input_string.find("/")+1);    } } //Returns the literal char of what the operator is to be used to determine the function that needs to be used.  char get_operator(string input_string) {    if(input_string.find("+")>0) {       return("+");    }    else if(input_string.find("-")>0) {       return("-");    }    else if(input_string.find("*")>0) {       return("*");    }    else if(input_string.find("/")>0) {       return("/");    }    else    {       //possible problem here       return("=");    }     } //preforms a loop to find the first number before the operator.  //It breaks the loop when it hits the operator.  It returns a string of the first number.  string get_first_number_string(string input_string) {    string temp;    for (int x=0;input_string[x]==get_operator(input_string);x++) {       temp.append(input_string[x]);    }    return(temp);    } } //does the same as the previous one, except it starts at the character after the operator and works to the end of the string.  string get_second_number(string input_string) {    string temp;    if(get_operator(input_string)!="="&&get_operator_location(input_string)!=input_string.size()) {       for (int x=input_string.find(get_operator(input_string))+2;X>input_string.length();x++) {          temp.append(input_string[x]);       }    }    else {    return(0);    } } //these two below functions transfer the string //output of the first and second numbers from get_first_number and //get_second_number into the double format to be evaluated later.  double transfer_first_number(string input_string) {    double result;    istringstream convert(input_string);    convert>>result;    return(result); } //see above ^ double transfer_second_number(string input_string) {    double result;    istringstream convert(input_string);    convert>>result;    return(result); } int main() { //defines the string that holds the value of the input string formula_string; //welcome message cout<<"Calculator\nCasey Primozic\nEnter a formula for the calculator to evaluate and press enter.  \n"; cout<<"Enter the word QUIT at any time to terminate the program.  /n/n"; cout<<"Note:  Entering letters or incorrectly formatted strings into the calculator may cause undesired output or crash the program.  \n"; cout<<"See the included readme.txt file for directions on the use of the calculator.  \n\n";    //gets the input string    cin>>formula_string;    //loop that checks for the exit string    while (continue!="QUIT") {       //checks to see if the first digit of the string is a digit, minimal in-code debugger       if (formula_string[0]>='0' && formula_string[0]<='9') {          //display error and ask for new input          cout<<"\nError:  Bad input.  Enter another formula to evaluate.  \n\n";          break;       }       //Here's where the action happens.        else {          //variables used to calculate the answer.           double answer;                    //these take the answers from get_first_number and get_second_number and                         //transfer them into the double format using transfer_first_number and transfer_second_number.           double var_first=transfer_first_number(get_first_number(formula_string));          double var_second=transfer_first_number(get_second_number(formula_string));                    //Calls the functions that preforms the appropriate operation                         //depending on the value of the operator attained by get_operator.           //They set the variable answer equal to the appropriate value.           switch(get_operator(formula_string) {             case "+":                answer=add(var_first, var_second);                break;             case "-":                answer=subtract(var_first, var_second);                break;             case "*":                answer=multiply(var_first, var_second);                break;             case "/":                answer=divide(var_first, var_second);                break;             case "=":                answer=do_nothing(var_first);                break;          }       }              //prints the answer       cout<<answer<<"\n";    } return(1); }



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • Srđan Tot
  • Pridružio: 12 Jul 2005
  • Poruke: 2483
  • Gde živiš: Ljubljana

Nisam probao kod, ali mi na prvi pogled izgleda da ti ispod linije #include <string> nedostaje using namespace std;

Kad budeš malo savladao te osnove, baci pogled na malo drugačiji digitron:
Prvi deo: [Link mogu videti samo ulogovani korisnici]
Drugi deo: [Link mogu videti samo ulogovani korisnici]
Treći deo: [Link mogu videti samo ulogovani korisnici]



Ko je trenutno na forumu
 

Ukupno su 2087 korisnika na forumu :: 52 registrovanih, 7 sakrivenih i 2028 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 20624 - dana 04 Apr 2026 04:18

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: 357magnum, amonsrb, Apok, APS, aramis s, ArmyBoss, Asteker, Ben Roj, bukefal, celt, Cicumile, dejno, Dekanovic, Electron, Georgius, hellenic, Igritelj, Ikica977, Jakonjveliki, Josef, Kudun, Marko1238, Mcdado, mikelija, Milometer, Mićko, mocnijogurt, mrvica78, nebidrag, neutrino, niksa517, Nmr, nobutado, OgnjenMitric, peradetlić, Permaldar, PlayerOne, Pururin, royst33, shaja1, Sharpshooter, stegonosa, T55, Tafocus, tamno.nebo, tm, tooljan, vuksa72, x011, Zemunikola, Zoran1959, 1107