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
|
#include <iostream>
#include <cmath>
using namespace std;
void main() {
double number, number1, sum = 0.0, sum1 = 0.0;
char X, operation;
while (operation != 'X')
{
cout<<"Enter the operation you wish to compute:\n+ for addition\n- for subtraction\n^ to raise a number to a power\ns for square root\n* for multiplication\n/ for division\nenter 'X' to exit the program\n";
cin>>operation;
switch (operation)
{
case '+': cout<<"Enter the number you wish to add, enter 'X' to finish: ";
cin>>number;
while (number=X) {
break;
}
while (number != X) {
sum = sum1 + number;
cout<<"Enter the number you wish to add, enter 'X' to finish: ";
cin>>number;
}
cout<<"The sum of the numbers you entered is "<<sum<<".\n";
break;
case '-': cout<<"Enter the number you wish to subtract from, enter 'X' to finish: ";
cin>>number1;
while (number != X) {
sum = number1-number;
cout<<"Enter the number you wish to subtract, enter 'X' to finish: ";
cin>>number;
}
cout<<"The difference of the numbers you entered is "<<sum<<".\n";
break;
case '*': cout<<"Enter the number you wish to multiply, enter 'X' to finish: ";
cin>>number1;
while (number!= X) {
sum= number1*number;
cout<<"Enter the number you wish to multiply, enter 'X' to finish: ";
cin>>number;
}
cout<<"The product of the numbers you entered is "<<sum<<".\n";
break;
case '/': cout<<"Enter the number you wish to divide, enter 'X' to finish: ";
cin>>number1;
while (number!= X && number1!= X && number!=0) {
sum=number1/number;
cout<<"Enter the number you wish to divide by, enter 'X' to finish: ";
cin>>number;
}
cout<<"The quotient of the numbers you entered is "<<sum<<".\n";
break;
case '^': cout<<"Enter the number you wish to raise to a power, enter 'X' to finish: ";
cin>>number1;
while (number1!= X && number!= X) {
sum= pow(number1, number);
cout<<"Enter the power you wish to raise the value to, enter 'X' to finish: ";
cin>>number;
}
cout<<"The value of "<<number1<<"to the "<<number<<" is "<<sum<<".\n";
break;
case 's':
case 'S': cout<<"Enter the number you wish to take the square root of, enter 'X' to finish: ";
cin>>number;
while (number!= X) {
sum= sqrt(number);
}
cout<<"The square root of "<<number<<" is "<<sum<<".\n";
break;
case 'x':
case 'X': break;
default: cout<<"Are you sure this is what you want to do?";
break;
}
}
}
|