Calculate Strings with int calculate(string exp) Function

  • Context: MHB 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Computation String
Click For Summary
SUMMARY

The discussion focuses on the implementation of the int calculate(string exp) function, which evaluates string expressions such as "54321+222" and returns their computed integer results. The function utilizes character manipulation to separate operators and operands, converting them from strings to integers. A key suggestion for converting strings to integers is the use of the atoi function, which stands for ASCII to integer. This approach simplifies the conversion process and enhances the function's efficiency.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with string manipulation in C++
  • Knowledge of basic arithmetic operations
  • Experience with C++ standard library functions, specifically atoi
NEXT STEPS
  • Explore advanced string parsing techniques in C++
  • Learn about error handling in C++ for invalid input
  • Investigate the use of std::stringstream for conversions
  • Study operator overloading in C++ for custom arithmetic operations
USEFUL FOR

Software developers, particularly those working with C++ who need to implement mathematical expression evaluation or string manipulation in their applications.

needOfHelpCMath
Messages
70
Reaction score
0
If input is 2: call int calculate(string exp) function. This function will return the
computation of a string expression (exp). For example:
calculate(“54321+222”) will return 54543
calculate(“120*20”) will return 2400
calculate(“235/3”) will return 78
calculate(“356-32”) will return 324My code:

HTML:
// function should return int, not void: int calculate(..)
int calculate(string exp) 
{
	int answer = 0; // store answer in here
	string left = "right";
	char op; //operator
	int i = 0;
	 // use to code left the operator and right seperating it in order to convert it into real numbers
	for (i = 0; i < exp.size(); ++i) {
		if (isdigit(exp.at(i))) { 
		
			left =  left + exp.at(i);
		}
		
		else {
			op = exp.at(i);
			break;
		}
	} ++i;
	
	for (int j = i; j < exp.size(); ++j) {
		right = "right" + exp.at(i);
	}
	// use ASCII char to get int conversion: '0' == 48, '1' == 49, '2' == 50, ... and so on
	// subtract each character in string by 48 to get int values
	// examle: if exp = "9", then exp[0] - 48 = 9
	
	for(i = 0; i < exp.length(); i++) {
		// put your if statements to check for plus, minus, times, divide
		if (i < exp.length()) {
			answer = exp.at(i)-48;
		}
			
		if (isdigit(exp.at(i))) {
		
			answer = exp.at(i)- 48;
		}
	}
	// check for a plus
		// if found add the two numbers
	// check for minus
		// if found, subtract the two numbers
	// check for a times
		// if found, multiply the two numbers
	// check for divide
		// if found, divide the to numbers
	// if nothing is found
		// return
	return answer;     
}
what I can't and don't understand is how convert into strings into numbers and also compute them.
 
Technology news on Phys.org
needOfHelpCMath said:
what I can't and don't understand is how convert into strings into numbers and also compute them.

Hey needOfHelpCMath! ;)

To convert a string into a number we can use:
Code:
int number = atoi(str.c_str());
[m]atoi[/m] is short for ascii-to-integer.
 
I like Serena said:
Hey needOfHelpCMath! ;)

To convert a string into a number we can use:
Code:
int number = atoi(str.c_str());
[m]atoi[/m] is short for ascii-to-integer.

thank you! ill try that out
 

Similar threads

Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
9K
Replies
3
Views
1K