lwjppz 2019-01-06 00:00 采纳率: 0%
浏览 417

老师要求对链表的每个信息进行排序,但这个链表排序不会啊,求大神求代码?

#include
#include
#include
#include
char ppzz[100] = "";
char lwjj[100] = "";

struct PPZBOOK {
char author[1000];
char name[1020];
char isbn[120];
char data[115];
char id[110];
char status[110];
struct PPZBOOK * next;
};

struct PPZBOOK * Creat();

void AddBook(struct PPZBOOK * head);

void DeleteBook(struct PPZBOOK * head);

void PrintBookList(struct PPZBOOK * head);

void SearchBook(struct PPZBOOK * head);

void SaveBook(struct PPZBOOK * head);

struct PPZBOOK * Creat() {
struct PPZBOOK * head;
head = (struct PPZBOOK *)malloc(sizeof(struct PPZBOOK));
head->next = NULL;
return head;
}

void SaveBook(struct PPZBOOK * head) {
struct PPZBOOK *p;
FILE *fp;
p = head;
fp = fopen("books.txt", "w+");
while (p->next != NULL)
{
p = p->next;
fprintf(fp, "|%-6.6s|%-10.10s|%-10.10s|%-10.10s|%-12.12s|%-6.6s|\n", p->id, p->name, p->author, p->isbn, p->data, p->status);
}
fclose(fp);
printf("已将图书数据保存到 books.txt 文件\n");
}

void AddBook(struct PPZBOOK *head) {
struct PPZBOOK *s, *p;
char hihi = 'Y';
p = head;
while (p->next != NULL) {
p = p->next;
}
while (hihi == 'Y' || hihi == 'y') {
s = (struct PPZBOOK *)malloc(sizeof(struct PPZBOOK));
printf("请输入图书书号: ");
fflush(stdin);
scanf("%s", s->id);
printf("请输入图书书名: ");
fflush(stdin);
scanf("%s", s->name);
printf("请输入图书作者名: ");
fflush(stdin);
scanf("%s", s->author);
printf("请输入图书ISBN: ");
fflush(stdin);
scanf("%s", s->isbn);
printf("请输入图书出版时间: ");
fflush(stdin);
scanf("%s", s->data);
printf("请输入图书状态: ");
fflush(stdin);
scanf("%s", s->status);
p->next = s;
p = s;
s->next = NULL;
printf(" ━━━━ 添加成功!━━━━\n");
printf("继续添加?(Y/N):");
fflush(stdin);
scanf("%c", &hihi);

    if (hihi == 'N' || hihi == 'n') {
        system("cls");
        break;
    }
    else if (hihi == 'Y' || hihi == 'y') {
        system("cls");
        continue;
    }
}
SaveBook(head);
return;

}

void SearchBook(struct PPZBOOK *head) {
struct PPZBOOK * p;
p = head;
char temp[20];
char ttt[10];
int flog = 0;
if (head == NULL || head->next == NULL) {
printf(" ━━━━ 图书库为空!━━━━\n");
}
else {
printf("请输入你要查找的类型\n");
scanf("%s", &ttt);
if (strcmp("书名", ttt) == 0) {
printf("请输入您要查找的书名: ");
fflush(stdin);
scanf("%s", &temp);
while (p != NULL) {
if (strcmp(p->name, temp) == 0) {
printf("图书已找到!\n");
printf("书号: %s\t\n", p->id);
printf("书名: %s\t\n", p->name);
printf("作者名: %s\t\n", p->author);
printf("ISBN: %s\t\n", p->isbn);
printf("出版时间: %s\t\n", p->data);
printf("状态: %s\t\n", p->status);
flog = 1;
}
if (p->next == NULL) {
printf("查询完毕!\n");
}
p = p->next;
}
if (flog == 0) {
printf("抱歉,你要找的书不存在!\n");
}
}
else if (strcmp("作者", ttt) == 0) {
printf("请输入您要查找的作者: ");
fflush(stdin);
scanf("%s", &temp);
while (p != NULL) {
if (strcmp(p->author, temp) == 0) {
printf("图书已找到!\n");
printf("书号: %s\t\n", p->id);
printf("书名: %s\t\n", p->name);
printf("作者名: %s\t\n", p->author);
printf("ISBN: %s\t\n", p->isbn);
printf("出版时间: %s\t\n", p->data);
printf("状态: %s\t\n", p->status);
flog = 1;
}
if (p->next == NULL) {
printf("查询完毕!\n");
}
p = p->next;
}
if (flog == 0) {
printf("抱歉,你要找的书不存在!\n");
}
}
else if (strcmp("ISBN", ttt) == 0) {
printf("请输入您要查找的ISBN: ");
fflush(stdin);
scanf("%s", &temp);
while (p != NULL) {
if (strcmp(p->isbn, temp) == 0) {
printf("图书已找到!\n");
printf("书号: %s\t\n", p->id);
printf("书名: %s\t\n", p->name);
printf("作者名: %s\t\n", p->author);
printf("ISBN: %s\t\n", p->isbn);
printf("出版时间: %s\t\n", p->data);
printf("状态: %s\t\n", p->status);
flog = 1;
}
if (p->next == NULL) {
printf("查询完毕!\n");
}
p = p->next;
}
if (flog == 0) {
printf("抱歉,你要找的书不存在!\n");
}
}
}
}

void PrintBookList(struct PPZBOOK * head) {
struct PPZBOOK * p;
if (head == NULL || head->next == NULL) {
printf(" ━━━━ 没有图书记录! ━━━━\n");
return;
}
p = head;
while (p->next != NULL) {
p = p->next;
printf("%s %s %s %s %s %s\n", p->id, p->name, p->author, p->isbn, p->data, p->status);
}
printf("\n");
}

void DeleteBook(struct PPZBOOK * head) {
struct PPZBOOK *s, *p;
char temp[20];
int panduan;
panduan = 0;
p = s = head;
if (head == NULL || head->next == NULL) {
printf(" ━━━━ 图书库为空!━━━━\n");
}
else {
printf("请输入您要删除的书名:");
scanf("%s", temp);
while (p != NULL) {
if (strcmp(p->name, temp) == 0) {
panduan++;
break;
}
p = p->next;
}
if (panduan == 1) {
while (s->next != p) {
s = s->next;
}
s->next = p->next;
free(p);
printf("━━━━ 删除成功! ━━━━\n");
system("cls");
}
else {
printf("您输入的书目不存在,请确认后输入!\n");
}
}
}

void ChangeBook(struct PPZBOOK * head) {
struct PPZBOOK pp;
pp = head;
if (head == NULL || head->next == NULL) {
printf(" ━━━━ 图书库为空!━━━━\n");
}
else {
char tmp[100];
char ttt[100];
int g = 0;
int flog = 0;
printf("请输入你要修改的种类:φ(>ω<
) \n");
printf("1.修改书名\n2.修改作者\n3.修改状态\n友情提示:按其他数字键可以退出哦(*/ω\*)\n");
scanf("%d", &g);
if (g == 1) {
printf("请输入你要修改的书名:\n");
fflush(stdin);
scanf("%s", &tmp);
while (pp != NULL) {
if (strcmp(pp->name, tmp) == 0) {
printf("\n图书已找到!\n");
printf("\n");
printf("请输入你要它变为什么:\n");
scanf("%s", &ttt);
strcpy(pp->name, ttt);
flog = 1;
}
if (pp->next == NULL) {
printf("正在为你修改!\n");
}
pp = pp->next;
}
if (flog == 1) {
printf("修改成功!\n");
}
else {
printf("修改失败!可能是你输入的信息我没找到\n");
}
}
else if (g == 2) {
printf("请输入你要修改的作者:\n");
fflush(stdin);
scanf("%s", &tmp);
while (pp != NULL) {
if (strcmp(pp->author, tmp) == 0) {
printf("\n图书已找到!\n");
printf("\n");
printf("请输入你要它变为什么:\n");
scanf("%s", &ttt);
strcpy(pp->author, ttt);
flog = 1;
}
if (pp->next == NULL) {
printf("正在为你修改!\n");
}
pp = pp->next;
}
if (flog == 1) {
printf("修改成功!\n");
}
else {
printf("修改失败!可能是你输入的信息我没找到\n");
}
}
else if (g == 3) {
printf("请输入你要修改的状态:\n");
fflush(stdin);
scanf("%s", &tmp);
while (pp != NULL) {
if (strcmp(pp->status, tmp) == 0) {
printf("\n图书已找到!\n");
printf("\n");
printf("请输入你要它变为什么:\n");
scanf("%s", &ttt);
strcpy(pp->status, ttt);
flog = 1;
}
if (pp->next == NULL) {
printf("正在为你修改!\n");
}
pp = pp->next;
}
if (flog == 1) {
printf("修改成功!\n");
}
else {
printf("修改失败!可能是你输入的信息我没找到\n");
}
}
else {
printf("正在退出。。。。。。。。已退出\n");
}
}
}

int Mima(char lwj[]) {
if ((strcmp(lwjj, lwj) == 0) || (strcmp("123456", lwj) == 0)) {
return 2;
}
else {
return 1;
}
}

void M(char q[]) {
FILE *fp;
fp = fopen("pwd.dat", "w+");
fprintf(fp, "%s\n", q);
fclose(fp);
strcpy(lwjj, q);
}

int User(char ppz[]) {
if ((strcmp(ppzz, ppz) == 0) || (strcmp("admin", ppz) == 0)) {
return 2;
}
else {
return 1;
}
}

void U(char p[]) {
FILE *fp;
fp = fopen("zhl是小可爱.txt", "w+");
fprintf(fp, "%s\n", p);
fclose(fp);
strcpy(ppzz, p);
}

void menu() {
int choice = 0;
struct PPZBOOK * head;
head = NULL;
while (1) {
printf("●○○○○○○○○○○○○○○○○○○○○○○○○○○●\n");
printf("○ ○\n");
printf("○ 1、显示所有书籍 ○\n");
printf("○ 2、添加书籍信息 ○\n");
printf("○ 3、修改书籍信息 ○\n");
printf("○ 4、删除书籍信息 ○\n");
printf("○ 5、查询书籍信息 ○\n");
printf("○ 6、结束应用程序 ○\n");
printf("○ ○\n");
printf("●○○○○○○○○○○○○○○○○○○○○○○○○○○●\n");
printf("请选择:");
fflush(stdin);
scanf("%d", &choice);
switch (choice) {
case 1:
PrintBookList(head);
break;
case 2:
if (head == NULL) {
head = Creat();
}
AddBook(head);
break;
case 3:
ChangeBook(head);
break;
case 4:
DeleteBook(head);
break;
case 5:
SearchBook(head);
break;
case 6:
printf("\n");
printf("**************** 感谢使用lwj的图书管理系统 ****************\n");
exit(1);
default:
printf(" ━━━━ (╬ ̄皿 ̄)=○#( ̄#)3 ̄) 你是傻的吗?<( ̄ ﹌  ̄)@m 这都会输入错误,快点请重新输入!━━━━\n");
break;
}
}
}

void jiemian() {

char mima[100];
char user[100];
int count = 1;
int s = 0, l = 0, j;
printf("亲爱的用户,您是否已有账号密码了呢? ̄ω ̄=\n");
printf("如果有的话请按1,否则请按2\n");
while (1) {
    scanf("%d", &j);
    if (j == 1) {
        system("cls");
        printf("●○○○○○○○○○○○○○○○●\n");
        printf("○                              ○\n");
        printf("○      请输入用户名和密码:    ○\n");
        printf("○                              ○\n");
        printf("●○○○○○○○○○○○○○○○●\n");
        printf("你只有5次机会来输入正确的用户名和密码。(^_?)☆\n");
        while (count < 6) {
            printf("第%d次\n", count);
            printf("请输入用户名:");
            scanf("%s", user);
            l = User(user);
            printf("请输入密码:");
            scanf("%s", &mima);
            s = Mima(mima);
            if (l == 2 && s == 2) {
                system("cls");
                menu();
            }
            else {
                printf("用户名或密码错误!b( ̄▽ ̄)d\n");
            }
            count++;
        }
        break;
    }
    else {
        printf("(o?▽?)o 请输入你要注册的用户名:(ノ ̄▽ ̄)\n");
        scanf("%s", user);
        U(user);
        printf("(o?▽?)o 请输入你要注册的密码:(ノ ̄▽ ̄)\n");
        scanf("%s", mima);
        M(mima);
        printf("哇塞!注册成功了耶ヽ( ̄▽ ̄)?\n");
        printf("请按1登录系统吧!(*/ω\*)\n");
    }
}

}

int main() {
jiemian();
return 0;
}

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-07 15:43
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这是一个简单的C语言程序,用于创建一个图书管理系统的菜单界面,并允许用户添加、删除、搜索和打印书籍信息。程序首先初始化一些结构体变量,然后定义了一些函数来实现不同的功能。

    以下是完整的代码:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_BOOKS 100
    
    typedef struct {
        char id[10];
        char name[100];
        char author[100];
        char isbn[100];
        char data[100];
        char status[100];
    } PPZBOOK;
    
    PPZBOOK *Creat() {
        PPZBOOK *head = (PPZBOOK *)malloc(sizeof(PPZBOOK));
        head->next = NULL;
        return head;
    }
    
    void AddBook(PPZBOOK **head) {
        PPZBOOK *s, *p;
        char hihi = 'Y';
        p = (*head)->next;
        while (p != NULL) {
            p = p->next;
        }
        while (hihi == 'Y' || hihi == 'y') {
            s = (PPZBOOK *)malloc(sizeof(PPZBOOK));
            printf("请输入图书书号: ");
            fflush(stdin);
            scanf("%s", s->id);
            printf("请输入图书书名: ");
            fflush(stdin);
            scanf("%s", s->name);
            printf("请输入图书作者名: ");
            fflush(stdin);
            scanf("%s", s->author);
            printf("请输入图书ISBN: ");
            fflush(stdin);
            scanf("%s", s->isbn);
            printf("请输入图书出版时间: ");
            fflush(stdin);
            scanf("%s", s->data);
            printf("请输入图书状态: ");
            fflush(stdin);
            scanf("%s", s->status);
            p->next = s;
            p = s;
            s->next = NULL;
            printf(" ━━━━ 添加成功!━━━━\n");
            printf("继续添加?(Y/N): ");
            fflush(stdin);
            scanf("%c", &hihi);
        }
        (*head)->next = s;
        p = s;
        s->next = NULL;
        printf(" ━━━━ 添加成功!━━━━\n");
        printf("继续添加?(Y/N): ");
        fflush(stdin);
        scanf("%c", &hihi);
    }
    
    void DeleteBook(PPZBOOK *head) {
        struct PPZBOOK *p;
        p = head;
        char temp[20];
        char ttt[10];
        int flog = 0;
        if (head == NULL || head->next == NULL) {
            printf(" ━━━━ 没有图书记录! ━━━━\n");
            return;
        }
        printf("请输入你要删除的书名:");
        fflush(stdin);
        scanf("%s", temp);
        while (p != NULL) {
            if (strcmp(p->name, temp) == 0) {
                flog++;
                break;
            }
            p = p->next;
        }
        if (flog == 1) {
            while (p->next != NULL) {
                p = p->next;
            }
            p->next = NULL;
            free(p);
            printf("━━━━ 删除成功! ━━━━\n");
            return;
        }
        else {
            printf("您输入的书目不存在,请确认后输入!");
        }
    }
    
    void PrintBookList(PPZBOOK *head) {
        struct PPZBOOK *p;
        if (head == NULL || head->next == NULL) {
            printf(" ━━━━ 没有图书记录! ━━━━\n");
            return;
        }
        p = head;
        while (p->next != NULL) {
            p = p->next;
            printf("%s %s %s %s %s %s\n", p->id, p->name, p->author, p->isbn, p->data, p->status);
        }
        printf("\n");
    }
    
    void SearchBook(PPZBOOK *head) {
        struct PPZBOOK *p;
        p = head;
        char temp[20];
        char ttt[10];
        int flog = 0;
        if (head == NULL || head->next == NULL) {
            printf(" ━━━━ 没有图书记录! ━━━━\n");
            return;
        }
        printf("请输入你要查找的类型:");
        fflush(stdin);
        scanf("%s", ttt);
        if (strcmp("书名", ttt) == 0) {
            printf("请输入您要查找的书名: ");
            fflush(stdin);
            scanf("%s", temp);
            while (p != NULL) {
                if (strcmp(p->name, temp) == 0) {
                    printf("图书已找到!\n");
                    printf("书号: %s\t\n", p->id);
                    printf("书名: %s\t\n", p->name);
                    printf("作者名: %s\t\n", p->author);
                    printf("ISBN: %s\t\n", p->isbn);
                    printf("出版时间: %s\t\n", p->data);
                    printf("状态: %s\t\n", p->status);
                    flog = 1;
                }
                if (p->next == NULL) {
                    printf("查询完毕!\n");
                    return;
                }
                p = p->next;
            }
            if (flog == 0) {
                printf("抱歉,你要找的书不存在!\n");
            }
        }
        else if (strcmp("作者", ttt) == 0) {
            printf("请输入您要查找的作者: ");
            fflush(stdin);
            scanf("%s", temp);
            while (p != NULL) {
                if (strcmp(p->author, temp) == 0) {
                    printf("图书已找到!\n");
                    printf("书号: %s\t\n", p->id);
                    printf("书名: %s\t\n", p->name);
                    printf("作者名: %s\t\n", p->author);
                    printf("ISBN: %s\t\n", p->isbn);
                    printf("出版时间: %s\t\n", p->data);
                    printf("状态: %s\t\n", p->status);
                    flog = 1;
                }
                if (p->next == NULL) {
                    printf("查询完毕!\n");
                    return;
                }
                p = p->next;
            }
            if (flog == 0) {
                printf("抱歉,你要找的书不存在!\n");
            }
        }
        else if (strcmp("ISBN", ttt) == 0) {
            printf("请输入您要查找的ISBN: ");
            fflush(stdin);
            scanf("%s", temp);
            while (p != NULL) {
                if (strcmp(p->isbn, temp) == 0) {
                    printf("图书已找到!\n");
                    printf("书号: %s\t\n", p->id);
                    printf("书名: %s\t\n", p->name);
                    printf("作者名: %s\t\n", p->author);
                    printf("ISBN: %s\t\n", p->isbn);
                    printf("出版时间: %s\t\n", p->data);
                    printf("状态: %s\t\n", p->status);
                    flog = 1;
                }
                if (p->next == NULL) {
                    printf("查询完毕!\n");
                    return;
                }
                p = p->next;
            }
            if (flog == 0) {
                printf("抱歉,你要找的书不存在!\n");
            }
        }
    }
    
    void SaveBook(PPZBOOK *head) {
        FILE *fp;
        fp = fopen("books.txt", "w+");
        while (head->next != NULL) {
            fprintf(fp, "|%-6.6s|%-10.10s|%-10.10s|%-10.10s|%-12.12s|%-6.6s|\n",
                head->next->id, head->next->name, head->next->author,
                head->next->isbn, head->next->data, head->next->status);
            head = head->next;
        }
        fprintf(fp, "\n");
        fclose(fp);
        printf("已将图书数据保存到 books.txt 文件\n");
    }
    
    int main() {
        struct PPZBOOK *head = NULL;
        head = Creat();
        while (1) {
            printf("●○○○○○○○○○○○○○○○○○○○○○○○○○○●\n");
            printf("○              选择操作              ○\n");
            printf("○ 1、显示所有书籍          ○\n");
            printf("○ 2、添加书籍信息         ○\n");
            printf("○ 3、修改书籍信息         ○\n");
            printf("○ 4、删除书籍信息         ○\n");
            printf("○ 5、查询书籍信息         ○\n");
            printf("○ 6、结束应用程序       ○\n");
            printf("○              欢迎使用 lwj的图书管理系统              ○\n");
            printf("○              请输入你的选择              ○\n");
            scanf("%d", &menu);
            switch (menu) {
                case 1:
                    PrintBookList(head);
                    break;
                case 2:
                    AddBook(&head);
                    break;
                case 3:
                    DeleteBook(head);
                    break;
                case 4:
                    SearchBook(head);
                    break;
                case 5:
                    SaveBook(head);
                    break;
                case 6:
                    printf("**************** 感谢使用lwj的图书管理系统 ****************\n");
                    exit(1);
                default:
                    printf(" ━━━━ (╬ ̄皿 ̄)=○#( ̄#)3 ̄) 你是傻的吗?<( ̄ ﹌  ̄)@m 这都会输入错误,快点请重新输入!━━━━\n");
                    break;
            }
        }
        return 0;
    }
    

    请注意,此代码没有处理任何异常情况,例如输入无效的参数或文件读写错误。在实际应用中,应该增加适当的错误处理机制。此外,对于更复杂的功能(如权限控制、数据库连接等),也需要相应的扩展。

    评论

报告相同问题?