当前位置:网站首页>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-03sample output 1:
0.00123400sample input 2:
-1.2E+10sample 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;
}边栏推荐
- ant使用总结(二):相关命令说明
- ORB_ Slam2 operation
- gplearn出现 assignment destination is read-only
- Skill self check | do you know these 6 skills if you want to be a test leader?
- Pat class B 1022 d-ary a+b
- Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators
- Real MySQL interview question (30) -- shell real estate order analysis
- PAT 乙等 1012 C语言
- Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration
- Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
猜你喜欢

Behind the hot digital collections, a strong technical team is needed to support the northern technical team

ant使用总结(三):批量打包apk

Skill self check | do you know these 6 skills if you want to be a test leader?

Adnroid activity screenshot save display to album view display picture animation disappear

jvm-05. garbage collection

Opportunities and challenges of digital collections from the perspective of technology development team

MySQL面试真题(二十一)——金融-贷款逾期

Use of visdom

New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!

Heimdall database proxy scale out 20 times
随机推荐
Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value
Leetcode topic resolution divide two integers
PAT 乙等 1017 C语言
Pat class B 1013 C language
PAT 乙等 1015 C语言
PAT 乙等 1022 D进制的A+B
Pat class B 1010 C language
Alibaba cloud ack one and ACK cloud native AI suite have been newly released to meet the needs of the end of the computing era
Real MySQL interview question (30) -- shell real estate order analysis
如何指定pig-register项目日志的输出路径
数字藏品市场才刚刚开始
Heimdall database proxy scale out 20 times
三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会
Pat class B 1011 C language
jvm-05. garbage collection
jvm-02. Guarantee of orderliness
True MySQL interview question (24) -- row column exchange
jvm-02.有序性保证
Excel sheet column number for leetcode topic resolution