Today, I will tell you how to make a basic calculator in c++. Well, you need to know about "WHAT IS A BASIC CALCULATOR". A basic calc (A short form of calculator) is for calculating +, -, *, /. So, copy or remember this code because it's really easy and you don't need any more library. See the code below 👇 in the rectangle box. I've test it on my computer, the worst computer ever :) and success.
The code may 10% not work on other program, so I strongly recommended you install Code::Block. It's completely free and suitable for week computer.
Please feel free to command below if the code does not work. Like this: Visual Studio C++ - Not work
🎞 Subcribe to my youtube at: 👉 https://www.youtube.com/channel/UCvpqlnV29UZks3FsiZGs75w. No video yet, sorry :)
#include <bits/stdc++.h>
using namespace std;
int main()
{
long double a,b; // Change long double into float or int or double, if you need the limit of the number.
cout << "Type two number here" << endl; // Change the text in the quote if you want
cin >> a >> b;
cout << "Type the type of calculator (+, -, *, /) expected" << endl; // Change the text in the quote if you want
char type; // You can change "type" into another name (Please follow the variable rules)
cin >> type;
switch (type)
{
case '+': cout << a + b; break;
case '-': cout << a - b; break;
case '*': cout << a * b; break;
case '/': cout << a / b; break;
default: cout << "EXPECTED (+, -, *, /)" << endl;
}
return 0;
}
Bình luận
Rất xin lỗi VNOJ vì đã đăng spoiler mã nguồn, nhưng trong bài đăng mình sợ nó có hơi nhiều chữ nên bỏ sang đây
Fixed: Deleted // definition