C printf未对齐输出
在这一行中,C printf未对齐输出,c,printf,hashtable,C,Printf,Hashtable,在这一行中,printf(“%ld,%ld,%s,%s\n”,i,j,songtitle,解释器) printf()给了我一个未对齐的输出,我不知道为什么。 e、 g: 这是我的打印功能: void print_hash(hashcontainer_t *hashcontainer) { long i ,j; if(hashcontainer == 0) { fprintf(stderr,"Hashtable is Empty!\n"); re
printf(“%ld,%ld,%s,%s\n”,i,j,songtitle,解释器)代码>
printf()
给了我一个未对齐的输出,我不知道为什么。
e、 g:
这是我的打印功能:
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);
}
}
}
}
void print\u hash(hashcontainer\u t*hashcontainer)
{long i,j;
if(hashcontainer==0)
{
fprintf(stderr,“哈希表为空!\n”);
返回;
}
printf(“\n”);
对于(i=0;ihashsize;i++)
{
if(hashcontainer->hasharrays[i].num\u entries==0)
{
printf(“%ld 0----\n”,i);
}
其他的
{
对于(j=0;jhashArray[i].num_条目;j++)
{
char*songtitle=hashcontainer->hasharrays[i]。条目[j]。songtitle;
char*interpreter=hashcontainer->hasharrays[i].entrie[j].解释器;
printf(“%ld,%ld,%s,%s\n”,i,j,歌曲标题,解释器);
}
}
}
}
这是我的全部代码:
#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;
}
}
}
#包括
#包括
#包括
类型定义结构哈希项
{
char songtitle[256],解释器[256];
}hashentry\u t;
类型定义结构哈希数组
{
hashentry_t*条目;
长数值输入;
}哈希数组;
类型定义结构哈希容器
{
hasharray_t*hasharray;
长散列大小;
}hashcontainer\u t;
长哈希_键(char songtile[],char解释器[],长哈希_max)
{
无符号长索引=0,i;
对于(i=0;ihasharray=calloc(hashsize,sizeof(hasharray_t));
如果(容器->哈希数组==0)
{
fprintf(stderr,“分配内存时出错!/n”);
免费(集装箱);
返回0;
}
容器->hashsize=hashsize;
返回容器;
}
void delete_散列(hashcontainer_t*hashcontainer)
{
龙我;
if(hashcontainer==0)
{
fprintf(stderr,“哈希表为空!/n”);
返回;
}
if(hashcontainer->hasharrays==0)
{
fprintf(stderr,“哈希数组未分配或为空!/n”);
免费(hashcontainer);
返回;
}
对于(i=0;ihashsize;i++)
免费(hashcontainer->hasharrays[i].entries);
hashcontainer->hashsize=0;
hashcontainer=0;
免费(hashcontainer->hasharray);
免费(hashcontainer);
}
void insert_条目(hashcontainer_t*hashcontainer,char songtile[],char解释器[])
{
长键=0,位置;
if(hashcontainer==0)
{
fprintf(stderr,“内存未分配!\n”);
返回;
}
key=hash_key(歌曲标题、解释器、hashcontainer->hashsize);
position=hashcontainer->hasharrays[key].num\u条目;
hashcontainer->hasharrays[key]。entries=realloc(hashcontainer->hasharrays[key]。entries,(位置+1)*sizeof(hashentry\u t));
if(hashcontainer->hasharrays[key].entries==0)
{
fprintf(stderr,“分配新大小时出错”);
返回;
}
strcpy(hashcontainer->hasharrays[key]。条目[position]。歌曲标题,歌曲标题);
strcpy(hashcontainer->hasharrays[key]。条目[position]。解释器,解释器);
hashcontainer->hasharrays[key].num_entries++;
}
无效打印\u散列(hashcontainer\u t*hashcontainer)
{long i,j;
if(hashcontainer==0)
{
fprintf(stderr,“哈希表为空!\n”);
返回;
}
printf(“\n”);
对于(i=0;ihashsize;i++)
{
if(hashcontainer->hasharrays[i].num\u entries==0)
{
printf(“%ld 0----\n”,i);
}
其他的
{
对于(j=0;jhashArray[i].num_条目;j++)
{
char*songtitle=hashcontainer->hasharrays[i]。条目[j]。songtitle;
char*interpreter=hashcontainer->hasharrays[i]。条目[j]。解释器;
printf(“%ld,%ld,%s,%s\n”,i,j,歌曲标题,解释器);
}
}
}
}
hashentry\u t*搜索\u entry(hashcontainer\u t*hashcontainer,char songtitle[],char解释器[])
{
长我,钥匙;
if(hashcontainer==0)
{
fprintf(stderr,“未分配哈希容器!\n”);
返回0;
}
key=hash_key(歌曲标题、解释器、hashcontainer->hashsize);
对于(i=0;ihashsize;i++)
{
if(strcmp(hashcontainer->hasharrays[key].entries[i].songtitle,songtitle)==0&&strcmp(hashcontainer->hasharrays[key].entri解释器,解释器)==0)
return&hashcontainer->hasharrays[key]。条目[i];
}
返回0;
}
int main()
{
hashcontainer_t*container=0;
hashentry_t*found=0;
字符c;
char songtitle[256],解释器[256];
长散列大小;
而(1)
{
printf(“\n”);
printf(“1-创建哈希\n”);
printf(“2-插入散列\n”);
printf(“3-打印散列\n”);
printf(“4-删除散列\n”);
printf(“5-搜索条目\n”);
scanf(“%c”、&c);
getchar();
开关(c)
{
案例“1”:
printf(“请插入哈希的大小:”);
scanf(“%ld”、&hashsize);
getchar();
printf(“\n”);
容器=创建\u散列(hashsize);
打破
案例“2”:
printf(“请插入歌曲标题:”);
fgets(歌曲名称,256,标准文本);
printf(“\n”);
printf(“请插入翻译:”);
fgets(翻译,256,标准输入法);
printf(“\n”);
插入输入(容器、歌曲标题、翻译);
打破
案例“3”:
printf(“\n”);
打印散列(容器);
打破
案例“4”:
删除散列(容器);
打破
案例“5”:
#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;
}
}
}
printf("%d,%d,%s,%s\n", 1, 10, "short", "short");
printf("%d,%d,%s,%s\n", 1, 100, "long long string", "short");
printf("%4d,%4d,%20s,%20s\n", 1, 10, "short", "short");
printf("%4d,%4d,%20s,%20s\n", 1, 100, "long long string", "short");