Calculator Script

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.

All help is Appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//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);

}


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.  
Last edited on
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


Last edited on
i compile and found 17 error

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 )
Yeah, I got a chance to compile it and it had like 30 once I closed a missing parenthesis. Thanks for all your help.
Topic archived. No new replies allowed.