当前位置:网站首页>Pat class B 1024 scientific notation C language

Pat class B 1024 scientific notation C language

2022-06-23 05:58:00 Octopus bro

1024. Scientific enumeration (20)


Scientific counting is a convenient way for scientists to express large or small numbers , It satisfies the regular expression [+-][1-9]"."[0-9]+E[+-][0-9]+, That is, the integer part of a number is only 1 position , The decimal part at least has 1 position , The sign of this number and its index part must be clearly given even if it is positive .

Now give real numbers in the form of scientific counting A, Please write a program to output in the normal digital representation A, And ensure that all valid bits are reserved .

Input format :

Each input contains 1 Test cases , That is, a real number represented by scientific counting A. The storage length of this number shall not exceed 9999 byte , And the absolute value of its index does not exceed 9999.

Output format :

For each test case , Output... In one line in the normal digital representation A, And ensure that all valid bits are reserved , Include at the end 0.

sample input 1:
+1.23400E-03
sample output 1:
0.00123400
sample input 2:
-1.2E+10
sample output 2:
-12000000000


Ideas :

1. First, the symbol of the whole number is output according to the sign bit of the base number , If it is a negative sign, output ‘-’, If positive, no output

2. Find... In the input E, Intercept the following index

3. Judge whether the index is positive or negative :

     (1) If the exponent is negative , The corresponding number of... Is output before the base according to the index size 0, Output finished 0 Then output the base number , Remember to skip the decimal point in the base ; if -0. Then the base number is directly output

     (2) If exponent is positive , If the exponent is less than the number of digits after the decimal point in the base , If yes, the decimal point will be shifted to the right , Then output the remaining base . If the exponent is greater than the number of digits after the decimal point in the base , be

               After the base number is output, the remaining 0; If the index is +0, Then the base number is directly output



One 、 Starting variable

1.num【10000】 Accept string

2.e_mark, Used to record... In the input string E The location of , So as to distinguish the index from the base

3.index_flag, The sign bit used to record the index

4.index_ch, Used to record index string

5.index, Used to record that index_ch The integer form of the transformed exponent

Two 、 Code

#include "stdio.h"
#include "math.h"
#include "string.h"
int main()
{
	char num[10000];
	scanf("%s",num);
	int i;
	
	// If it's negative , Print symbols directly ; 
	if(num[0] == '-')
	{
		printf("-");
	}
	
	// find E The subscript , Used to split base and index 
	int e_mark;
	for(i = 1; i < strlen(num); i++)
	{
		if(num[i] == 'E')
		{
			e_mark = i;
			break;
		}		
	}// When the loop is complete , The following table is located at E The location of  
	
	i++;// Move the subscript to the sign bit of the exponent  
	
	// Record the exponent sign bit  
	char index_flag = num[i];
	
	// Get index string  
	char index_ch[5];// The index has at most 4 position , Add one bit of storage \0 
	int j = 0;
	for(i = i + 1; i < strlen(num); i++)// Initialize to i+1, the i Move to the first digit of the index  
	{
		index_ch[j] = num[i];
		j++;
	}
	
	// Converts an index string to an integer index  
	int index = 0;
	// Initial index weight , That is, the exponent length minus one , For example, the index is 1234, be 1 Weight is 10 Of (4-1) Power  
	int index_right = strlen(index_ch) - 1;
	for(i = 0; i < strlen(index_ch); i++)
	{
		index += (index_ch[i] - '0') * pow(10,index_right);
		index_right--;
	}// So far, the index has been calculated  
	
	if(index_flag == '-')// The discussion index is - When  
	{
		if(index != 0)
		{
			printf("0.");// As long as the index is less than 0, It will produce ‘0.’ 
			for(i = 0; i < index - 1; i++)// Because... Has been output “0.” 了 , Therefore, less than one zero is output  
			{
				printf("0");
			} 
			for(i = 1; i < e_mark; i++)// Output num The base of , from 1 Start ,0 Sign bit with base bit , Has just started to output  
			{
				if(i == 2)// Skip the decimal point of scientific notation  
				{
					continue;
				}
				printf("%c",num[i]);
			}
		}
		else if(index == 0)// Index is 0 
		{
			for(i = 1; i < e_mark; i++)// Output num The content in  
			{
				printf("%c",num[i]);
			}
		}

	}
	else if(index_flag == '+')
	{
		if(index != 0)
		{
			if(index < e_mark - 3)// The number of digits whose exponent is less than the base after the decimal point  
			{
				for(i = 1; i < e_mark ; i++) 
				{
					if(i == 2)// Skip decimal point  
					{
						continue;
					}
					else if(i == 2 + index)// When the index is 0 Output decimal point at the position of  
					{
						printf("%c",num[i]);
						printf("."); 
					}
					else 
					{
						printf("%c",num[i]);
					}
				
				}
			}
			else if(index == e_mark - 3) // The exponent is equal to the base digit after the decimal point  
			{
				for(i = 1; i < e_mark ; i++)
				{
					if(i == 2)// Skip decimal point  
					{
						continue;
					}
					else 
					{
						printf("%c",num[i]);
					}
				
				}
			}
			else if(index > e_mark - 3)// The exponent is greater than the base digit after the decimal point  
			{
				for(i = 1; i < e_mark; i++)
				{
					if(i == 2)// Skip base decimal point  
					{
						continue;
					}
					else 
					{
						printf("%c",num[i]);
					}
				}
				// Because the exponent is greater than the base digit after the decimal point , After the base number is output, you need to output 0 
				for(i = 0; i < index - (e_mark - 3); i++)
				{
					printf("0");
				}
			}
		}
		else if(index == 0)// Index is 0 
		{
			for(i = 1; i < e_mark; i++)// Output num The content in  
			{
				printf("%c",num[i]);
			}
		}
		
	}
	
	
	return 0;
}


原网站

版权声明
本文为[Octopus bro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230410347507.html