PAT 1073. Scientific notation (20)

Source: Internet
Author: User

http://pat.zju.edu.cn/contests/pat-a-practise/1073

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

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

Input Specification:

Each input file contains one test case. For each case, there are 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 is no more than 9999.

Output Specification:

For each test case, print at one line the input number A in the conventional notation and 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

Malle an Egg ~ positive number to go ' + ', did not notice that this wasted too much time ~ ~

#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];
	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) {
		cout << ans << num << endl;
		return 0;
	}
	if (x = = ' + ') {
		if (E < Num.size ()-2) {
			ans = ans + num[0] + num.substr (2, E) + "." + num.substr (E + 2, NUM.S Ize ()-e-2);
		else{
			ans = ans + num[0] + num.substr (2, Num.size ()-2);
			for (int i = 0; i < e-num.size () + 2; i++)
				ans + = "0";
		}
	}
	if (x = = '-') {
		ans = ans + "0.";
		while (e--! = 1)
			ans + = "0";
		Ans = ans + num[0] + num.substr (2, Num.size ()-2);
	}
	cout << ans << endl;
	return 0;
}


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.