1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
//Assignment 3: Math
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main ()
{
string operation;
cout<<"Please choose an operation: "<<endl;
cin>>operation;
if ((operation=="addition") && (operation=="subtraction")
&& (operation=="division") && (operation == "multiplication")
&& (operation =="pythagorean"))
{
double n1,n2,quotient,sum,difference,product,c;
cout << "Please enter your first number and second number seperated by a space: "<<endl;
cin >> n1 >>n2;
cout<<""<<endl;
if (operation=="addition")
{
sum=n1+n2;
cout<< "Equation : "<< n1 <<" + "<< n2 <<endl;
cout<< "Result: " << sum <<endl;
}
else if (operation=="subtraction")
{
difference=n1-n2;
cout<< "Equation : "<< n1 <<" - "<< n2 <<endl;
cout<< "Result: " << difference <<endl;
}
else if (operation == "multiplication")
{
product=n1*n2;
cout<<"Equation : "<< n1 <<" * "<< n2 <<endl;
cout<<"Result: " << product <<endl;
}
else if (operation =="pythagorean")
{
c=sqrt(n1*n1+n2*n2);
cout<<"Equation: c = sqrt( "<<n1<<"^2"<< " + "<<n2<<"^2 )"<<endl;
cout<< "Result: "<<c<<endl;
}
else if ((operation=="division") && (n2==0))
{
quotient = n1/n2;
cout<< "Equation : " << n1 <<" / "<< n2 <<endl;
cout<< "Error: Cannot divide by zero."<<endl;
}
else
{
quotient = n1/n2;
cout << "Equation : "<< n1 << " / " << n2 <<endl;
cout << "Result : "<<quotient<<endl;
}
}
else if ((operation == "fabs" )&& (operation =="square_root"))
{
double n1,absolute_value,square;
cout <<"Please enter a number: "<<endl;
cin>>n1;
cout<<""<<endl;
if (operation == "fabs")
{
absolute_value=fabs(n1);
cout<<"Equation: fabs "<<"("<<n1<<")"<<endl;
cout<<"Result: "<<absolute_value<<endl;
}
else if ((operation =="square_root")&& (n1 < 0))
{
cout<<"Cannot take square root of negative number."<<endl;
}
else
{
square=sqrt(n1);
cout<<"Equation: squrt( "<<n1<<" )"<<endl;
cout<<"Result: "<<square<<endl;
}
}
else if (operation =="quadratic")
{
double a,b,c,x1,x2;
cout<<"Please enter your first, second, and third number each seperated by a space: "<<endl;
cin>>a>>b>>c;
cout<<""<<endl;
if (operation =="quadratic") && (c > 0)
{
cout<<"Cannot take square root of negative number."<<endl;
}
else
{
x1=-b+sqrt(b*b-4*a*c)/2*a;
x2=-b-sqrt(b*b-4*a*c)/2*a;
cout<<"Equation: "<<a<<"x^2"<<" + "<<b<<"x"<<" + "<<c<< " = "<<" 0 "<<endl;
cout<<"Result: "<<x1<<" , "<<x2<<endl;
}
}
else
{
cout<<"Operation not supported"<<endl;
}
return 0;
}
|