-2
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define FILE_NAME 20
    #define LIST_SIZE 50

    typedef struct
    {
    char *name;
    int score;
    }RECORD;

    float calMean(RECORD list[], int count);
    void sortData(RECORD list[], int count);
    float calMedian(RECORD list[], int size);
    int calMode(RECORD list[], int count);
    int main (void)
{
       // Declarations
         float mean;
         float median;
         int mode;
         FILE *fp;
           char fileName[FILE_NAME];
           RECORD list[LIST_SIZE];
           char buffer[100];
           int count = 0;
           int i;
       // Statements
           printf("Enter the file name: ");
           gets(fileName);
           fp = fopen(fileName, "r");
           if(fp == NULL)
           printf("Error cannot open the file!\n");
           while(fgets(buffer, 100, fp) != NULL)
           {
             if( count >= LIST_SIZE)
             {
                printf("Only the first 50 data will be read!\n");
                 break;
             }
             if( count < LIST_SIZE)
            {
                list[count].name = (char*) malloc(strlen(buffer)*sizeof(char));
                   sscanf(buffer,"%[^,], %d", list[count].name, &list[count].score);
                  printf("name is %s and score is %d\n", list[count].name, list[count].score);
                  count++;
            }
                for( i =0; i < (LIST_SIZE - count); i++)
            {
                list[count + i].name = 0;
                list[count + i].score = 0;
            }
         }
           printf("Read in %d data records\n", count);
           mean = calMean(list, count);
         sortData(list, count);
         mode = calMode(list, count);
           printf("%2.2f\n", mean);
           fclose(fp);
           return 0;
}

float calMean(RECORD list[], int count)
{
       float tempMean;
        int sum = 0;
        int i;
        for(i = 0; i < count; i++)
        sum += list[i].score;
        tempMean = (float) sum/count;
        return tempMean;
}

void sortData(RECORD list[], int count)
{
    int temp;
    int current;
    int walker;
    float median;
    int size = count;
    for(current = 0; current  < count; current++)
    {
       for( walker = count-1; walker > current; walker--)
          if(list[walker].score < list[walker -1].score)
          {
             temp = list[walker].score;
             list[walker].score = list[walker -1].score;
             list[walker -1].score = temp;
          }
          printf("%d\n", list[current].score);
    }
    median = calMedian(list, size);
    printf("%2.2f\n", median);
    return;
}

float calMedian(RECORD list[], int size)
{
    float tempMedian;
    printf("size is: %d\n", size);
    if ( size % 2 == 0) 
       tempMedian = (float) ((list[size/2].score + list[size/2-1].score)/2.0) ;                   
       else                                                    
         tempMedian = (float) list[size/2 - 1].score; 
return tempMedian;
}

int calMode(RECORD list[], int count)
{
    int tempMode = 1;
    int i, j;

    for(i=0;i<10;i++)
    {
       list[list[i].score].score++;
    }
    for(i = 0; i < count; i++)
    {
       printf("\n%d:",list[i].score);
       for(j = 0; j < list[i].score; j++)
       {
          printf("*");
       }
    }
    printf("\n\n");
    return tempMode;
}

嗨,我正在尝试编写一个直方图,列出所有分数和出现频率,然后使用直方图查找所有分数的模式,它们都在 calMode 函数中。

上面的代码是我写这个直方图的尝试,但它不正确

我得到了这个输出:


    Enter the file name: in.txt
    name is Ada Lovelace and score is 66
    name is Linus Torvalds and score is 75
    name is Peter Norton and score is 82
    name is Ken Thompson and score is 82
    name is Steve Wozniak and score is 79
    name is Marc Andreessen and score is 60
    name is Donald Knuth and score is 60
    name is Adele Goldberg and score is 71
    name is Grace Hopper and score is 82
    name is Bill Joy and score is 91
    name is Andrew Tanenbaum and score is 71
    name is Brian Kernighan and score is 72
    Read in 12 data records
    60
    60
    66
    71
    71
    72
    75
    79
    82
    82
    82
    91
    size is: 12
    73.50

    60:************************************************************
    60:************************************************************
    66:******************************************************************
    71:***********************************************************************
    71:***********************************************************************
    72:************************************************************************
    75:***************************************************************************
    79:*****************************************************************************
    **
     82:*****************************************************************************
*****
    82:*****************************************************************************
*****
    82:*****************************************************************************
*****
    91:*****************************************************************************
**************

74.25

对这个直方图函数的算法有什么建议吗?

4

1 回答 1

0

您的代码正在编写正确数量的星号,但您运行此代码的控制台窗口不够宽,无法在不换行的情况下显示该数量的星号。您可以更改控制台窗口的宽度。我不确定您正在运行哪个操作系统,因此我无法指导您完成更改控制台窗口宽度的过程。

不过,更改比例是一个简单的选择。只需更改您的循环,使其以分数除以任何比例结束。例如,如果您希望一个星号代表两个:

   for(j = 0; j < list[i].score / 2; j++)
   {
      putchar('*');
   }

现在您遇到的问题是两个的余数未显示在直方图中。也许您可以更改输出':'比例以表示两个,以及'.'任何余数:

   for(j = 0; j < list[i].score / 2; j++)
   {
      putchar(':');
   }

   if (list[i].score % 2 == 1) {
      putchar('.');
   }
于 2013-04-16T06:40:23.413 回答