PAT basic 1024 科学计数法 (20分) C++

一、题目描述

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式 [±][1-9].[0-9]+E[±][0-9]+,即数字的整数部分只有 1 位,小数部分至少有 1 位,该数字及其指数部分的正负号即使对正数也必定明确给出。

现以科学计数法的格式给出实数 A,请编写程序按普通数字表示法输出 A,并保证所有有效位都被保留。

输入格式:
每个输入包含 1 个测试用例,即一个以科学计数法表示的实数 A。该数字的存储长度不超过 9999 字节,且其指数的绝对值不超过 9999。

输出格式:
对每个测试用例,在一行中按普通数字表示法输出 A,并保证所有有效位都被保留,包括末尾的 0。

输入样例 1:
+1.23400E-03

输出样例 1:
0.00123400

输入样例 2:
-1.2E+10

输出样例 2:
-12000000000

二、代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
//1024


int main()
{
	//对符号进行处理
		char a;
		a = getchar();
		if (a == '-')
			cout << a;
		char b = getchar();
		getchar();
		char s[10000];
		char temp = getchar();
		int pos = 0;
		while (temp != 'E')
		{
			s[pos] = temp;
			temp = getchar();
			pos++;
		}
		s[pos] = '\0';
		char a2 = getchar();
		int num;
		cin >> num;
		if (a2 == '-'&&num != 0)
		{

			num--;
			cout << "0.";
			for (int j = 0; j < num; j++)
				cout << "0";
			cout << b << s;
		}
		else
			if (a2 == '+')
			{

				cout << b;
				if (num > strlen(s))
				{
					cout << s;
					for (int j = 0; j < num - strlen(s); j++)
						cout << "0";
				}
				else
				{
					if (num != strlen(s)) 
					{
						for (int j = 0; j < num; j++)
							cout << s[j];
						cout << ".";
						for (int j = num; j < strlen(s); j++)
							cout << s[j];
					}
					else
					{
						cout << s;
					}
				}
			}
			else
			{
				cout << b << "." << s;
			}
	system("pause");
	return 0;
}

三、运行结果

在这里插入图片描述

题目合集

在这里呦~

发布了42 篇原创文章 · 获赞 0 · 访问量 747

猜你喜欢

转载自blog.csdn.net/qq_44352065/article/details/104073389