PAT1024 科学计数法 (20)

这个代码需要化简,

#include<iostream>
using namespace std;


char output[10010];
int  lastNumberIndex=-1;
int  pointPos=-1;//the index of the last number which before the point
int  pow;
bool afterPoint=false;
bool numberPositive=true;
bool powPositive=true;
bool powStart=false;
bool hasShowPoint=false;

int main()
{
	char temp;

	temp=cin.get();
	if(temp=='-')
	{
		numberPositive=false;
	}



	while(true)
	{
		temp=cin.get();
		if(powStart==false)
		{



			if(temp=='E')
			{
				powStart=true;
				continue;
			}

			if(temp=='.')
			{
				afterPoint=true;
				continue;
			}


			output[++lastNumberIndex]=temp;

			if(!afterPoint)
			{
				pointPos++;
			}

		}
		else
		{
			if(temp=='\n')
			{
				break;
			}
			if(temp=='-')
			{
				powPositive=false;
				continue;
			}

			if(temp=='+')
			{
				continue;
			}
			pow=pow*10+temp-'0';

		}

	}


	//display
	if(!numberPositive)
	{
		cout<<'-';
	}

	if(powPositive)
	{
		for(int i=0;i<=lastNumberIndex;i++)
		{
			if(i>pointPos)
			{
				if(pow>0)
				{
					pow--;
				}
				else if(!hasShowPoint)
				{
					cout<<'.';
					hasShowPoint=true;
				}
			}
			cout<<output[i];


		}
		while(pow>0)
		{
			cout<<'0';
			pow--;
		}
		hasShowPoint=true;

	}
	else
	{

		if(pointPos<pow)
		{
			int diff=pow-pointPos;
			cout<<"0.";
			hasShowPoint=true;
			for(int i=0;i<diff-1;i++)
			{
				cout<<0;
			}


			for(int i=0;i<=lastNumberIndex;i++)
			{
				cout<<output[i];

			}
		}
		else
		{

			pointPos=pointPos-pow;
			for(int i=0;i<=lastNumberIndex;i++)
			{
				cout<<output[i];
				if(i==pointPos&&!hasShowPoint)
				{
					hasShowPoint=true;
					cout<<".";
				}
			}

		}

	}
	cout<<endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值