Calculate Strings with int calculate(string exp) Function

In summary, to convert a string into a number, you can use the function atoi which stands for ascii-to-integer. This function takes in a string and returns an integer value. Once you have converted the string into an integer, you can perform mathematical operations on it.
  • #1
needOfHelpCMath
72
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
  • #2
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.
 
  • #3
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
 

FAQ: Calculate Strings with int calculate(string exp) Function

What does the calculate() function do?

The calculate() function is a custom function that takes in a string containing a mathematical expression and returns the result of the calculation. It can handle basic arithmetic operations such as addition, subtraction, multiplication, and division.

How do I use the calculate() function in my code?

To use the calculate() function, you need to pass in a string containing a mathematical expression as the argument. The function will then evaluate the expression and return the result. For example, calculate("2+3") will return 5.

What happens if the string passed into the calculate() function is not a valid mathematical expression?

If the string passed into the calculate() function is not a valid mathematical expression, the function will return an error or throw an exception. It is important to ensure that the string passed in is a valid expression before using the calculate() function.

Can the calculate() function handle complex mathematical expressions?

Yes, the calculate() function can handle complex mathematical expressions with multiple operations and parentheses. It follows the standard order of operations, evaluating parentheses first, then multiplication and division, and finally addition and subtraction.

Are there any limitations to the calculate() function?

The calculate() function can handle most basic arithmetic operations, but it may not work with more advanced mathematical concepts such as trigonometric functions or logarithms. It is designed to be a simple and efficient way to calculate basic mathematical expressions.

Similar threads

Back
Top