求大佬解救

肆 意 2021-03-04 07:23:48
...全文
104 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-06
  • 打赏
  • 举报
回复
希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html
赵4老师 2021-03-05
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define _CRT_SECURE_NO_WARNINGS
struct commodity {
    int num;
    char name[20];
    char price[3];
    int storage;
};
struct Node {
    //int data;
    struct commodity data;
    struct Node* next;
} Node;
struct Node* list;
struct Node* createlist() {
    struct Node* headNode=(struct Node*)malloc(sizeof(struct Node));
    headNode->next =NULL;
    return headNode;
}
//创造节点
struct Node* createNode(struct commodity data) {
    struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data =data;
    newNode->next =NULL;
    return newNode;
}
//插入
void insertNodeByHead(struct Node* headNode, struct commodity data) {
    struct Node* newNode = createNode(data);
    newNode->next = headNode->next;
    headNode->next = newNode;
}
//查找
struct Node*findByName(struct Node* headNode, char * name) {
    struct Node* pMove = headNode->next;
    while (pMove) {
        if (!strcmp(pMove->data.name,name)) {
            return pMove;
        }
        //没有往下走pMove = pMove->next;
    }
    return NULL;
}
//删除
void deleteNodeByName(struct Node* headNode, char * name) {
    //找到指定位置那个结点,以及制定位置前面那个结点//要有两个相邻的指针
    struct Node* p = headNode;
    struct Node* q = headNode->next;
    if (q == NULL) {
        printf("么有(没有)可用信息,无法删除 ");
    } else {
        while (strcmp(q->data.name,name)) {
            p = q;
            q = p->next;
            if (q == NULL) {
                printf("未找到制定位置,无法删除 ");
                return;
            }
        }
        p->next = q->next;
        free(q);
    }
}
//删除
void deleteNodeByNum(struct Node* headNode, int num) {
    //找到指定位置那个结点,以及制定位置前面那个结点//要有两个相邻的指针
    struct Node* p = headNode;
    struct Node* q = headNode->next;
    if (q == NULL) {
        printf("么有(没有)可用信息,无法删除 ");
    } else {
        while (q->data.num!=num) {
            p = q;
            q = p->next;
            if (q == NULL) {
                printf("未找到制定位置,无法删除 ");
                return;
            }
        }
        p->next = q->next;
        free(q);
    }
}
//打印
void printList(struct Node*headNode) {
    struct Node* pMove = headNode->next;
    printf("商品代码 商品名称 商品单价 商品库存量");
    while (pMove) {
        printf("%d %s %s %d ", pMove->data.num, pMove->data.name, pMove->data.price, pMove->data.storage);
        pMove = pMove->next;
    }
    printf(" ");
}
void menu() {
    printf(" ");
    printf(" 0.退出系统 ");
    printf(" 1.录入信息 ");
    printf(" 2.显示信息 ");
    printf(" 3.删除信息 ");
    printf(" 4.查找信息 ");
    printf(" 5.保存到文件 ");
}
void menuOfDelete() {
    printf(" 1.按照商品名称删除 ");
    printf(" 2.按照商品代码删除 ");
}
void keyDownOfDelete() {
    int choice;
    char name[20];
    int num;
    scanf("%d", &choice);
    switch (choice) {
    case 1://1.按照商品名称删除
        printf("请输入要删除的商品名称:");
        scanf("%s", name);
        deleteNodeByName(list, name);
        break;
    case 2://2.按照商品代码删除
        printf("请输入要删除的商品代码:");
        scanf("%d", &num);
        deleteNodeByNum(list, num);
        break;
    default:printf("输入错误,无法删除 ");
    }
}
//保存到文件
void saveInfoToFile(struct Node* list, char *filePath, char *mode) {
    struct Node* pMove = list->next;
    FILE *fp =fopen(filePath, mode);
    while (pMove) {
        fprintf(fp, "%d %s %s %d ", pMove->data.num, pMove->data.name, pMove->data.price, pMove->data.storage);
        pMove = pMove->next;
    }
    fclose(fp);
}
//读文件
void readInfoFromFile(struct Node* list, char *filePath, char *mode) {
    struct commodity data;
    FILE *fp =fopen(filePath, mode);
    while (fscanf(fp, "%d %s %s %d ", &data.num, data.name, data.price, &data.storage) !=EOF) {
        insertNodeByHead(list, data);
    }
    fclose(fp);
}
//交互
void keyDown() {
    int choice;
    struct commodity comInfo;
    char temp= ' ';
    choice=getchar();
    switch (choice) {
    case 0:
        system("pause");
        exit(0);
        break;
    case 1://1.录入信息
        while (1) {
            printf("请输入商品代号,商品名称,商品价格,商品库存量:");
            fflush(stdin);
            scanf("%d%s%s%d", &comInfo.num, comInfo.name, comInfo.price, &comInfo.storage);
            //链表的插入
            insertNodeByHead(list, comInfo);
            printf("是否继续?(N)");
            fflush(stdin);
            temp = getchar();
            if (temp == 'N' ||temp== 'n')break;
        }
        break;
    case 2://2.显示信息
        printList(list);
        break;
    case 3://3.删除信息
        menuOfDelete();
        keyDownOfDelete();
        break;
    case 4://4.查找信息
        printf("请输入要查找的商品名称:");
        scanf("%s", comInfo.name);
        if (findByName(list, comInfo.name) != NULL) {
            printf("%d %s %s %d ", findByName(list, comInfo.name)->data.num,findByName(list, comInfo.name)->data.name, findByName(list, comInfo.name)->data.price,findByName(list, comInfo.name)->data.storage);
        } else {
            printf("未找到相关信息! ");
        }
        break;
    case 5://5.保存到文件
        saveInfoToFile(list,"1.txt", "w");
        break;
    default:
        printf("输入错误,重新输入 ");
        break;
    }
}
int main() {
    list=createlist();
    readInfoFromFile(list, "1.txt", "r");
    while (1) {
        menu();
        keyDown();
        system("pause");
        system("cls");
    }
    return 0;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧