PAT (Advanced) 1073. Scientific Notation (20)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main()
{
	string raw;
	cin >> raw;
	if (raw[0] == '-')
		cout << '-';
	string s(raw.begin() + 1, raw.end());
	int lens = s.length();
	string s1(s.begin(), s.begin() + 1);
	int pose = s.find('E');
	string s2(s.begin() + 2, s.begin() + pose);
	int lens2 = s2.length();
	int flag;
	if (s[pose + 1] == '-')
		flag = -1;
	else if (s[pose + 1] == '+')
		flag = 1;
	string s3(s.begin() + pose + 2, s.end());
	int exp = 0;
	int lens3 = s3.length();
	for (int i = 0; i < lens3; i++)
	{
		exp *= 10;
		exp += s3[i]-'0';
	}
	if (exp == 0)
		cout << s1 << "." << s2;
	else
	{
		if (flag == 1)
		{
			if (exp == lens2)
				cout << s1 << s2;
			else if (exp > lens2)
			{
				cout << s1 << s2;
				int zeros = exp - lens2;
				for (int i = 0; i < zeros; i++)
					cout << "0";
			}
			else
			{
				cout << s1;
				for (int i = 0; i < exp; i++)
					cout << s2[i];
				cout << ".";
				for (int i = exp; i < lens2; i++)
					cout << s2[i];
			}
		}
		else
		{
			cout << "0.";
			int zeros = exp - 1;
			for (int i = 0; i < zeros; i++)
				cout << "0";
			cout << s1 << s2;
		}
	}
	cout << endl;
	return 0;

}


你可能感兴趣的:(PAT (Advanced) 1073. Scientific Notation (20))