2014-02-09 57 views
2
#include <stdlib.h> 
#include <stdio.h> 

#include "histo.h" 

int main(int args, char *argv[]) 
{ 
    int histo[256]; 

    if (args == 2) 
    { 
    init_histogram(histo); 
    cons_histogram(argv[1], histo); 
    display_histogram(histo); 
    } 
    else 
    exit(1); 

    return 0; 
} 

histo.h 
#define MAX_CHAR 255 // largest ASCII(Extended) value for characters 

typedef unsigned char byte; // may be useful for casting(s) 

void init_histogram(int histo[]); // set all elements of the histogram to zero 
void cons_histogram(const char string[], int histo[]); // construct the histogram from string 
void most_frequent(const int histo[], char* ret_val); // report a most occurring character 
void display_histogram(const int histo[]); // display the histogram sparsely 

我給這個main.c和histo.h實現了頭文件中的那些方法之後的histo.c文件。我對int數組的歷史感到困惑。 display_histogram只需要一個參數,它爲您打印輸出:如何使用指針/數組在C中實現這些方法?

% ./main hgfjkddjkrui3 
3 appeared 1 times 
d appeared 2 times 
f appeared 1 times 
g appeared 1 times 
h appeared 1 times 
i appeared 1 times 
j appeared 2 times 
k appeared 2 times 
r appeared 1 times 
u appeared 1 times 

d是一個最經常發生 任何人都可以向我解釋都應該被存儲在HISTO []爲了得到這個輸出什麼樣的價值觀?

回答

4

推測hist[]是一個數組,其中每個元素包含給定ASCII字符的頻率,並且它由ASCII字符索引。 init_histogram將其設置爲0,並且display_histogram正在打印非零條目。

0

它看起來像那些函數計算字符串的直方圖。我想histo[]數組需要存儲每個字符的計數。因爲您已將MAX_CHAR定義爲255,我認爲histo[]數組是一個256個整數的數組。

0
//vertical HISTOGRAM 


#include<stdio.h> 
#include<stdlib.h> 
int main() 
{ 
int c,i,j,arr[10],height=0; 
system("clear"); 

for(i=0 ; i<10 ; i++) 
    arr[i]=0; 

while((c=getchar()) != EOF) 
    { 
    if(c >= '0' || c <='9') 
    ++arr[c-'0']; 
    if(arr[c-'0'] > height) 
     { 
     height = arr[c-'0']; 
     } 
    } 
printf("\n"); 
for(j=height ; j>0 ; j--)  // row 
{ 
    printf("%2d|",j); 
    for (i=0 ; i<=9 ; i++) // column 

    { 
    if(j == arr[i]) 
    { 
    printf(" *|"); 
    arr[i]--; 
    } 
    else 
     printf(" |"); 
    } 

    printf("\n"); 
} 
    printf(" |"); 
for (i=0 ; i<=9 ; i++) 
    printf(" %d|",i); 
    printf("\n ------------DIGITS-------------"); 
    printf("\n"); 
return(0); 
} 

enter image description here