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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#include <iostream>
#include <cmath>
#include <cstdlib>
#include<iomanip>
#include <ctime>
using namespace std;
void menu();
float add(float&);
float subtract(float&);
float divide(float&, float&);
float multiply(float&);
float sqcube(float&);
float sin(float);
int fact(int);
int main() {
int choice;
cout << "================================================================================================\n"
<< "======================================================================================\n \n \n \n \n";
menu();
cout << "Pick an operation from the menu: "; cin >> choice;
float x, y, n, sum, tsum, vsum, pro, div, result;
char cont, sq;
sum = 0, tsum = 0, vsum = 1;
switch (choice)
{
case 0: {
add(sum);
cout << sum << " ";
break;
}
case 1: {
subtract(tsum);
cout << tsum << " ";
break;
}
case 2: {
divide(x, y);
div = divide(x, y);
cout << div << " ";
break;
}
case 3: {
sqcube(result);
cout << result << " ";
break;
}
}
}
void menu() {
using namespace std;
cout << " -------------Basic Calculator--------- \n";
cout << " Choose a number on the menu \n";
cout << "0. Add \n";
cout << "1.Subtract\n";
cout << "2.Divide\n";
cout << "3.Multiply\n";
cout << "4.Modulus\n";
cout << "5.Square or cube\n";
cout << "6.Any power\n";
cout << "7.Square root of a number\n";
cout << "8. Sin\n";
cout << "9. Cos\n";
cout << "10. Exit\n";
}
float add(float& sum) {
int n, x;
sum = 0;
cout << "How many numbers do you want to add?: "; cin >> n;
cout << "Enter " << n << " numbers\n";
for (int i = 1; i <= n; ++i) {
cin >> x;
sum += x;
}
return sum;
}
float subtract(float& sum) {
int n, x;
sum = 0;
cout << "How many numbers do you want to subtract?: "; cin >> n;
cout << "Enter " << n << " numbers\n";
for (int i = 1; i <= n; ++i) {
cin >> x;
sum -= x;
}
return sum;
}
float multiply(float& product) {
int n, x;
product = 1;
cout << "How many numbers do you want to multiply?: "; cin >> n;
cout << "Enter " << n << " numbers\n";
for (int i = 1; i <= n; ++i) {
cin >> x;
product *= x;
}
return product;
}
float divide(float& a, float& b) {
int choose;
cout << "Enter two numbers of your choice \n";
cout << "Number 1: "; cin >> a;
cout << "Number 2: "; cin >> b;
cout << "Options:\n"
<< "1. divide num 1/num2"
<< "2. divide num 2/num1"; cin >> choose;
if (choose == 1) {
return (a / b);
}
else if (choose == 2) {
return (b / a);
}
else {
cout << "You can only choose 1 or 2. Enter again: "; cin >> choose;
}
}
float sqcube(float& output) {
float x;
char sc;
cout << "Enter your number to square or cube: "; cin >> x;
cout << "Do you want to square or cube? type 's' for square, 'c' for cube: "; cin >> sc;
if (sc == 's') {
output = x * x;
}
else if (sc == 'c') {
output = x * x * x;
}
else {
cout << "Invalid character\n";
}
return output;
}
float sin(float x) {
int i, j, n, fact, sign = -1;
float p, sum = 0;
cout << "Enter the value of x : ";
cin >> x;
cout << "Enter the value of n : ";
cin >> n;
for (i = 1;i <= n;i += 2)
{
p = 1;
fact = 1;
for (j = 1;j <= i;j++)
{
p = p * x;
fact = fact * j;
}
sign = -1 * sign;
sum += sign * p / fact;
}
cout << "sin " << x << "=" << sum;
}
int fact(int n) {
int x, output = 1;
for (int i = 1; i < x; ++i) {
output *= i;
}
return output;
}
|