// Сделаем программу для создания калькулятора, который умеет сложить, вычитать, умножить и делить 2 числа. #include <iostream> // Включаем библиотеку #include <math.h> //для выполнения математических операции using namespace std; // Подключить пространство имен, чтобы не писать std:: перед блоками ввода и вывода void Numbers(double x ) { // Начало подпрограммы double y , z; cout << " Введите любое число " << endl; cin >> x; // Ввод любого числа y = sqrt(x) ; // формула для находки квадратного кореня числа cout << " Квадратный корень числа " << y << endl; z = cbrt(x) ; // формула для находки кубического кореня числа cout << " Кубический корень числа " << z << endl; } // Окончание подпрограммы int main() // Запуск программы { setlocale(LC_ALL, "Russian"); // Доступ к писанию кириллицы в коде double a , b , d , e ; // Обьявление перемен с плавающей точкой char c;// обьявление вспомогательной перемены – символа int f , g ; // обьявление вспомогательной перемены – строки cout << " Введите цифру от 1 до 3 " << endl; cin >> f; switch(f){ case 1: cout << " Введите первое число: " << endl; cin >> a; //Ввод а cout << " Введите второе число: " << endl; cin >> b; // Ввод b cout << " Введите '+' или '-' или '*' или '/': " << endl; cin >> c; // Ввод с switch(c) { //Включаем оператор множественного выбора – switch case '+': cout << " Сумма чисел равна: " << a + b << endl; break; // заканчиваем оператор switch case '-': cout << " Вычитание чисел равна: " << a - b << endl; break; case '*': cout << " Умножение чисел равна: " << a * b << endl; break; case '/': cout << " Деление чисел равна: " << a / b << endl; break; default: // Случай по умолчанию, если ни один из случаев не был реализован cout << " Такой арифметической операции не существует " << endl; } break; case 2: cout << " Введите угол в радианах " << endl ; // Начало тригонометрического калькулятора cin >> a; // ввод угла( в радианах ) cout << " Введите число от 1 до 4 " << endl; switch(g){ case 1: cout << " Синус угла равен " << sin(a) << endl; break; case 2: cout << " Косинус угла равен " << cos(a) << endl; break; case 3: cout << " Тангенс угла равен " << tan(a) << endl; break; cout << " Котангенс угла равен " << 1 / tan(a) << endl; break; } break; case 3: Numbers(e); // Вызов подпрограммы break; default: cout << " К сожалению, калькулятор пока не может выполнить такие функции" << endl; } return 0; // Окочание функции }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}