calculator
NguyenAnhQuan
c_cpp
2 years ago
1.3 kB
9
Indexable
#include <bits/stdc++.h>
#define ll long long
#define LIM 1000005
#define X first
#define Y second
#define EL cout<<"\n"
using namespace std;
float num[LIM];
char msign[LIM];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
string s; cin >> s;
int c1 = 0, c2 = 0, tmp = 0;
for (int i = 0; i < (int)s.length(); i++)
{
if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
{
msign[++c2] = s[i];
num[++c1] = int(tmp);
tmp = 0;
}
else
{
tmp = tmp * 10 + int(s[i] - '0');
}
}
num[++c1] = tmp;
tmp = 0;
for (int i = 1; i <= c2; i++)
{
if (msign[i] == '*')
{
num[i] = num[i] * num[i + 1];
num[i + 1] = 1;
}
if (msign[i] == '/')
{
num[i] = num[i] / num[i + 1];
num[i + 1] = 1;
}
}
float ans = num[1];
for (int i = 1; i <= c2; i++)
{
if (msign[i] == '+')
{
ans += num[i + 1];
}
if (msign[i] == '-')
{
ans -= num[i + 1];
}
}
cout << ans;
return 0;
}
Editor is loading...
Leave a Comment