Calculator program using '^' power of 2

Write an even better calculator program calc3.cpp that can understand squared numbers. We are going to use a simplified notation X^ to mean X2. For example, 10^ + 7 - 51^ should mean 102 + 7 − 512.

Example:
When reading input file formulas.txt

5^;
1000 + 6^ - 5^ + 1;
the program should report:

$ ./calc3 < formulas.txt //if this doesn't work g++ (yourfile).cpp
//then ./a < formulas.txt
25
1012
A hint:
To take into account ^, don’t add or subtract new numbers right away after reading them. Instead, remember the number, read the next operator and if it is a ^, square the remembered number, then add or subtract it.

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
#include <iostream>
using namespace std;
	
int readnumber() //this int prototype I found online it works though not sure how
{
	int val;
    cin >> val;
    if (cin.peek() == '^')
    { // found ^
        val *= val;   // square the value
        cin.ignore(); // remove the ^ so no one else trips over it
    }
    return val;
}

int main()
{
	int result=0;
	char op;
	int operand=0;
	
	result = readnumber();
	
	cin >> result;                      
    while (cin >> op >> operand) {
		
	operand=readnumber();
		
			if (op ==';')
			{
				cout<<result<<endl;
			}
			
		operand = readnumber();
		
		switch(op)
		{
			case '+': //addition
			result += operand;
			break;
			
			case '-': //subtraction
			result -= operand;
			break;
			
			case ';': //end on semicolon
			break; 
		}
		cout<<result;
	}
	cout<<result;
}
	
}


I know this doesn't work and cout a 0 for result, I had to modify it so it won't resemble my code too much though I need help with the '^' and ';' statements because currently I only get 25 as the result when reading 5^ but it neglects the arithmetic in the second line of the .txt file which is 1000 + 6^ - 5^ + 1;. I wonder if anyone know how to get the second line of formulas.txt to work.
Without input error handling:

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
#include <iostream>

int get_number()
{
    // read the number
    int number ;
    std::cin >> number ;

    // read the next character
    char next_char ;
    std::cin.get(next_char) ;

    // if the next character is ^, square the number
    if( next_char == '^' ) number = number * number ;

    // otherwise, don't consume next_char: put it back
    else std::cin.putback(next_char) ;

    return number ;
}

int main()
{
    int result = get_number() ; // initial result is the first number

    char oper ;
    while( std::cin >> oper && oper != ';' ) // till a ; is encountered 
    {
        const int operand = get_number() ; // read in the operand

        switch(oper)
        {
            case '+' : result += operand ; break ;
            case '-' : result -= operand ; break ;
            case '*' : result *= operand ; break ;
            // etc.
        }
    }

    std::cout << result << '\n' ;
}
Last edited on
@JLBorges I ran your code and it cout the same answer I got which was only 25 though my problem was trying to get a statement to read the second line of formulas.txt and print the answer. The result should've printed out 25 and 1012 as outputs. I've been struggling to get it to print the second answer and your code does the same thing as mine which only prints out 25. I can't figure out why though since everything looks fine.
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
#include <iostream>

int get_number()
{
    // read the number
    int number ;
    std::cin >> number ;

    // read the next character
    char next_char ;
    std::cin.get(next_char) ;

    // if the next character is ^, square the number
    if( next_char == '^' ) 
    {
        #ifndef NDEBUG
            std::cout << "        get_number: " << number << "^ == " << number 
                      << '*' << number << " == " << number*number << '\n' ;
        #endif // NDEBUG
        number = number * number ;
    }

    // otherwise, don't consume next_char: put it back
    else 
    {
        #ifndef NDEBUG
            std::cout << "       get_number: number == " << number << '\n' ;
        #endif // NDEBUG
        std::cin.putback(next_char) ;
    }

    return number ;
}

int main()
{
    int result = get_number() ; // initial result is the first number
    #ifndef NDEBUG
        std::cout << "    detail: initial result: " << result << '\n' ;
    #endif // NDEBUG

    char oper ;
    while( std::cin >> oper && oper != ';' ) // till a ; is encountered
    {
        const int operand = get_number() ; // read in the operand

        #ifndef NDEBUG
            std::cout << "    detail: " << result << ' ' << oper << ' ' << operand ;
        #endif // NDEBUG

        switch(oper)
        {
            case '+' : result += operand ; break ;
            case '-' : result -= operand ; break ;
            case '*' : result *= operand ; break ;
            // etc.
        }

        #ifndef NDEBUG
            std::cout << " == " << result << '\n' ;
        #endif // NDEBUG
    }

    std::cout << "\n*** final result: " << result << " ***\n" ;
}


echo && echo ------------ && echo && g++ -std=c++17 -O3 -Wall -Wextra -pedantic-errors main.cpp 
echo && echo 'input: 5^;' && ./a.out <<< '5^;' 
echo && echo 'input: 1000 + 6^ - 5^ + 1;' && ./a.out <<< '1000 + 6^ - 5^ + 1;'

echo && echo ------------ && echo && clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp -lsupc++
echo && echo 'input: 5^;' && ./a.out <<< '5^;' 
echo && echo 'input: 1000 + 6^ - 5^ + 1;' && ./a.out <<< '1000 + 6^ - 5^ + 1;'

------------


input: 5^;
        get_number: 5^ == 5*5 == 25
    detail: initial result: 25

*** final result: 25 ***

input: 1000 + 6^ - 5^ + 1;
       get_number: number == 1000
    detail: initial result: 1000
        get_number: 6^ == 6*6 == 36
    detail: 1000 + 36 == 1036
        get_number: 5^ == 5*5 == 25
    detail: 1036 - 25 == 1011
       get_number: number == 1
    detail: 1011 + 1 == 1012

*** final result: 1012 ***

------------


input: 5^;
        get_number: 5^ == 5*5 == 25
    detail: initial result: 25

*** final result: 25 ***

input: 1000 + 6^ - 5^ + 1;
       get_number: number == 1000
    detail: initial result: 1000
        get_number: 6^ == 6*6 == 36
    detail: 1000 + 36 == 1036
        get_number: 5^ == 5*5 == 25
    detail: 1036 - 25 == 1011
       get_number: number == 1
    detail: 1011 + 1 == 1012

*** final result: 1012 ***

http://coliru.stacked-crooked.com/a/c3d2877eb70feeed
@JLBorges Sorry if I was not being specific enough, the program suppose to run both input 5^;
1000 + 6^ - 5^ + 1;
simultaneously by redirecting the formulas.txt to the .cpp file. Right now it would cout one answer at a time depending on which arithmetic expression you input. Since a bot algorithm checks this, it would only redirect the ./a < formulas.txt and expect both
25
1012
be printed out this way with each answer on its separate line. Though I would had misunderstood the directions too and sorry for not explaining it clearly enough.
So, put the evaluation of the expression in a loop,
which keeps running till there are no more expressions to be evaluated.

For instance:
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
#include <iostream>

int get_number()
{
    // read the number
    int number ;
    std::cin >> number ;

    // read the next character
    char next_char ;
    std::cin.get(next_char) ;

    // if the next character is ^, square the number
    if( next_char == '^' ) number = number * number ;

    // otherwise, don't consume next_char: put it back
    else std::cin.putback(next_char) ;

    return number ;
}

bool evaluate_expression() // return true if an expression was evaluated
{
    int result = get_number() ; // initial result is the first number

    char oper ;
    while( std::cin >> oper && oper != ';' ) // till a ; is encountered
    {
        const int operand = get_number() ; // read in the operand

        switch(oper)
        {
            case '+' : result += operand ; break ;
            case '-' : result -= operand ; break ;
            case '*' : result *= operand ; break ;
            // etc.
        }
    }

    // if an expression was read, print the result
    if( std::cin ) std::cout << result << '\n' ;

    return std::cin.good() ; // return true if expression input was successful
}

int main()
{
    // keep evaluating expressions one by one
    // till there is no more input that can be read
    do std::cout << "enter expression: " ;
    while( evaluate_expression() ) ;
}
Topic archived. No new replies allowed.