1024 scientific notation, C

This is a very troublesome problem, various decimal point and sign out of the chaos to die, to see when look carefully, I deliberately use 1.1 to 2.3 indicated six cases, in fact, only three sub-species, the first three understand the situation, the latter is the corresponding places -1 problems

#include <stdio.h>
#include <math.h>
int main()
{
	char a[20000] = {0};	//输入
	char b[20000] = {0};	//输出
	int d=0,i=2,x=0,n=0,j=0;

	scanf("%s",a);
	//printf("%s\n",a);		检验是否正确接收字符串
	while( a[n]!='E')	n++;	//结束后,a[n]=='E'
	while( a[n+x] )		x++;	x = x-2;	//结束后,指数部分有x位数
	while(x)	{d += (a[n+i]-'0')* pow(10,x-1);	i++;	x--;}
	if(a[n+1] == '-')	d = d * (-1);	//d是小数点要左或右移动多少位
	
	//printf("%d\n",d);		检验指数部分d是否正确
	i=0;
	if( a[0]==45 )	//1.负数,负数与正数只有第一个符号有无的区别,内容相似
	{	b[0] = a[0];
		if( d>0 )			//1.1	- +情况
		{
			b[1] = a[1];
			while( d && a[3+i] != 69 )	//69是字母'E'
			{
				b[2+i] = a[3+i];	
				i++;
				d--;
			}
			if( a[3+i] != 69 )	//要加'.'
			{
				b[2+i] = '.';	i++;
				while(a[2+i] != 69)	{b[2+i] = a[2+i];	i++;}
			}
			while( d )	//要补'0'
			{
				b[2+i] = '0';
				i++;
				d--;
			}
		}
		else if(d==0)		//1.2	- 0情况
			while(a[i] != 69)	{b[i] = a[i]; i++;}
		else				//1.3	- -情况
		{
			b[1] = '0'; b[2] = '.';	d++;
			while( d!=0 )	
			{
				b[3+i] = '0';
				d++;
				i++;
			}
			b[3+i] = a[1];	i++;
			while( a[3+j] != 69 )
			{
				b[3+i] = a[3+j];
				i++;
				j++;
			}
		}
	}

	else		//2.正数
	{
		if( d>0 )			//2.1	+ +情况
		{
			b[0] = a[1];
			while( d && a[3+i] != 69 )
			{
				b[1+i] = a[3+i];	
				i++;
				d--;
			}
			if( a[3+i] != 69 )	
			{
				b[1+i] = '.';	i++;
				while(a[2+i] != 69)	{b[1+i] = a[2+i];	i++;}
			}
			while( d )
			{
				b[1+i] = '0';
				i++;
				d--;
			}
		}
		else if(d==0)		//2.2	+ 0情况
			while(a[i+1] != 69)	{b[i] = a[i+1]; i++;}
		else				//2.3	+ -情况
		{
			b[0] = '0'; b[1] = '.';	d++;
			while( d!=0 )	
			{
				b[2+i] = '0';
				d++;
				i++;
			}
			b[2+i] = a[1];	i++;
			while( a[3+j] != 69 )
			{
				b[2+i] = a[3+j];
				i++;
				j++;
			}
		}
	}
	printf("%s\n",b);
	
	return 0;
}
Published 44 original articles · won praise 0 · Views 881

Guess you like

Origin blog.csdn.net/weixin_43916400/article/details/104415221
Recommended