gpt4 book ai didi

控制 double

转载 作者:行者123 更新时间:2023-12-02 08:35:00 27 4
gpt4 key购买 nike

大家好,我完成了这个基本的 C 程序,它向输入任何给定数字集的用户显示有序集、最小值、最大值、平均值和中值。我遇到的问题是,当我打印数字时,我必须使用诸如“3.2%f”之类的东西来设置标准的精度,我怎样才能只打印它们我想要的每个数字的最小小数位数?例如,如果我输入 5、5.5、-0.2313 和 4,我的程序将按顺序显示它们为 -0.23、4.00、5.00 和 5.50。如何让它读取 -.2313、4、5 和 5.5?预先感谢您的帮助。

#include <stdio.h>

int findSize();
void sortArray(int size, double num[]);
double findAverage(int size, double num[]);
double findMedian(int size, double num[]);
void findLowtoHigh(int size, double num[]);
void findHightoLow(int size, double num[]);

int main()
{
while(1)
{
int size = findSize();

if(size <= 1)
{
return 0;
}

double num[size];
double lowest = 0;
double highest = 0;
double average = 0;
double median = 0;

fprintf(stdout, "\n");

sortArray(size, num);
findLowtoHigh(size, num);
findHightoLow(size, num);
average = findAverage(size, num);
median = findMedian(size, num);

fprintf(stdout, "\n\nLowest Value: %3.4f", num[0]);
fprintf(stdout, "\nHighest Value: %3.4f", num[size-1]);
fprintf(stdout, "\n\nAverage Value: %3.4f\n", average);
fprintf(stdout, "Median Value: %3.4f", median);

fprintf(stdout, "\n");
}

}

int findSize()
{
int size;

fprintf(stdout, "\nPlease enter size of the array: ");
scanf("%d", &size);

return size;
}

void sortArray(int size, double num[])
{
for(int i = 0; i <= size - 1; i++)
{
int j = i+1;
fprintf(stdout, "Please enter number %d: ", j);
fscanf(stdin, "%lf", &num[i]);
}

if(size > 1)
{
double holder = 0;

for(int y = 0; y < size - 1; y++)
{
for(int k = 0; k < size - 1; k++)
{
if(num[k] > num[k+1])
{
holder = num[k];
num[k] = num[k+1];
num[k+1] = holder;
}
}
}
}
}

void findLowtoHigh(int size, double num[])
{
fprintf(stdout, "\nFrom least to greatest: ");

for(int x = 0; x <= size - 1; x++)
{
fprintf(stdout, "%3.2f ", num[x]);
}
}

void findHightoLow(int size, double num[])
{
fprintf(stdout, "\nFrom greatest to least: ");

int reverse = size - 1;

while(reverse != -1)
{
fprintf(stdout, "%3.2f ", num[reverse]);
reverse--;
}
}

double findAverage(int size, double num[])
{
double average = 0;

for(int a = 0; a <= size - 1; a++)
{
average = average + num[a];
}

average = average / size;

return average;
}

double findMedian(int size, double num[])
{
double median = 0;

if(size % 2 == 0)
{
median = (num[size/2 - 1] + num[size/2])/2;
}
else
{
median = num[size/2];
}

return median;
}

最佳答案

sprintf() 完成繁重的工作,然后去掉尾随的零。

char *print_min_precision(char * buffer) {
// look fo rthe decimal point, if not found return without altering buffer
if (strchr(buffer, '.') == NULL)
return buffer;
// Find the last digit
char *p = &buffer[strlen(buffer) - 1];
// While not at the beginning and digit is zero ...
while (p > buffer && *p == '0') {
// Change '0' to `\0`
*p-- = '\0';
}
if (p > buffer && *p == '.') {
*p-- = '\0';
}
return buffer;
}


char buf[400]; // a BA buffer
// Print the number using some %f format.
// 6 here is the maximum number of fractional digits to use
sprintf(buf, "%.6f", num[reverse]);
// Print the reduced number.
fprintf(stdout, "%s ", print_min_precision(buf));

一般来说,试图用数字计算出尾随小数点有很多边界条件。最好打印到缓冲区并对其进行后处理。请务必为以下内容提供足够的缓冲区:

print_min_precision(sprintf(buf, "%.6f", 10e300));

关于控制 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com