Hello, everyone. I've been working on a script that works as a calculator to evaluate input and return the appropriate value. I don't have a compiler to work with at the moment, but will compile it as soon as possible. I'm just interested to see if anyone spots any serious errors in the code. I looked through it and commented it myself, and I think it looks good.
It takes the input 5+2, for example, and gives 7. The process is explained in the code, and it's well commented.
I will also include the readme file I wrote to go along with it. That is below the code.
//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);
}
elseif(input_string.find("-")>0) {
return(input_string.find("-")+1);
}
elseif(input_string.find("*")>0) {
return(input_string.find("*")+1);
}
elseif(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("+");
}
elseif(input_string.find("-")>0) {
return("-");
}
elseif(input_string.find("*")>0) {
return("*");
}
elseif(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);
}
Readme file:
The calculator program evaluates input and preforms basic mathematical operations on the input.
Basic input such as 1+1 or 3/3 are accepted.
The first digit of the string must be a digit, and there must be an operator in the string.
The only exception is entering a number without any operators.
This will return only the value of the number entered.
Any letters entered in the input will likely cause the program to crash completely, and any data previously entered will be lost.
Never enter anything but numbers and the approved operators into the input. Below are the list of approved operators:
+ : plus
- : minus
* : multiply
/ : divide
Entering a string with an operator as the last character of the string will act as if the last character of the string is a zero.
For example, 4* will be treated as 4*0 and will return 0.
Only include one operator per string, or it may cause an error.
I compiled it to check for errors, it found a total of 6 errors:
\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
function calculator.cpp:35: error: expected constructor, destructor, or type conversion before '(' token
function calculator.cpp:35: error: expected `,' or `;' before '(' token
function calculator.cpp:51: error: `string' was not declared in this scope
function calculator.cpp:51: error: expected `,' or `;' before '{' token
function calculator.cpp:74: error: `string' does not name a type
function calculator.cpp:81: error: expected declaration before '}' token
Execution terminated
Info :Compiling C:\BC5\BIN\noname00.cpp
Warn : string.h(549,3):Functions containing for are not expanded inline
Warn : string.h(557,3):Functions containing while are not expanded inline
Warn : string.h(563,3):Functions containing for are not expanded inline
Warn : string.h(575,3):Functions containing for are not expanded inline
Warn : string.cc(686,32):Comparing signed and unsigned values
Warn : string.cc(658,2):Cannot create pre-compiled header: code in header
Warn : noname00.cpp(36,29):Use qualified name to access member type 'std::string'
Error: noname00.cpp(37,18):Undefined symbol 'input_string'
Warn : noname00.cpp(49,2):Function should return a value
Warn : noname00.cpp(49,2):Parameter 'input_string_a' is never used
Warn : noname00.cpp(52,25):Use qualified name to access member type 'std::string'
Error: noname00.cpp(54,15):Cannot convert 'char *' to 'char'
Error: noname00.cpp(57,15):Cannot convert 'char *' to 'char'
Error: noname00.cpp(60,15):Cannot convert 'char *' to 'char'
Error: noname00.cpp(63,15):Cannot convert 'char *' to 'char'
Error: noname00.cpp(68,15):Cannot convert 'char *' to 'char'
Warn : noname00.cpp(75,7):Use qualified name to access member type 'std::string'
Warn : noname00.cpp(75,54):Use qualified name to access member type 'std::string'
Warn : noname00.cpp(76,14):Use qualified name to access member type 'std::string'
Error: noname00.cpp(82,2):Unexpected }
Warn : noname00.cpp(85,7):Use qualified name to access member type 'std::string'
Warn : noname00.cpp(85,48):Use qualified name to access member type 'std::string'
Warn : noname00.cpp(86,14):Use qualified name to access member type 'std::string'
Error: noname00.cpp(87,33):Cannot convert 'char' to 'char *'
Warn : noname00.cpp(87,75):Comparing signed and unsigned values
Error: noname00.cpp(88,64):Undefined symbol 'X'
Warn : noname00.cpp(96,2):Function should return a value
Warn : noname00.cpp(101,36):Use qualified name to access member type 'std::string'
Error: noname00.cpp(103,23):Undefined symbol 'istringstream'
Error: noname00.cpp(103,23):Statement missing ;
Error: noname00.cpp(104,11):Undefined symbol 'convert'
Warn : noname00.cpp(105,17):Possible use of 'result' before definition
Warn : noname00.cpp(106,2):Parameter 'input_string' is never used
Warn : noname00.cpp(109,37):Use qualified name to access member type 'std::string'
Error: noname00.cpp(111,23):Undefined symbol 'istringstream'
Error: noname00.cpp(111,23):Statement missing ;
Error: noname00.cpp(112,11):Undefined symbol 'convert'
Warn : noname00.cpp(113,17):Possible use of 'result' before definition
Warn : noname00.cpp(114,2):Parameter 'input_string' is never used
Warn : noname00.cpp(119,23):Use qualified name to access member type 'std::string'
Error: noname00.cpp(130,17):Expression syntax
Error: noname00.cpp(173,11):While statement missing )