/*CALCULATOR PROGRAM BY PIKAPIKA*/
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
#define pi 3.14159
// string str[] = { "+", "-", "*", "/", "sin", "cos", "mu", "canbac2"};
float addition(float a , float b) {
return a + b;
}
float subtraction(float a, float b) {
return a - b;
}
float multiplication(float a, float b) {
return a * b;
}
float division(float a, float b) {
return a / b;
}
double Sin(double a) {
return sin(a);
}
double Cos(double b) {
return cos(b);
}
float exponential(float a, float b) {
return pow(a, b);
}
float squareroot(float a) {
return sqrt(a);
}
/*Hàm nhập giá trị cho biến tham trị*/
float getUserInput(){
cout << "Enter a value: ";
float value;
cin >> value;
return value;
}
void build(){
cout << "This program is under construction." << endl;
}
void calculator() {
int option;
float a, b;
do {
cout << "\t MENU \t" << endl;
cout << "1. Addtiontion." << endl;
cout << "2. Subtraction." << endl;
cout << "3. Multiplication." << endl;
cout << "4. Division." << endl;
cout << "5. Sin." << endl;
cout << "6. Cos." << endl;
cout << "7. Exponential." << endl;
cout << "8. Squarefoot." << endl;
cout << "Input your selection: ";
cin >> option;
} while (option < 1 && option > 8);
switch (option) {
case 1:
a = getUserInput();
b = getUserInput();
cout << a << " + " << b << " = " << addition(a, b) << endl;
break;
case 2:
a = getUserInput();
b = getUserInput();
cout << a << " - " << b << " = " << subtraction(a, b) << endl;
break;
case 3:
a = getUserInput();
b = getUserInput();
cout << a << " * " << b << " = " << multiplication(a, b) << endl;
break;
case 4:
a = getUserInput();
b = getUserInput();
cout << a << " / " << b << " = " << division(a, b) << endl;
break;
case 5:
a = getUserInput();
cout << "Sin(" << a << ") = " << Sin(a*pi/180) << endl;
break;
case 6:
a = getUserInput();
cout << "Cos(" << a << ") = " << Cos(a*pi/180) << endl;
break;
case 7:
a = getUserInput();
b = getUserInput();
cout << "a mu b = "<< pow(a, b) << endl;
break;
case 8:
a = getUserInput();
cout << "Can bac 2 cua " << a << " = " << sqrt(a) << endl;
break;
default:
cout << "Lua chon khong hop le " << endl;
return;
}
}
int main() {
int choice;
do {
cout << "\t MENU \t" << endl;
cout << "1. Calculator Program." << endl;
cout << "2. Other program." << endl;
cout << "3. Exit" << endl;
cout << "Input your selection: ";
cin >> choice;
} while (choice < 1 || choice > 3);
switch (choice) {
case 1:
calculator();
break;
case 2:
build();
break;
case 3:
cout << "Existing..." << endl;
exit;
}
system("pause");
return 0;
}
Mình làm 2 menu cho dễ nhìn các cậu tham khảo