Write a program that will demonstrate some of the C++ Library Functions, Stream
ID: 3744817 • Letter: W
Question
Write a program that will demonstrate some of the C++ Library Functions, Stream Manipulators, and Selection Control Structures.
The user will be given a menu of four choices. They can input a 1 for finding Cosines, 2 for finding Logarithms, 3 for converting between Decimal and Hexadecimal, or 4 to Exit the program.
You must use the proper functions and/or stream manipulators to find the answers.
1. If the user picks the cosine, ask them if they want to find the cosine, arc cosine, or hyperbolic cosine. Then input a floating point number (in radians) and find and print to three decimal places the proper type of cosine.
2. If the user picks the logarithms, ask them if they want to find the common logarithm or the natural logarithm. Then input a floating point number and find and print to three decimal places and a plus or minus sign (for positive or negative numbers) the proper type of logarithm.
3. If the user picks the conversion, ask them if they want to convert decimal to hexadecimal or hexadecimal to decimal. If they pick decimal to hexadecimal, ask them if they want to use lowercase or uppercase letters in the printing of the hexadecimal number. Use boolean input, so input the answer as true or false and put the user's answer in a boolean alphabetic variable. Then input a whole number in the proper base. Print the inputted number in the user's base and in the converted base. Use the prefix for printing the hexadecimal numbers.
4. If the user picks the exit, use the actual exit function to end the program
Explanation / Answer
/* user will given four choices
1 - cosine
2 - logarithm
3 - conversion
*/
/* library function used
for finding cosine
- simple - cos(x)
- arc - acos(x)
- hyperbolic - cosh(x)
for logarithm
- natural - log(x)
- common - log10(x)
for precision
- fixed<<setprecision(3)
for upper/lowercase
- transform
*/
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
using namespace std;
#define PI 3.14159265
void findcosines(){
// finding cosine
int choice;
cout<<"enter 1 if you want to find simple cosine, 0 otherwise"<<endl;
cin>>choice;
cout<<fixed<<setprecision(3); // for limit the number of digits after decimal i.e. 3
if(choice == 1){
double x;
cin>>x;
// in radians
// x = x*(PI/180);
cout<<"cosine of "<<x<<" is "<<cos(x)<<" ";
}else{
cout<<"enter 1 if you want to find arc cosine, 0 otherwise"<<endl;
cin>>choice;
if(choice == 1){
double x;
cin>>x;
cout<<"cosine of "<<x<<" is "<<acos(x)<<endl;
}else{
cout<<"enter 1 if you want to find hyperbolic cosine, 0 otherwise "<<endl;
cin>>choice;
if(choice == 1){
double x;
cout<<"hyperbolic cosine of "<<x<<" is "<<cosh(x)<<endl;
}
}
}
}
void findlog(){
int choice;
double x;
cout<<"enter 1 if you want to find common logarithm, 0 otherwise "<<endl;
cin>>choice;
if(choice == 1){
cin>>x;
cout<<"common logarithm of "<<x<<" is "<<log10(x)<<" ";
}else{
cout<<"enter 1 if you want to find natural logarithm, 0 otherwise"<<endl;
cin>>x;
cout<<"natural logarithm of "<<x<<" is "<<log(x)<<endl;
}
}
void conversion(){
int choice;
bool lower_or_upper;
cout<<"enter 1 if you want to convert decimal to hex, 0 otherwise"<<endl;
cin>>choice;
if(choice == 1){
std::stringstream s;
int i;
cin>>i;
s<< std::hex << i;
std::string hexa ( s.str() );
cout<<"enter 1 if you want to print hex in lower, 0 for upper "<<endl;
cin>>lower_or_upper;
cout << "0x";
if(lower_or_upper)
transform(hexa.begin(), hexa.end(), hexa.begin(), ::tolower);
else
transform(hexa.begin(), hexa.end(), hexa.begin(), ::toupper);
cout<<"value in user's base: "<< i<<endl;
cout<<"converted hex value is: "<<hexa<<endl;
}else{
string hexa,h;
cin>>hexa;
h = hexa;
transform(hexa.begin(), hexa.end(), hexa.begin(), ::toupper);
int l = hexa.length();
int base = 1;
int deci = 0;
for (int i=l-1; i>=0; i--)
{
if (hexa[i]>='0' && hexa[i]<='9')
{
deci+= (hexa[i] - 48)*base;
base = base * 16;
}
else if (hexa[i]>='A' && hexa[i]<='F')
{
deci += (hexa[i] - 55)*base;
base = base*16;
}
}
cout<<"value in user's base: "<<h<<endl;
cout<<"converted decimal value is : "<<deci<<endl;
}
}
int main()
{
int user_choice;
cin>>user_choice;
switch(user_choice){
double result;
case 1: findcosines();
break;
case 2: findlog();
break;
case 3: conversion();
break;
default: exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.