95分尽力局,真的找不出错误来了
查看原帖
95分尽力局,真的找不出错误来了
1089623
aouyou楼主2024/10/18 14:49

写的可能有点乱

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
string s;
string a, b;
int n1, n2;
int main()
{
	cin >> s;
	int n = s.length();
	int pos = 0;
    //判断是否为小数
	if (pos = s.find('.', 0) != -1)
	{
		if (s[0] == '0' && s[2] == '0'&&n==3)
		{
			cout << "0.0" << endl;
			return 0;
		}
		else if (s[0] == '0'&&n>3)
		{
			cout << "0.";
			pos = s.find('.', 0);
			b = b.append(s, pos + 1, n - pos - 1);
			n2 = b.length();
			int m2 = 0;
			while (b[n2-1] == '0')
			{
				n2--;
			}
			while (b[m2] == '0')
			{
				m2++;
			}
			for (int i = n2 - 1; i >= m2; i--)
			{
				cout << b[i];
			}
			return 0;
		}
		pos = s.find('.', 0);
		a = a.append(s, 0, pos);
		b = b.append(s, pos + 1, n - pos - 1);
		n1 = a.length();
		n2 = b.length();
		while (a[n1 - 1] == '0')
		{
			n1--;
		}
		for (int i = n1 - 1; i >= 0; i--)
		{
			cout << a[i];
		}
		cout << '.';
		
		if (n2 == 0&&b[0]=='0')cout << '0' << endl;
		else
		{
			int m1 = 0;
			while (b[m1] == '0')
			{
				m1++;
			}
			n2 = b.length();
			while (b[n2-1] == '0')
			{
				n2--;
			}
			for (int i = n2 - 1; i >= m1; i--)
			{
				cout << b[i];
			}
		}
	}
    //判断是否为分数
	else if (pos = s.find('/', 0) != -1)
	{
		if (s[0] == '0')
		{
			cout << "0/";
			pos = s.find('/', 0);
			b = b.append(s, pos + 1, n - pos - 1);
			n2 = b.length();
			while (b[n2 - 1] == '0')n2--;
			for (int i = n2 - 1; i >= 0; i--)
			{
				cout << b[i];
			}
			return 0;
		}
		pos = s.find('/', 0);
		a = a.append(s, 0, pos);
		b = b.append(s, pos + 1, n - pos - 1);
		n1 = a.length();
		n2 = b.length();
		while (a[n1 - 1] == '0')
		{
			n1--;
		}
		for (int i = n1 - 1; i >= 0; i--)
		{
			cout << a[i];
		}
		cout << '/';
		while (b[n2 - 1] == '0')
		{
			n2--;
		}
		for (int i = n2 - 1; i >= 0; i--)
		{
			cout << b[i];
		}
	}
    //判断是否为百分数
	else if (s[n - 1] == '%')
	{
		if (s[0] == '0')
		{
			cout << "0%" << endl;
			return 0;
		}
		a = a.append(s, 0, n - 1);
		n1 = a.length();
		while (a[n1 - 1] == '0')
		{
			n1--;
		}
		for (int i = n1 - 1; i >= 0; i--)
		{
			cout << a[i];
		}
		cout << '%';

	}
    //整数
	else
	{
		if (s == "0")
		{
			cout << "0" << endl;
			return 0;
		}
		while (s[n - 1] == '0')
		{
			n--;
		}
		for (int i = n - 1; i >= 0; i--)
		{
			cout << s[i];
		}
	}
	return 0;
}
2024/10/18 14:49
加载中...