1. Write a C++ program that will display the calculator menu.
The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks the user to input two integer vales for the calculation. See the sample below.MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5. Modulus
Enter your choice: 1
Enter your two numbers: 12 15
Result: 27
Continue? y
The program also asks the user to decide whether he/she wants to continue the operation. If he/she input ‘y’, the program will prompt the user to choose the operation gain. Otherwise, the program will terminate.
2. Subtract
3. Multiply
4. Divide
5. Modulus
Enter your choice: 1
Enter your two numbers: 12 15
Result: 27
Continue? y
The program also asks the user to decide whether he/she wants to continue the operation. If he/she input ‘y’, the program will prompt the user to choose the operation gain. Otherwise, the program will terminate.
Solution:
#include <cstdlib> #include <iostream>
#include<iomanip>
using namespace std;
void displaymenu(){
cout<<"==================================================="<<"\n";
cout<<" MENU "<<"\n";
cout<<"==================================================="<<"\n";
cout<<" 1.Add"<<"\n";
cout<<" 2.Subtract"<<"\n";
cout<<" 3.Multiply"<<"\n";
cout<<" 4.Divide"<<"\n";
cout<<" 5.Modulus"<<"\n";
}
int Add(int a,int b){
return(a+b);
}
int Substract(int a, int b){
return(a-b);
}
int Multiply(int a, int b){
return(a*b);
}
float Divide(int a,int b){
return(a/b);
}
int Modulus(int a, int b){
return(a%b);
}
int main(int argc, char *argv[])
{
//show menu
displaymenu();
int yourchoice;
int a;
int b;
char confirm;
do
{
cout<<"Enter your choice(1-5):";
cin>>yourchoice;
cout<<"Enter your two integer numbers:";
cin>>a>>b;
cout<<"\n";
switch(yourchoice){
case 1:cout<<"Result:"<<Add(a,b);break;
case 2:cout<<"Result:"<<Substract(a,b);break;
case 3:cout<<"Result:"<<Multiply(a,b);break;
case 4:cout<<"Result:"<<Divide(a,b);break;
case 5:cout<<"Result:"<<Modulus(a,b);break;
default:cout<<"invalid";
}
cout<<"\nPress y or Y to continue:";
cin>>confirm;
}while(confirm=='y'||confirm=='Y');
system("PAUSE");
return EXIT_SUCCESS;
}
2. Write a C++ program (using function overloaded) to sort 10 integer values, or 10 long values, or 10 double values.
Solution:
#include <cstdlib> #include <iostream>
#include<iomanip>
#include<cmath>
using namespace std;
void mysort(int vallist[]){
int i,j;
int temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}
for(i=0;i<10;i++)cout<<vallist[i]<<"\n";
}
void mysort(float vallist[]){
int i,j;
float temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}
for(i=0;i<10;i++)cout<<vallist[i]<<"\n";
}
void mysort(double vallist[]){
int i,j;
double temp;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(vallist[j]>vallist[j+1]){
temp=vallist[j];
vallist[j]=vallist[j+1];
vallist[j+1]=temp;
}
for(i=0;i<10;i++)cout<<vallist[i]<<"\n";
}
int main(int argc, char *argv[])
{
int vallist[]={23,2,34,23,43,22,32,32,43,34};
mysort(vallist);
system("PAUSE");
return EXIT_SUCCESS; }
No comments
Post a Comment