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);
}
|