2

私は過去2日間どこでも検索してきましたが、これを理解することはできません。出力の小さなサンプルを次に示します。

それは「そして喜びを見つける」と読むべきです:

f

ind

a

p

レジャー

================

長さカウント

================

1 9054

210102

3 9336

4 5944

5 3311

6 1656

7 1292

================

平均2.86

================

以下はコードです:

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

#define DELIM " ,.;:'\"&!? -_\n\t\0"

int process(int *count, char *buffer);
int printOut(int *count);

int main() {
    char *buffer = (char *) calloc(1, 50*sizeof(char*));
    int *count =(int*) calloc(50, 50*sizeof(long));

    while (fgets(buffer, sizeof buffer, stdin)) {
        process(count, buffer);
    }

    printOut(count);
    free(count);
    return 0;
}

int process(int *count, char *buffer) {
    int word_len=0, i;
    char *pch;
    pch = strtok(buffer, DELIM);
    while (pch != NULL) {
        for(i=0; pch[i] != '\0'; i++) {
                word_len++;
        }
        count[word_len]++;
        word_len=0;
        printf("%s\n", pch);
        pch = strtok(NULL, DELIM);
    }
    return 0;
}

int printOut(int *count) {
    int i;
    double num=0;
    double total=0;
    double average=0;
    printf("================\n");
    printf("len  count\n");
    printf("================\n");
    for(i=0;i<50;i++){
        if(count[i]!=0){
            num=count[i]+num;
            total=total+(count[i]*i);
            printf("%d   %d\n",i,count[i]);
        }
    }
    average = total/num;
    printf("================\n");
    printf("average %.2f\n", average);
    printf("================\n");
    return 0;
}
4

3 に答える 3

3

これは間違っています:

 char *buffer = (char *) calloc(1, 50*sizeof(char*));

そのはず:

 sizeof(char) // or since this is always 1: 'calloc(1, 50)'

しかし、本当の問題はここにあります:

fgets(buffer, sizeof buffer, stdin);

Sizeofバッファは4(またはsizeofポインタが何であれ)です。これはポインタであるため、次のようにする必要があります。

fgets(buffer, 50, stdin);

また、sizeof(long)callocの呼び出しcountは重要ではありません。sizeof(int)そうでない場合は、意図したよりも多くのバイトを割り当てる必要があります(アーキテクチャによって異なります)。したがって、これは次のようになります。

int *count =(int*) calloc(50, sizeof(int));
于 2013-03-06T00:33:43.220 に答える
0
int main() {
    char *buffer =  calloc(50, 1);
    int  *count  =  calloc(50, sizeof(int));

    while (fgets(buffer, 50, stdin)) {
        process(count, buffer);
    }

元のfgetsは、ポインタのsizeofを「読み取る」だけです。4バイトまたは8バイトであり、Enteryバッファまたは50文字ではありません。

于 2013-03-06T00:37:21.193 に答える
0

この反例は役立つかもしれません:

/*
 * Sample output:
 *   word= and, length(word)= 3
 *   word= find, length(word)= 4
 *   word= a, length(word)= 1
 *   word= pleasure, length(word)= 8
 *   Done.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF_LEN 50
#define DELIM " ,.;:'\"&!? -_\n\t\0"

void process(int *count, char *buffer);

int main() {
  char buffer[BUF_LEN];
  int count[BUF_LEN];

  strcpy (buffer, "and find a pleasure");
  process(count, buffer);

  printf ("Done.\n");
  return 0;
}

void process(int *count, char *buffer) {
  char *pch = strtok(buffer, DELIM);
  while (pch) {
    printf ("word= %s, length(word)= %d\n",
      pch, strlen(pch));
    pch = strtok (NULL, DELIM);
  }
}
于 2013-03-06T00:39:51.937 に答える