ZZULIOJ:1166: 实数取整(指针专题)

题目描述

读入一个实数,输出实数的整数部分。注意该实数的位数不超过100位。输入的整数部分可能含有不必要的前导0,输出时应去掉,当然,若整数部分为0,则该0不能去掉。如输入0023.56732,输出应为23,而不是0023;0.123对应的输出应为0。当然输入也可能不含小数部分。
要求定义并使用rounding()函数,原型如下:
char *rounding(char *p)
{
//将字符串p表示的实数取整后生成新的字符串,并由函数返回
}

输入

输入一个实数.

输出

输出整数部分。

样例输入 Copy
0012345678900.56789
样例输出 Copy
12345678900

源代码

#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
string rounding(string p)
{
    int tail = p.find('.');
    if(tail == -1)tail = p.size();
    int head = 0;
    while(p[head] == '0')head ++ ;
    if(head == tail)p = "0";
    else
    {
        string s;
        for(int i = head;i < tail;i ++ )
        {
            s = s + p[i];
        }
        p = s;
    }
    return p;
}
int main()
{
    string s;
    cin >> s;
    cout << rounding(s) << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/couchpotatoshy/article/details/126111022