Your program first prints out a welcome message, then asks the user which type o
ID: 671054 • Letter: Y
Question
Your program first prints out a welcome message, then asks the user which type of operations he/she likes to carry out: integer (i) or Boolean (b). • If the choice is 'i' (integer operations), ask for the mathematical operation he/she is interested in. The input from the user has to be: o '+' for addition, o '-' for subtraction, o '*' for multiplication, o /' for division, The program then asks for the two numbers that the operation is going to be performed on, calculates the output and prints out the results. • If the choice is 'b' (Boolean operations), ask for the Boolean operation he/she is interested in (the input from the user has to be 'a' for AND, 'o' for OR). The program then asks for the two binary numbers that the operation is going to be performed on, calculates the output and prints out the results. For getting the inputs or printing out the result, you need to print proper messages on the screen.
This is what i have done so fay but i got stuck on the Boolean part:
#include <iostream>
using namespace std;
int main()
{
int i;
char ch;
cout<<"Hello, welcome! "<<endl;
cout<<"Which operation would you like to carry out: integer (i) or Boolean (b)";
cin>>i;
if (ch=='i')
{
cout<<" You selected integer ";
cout<<" Now choose your first value";
cin>>i;
cout<<" Now choose what mathematical operation you would like to use: '+','-','*','/' ";
cin>>ch;
if(ch=='+'){
}
else if(ch=='-'){
}
else if(ch=='*'){
}
else if(ch=='/'){
}
cout<<"Now choose your second value";
cin>>i;
}
if(ch=='b')
{
cout<<"You selected Boolean /n";
cout<<"Which Boolean operation you would like to peform: AND (a) or OR (o)";
cin>>ch;
if(ch=='a')
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
cout<<"Welcome!"<<endl;
cout<<"Which type of operation would you like to carry out integer(i) or boolean(b)?"<<endl;
char input;
cin>>input;
if (input == 'i')
{
int c;
int d;
cout<<"please enter two numbers"<<endl;
cin>>c>>d;
char f;
cout<<"please enter an operation"<<endl;
cin>>f;
switch (f)
{
case '+':
cout<< c+d <<endl;
break;
case '-':
cout<< c-d <<endl;
break;
case '*':
cout<< c*d <<endl;
break;
case '/':
cout<< c/d <<endl;
break;
default:
cout<<"enter an operation";
}
}
else if (input =='b')
{
int input1;
int input2;
char x;
cout<<"What type of boolean operation are you interrested in?"<<endl;
cout<<"and(a) or or(o)"<<endl;
cin>>x;
if(x == 'a')
{
cout<<"enter two binary numbers:"<<endl;
cin>>input1>>input2;
if(input1 == 0 && input2 == 0)
cout<<"false"<<endl;
else if(input1 == 1 && input2 == 0)
cout<<"false"<<endl;
else if(input1 == 0 && input2 == 1)
cout<<"false"<<endl;
else if(input1== 1 && input2 == 1)
cout<<"true"<<endl;
}
else if(x == 'o')
{
cout<<"enter two binary numbers:"<<endl;
cin>>input1>>input2;
if(input1 == 0 || input2 == 0)
cout<<"false"<<endl;
else if(input1 == 0 || input2 == 1)
cout<<"true"<<endl;
else if(input1 == 1 || input2 == 0)
cout<<"true"<<endl;
else if(input1 == 1 || input2 == 1)
cout<<"true"<<endl;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.