Hello, I need help solving this and I am not sure how to do it: Write C++ code t
ID: 3723345 • Letter: H
Question
Hello, I need help solving this and I am not sure how to do it:
Write C++ code to replicate calculator functionality on integer variables using only ++,-- operators. Your code should have following features:
1. Perform addition, subtraction, multiplication and division depending on user choice of operation like we did in lab04.
2. Use only ++ or -- operator in your code. Usage of +,-,*,/ operators are not allowed.
3. Your code should not exit after performing one operation; instead it should run again or exit depending on user choice. After each completed operation. Ask user whether he/she wants to run again or exit the code.
Explanation / Answer
The idea is that ++ and -- increment and decrement the operand by 1.
Say, you want to add 3 and 4: You're going to increment 3 four times.
a = 3, b = 4. i starts from 1 and goes up till 4. (i.e. you increment 'a', four times)
when i = 1 : a++ -> a = 4
i = 2: a++ -> a = 5
i = 3: a++ -> a = 6
i = 4: a++ -> a = 7
Similarly, for subtracting 4 - 2. Decrement 4, two times.
first time : a-- -> a = 3
second time : a-- -> a = 2
Thus, 4 - 2 = 2.
For multiplication, 2 * 3 means 2 + 2 + 2. That is, add 2 to the sum 3 times.
You can use the add function that we defined to achieve this. Initialize sum to 0. Run the loop 'b' times (here 3)
First, call add(a, sum) i.e. add 2 to 0. You get 2.
Then again can call add(a, sum) -> you get 4. (prev sum = 2 + a = 2).
Again call add(a, sum) -> you get 4 + 2 = 6.
For division, you have to keep subtracting 'b' from 'a' until 'a' becomes less than 'b'. Keep incrementing a count k (starting from 0). This count will be your quotient for integer division.
Ex- 12 / 4.
12 >= 4 -> call sub(12, 4) you get 8. count of k becomes 1
Now, call sub(8,4) -> you get 4. count of k becomes 2.
Again since 4 >= 4, call sub(4,4), count of k becomes 3.
Now, your a has become 0. 0 is not greater than or equal to 4. Therefore, exit the loop. Return k = 3.
The code is :
#include <iostream>
using namespace std;
int addnum(int a, int b){
for(int i = 1; i <= b; i++)
a++;
return a;
}
int subnum(int a, int b){
for(int i = 1; i <= b; i++)
a--;
return a;
}
int mulnum(int a, int b){
int sum = 0;
for(int i = 1; i <= b; i++){
sum = addnum(a, sum);
}
return sum;
}
int divnum(int a, int b){
int k = 0;
if(b == 0){
cout << "Cannot divide by 0 >> Returning -1. ";
return -1;
}
else{
while(a >= b){
a = subnum(a, b);
k++;
}
}
return k;
}
int main()
{
int num1, num2;
char op;
char ch;
do{
cout << "MENU (+)Addition (-)Subtraction (*)Multiplication (/)Division " << endl;
cout << "Enter an equation of the form num1 operator num2 ";
cin >> num1 >> op >> num2;
switch(op){
case '+': cout << "Sum of " << num1 << " and " << num2 << " is " << addnum(num1, num2) << " ";
break;
case '-': cout << "Difference of " << num1 << " and " << num2 << " is " << subnum(num1, num2) << " ";
break;
case '*': cout << "Product of " << num1 << " and " << num2 << " is " << mulnum(num1, num2) << " ";
break;
case '/': cout << "Quotient of " << num1 << " and " << num2 << " is " << divnum(num1, num2) << " ";
break;
default : cout << "Invalid operator ";
}
cout << "Type x to exit. Any other character to run again ";
cin >> ch;
}while(ch != 'x');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.