PAT乙级1024 科学计数法 (20 分)测试点4

https://pintia.cn/problem-sets/994805260223102976/problems/994805297229447168

测试点4
测试用例:
+1.23456E+03

本题未考察指数为0的情况

#include 
using namespace std;
int main(){
	string tmp;
	int e = 0, flag = 0, subE, subN, cnt = 0;
	cin >> tmp;
	for(int i = 0; i < tmp.size(); i++){
		if(tmp[i]=='.')
			subN = i;
		else if(tmp[i] == 'E'){
			subE = i;
			for(int j = i+2; j < tmp.size(); j++)
					e = 10*e + tmp[j]-'0';
			if(tmp[i+1] == '+')
				flag = 1;
			break;	 
		}
	}
	if(tmp[0] == '-')
		cout << '-';
	//指数为负 
	if(!flag && e){
		cout << "0.";
		e -= 1;
		for(int i = 0; i < e; i++)
			cout << "0";
		for(int i = 1; i < tmp.size(); i++){
			if(tmp[i] == 'E')
				break;
			else if(tmp[i] == '.');
			else
				cout << tmp[i];
		}
	}
	//指数为正 
	else if(flag && e){
		for(int i = 1; i < tmp.size(); i++){
			if(tmp[i] == '.'){
				for(int j = i+1; j < subE; j++){
                    if(cnt == e)
						cout << ".";
					cnt++;
					cout << tmp[j];
				}
				break;
			}
			else
				cout << tmp[i];
		}
		for(int i = 0; i < e-cnt; i++)
			cout << "0";
	}
    //指数为0,但是未考察
	else{
		for(int i = 1; i < subE; i++)
			cout << tmp[i];
	}
	return 0;
}

你可能感兴趣的:(PAT乙级)