Project -CIS3301 You are to write a \'calculator\' program. The calculator will
ID: 3854632 • Letter: P
Question
Project -CIS3301 You are to write a 'calculator' program. The calculator will allow the user to perform the following operations Get the area of a square addition Get the exponent of 2 integers Convert from decimal to bases 2 - 16 Get the quotient of 2 integers Get Factorial Get the real division of 2 integers Find out if an integer is even or odd Get the remainder of 2 integers Get the area of a circle Multiplication Get the area of a rightisosceles triangle Subtraction ALL of these operations MUST be done in separate functions (i.e. NOT in the main function). You must present the options to the user (also in a separate function). One of the options in the menu must be the option to quit. If that is the option, the program must end as soon as it returns to the main function. If that is not the option, you must determine if the user has made a legal option (i.e. one on the menu). If they entered an illegal option, you must notify the user that it is an illegal choice, redisplay the menu, and get the user's new choice. The user cannot leave the menu function until they enter a legal option (The user's choice to quit is a legal option). When a legal choice is entered, the choice must be returned to the main function. When the user's choice is returned to the main function the first thing you must determine is whether the user wants to quit, if so, quit (the main function should return a value, so you can return that value). Otherwise, you must determine how many parameters must be passed to the required function. Some require only 1 integer (conversions, factorial, and the area of a circle or square) the others require 2 (all of the user's choices must be integers). You may not use any global variables (except for the assignment of the output file name). About half the external functions must return the result of the operations (where they will be printed), the remaining operations can be printed in the function). Your program must print out the result to the console and to an external file. Your main function must be presented as a loop. Once it completes the operation, it must again present the menu and perform all of the operations, until the user decides to quit. If I find that there are errors duplicated by multiple individuals, I must assume that the authors of the code shared their code with others. In this case I will submit the findings to the Dean of student affairs who will decide their fates. Failure to abide by any of the conditions will result in relatively severe deductions from the grades of those involved. If you decide not to come to any of the classes during the project, I will assume you don't care about understanding the project and will deduct points (i.e. I will take attendance). I will give help to anyone who needs it during our regularly scheduled class time (we will spend a few weeks on this project. I will also be available after class). The final test, which you will take on the computer in class, we will have will be a shortened replication of this project. If you decide you don't need to fully understand this project, and decide to copy your friend's code, what do you suppose your grade on that test will be? The final test will have the greatest weighting in your grade) If there is some problem I did not point to in the above instructions, I will rewrite the instructions and email it to you.Explanation / Answer
Code:-
#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
class calculator{
public:
int addition(int a,int b){
return (a+b);
}
int subtraction(int a,int b){
return (a-b);
}
int multiply(int a,int b){
return (a*b);
}
int quotient(int a,int b){
return (a/b);
}
int remainder_result(int a,int b){
return (a%b);
}
float real_division(int a,int b){
float temp;
temp=(float)a/(float)b;
return temp;
}
int power(int a,int b){
return (pow(a,b));
}
int area_of_square(int side){
return (side*side);
}
double area_of_circle(float radius){
return (4*(3.14)*radius*radius);
}
double area_of_triangle(float base,float height){
return (0.5*base*height);
}
char* fromDeci(char res[], int base, int inputNum)
{
int index = 0; // Initialize index of result
// Convert input number is given base by repeatedly
// dividing it by base and taking remainder
while (inputNum > 0)
{
res[index++] = reVal(inputNum % base);
inputNum /= base;
}
res[index] = '';
// Reverse the result
strev(res);
return res;
}
char reVal(int num)
{
if (num >= 0 && num <= 9)
return (char)(num + '0');
else
return (char)(num - 10 + 'A');
}
// Utility function to reverse a string
void strev(char *str)
{
int len = strlen(str);
int i;
for (i = 0; i < len/2; i++)
{
char temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}
}
int factorial(int num){
int fact=1;
for(int i = 1; i <=num; ++i)
{
fact *= i;
}
}
void even_or_odd(int num){
if(num%2==0){
cout<<"EVEN NUMBER"<<endl;
}
else{
cout<<"ODD NUMBER"<<endl;
}
}
char menu(){
cout << "ENTER THE ONE OF THE OPTIONS :"<<endl;
cout<<"1 : ADDING TWO NUMBERS"<<endl;
cout<<"2 : SUBTRACTING TWO NUMBERS"<<endl;
cout<<"3 : MULTIPLYING TWO NUMBERS"<<endl;
cout<<"4 : QUOTIENT OF TWO NUMBERS"<<endl;
cout<<"5 : REMAINDER OF TWO NUMBERS"<<endl;
cout<<"6 : REAL DIVISION TWO NUMBERS"<<endl;
cout<<"7 : EXPONENT OF TWO NUMBERS"<<endl;
cout<<"8 : FACTORIAL OF A NUMBER"<<endl;
cout<<"9 : CONVERSION OF DECIMAL TO ANY BASE"<<endl;
cout<<"S : AREA OF SQUARE"<<endl;
cout<<"T : AREA OF TRAIANGLE"<<endl;
cout<<"C : AREA OF CIRCLE"<<endl;
cout<<"E : TO FIND A INTEGER EVEN OR ODD"<<endl;
cout<<"Q : QUIT"<<endl;
char ch;
cin>>ch;
return ch;
}
};
int main() {
calculator obj;
int check=1;
char option;
int input1;
int input2;
char rest[50];
int result;
double result1;
char *rest_final;
while(check == 1){
option=obj.menu();
switch(option){
case '1':
cout<<"input 2 numbers:";
cin>>input1;
cin>>input2;
result=obj.addition(input1,input2);
cout<<"result="<<result<<endl;
break;
case '2':
cout<<"input 2 numbers:";
cin>>input1;
cin>>input2;
result=obj.subtraction(input1,input2);
cout<<"result="<<result<<endl;
break;
case '3':
cout<<"input 2 numbers:";
cin>>input1;
cin>>input2;
result=obj.multiply(input1,input2);
cout<<"result="<<result<<endl;
break;
case '4':
cout<<"input 2 numbers:";
cin>>input1;
cin>>input2;
result=obj.quotient(input1,input2);
cout<<"result="<<result<<endl;
break;
case '5':
cout<<"input 2 numbers:";
cin>>input1;
cin>>input2;
result=obj.remainder_result(input1,input2);
cout<<"result="<<result<<endl;
break;
case '6':
cout<<"input 2 numbers:";
cin>>input1;
cin>>input2;
result1=obj.real_division(input1,input2);
cout<<"result="<<result1<<endl;
break;
case '7':
cout<<"input 2 numbers:";
cin>>input1;
cin>>input2;
result=obj.power(input1,input2);
cout<<"result="<<result<<endl;
break;
case '8':
cout<<"input number:";
cin>>input1;
result=obj.factorial(input1);
cout<<"result="<<result<<endl;
break;
case '9':
cout<<"input base,input number:";
cin>>input1;
cin>>input2;
rest_final=obj.fromDeci(rest,input1,input2);
cout<<"result="<<result<<endl;
break;
case 'S':
cout<<"input side:";
cin>>input1;
result=obj.area_of_square(input1);
cout<<"result="<<result<<endl;
break;
case 'T':
cout<<"input base and heighth";
cin>>input1;
cin>>input2;
result1=obj.area_of_triangle(input1,input2);
cout<<"result="<<result1<<endl;
break;
case 'C':
cout<<"input radius";
cin>>input1;
result1=obj.area_of_circle(input1);
cout<<"result="<<result1<<endl;
break;
case 'E':
cout<<"input number:";
cin>>input1;
obj.even_or_odd(input1);
break;
case 'Q':
check=0;
break;
default:
cout<<"WRONG OPTION ...DO IT AGAIN"<<endl;
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.