1073. Scientific Notation (20)

题目:

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9]"."[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent's signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input Specification:

Each input file contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent's absolute value is no more than 9999.

Output Specification:

For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros,

Sample Input 1:
+1.23400E-03
Sample Output 1:
0.00123400
Sample Input 2:
-1.2E+10
Sample Output 2:
-12000000000

注意:
1、由于这里输入的有效数据位很长,所以不能直接用数字类型,而应该用字符串来处理。
2、代码中k是10的幂级次,我是分成k=0,k<0,k>0三种情况分别处理。
3、科学技术法中小数点肯定是在第一位有效数字的后面。
4、注意小数点的输出。
5、考虑+1.23400E+3时应该如何输出。

代码:
//1073
#include<iostream>
using namespace std;

int main()
{
	char num[10000];
	int k;//
	char positive;//'+' or '-' flag
	scanf("%c",&positive);
	int i=0;//record the effective length of num
	while(1)
	{
		scanf("%c",num+i);
		if(num[i]=='E')
			break;
		if(num[i]!='.')
			++i;
	}
	num[i]='\0';
	scanf("%d",&k);
	if(positive=='-')
		printf("%c",positive);
	if(k==0)
	{//k=0, print the original num with dot
		printf("%c.",num[0]);
		int j=1;
		while(num[j]!='\0')
			printf("%c",num[j++]);
	}
	else if(k<0)
	{//if k<0, print corresponding number of '0' before num
		printf("0.");
		for(int j=0;j<-k-1;++j)
			printf("0");
		printf("%s",num);
	}
	else
	{//if k>0, be care of dot and '0' int the back
		for(int j=0;j<i || j<=k;++j)
		{//i is the effective length of num
			if(j==k+1)
				printf(".");
			if(j>=i)//the length of effective digit is less than k
				printf("0");
			else
				printf("%c",num[j]);
		}
	}
	printf("\n");
	return 0;
}


你可能感兴趣的:(考试,pat,浙江大学)