Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi im trying to make a C++ calculator to auto solve a calculation. I have a string/char* that is in the format of e.g 1+1 and i would like it to solve the calculation and display it.

C++
int main()
{
	char input[] = "3+2";
	float num1, num2;
	char operation;

	printf("%c \n", input[0]);
	printf("%c \n", input[1]);
	printf("%c \n", input[2]);

	num1 = input[0];
	operation = input[1];
	num2 = input[2];
	//printf("Enter operator either + or - or * or divide : ");
	//scanf("%c", &operation);
	//printf("Enter two operands: ");
	//scanf("%f%f", &num1, &num2);
	if (operation == '+')
			printf("%.1f + %.1f = %.1f\n", num1, num2, num1 + num2);
		if (operation == '-')
			printf("=%f\n", num1 - num2);
		if (operation == '*')
			printf("=%f\n", num1*num2);
		if (operation == '/')
			printf("=%f\n", num1 / num2);
	system("pause");
	return 0;
}


Output
3
+
2
51.0 + 50.0 = 101.0
Press any key to continue . . .


Its taking the input numbers as the ASCII equivalent and then adding them do i need to convert the numbers before the calculation takes place e.g char to float if so how do i do that without converting the entire char?

What I have tried:

Converting separate chars to floats:

char* input = "3+2";

float num1 = atof(input[0]);
float num2 = atof(input[2]);
Posted
Updated 22-Mar-16 13:46pm
v3
Comments
PIEBALDconsult 18-Mar-16 22:25pm    
Well, what do _you_ think?
Also look into the Shunting Yard Algorithm.
Member 12267258 18-Mar-16 22:31pm    
Well to me i think i have to convert the first and third chars in input to floats but i cant find anything on how to do that. I have only found how to convert entire char strings :-/

Have a look at: boost spirit: Plain calculator example[^]

It's a simple demonstration of boost spirit[^] - which is a rather nice library that helps you with this kind of stuff.

What you are trying to do is called parsing[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-Mar-16 1:04am    
Makes sense, a 5.
—SA
Espen Harlinn 19-Mar-16 8:04am    
Thank you :-)
Your parsing goals look very modest. Your code is all ANSI C so maybe this is really a C question.

If the expressions you want to accept are number op number where number is any decimal value and "op" is one of the usual math operators like +, -, *, / ... take a look at the strtod function. It's like atof, only better.

strtod - C++ Reference[^]

This function will parse a number - given a character pointer, and return another pointer that points to *after* that number.

C++
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char text[] = "123.0+456.0";
    char *cursor = text;
    double term1 = strtod(cursor, &cursor);
    char op = *cursor++;
    if (!op)
        return 2;
    double term2 = strtod(cursor, &cursor);
    double result = 0.f;
    switch (op)
    {
        case '+':
            result = term1 + term2;
            break;
        case '-':
            result = term1 - term2;
            break;
        case '*':
            result = term1 * term2;
            break;
        case '/':
            result = term1 / term2;
            break;
        default:
            return 1;
    }
    printf("%f%c%f=%f\n", term1, op, term2, result);
    return 0;
}


This code is meant to express an idea - not be a complete solution. It does not consider divide by zero, white space, or more complex expressions. I hope it leads you in a better direction.
 
Share this answer
 
v2
Comments
Espen Harlinn 23-Mar-16 6:36am    
Nice example - well done :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900