PAT B 1024 Scientific Notation

I haven't updated my blog for a long time (although no one reads it...the level is limited), I've been brushing PAT recently, and this question took half an hour. I feel it is necessary to write it on the blog and post the title first.
write picture description here

The title requirement is to convert scientific notation to output numbers that are not scientific notation.
The steps are as follows:
1. Enter the "+-" sign, the number part and the index part respectively.
2. According to the positive and negative sign, first decide whether to output the negative sign "-" before outputting the number.
3. Determine the moving direction of the decimal point, whether it is left or right.
4. If the decimal point needs to be moved to the right, there are three cases (1) zero-filling is not required during the decimal point movement [the number of digits after the decimal point is less than the exponent after the E] (2) zero-filling is not required during the decimal point moving process [The number of digits after the decimal point is exactly equal to the exponent after E] (3) During the movement of the decimal point, it needs to be filled with zeros [the number of digits after the decimal point is greater than the exponent after E]. The above three cases are dealt with separately.
5. If the decimal point moves to the left, first output "0.", then fill with zeros, and then output the subsequent digits

There is a place in the code that I personally think it is necessary to say something

scanf("%c%c.%[0-9]E%d", &head, &a[0], a + 1, & ex);

The first "%c" in this statement stores the "+-" symbol in the character variable head, the second "%c" stores the first number in the character array a[0], and the ". "Skip the decimal point, followed by "%[0-9]E" This is a regular expression, the function is to enter all the following numbers, and it will end if it encounters a non-number, and the following "E" skips the entry of E , the last "%d" is entered in the index part after E.
That is to say, after this statement is executed, the value of each variable should be as follows:
Input : +1.23400E-03
Variable state:
character variable head='+';
character array a=”123400”;
integer variable ex=-3 ;

Paste the code (can be submitted directly to PAT):

#include <iostream>
#include <stdio.h>
#include <string.h》

using namespace std;

int main()
{
    char head, a[9999] = { 0 };
    int ex = 0, i = 0//如果用的是VS就把下面这行注释取消了,
    //scanf_s("%c%c.%[0-9]E%d", &head, sizeof(char), &a[0], sizeof(char), a + 1, sizeof(char) * 998 ,& ex);
    //如果用的是VS就注释下面这行
    scanf("%c%c.%[0-9]E%d", &head, &a[0], a + 1, & ex);
    if (head == '-')
        cout << "-";
    if (ex >= 0)
    {
        //如果小数点后的数字位数小于E后面的指数(需要补零)
        if (strlen(a) - 1 < ex)
        {
            for (i = 0; a[i] != '\0'; i++)
            {
                //避免出现输入 +0.1234E+E+5 后 输出 012340情况
                if (i == 0 && a[i] == '0')
                    continue;
                cout << a[i];
            }
            //计算需要补零的数量并输出指定位数的零
            for (int j = 0; j < ex - i + 1; j++)
                cout << 0;
        }
        //如果小数点后的数字位数大于E后面的指数(不需要补零)
        else if (strlen(a) - 1 > ex)
        {
            for (i = 0; a[i] != '\0'; i++)
            {
                //避免出现输入 +0.1234E+E+2 后 输出 012.34情况
                if (i == 0 && a[i] == '0')
                    continue;
                if (i == 1 + ex)
                {
                    //输出到小数点的位置时额外输出小数点
                    cout << "." << a[i];
                    continue;
                }
                cout << a[i];
            }
        }
        else
        {
            //如果小数点后的数字位数等于E后面的指数(不需要补零)
            for (i = 0; a[i] != '\0'; i++)
            {
                //避免出现输入 +0.1234E+E+4 后 输出 01234情况
                if (i == 0 && a[i] == '0')
                    continue;
                cout << a[i];
            }
        }
    }
    else
    {
        cout << "0.";
        for (int j = 0; j != ex + 1; j--)
            cout << "0";
        for (i = 0; a[i] != '\0'; i++)
        {
            cout << a[i];
        }
    }
    //如果用的是VS就取消注释下一行
    //system("pause");
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325798645&siteId=291194637