A1073_B1024 Scientific Notation (20 分)

#include <iostream>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    
    
	
	char str[10010];
	
	cin.getline(str, 10010);
	int len = strlen(str);
	char c = str[0];
	
	if(c == '-'){
    
    
		cout << "-";
	} 
	
	int pos = 0; //位置 
	
	while(str[pos] != 'E'){
    
    
		pos++;
	}
	
	int exp = 0; //指数 
	
	for(int i = pos+2; i < len; i++){
    
    
		exp = exp * 10 + (str[i] - '0');
	}
	int n = exp;
	//指数为负数 
	if(str[pos+1] == '-'){
    
    
		cout << "0.";
		for(int i = 0; i < exp - 1; i++){
    
    
			cout << "0";
		}
		for(int i = 1; i < pos; i++){
    
    
			if(str[i] != '.'){
    
    
				cout << str[i];
			}
		}
	} else {
    
    
		int m = 1;
		for(int i = 1; i < pos && i < n+3; i++){
    
    
			m++;
			if(str[i] != '.'){
    
    
				cout << str[i];
				exp--;
			}
		}
		exp++;
		
		//考虑补0的情况 
		if(exp > 0){
    
    
			for(int i = 0; i < exp; i++){
    
    
				cout << "0";
			}
		}
		 
		if(pos - n - 3 > 0){
    
    
			cout << ".";
			for(int i = 0; i < pos - n - 3; i++){
    
    
				cout << str[i+m];
			}
		}
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/alovelypeach/article/details/114267966