C语言用函数与指针解决问题

img

img

img

img


各位有谁知道这几题的思路吗
用题目中给的要求作答
在线等待解惑
谢谢😊

非标准答案,仅供参考!因为精度问题,长度无法输出一模一样。要高精度用字符串形式处理。

img

img


#include <stdio.h>
#include <math.h>
#include <string.h>

double tail(double x, int *n)
{
    double y = fabs(x), fl;

    int m = 0;

    fl = x > 0.0 ? 1.0 : -1.0;

    if (y >= 1)
        while (y >= 1)
        {
            y /= 10.0;
            m++;
        }
    else if (y < 0.1)
        while (y < 0.1)
        {
            y *= 10.0;
            m--;
        }

    *n = m;

    return y * fl;
}

int main(int argc, char *argv[])
{
    double x, outx;
    int n;
    char buf[64] = {'\0'};
    scanf("%lf", &x);
    sprintf(buf, "%lf", x);
    char *p = strchr(buf, '.');
    int wid = strlen(buf) - (p - buf) - 1;
    outx = tail(x, &n);
    printf("%.*lfe%d\n", wid, outx, n);
    return 0;
}