AC代码:
#include<iostream>
#include<string>
#include<cmath>
#include<iomanip>
#include<bits/stdc++.h>
using namespace std;
int count(char a[])
{
int cnt = 0;
for (int i = 0; a[i] != '\0'; i++)
{
if (a[i] > '0'&&a[i] <= '9')
{
cnt++;
}
}
return cnt;
}
void trans(int num)
{
cout << 1 << "/" << num;
}
double reverse(int num)
{
return(1.0 / num);
}
int main()
{
char a[300] = { 0 };
while (cin.getline(a,300))
{
bool flag = false;//初始时flag设为0,一旦有一个数,转为true
double sum = 0;
int k = count(a);//k为字符串中非0的数字个数
for (int i = 0; a[i] != '\0'; i++)
{
if (a[i] > '0'&&a[i] <= '9')
{
flag = true;
a[i] = a[i] - '0';
trans(a[i]);
sum += reverse(a[i]);
k--;
if (k>=1)cout << "+";
if(k==0) cout << "=";
}
}
if(flag)cout <<fixed<<setprecision(2)<< sum << endl;
else cout << 0 << endl;//如果一个数都没有,按照题目要求输出0
}
//system("pause");
return 0;
}