2014-11-21 43 views
-1

在這一行printf("%ld,%ld,%s,%s\n", i, j, songtitle, interpreter); printf()給了我一個未對齊的輸出,我找不出原因。 e.g:printf未對齊輸出

1. 0,0,2 
2.  2 
3. 1 0  ---- ---- 
4. 2 0  ---- ---- 

這裏是我的打印功能:

void print_hash(hashcontainer_t *hashcontainer) 
{ long i ,j; 
    if(hashcontainer == 0) 
    { 
     fprintf(stderr,"Hashtable is Empty!\n"); 
     return; 
    } 
    printf("\n"); 
    for(i=0;i<hashcontainer->hashsize;i++) 
    { 
     if(hashcontainer->hasharrays[i].num_entries == 0) 
     { 
      printf("%ld  0  ----  ----\n",i); 
     } 
     else 
     { 
      for(j=0;j<hashcontainer->hasharrays[i].num_entries;j++) 
      { 
      char *songtitle = hashcontainer->hasharrays[i].entries[j].songtitle; 
      char *interpreter = hashcontainer->hasharrays[i].entrie[j].interpreter; 
      printf("%ld,%ld,%s,%s\n", i, j, songtitle, interpreter); 
      }  
     }  
    }  
} 

這裏是我的全碼:

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

typedef struct hashentry_s 
{ 
    char songtitle[256], interpreter[256]; 
} hashentry_t; 

typedef struct hasharray_s 
{ 
    hashentry_t *entries; 
    long num_entries; 
} hasharray_t; 

typedef struct hashcontainer_s 
{ 
    hasharray_t *hasharrays; 
    long hashsize; 
} hashcontainer_t; 


long hash_key(char songtitle[], char interpreter[], long hash_max) 
{ 
    unsigned long index = 0, i; 
    for (i = 0; i < strlen(songtitle); ++i) 
     index = 64 * index + (long)(songtitle[i]); 
    for (i = 0; i < strlen(interpreter); ++i) 
     index = 64 * index + (long)(interpreter[i]); 
    return index % hash_max; 
} 

hashcontainer_t * create_hash (long hashsize) 
{ 
    hashcontainer_t *container=0; 
    container = calloc(1,sizeof(hashcontainer_t)); 
    if(container == 0) 
    { 
     fprintf(stderr,"Error allocating Memory!\n"); 
     return 0; 
    } 
    container->hasharrays = calloc(hashsize,sizeof(hasharray_t)); 
    if(container->hasharrays == 0) 
    { 
     fprintf(stderr,"Error Allocating Memory!/n"); 
     free(container); 
     return 0; 
    } 
    container->hashsize = hashsize; 
    return container; 
} 

void delete_hash (hashcontainer_t * hashcontainer) 
{ 
    long i; 
    if(hashcontainer == 0) 
    { 
     fprintf(stderr,"Hashtable is Empty!/n"); 
     return; 
    } 
    if(hashcontainer->hasharrays == 0) 
    { 
     fprintf(stderr,"Hasharrays not Allocated or Empty!/n"); 
     free(hashcontainer); 
     return; 
    } 
    for(i=0;i<hashcontainer->hashsize;i++) 
      free(hashcontainer->hasharrays[i].entries); 
    hashcontainer->hashsize = 0; 
    hashcontainer =0; 
    free(hashcontainer->hasharrays); 
    free(hashcontainer); 
} 


void insert_entry(hashcontainer_t *hashcontainer, char songtitle[], char interpreter[]) 
{ 
    long key =0,position; 
    if(hashcontainer == 0) 
    { 
     fprintf(stderr,"Memory is not Allocated!\n"); 
     return; 
    } 
    key = hash_key(songtitle,interpreter,hashcontainer->hashsize); 
    position = hashcontainer->hasharrays[key].num_entries; 
    hashcontainer->hasharrays[key].entries = realloc(hashcontainer->hasharrays[key].entries,(position+1)* sizeof(hashentry_t)); 
    if(hashcontainer->hasharrays[key].entries == 0) 
    { 
     fprintf(stderr,"Error Allocating New size\n"); 
     return; 
    } 
    strcpy(hashcontainer->hasharrays[key].entries[position].songtitle,songtitle); 
    strcpy(hashcontainer->hasharrays[key].entries[position].interpreter,interpreter); 
    hashcontainer->hasharrays[key].num_entries++; 
} 


void print_hash(hashcontainer_t *hashcontainer) 
{ long i ,j; 
    if(hashcontainer == 0) 
    { 
     fprintf(stderr,"Hashtable is Empty!\n"); 
     return; 
    } 
    printf("\n"); 
    for(i=0;i<hashcontainer->hashsize;i++) 
    { 
     if(hashcontainer->hasharrays[i].num_entries == 0) 
     { 
      printf("%ld  0  ----  ----\n",i); 
     } 
     else 
     { 
     for(j=0;j<hashcontainer->hasharrays[i].num_entries;j++) 
     { 
      char *songtitle = hashcontainer->hasharrays[i].entries[j].songtitle; 
      char *interpreter = hashcontainer->hasharrays[i].entries[j].interpreter; 
      printf("%ld,%ld,%s,%s\n", i, j, songtitle, interpreter); 
     } 

     }  
    }  
}  

hashentry_t * search_entry (hashcontainer_t * hashcontainer ,char songtitle [], char interpreter []) 
{ 
    long i ,key; 
    if(hashcontainer == 0) 
    { 
     fprintf(stderr,"hashcontainer not allocated!\n"); 
     return 0; 
    } 
    key = hash_key(songtitle,interpreter,hashcontainer->hashsize); 
    for(i=0;i<hashcontainer->hashsize;i++) 
    { 
     if(strcmp(hashcontainer->hasharrays[key].entries[i].songtitle,songtitle)==0 && strcmp(hashcontainer->hasharrays[key].entri interpreter,interpreter)==0) 
      return &hashcontainer->hasharrays[key].entries[i]; 
    } 
    return 0; 
} 
int main() 
{ 
    hashcontainer_t *container=0; 
    hashentry_t *found=0; 
    char c; 
    char songtitle[256],interpreter[256]; 
    long hashsize; 
    while(1) 
    { 
     printf("\n"); 
     printf("1- Creat Hash\n"); 
     printf("2- Insert Hash\n"); 
     printf("3- Print Hash\n"); 
     printf("4- Delete Hash\n"); 
     printf("5- Search Entry\n"); 
     scanf("%c",&c); 
     getchar(); 
     switch (c) 
     { 
     case '1': 
      printf("Please Insert the size of your hash: "); 
      scanf("%ld",&hashsize); 
      getchar(); 
      printf("\n"); 
      container = create_hash(hashsize); 
      break; 
     case '2': 
      printf("Please Insert a Songtitle: "); 
      fgets(songtitle,256,stdin); 
      printf("\n"); 
      printf("Please Insert an Interpreter: "); 
      fgets(interpreter,256,stdin); 
      printf("\n"); 
      insert_entry(container,songtitle,interpreter); 
      break; 
     case '3': 
      printf("\n"); 
      print_hash(container); 
      break; 
     case '4': 
      delete_hash(container); 
      break; 
     case '5': 
      printf("Please Insert the Songtitle that you want to search for: "); 
      fgets(songtitle,256,stdin); 
      printf("\n"); 
      printf("Please Insert the Interpreter: "); 
      fgets(interpreter,256,stdin); 
      printf("\n"); 
      found = search_entry(container,songtitle,interpreter); 
      if(found == 0) 
       printf("No elements foundn\n"); 
      else 
       printf("Found at %p\n",found); 
      break; 
     default: 
      break; 
     } 
    }  
} 
+0

請首先'對齊'[請閱讀:格式]您的代碼示例。 – 2014-11-21 10:43:02

+0

在輸出中是否使用了固定大小的字體? – tiledcode 2014-11-21 10:45:31

+0

是的,大小是固定的 – 2014-11-21 11:01:14

回答

0

或者你可以做的;

printf ("%ld\t0\t----\t----\n", i)

\t簡單地創建一個標籤。這可能會增加間距。

1

這不是從你的問題完全清楚什麼叫「對準」但你要我理解你的數據對齊成列這樣的意思:

1, 100, hello world, 7 
100, 100,   no, 100 

你可以看到它在這個簡單的例如:

printf("%d,%d,%s,%s\n", 1, 10, "short", "short"); 
printf("%d,%d,%s,%s\n", 1, 100, "long long string", "short"); 

生產:

1,10,short,short 
1,100,long long string,short 

當你採取printf() documentation支持的格式一看就是:

%[flags][width][.precision][length]specifier

0標記(您想打印寬度固定號碼的情況下「零填充10 => 0010)。

0當指定填充(請參閱寬度子分隔符)時,使用零填充數字(0)而不是空格。

width和參數:字符的

最少要被打印。如果要打印的值小於此數字,則結果將填充空白。即使結果較大,該值也不會被截斷。

因此延長我的例子:

printf("%4d,%4d,%20s,%20s\n", 1, 10, "short", "short"); 
printf("%4d,%4d,%20s,%20s\n", 1, 100, "long long string", "short"); 

會產生:

1, 10,    short,    short 
    1, 100, long long string,    short 
^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^ 
    4 4     20     20 

你只需要決定列寬前期(以及任何數據長於列將打破該行)。