1073. Scientific Notation (20)

Source: Internet
Author: User

The topics are as follows:

Scientific notation is the the-the-by-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 have exactly one digit, there is at least one digit in the Fractiona L portion, and the number and its exponent ' s signs is always provided even when they is positive.

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

Input Specification:

Each input file contains the one test case. There is one line containing the real number A in scientific notation. The number is no further than 9999 bytes in length and the exponent ' s absolute value are no more than 9999.

Output Specification:

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

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


The topic requires the analysis of a given scientific notation, and the output of the traditional notation representation of the number, requiring positive numbers without a plus sign, decimals retain the original suffix 0 number.

The key to this problem is to combine string's find, substr method lookup and intercept, using StringStream to convert strings to numbers.

The most important of these issues is to seize the points of the classification discussion and to deal with as few cases as possible .

At first I used the traversal method to find the location of the various parts, found more cumbersome, and later reference to the xtzmm1215 solution, found that the use of find and substr will be much easier.

Here's the code for xtzmm1215, and I've added some comments to make it easy to read:

#include <cstdio> #include <iostream> #include <string> #include <sstream>using namespace std; int main () {string s, ans = ""; Getline (cin, s); if (s[0] = = '-') ans + = s[0]; Negative numbers are preceded by a minus sign int indexe = S.find ("E"); string num = S.substr (1, indexE-1); char x = s[indexe + 1];string exp = s.substr (indexe +2, S.size ()-indexE-2); StringStream ss;ss << exp;int e;ss >> e;if (E = = 0) {//Index 0 Direct output cout << ans & lt;< num << endl;return 0;} if (x = = ' + ') {//positive exponent if (E < Num.size ()-2) {//Num.size ()-2 is minus the decimal point and 1 after the part, if the index is not reached, you need to add a decimal point in the appropriate place. Ans = ans + num[0] + num.substr (2, E) + "." + num.substr (E + 2, num.size ()-e-2);} else{//Num.size ()-2 if the index is equal, it needs to be supplemented by a widening of the number. Ans = ans + num[0] + num.substr (2, Num.size ()-2); for (int i = 0; i < e-num.size () + 2; i++) ans + = "0";} if (x = = '-') {//negative exponent, the pre-0,0 is required. Counted as an exponent, so the index should be stopped to 1 before the 0.ans = ans + "0."; while (e--! = 1) ans + = "0"; ans = ans + num[0] + num.substr (2, Num.size ()-2);} cout << ans << endl;return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1073. Scientific Notation (20)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.