当前位置:网站首页>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;
}边栏推荐
- Real MySQL interview question (30) -- shell real estate order analysis
- Basic calculator II for leetcode topic analysis
- The official artifact of station B has cracked itself!
- The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
- PAT 乙等 1023 组个最小数
- Implementation of linear list linked list structure
- Prometheus, incluxdb2.2 installation and flume_ Export download compile use
- 技能自检 | 想当测试Leader,这6项技能你会吗?
- Ansible 使用普通用户管理被控端
- Real MySQL interview questions (25) -- common group comparison scenarios
猜你喜欢

MDM data cleaning function development description

ant使用总结(二):相关命令说明

Real MySQL interview questions (25) -- common group comparison scenarios

Digital collections - new investment opportunities

jvm-04. Object's memory layout
![[Stanford Jiwang cs144 project] lab2: tcpreceiver](/img/70/ceeca89e144907226f29575def0e4d.png)
[Stanford Jiwang cs144 project] lab2: tcpreceiver

Data migration from dolphin scheduler 1.2.1 to dolphin scheduler 2.0.5 and data test records after migration

Centos7 deploy radius service -freeradius-3.0.13-15 EL7 integrating MySQL

The hierarchyviewer tool cannot find the hierarchyviewer location
![[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale](/img/2d/96a370c90dcb7091038afad33bc4b4.png)
[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
随机推荐
Add and multiply two polynomials using linked list
技术开发团队视角看到的数字藏品机遇与挑战
Opencv display image
Real MySQL interview questions (XXVII) -- Classification of users by RFM analysis method
jvm-02.有序性保证
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-7 personal solutions
Work accumulation - judge whether GPS is on
Centos7 installation of postgresql8.2.15 and creation of stored procedures
Android handler memory leak kotlin memory leak handling
Operating mongodb in node
ssm项目搭建
Skill self check | do you know these 6 skills if you want to be a test leader?
How to specify the output path of pig register Project Log
jvm-01.指令重排
PAT 乙等 1025 反转链表
PAT 乙等 1023 组个最小数
编址和编址单位
PAT 乙等 1012 C语言
【Cocos2d-x】可擦除的Layer:ErasableLayer
Dolphin scheduler dolphin scheduling upgrade code transformation -upgradedolphin scheduler