It's suppose to read 3 numbers and 2 operations? Where are the loops? I see only one call to read anything from the console at all. Where are
expression
or
numbers
ever used?
If this is a school assignment then you need to go to your professor and ask for help. If not then you really should pick up a beginners book and work through it because what you have so far is
way off base.
Regardless here's some psuedo code to help:
int number[3]
string operations[2]
for(i = 1 to 3)
{
print "Enter a number"
read numbers[i]
}
for(i = 1 to 2)
{
print "Enter an operation"
read operation[i]
}
That will read your inputs, you should also validate them to make sure that what they enter is valid input and ask them again until what they enter is OK.
Then make a simple subroutine to do a calculation:
sub DoCalculation(string operation, int lhs, int rhs)
{
if (operation == "+")
{
return lhs + rhs
}
else if (operation == "-")
{
return lhs - rhs
}
}
Then you need to calculate. Is order of operations important or should it use a simple left to right scheme?
Left to right ordering would be this:
int result = DoCalculation(operation[1], numbers[1], numbers[2])
result = DoCalculation(operation[2], result, number[3])
print result
Proper Order of Operations would be this:
int result = 0
if (operation[1] == "*" or operation[1] == "/")
{
result = DoCalculation(operation[1], numbers[1], numbers[2])
result = DoCalculation(operation[2], result, number[3])
}
else if (operation[2] == "*" or operation[2] == "/")
{
result = DoCalculation(operation[2], numbers[2], numbers[3])
result = DoCalculation(operation[1], numbers[1], result)
}
else
{
result = DoCalculation(operation[1], numbers[1], numbers[2])
result = DoCalculation(operation[2], result, number[3])
}
print result
Both of the calculations bits could/should be loop-ified but I'll leave that as an exercise for the reader :)