嘿伙计们,我完成了这个基本的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;
}
答案 0 :(得分:3)
如果没有自定义编码,很难得到你想要的一切,但“%g
”至少可以处理你想要的大部分内容。领先的零是非常不可协商的,是全部。它通常选择最紧凑的符号表示法,包括在极端情况下的科学记数法。
答案 1 :(得分:3)
让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));