PAT_B_1024 scientific notation

Subject description:

Scientists scientific notation is used to represent a convenient method for large or very small number, which is the regular expression [+ -]. [1-9] [0-9] + E [+ -] [0 -9] +, i.e., only the integer part of the number one, there is at least one decimal part, the digital sign and exponent parts must explicitly given of even positive. 
A real numbers are now given in scientific notation format, write program output as ordinary digital representation of Method A, and to ensure that all the valid bits are reserved. 
Input format: 
Each input comprises a test, i.e., a real number in scientific notation represented by A. This number does not store the length exceeds 9999 bytes, and the absolute value is not more than 9999 index. 
Output format: 
for each test case, in a row by general method A digital representation of the output, and to ensure that all the valid bits are retained, including 0 at the end. 

Input Sample 1: 
+ 03 1.23400E- 
Output Sample 1: 
0.00123400 
Input Sample 2: 
-1.2E + 10 
Output Sample 2: 
-12000000000

The key problem-solving:

1. Use of a digital read character string;

2. The determination of symbols, numbers do not have a positive output, the negative number must be output;

3. Discussion of the index, for the first string into an integer of the positive and negative points are calculated;

(1) index is negative, the sign bit is directly output after '0.', and then inserted between a first non-zero decimal number '0', the output of the decimal point is not necessary again;

(2) The index is positive, and the decimal point is determined between the length len E exp exponential size of the original character string, and then determines the position of the decimal point.

AC Code:

// PAT_1024_ scientific notation 
# the include <stdio.h> 
# the include <string.h> 
# the include <stdlib.h> 
# 100000 DEFINE Max 

int main (void) 
{ 
	char S [Max], S_num [Max]; 
	int 0 = I, J = 0, exp, 0 = len; 
	the gets (S); 
	// the puts (S); 
	// number of the output symbols (numbers need not output) 
	IF (S [0] == '-') 
		the printf ( "% C", S [0]); 
	the while (! S [I] = 'E') I ++; 
	// number after extracting E 
	strcpy (S_num, S + I +. 1); 
	// the puts (S_num ); 
	// converted to integer numbers 
	exp = atoi (S_num); 
	// the printf ( "% D \ n-", exp); 
	// index of positive and negative points discussed, when a negative 
	IF (exp <0) 
	{ 
		the printf ( " 0. "); 
		// output 0 after the decimal point in  
		for (j = exp; j < -1;j++) 
			the printf (" 0 ");
		// output character other than a decimal point between the outer the sign E
		for (I =. 1;! S [I] = 'E'; I ++) 
		{ 
			IF (S [I] =! '.') 
				the printf ( "% C", S [I]); 
		} 
	} 
	// index timing 
	the else 
	{ 
		// record to decimal string length between E 
		for (I = 2;! S [I] = 'E'; I ++) 
		{ 
			IF (! '.' S [I] =) 
				len ++; 
		 } 
		 length // 
		 // the printf ( "% D \ n-", len); 
		 // exponent exp and comparing the size of length len 
		 // exp> len zero padding at the end, without the decimal output 
		 IF (exp> = len) 
		 { 
		 	for ( I =. 1;! S [I] = 'E'; I ++) 
		 	{ 
		 		IF (S [I] =)! '.' 
		 			the printf ( "% C", S [I]); 
			 } 
			 for (I = 0; I <exp-len; I ++) 
			 	the printf ( "0"
		 }
		 // exp <len at the exp-bit output of the decimal point 
		 the else 
		 { 
		 	 for (I =. 1;! S [I] = 'E'; I ++) 
			 { 
			 	IF (! '.' S [I] =) 
			 		the printf ( "% C" , S [I]); 
			 	IF (I + exp == 2) 
			 		the printf () "."; 
		     } 
			  
		 } 
	} 
	 
	return 0; 
}

RRR

Guess you like

Origin www.cnblogs.com/Robin5/p/11297702.html