# 山寨版计算器

## 样例输入

1 + 2 - 1 * 4 / 5 + 1 - 1 =

## 样例输出

1.600


## 提示

1 + 2 - 1 * 4 / 5 + 1 - 1 =

#include<iostream>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
string str;
getline(cin, str);
int num = str.size();
double a[10000] = {};
char ch[10000];
int count = 0;
for (int i = 0; i < num - 1; i++)
{
if (str[i] == ' ')
count++;
}
int a_k = (count + 1) / 2 - 1;
int ch_k = (count - 1) / 2 - 1;
int a_kk = a_k, ch_kk = ch_k;
for (int i = num - 3; i >= 0;)
{
if (str[i] == ' ')
{
if (str[i + 1] == '+' || str[i + 1] == '-' || str[i + 1] == '*' || str[i + 1] == '/')
ch_k--;
i--;
}
else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
{
ch[ch_k] = str[i];
i--;
}
else
{
int flag = 0;
int k = i;
for (; k >= 0; k--)
{
if (str[k] == ' ' || k == 0)
break;
if (str[k] == '.')
{
flag = 1;
break;
}
}
if (flag)
{
k = k - i;
int j = i;
for (; j >= 0; j--)
{
if (str[j] == ' ')
break;
if (str[j] != '.')
{
a[a_k] += (str[j] - '0')*pow(10, k);
k++;
}
}
i = j - 1;
}
else
{
int j = i;
k = 0;
for (; j >= 0; j--)
{
if (str[j] == ' ')
break;
else
{
a[a_k] += (str[j] - '0')*pow(10, k);
k++;
}
}
i = j - 1;
}
a_k--;
}
}
double sum = a[0];
for (int i = 0; i <= ch_kk; i++)
{
if (ch[i] == '+')
sum += a[i + 1];
else if (ch[i] == '-')
sum -= a[i + 1];
else if (ch[i] == '*')
sum *= a[i + 1];
else if (ch[i] == '/')
sum /= a[i + 1];
}
cout << fixed << setprecision(3) << sum << endl;
return 0;
}
/**************************************************************
Problem: 1735
User: 201730685257
Language: C++
Result: 正确
Time:0 ms
Memory:1500 kb
****************************************************************/

#include<iostream>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
char ch[10000];
char s=' ';
double num = 0, sum = 0;
int i = 0, k = 0;
int flag = 0;
while ((ch[i] = getchar())!= '=')
{
if (ch[i] == ' ')
{
flag = 0;
if (ch[i - 1] == '+' || ch[i - 1] == '-' || ch[i - 1] == '*' || ch[i - 1] == '/')
continue;
num = 0;
int j = i - 1;
for (; j >= k; j--)
{
if (ch[j] == '.')
flag = 1;
}
j = j - i;
if (flag)
{
for (int a = i - 1; a >= k; a--)
{
if(ch[a]!='.')
{
num += (ch[a] - '0')*pow(10, j+3);
j++;
}
}
}
else
{
j = 0;
for (int a = i - 1; a >= k; a--)
num += (ch[a] - '0')*pow(10, j);
j++;
}

if (s == ' ' || s == '+')
sum += num;
else if (s == '-')
sum -= num;
else if (s == '*')
sum *= num;
else if (s == '/')
sum /= num;
}
else if (ch[i] == '+' || ch[i] == '-' || ch[i] == '*' || ch[i] == '/')
{
s = ch[i];
k = i + 1;
}
i++;
}
cout << fixed << setprecision(3) << sum << endl;
return 0;
}
/**************************************************************
Problem: 1735
User: 201730685257
Language: C++
Result: 正确
Time:0 ms
Memory:1496 kb
****************************************************************/

#include<iostream>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
string str;
getline(cin, str);
int num = str.size();
double a[10000] = {};
char ch[10000];
int count = 0;
for (int i = 0; i < num - 1; i++)
{
if (str[i] == ' ')
count++;
}
int a_k = (count + 1) / 2 - 1;
int ch_k = (count - 1) / 2 - 1;
int a_kk = a_k, ch_kk = ch_k;
for (int i = num - 3; i >= 0;)
{
if (str[i] == ' ')
{
if (str[i + 1] == '+' || str[i + 1] == '-' || str[i + 1] == '*' || str[i + 1] == '/')
ch_k--;
i--;
}
else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
{
ch[ch_k] = str[i];
i--;
}
else
{
int flag = 0;
int k = i;
for (; k >= 0; k--)
{
if (str[k] == ' ' || k == 0)
break;
if (str[k] == '.')
{
flag = 1;
break;
}
}
if (flag)
{
k = k - i;
int j = i;
for (; j >= 0; j--)
{
if (str[j] == ' ')
break;
if (str[j] != '.')
{
a[a_k] += (str[j] - '0')*pow(10, k);
k++;
}
}
i = j - 1;
}
else
{
int j = i;
k = 0;
for (; j >= 0; j--)
{
if (str[j] == ' ')
break;
else
{
a[a_k] += (str[j] - '0')*pow(10, k);
k++;
}
}
i = j - 1;
}
a_k--;
}
}

for(int i=0;i<=ch_kk;i++)
{
if(ch[i]=='*')
{
a[i+1]=a[i]*a[i+1];
a[i]=0;
ch[i]=='+';
}
else if(ch[i]=='/')
{
a[i+1]=a[i]/a[i+1];
a[i]=0;
ch[i]='+';
}
}
double sum = a[0];
for (int i = 0; i <= ch_kk; i++)
{
if (ch[i] == '+')
sum += a[i + 1];
else if (ch[i] == '-')
sum -= a[i + 1];
}
cout << fixed << setprecision(3) << sum << endl;
return 0;
}



• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120