1024 Scientific notation (20 points) (C++ implementation) Test points 2, 3, 4

topic link


Test point analysis

  • Test points 2, 3, and 4 are all judging the situation where 0 is placed behind (the question is still quite tricky)
  • The example of test point 4 judgment is  +1.234564E+2    output should be  123.4564
  • The example of the judgment of test points 2 and 3 is  +1.23E+2   and the output should be  123

attach code

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;

int main(){
	string str;
	string strNumber="";
	int numE,i,number;
	char type;
	int count;
	int temp,t;
	
	cin>>str;
	if(str[0]=='-'){//正负 
		cout<<str[0];
	}
	
	numE=str.find('E');
	type=str[numE+1];
	strNumber=str.substr(numE+2,str.length()-1);//截取字符串 
	count=0;
	temp=1;
	for(i=strNumber.length()-1;i>=0;i--){//截取字符串转为int 
		count+=(strNumber[i]-'0')*temp;
		temp*=10;
	}
	
	if(type=='+'){//0放后面 
		cout<<str[1];

		for(i=3,t=0;i<numE;i++,t++){
			cout<<str[i];
			if(t==count-1&&i!=numE-1){//小数点后还有数字 
				cout<<'.';
			}
		}
		
		for(i=0;i<count-(numE-3);i++){
			cout<<0;
		}
		cout<<endl;
	}
	else{//0放前面 
		for(i=0;i<count;i++){
			cout<<0;
			if(i==0){
				cout<<'.';
			}
		}
		cout<<str[1];
		for(i=3;i<numE;i++){
			cout<<str[i];
		}
		cout<<endl;
	}
	
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44789957/article/details/123637459