使用链表和文件操作创建电话簿

问题描述 投票:0回答:2

我正在使用单链表在 C 中创建电话簿。我在 GCC 中遇到的问题是,在某些行,GCC 会抛出带有数组类型错误的表达式赋值。在某些行,它向我发出警告,例如 format %s 需要 char* 类型的参数。

我尝试过不使用排序功能,将其放在注释中并执行。该程序执行并询问号码、名字和姓氏。但只有名字和姓氏才能正确显示。电话号码显示一些奇怪的值,看起来像内存地址。

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

struct node
{
    char firstname[20];
    char lastname[20];
    int number[15];
    struct node *next;
};

struct node *start=NULL;

struct node *getnode()
{
    return((struct node *)malloc(sizeof(struct node)));
} 


void display(struct node *start)
{
    struct node *temp;
    temp=start;
    while(temp!=NULL)
    {
        printf("%s\n",temp->firstname);
        printf("%s\n",temp->lastname);
        printf("%d\n",temp->number);
        temp=temp->next;
    }
}
/*void sort()
{
    struct node *temp,*pretemp;
    char *p;
    temp=start;
    pretemp=start->next;
    while(pretemp!=NULL)
    {
    if(strcmp(temp->lastname,pretemp->lastname)>0)
    {
        if(temp->lastname==pretemp->lastname)
        {
            if(strcmp(temp->firstname,pretemp->firstname)>0)
            {
                p=temp->firstname;
                temp->firstname=pretemp->firstname;
                pretemp->firstname=p;   
            }
        }
        else
        {
                p=temp->lastname;
                temp->lastname=pretemp->firstname;
                pretemp->firstname=p;
        }
    }
    }
}
*/
void insert()
{
    struct node *temp,*nn;
    nn=getnode();
    temp=start;
    while(temp->next!=NULL)
    {
        temp=temp->next;
        
    }
    printf("Enter First name:\n");
    scanf("%s",&nn->firstname);
    printf("Enter Last name:\n");
    scanf("%s",&nn->lastname);
    printf("Enter number:\n");
    scanf("%d",&nn->number);
    temp->next=nn;
    nn->next=NULL;
    display(start);
}

struct node *create()
{
    struct node *temp,*nn;
    if(start!=NULL)
        insert();
    else
    {
    nn=getnode();
    start=nn;
    temp=start;
    printf("Enter First name:\n");
    scanf("%s",&nn->firstname);
    printf("Enter Last name:\n");
    scanf("%s",&nn->lastname);
    printf("Enter number:\n");
    scanf("%d",&nn->number);
    nn->next=NULL;
    display(start);
    }
}
void search()
{
    struct node *temp;
    char *f,*l;
    temp=start;
    printf("Enter name to be searched:\n");
    scanf("%s",&f);
    scanf("%s",&l);
    while((temp->firstname==f)&&(temp->lastname==l))
    {
        temp=temp->next;
    }
    printf("%s\n",temp->firstname);
    printf("%s\n",temp->lastname);
    printf("%d\n",temp->number);
}

void del()
{
    struct node *pretemp,*temp;
    char *f,*l;
    temp=start;
    pretemp=start->next;
    printf("Enter name :");
    scanf("%s",&f);
    scanf("%s",&l);
    while(temp!=NULL)
    {
        if((pretemp->firstname==f)&&(pretemp->lastname==l))
        {
            printf("%s ",temp->firstname);
            printf("%s ",temp->lastname);
            printf("%s ",temp->number);
            temp->next=pretemp->next;
            free(pretemp);
            break;
        }
        else 
        {
            temp=temp->next;
            pretemp=pretemp->next;
        }
        
        
    }
}

int main()
{
    int op,ch;
    do{
        printf("-------Welcome--------\n ");
        printf("1.Create\n2.Insert\n3.Display\n4.Delete\n5.Search\n6.Sort\n");
        printf("Enter your choice:");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: create();
            break;
            case 2: insert();
            break;
            case 3: display(start);
            break;
            case 4: del();
            break;
            case 5:search();
            break;
            /*case 6:sort();
            break;*/
        }
        printf("Do you want to quit ? 1 for no / 0 for yes:");
        scanf("%d",&op);
    }while(op!=0);
return 0;
}
roschlynn@Roschlynn-Asus:/mnt/c/Users/ROSCHLYNN D'SOUZA/Desktop/Test$ ./a.out
-------Welcome--------
 1.Create
2.Insert
3.Display
4.Delete
5.Search
6.Sort
Enter your choice:1
Enter First name:
Ivan
Enter Last name:
Dsouza
Enter number:
123456789
Ivan
Dsouza
-499915096
Do you want to quit ? 1 for no / 0 for yes:0
roschlynn@Roschlynn-Asus:/mnt/c/Users/ROSCHLYNN D'SOUZA/Desktop/Test$

我希望输出是我输入的电话号码,但它似乎正在获取该号码的内存地址。

c data-structures
2个回答
0
投票

如果您只是将

int number[15];
更改为
long number;
,它就会按预期工作。


0
投票

您需要使用

uint64_t
来存储电话号码,请参阅以下更新的代码,

#include<stdint.h>
 struct node
{
    char firstname[20];
    char lastname[20];
    uint64_t number;
    struct node *next;
};

更改 scanf() 和 printf(),

scanf("%llu",&nn->number);
printf("%llu",temp->number);
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.