Source Code
copy
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string s, sa;
cout << "\n\n\n -----> Welcome to our calculater <-----" << endl
<< "-----> Write source-code of Calculator in C++ by SA Coder. Website = https://www.sacoder.com/ <-----" << endl
<< "--x-----x-----x-----x-----x-----x-----x-----x----x--\n\n\n"
<< " Proform any arithmetical operation( / , * , + , - )" << endl
<< " If you want to exit type 'z'" << endl
<< " Type 'clear' for remove previous data" << endl
<< "--x-----x-----x-----x-----x-----x-----x-----x----x--\n\n\n";
cout << ")-->";
while (1)
{
try
{
getline(cin, s);
sa.append(s);
if (s == "clear")
{
sa.clear();
cout << ")-->";
continue;
}
else if (s == "z")
{
exit(0);
}
else
{
s.clear();
}
for (int i = 0; i < (sa.length()); i++)
{
int k = -1;
int cou[2];
for (int j = 0; j < (sa.length()); j++)
{
if (sa.at(j) == '(' || sa.at(j) == '{' || sa.at(j) == '[')
{
k = j;
cou[0] = j;
}
}
for (; k < sa.length() && k != -1; k++)
{
if (sa.at(k) == ')' || sa.at(k) == '}' || sa.at(k) == ']')
{
cou[1] = k;
i = 0;
break;
}
}
if (k != -1)
{
s = sa.substr(cou[0] + 1, (cou[1] - cou[0]) - 1);
sa.erase(cou[0], (cou[1] - cou[0]) + 1);
}
else
{
s = sa;
sa.clear();
cou[0] = 0;
}
for (int j = 0; j < (s.length()); j++)
{
float num[2];
int del[2], i, k;
if (s.at(j) == '/')
{
{
k = j - 1;
for (int l = 0; (k != -1) && (s.at(k) != '*') && (s.at(k) != '/'); k--)
{
if (s.at(k) != ' ')
{
l = 1;
}
i = k;
if ((s.at(k) == ' ') && (l == 1))
{
break;
}
if ((s.at(k) == '+') || (s.at(k) == '-'))
{
break;
}
}
del[0] = i;
num[0] = stof(s.substr(i, (j - i)));
}
{
k = j + 1;
for (int a = 0; k < (s.length()) && (s.at(k) != '*') && (s.at(k) != '/'); k++)
{
if (s.at(k) != ' ')
{
if ((a != 0) && ((s.at(k) == '-') || (s.at(k) == '+')))
{
break;
}
a++;
}
i = k;
del[1] = i;
}
num[1] = stof(s.substr(j + 1, i - j));
}
float sum = num[0] / num[1];
s.erase(del[0], (del[1] - del[0]) + 1);
s.insert(del[0], " ");
s.insert(del[0], to_string(sum));
s.insert(del[0], " ");
j = 0;
}
}
for (int j = 0; j < (s.length()); j++)
{
float num[2];
int del[2], i, k;
if (s.at(j) == '*')
{
{
k = j - 1;
for (int l = 0; (k != -1) && (s.at(k) != '*') && (s.at(k) != '/'); k--)
{
if (s.at(k) != ' ')
{
l = 1;
}
i = k;
if ((s.at(k) == ' ') && (l == 1))
{
break;
}
if ((s.at(k) == '+') || (s.at(k) == '-'))
{
break;
}
}
del[0] = i;
num[0] = stof(s.substr(i, (j - i)));
}
{
k = j + 1;
for (int a = 0; k < (s.length()) && (s.at(k) != '*') && (s.at(k) != '/'); k++)
{
if (s.at(k) != ' ')
{
if ((a != 0) && ((s.at(k) == '-') || (s.at(k) == '+')))
{
break;
}
a++;
}
i = k;
del[1] = i;
}
num[1] = stof(s.substr(j + 1, i - j));
}
float sum = num[0] * num[1];
s.erase(del[0], (del[1] - del[0]) + 1);
s.insert(del[0], " ");
s.insert(del[0], to_string(sum));
s.insert(del[0], " ");
j = 0;
}
}
for (int j = 0; j < (s.length()); j++)
{
float num[2];
int del[2], i, k;
if (s.at(j) == '+')
{
{
k = j - 1;
for (int l = 0; (k != -1) && (s.at(k) != '*') && (s.at(k) != '/'); k--)
{
if (s.at(k) != ' ')
{
l = 1;
}
i = k;
if ((s.at(k) == ' ') && (l == 1))
{
break;
}
if ((s.at(k) == '+') || (s.at(k) == '-'))
{
break;
}
}
del[0] = i;
num[0] = stof(s.substr(i, (j - i)));
}
{
k = j + 1;
for (int a = 0; k < (s.length()) && (s.at(k) != '*') && (s.at(k) != '/'); k++)
{
if (s.at(k) != ' ')
{
if ((a != 0) && ((s.at(k) == '-') || (s.at(k) == '+')))
{
break;
}
a++;
}
i = k;
del[1] = i;
}
num[1] = stof(s.substr(j + 1, i - j));
}
float sum = num[0] + num[1];
s.erase(del[0], (del[1] - del[0]) + 1);
s.insert(del[0], " ");
s.insert(del[0], to_string(sum));
s.insert(del[0], " ");
j = 0;
}
}
for (int j = 0, t = 0; j < (s.length()); j++)
{
float num[2];
int del[2], i, k;
if (s.at(j) != ' ')
{
if (t == 0 && s.at(j) == '-')
{
t = 1;
continue;
}
t = 1;
}
if (s.at(j) == '-')
{
{
k = j - 1;
for (int l = 0; (k != -1) && (s.at(k) != '*') && (s.at(k) != '/'); k--)
{
if (s.at(k) != ' ')
{
l = 1;
}
i = k;
if ((s.at(k) == ' ') && (l == 1))
{
break;
}
if ((s.at(k) == '+') || (s.at(k) == '-'))
{
break;
}
}
del[0] = i;
num[0] = stof(s.substr(i, (j - i)));
}
{
k = j + 1;
for (int a = 0; k < (s.length()) && (s.at(k) != '*') && (s.at(k) != '/'); k++)
{
if (s.at(k) != ' ')
{
if ((a != 0) && ((s.at(k) == '-') || (s.at(k) == '+')))
{
break;
}
a++;
}
i = k;
del[1] = i;
}
num[1] = stof(s.substr(j + 1, i - j));
}
float sum = num[0] - num[1];
s.erase(del[0], (del[1] - del[0]) + 1);
s.insert(del[0], " ");
s.insert(del[0], to_string(sum));
s.insert(del[0], " ");
j = 0;
t = 0;
}
}
sa.insert(cou[0], s);
s.clear();
}
cout << ")-->" << sa;
}
catch (const std::exception &e)
{
// std::cerr << e.what() << '\n';
cout << " ------> Wrong Input <------ \n";
}
}
return 0;
}
Explain
This is a C++ program that implements a basic calculator. It takes input from the user and evaluates arithmetic expressions containing the operators: addition, subtraction, multiplication, and division. The program also supports the use of parentheses in the expression.
The program uses several C++ libraries such as iostream, string, and cstring. The iostream library is used to take input and display output to the user through the console. The string library is used to handle the input string from the user and perform string manipulations such as substring operations. Finally, the cstring library is used to manipulate character arrays.
The program uses a while loop to continuously take input from the user and evaluate the expression until the user decides to exit by typing 'z'. Within the while loop, the program uses try-catch blocks to handle exceptions and ensure the program does not crash in the case of invalid inputs.
The program first displays a welcome message to the user and provides instructions on how to use the calculator. The user can input an arithmetic expression containing any of the four basic arithmetic operators and the program will evaluate the expression and display the result. The user can also use the keyword 'clear' to remove the previous data and start afresh.
The program also handles the use of parentheses in the input expression by first searching for the first opening bracket and finding the corresponding closing bracket. It then evaluates the expression inside the parentheses and replaces the parentheses and its contents with the result.
Overall, this program is a basic implementation of a calculator and can be used for simple arithmetic operations. However, it does not support more complex mathematical operations or functions.
Click on the Previous button for more explanation.