码迷,mamicode.com
首页 > 其他好文 > 详细

PAT1073. Scientific Notation

时间:2015-02-17 21:03:54      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

Scientific notation is the way that scientists easily handle very large numbers or very small numbers.  The notation matches the regular expression [+-][1-9]"."[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent‘s signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input Specification:

Each input file contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent‘s absolute value is no more than 9999.

Output Specification:

For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros,

Sample Input 1:

+1.23400E-03

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000

思路:多考虑几种可能性,本次编程最深的体会就是一定要仔细上分析后在进行编程,这样会更加顺利很多。
技术分享
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     string str;
 8     cin>>str;
 9     if(str[0]==-)
10     {
11         cout<<"-";
12     }
13     str.erase(str.begin());
14     int e=0;
15     int pt;
16     int i;
17     for(i=0;i<str.length();i++)
18     {
19          if(str[i]==E)
20          {
21             break;
22         }
23     }
24     str.erase(str.begin()+i);
25     bool positive=false;
26     if(str[i]==+)
27     {
28         positive=true;
29     }
30     str.erase(str.begin()+i);
31     while(i<str.length())
32     {
33         e=e*10+str[i]-0;
34         str.erase(str.begin()+i);
35     }
36     for(int i=0;i<str.length();i++)
37     {
38         if(str[i]==.)
39         {
40             pt=i;
41             break;
42         }
43     }
44     str.erase(str.begin()+pt);
45     if(positive)
46     {
47         while(e>0&&pt<str.length())
48         {
49             e--;
50             pt++;
51         }
52         if(e==0&&pt<str.length())  //有可能是正好小数点的情况 
53         {
54             str.insert(str.begin()+pt,.);
55         }
56         else
57         {
58             while(e--)
59             {
60                 str.insert(str.end(),0);
61             }
62         }
63     }
64     else
65     {
66         while(e>0&&pt>0)
67         {
68             e--;
69             pt--;
70         }
71         if(e==0)
72         {
73             if(pt==0)
74             {
75                str.insert(str.begin(),.);
76                str.insert(str.begin(),0);
77             }
78             else
79             {
80               str.insert(str.begin(),.);
81             }
82         }
83         else
84         {
85             while(e--)
86             {
87                 str.insert(str.begin(),0);
88                 
89             }
90             str.insert(str.begin(),.);
91             str.insert(str.begin(),0);
92         }
93     }
94     cout<<str<<endl;
95 
96     return 0;
97 }
View Code

 

PAT1073. Scientific Notation

标签:

原文地址:http://www.cnblogs.com/GoFly/p/4295447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!