20届icoding实验六(1~3)

仅供参考,望自己多多思考

01-软件界面控制
实现一个数字选项式的启动界面,程序输入数据为(1-8),参考界面如下:
  1.显示所有商品的信息
  2.修改某个商品的信息
  3.插入某个商品的信息
  4.删除某个商品的信息
  5.查找某个商品的信息
  6.商品存盘并退出系统
  7.对商品价格进行排序
  8.(慎用)删除所有内容
  其他.不存盘并退出系统

相关结构及函数声明如下:

#define GOODS_FILE_NAME "goodsinfo.txt"

#define MAX_ID_LEN 30

#define MAX_NAME_LEN 30

#define MAX_PRICE_LEN 30

#define MAX_DISCOUNT_LEN 30
typedef struct {
char goods_id[MAX_ID_LEN];
char goods_name[MAX_NAME_LEN];
int goods_price;
char goods_discount[MAX_DISCOUNT_LEN];
int goods_amount;
int goods_remain;
} GoodsInfo;

 

typedef struct node
{
GoodsInfo data;
struct node *next;
} GoodsList;

 

GoodsInfo read_goods_info();
void init_list(GoodsList **pL);
void destory_list(GoodsList **pL);
void destory_list_and_file(GoodsList **pL);
int save_to_file(GoodsList *L);
void output_one_item(GoodsList *L);
void output_all_items(GoodsList *L);
bool insert_item(GoodsList *L, GoodsInfo item, int choice);
bool delete_item(GoodsList *L, char* goods_id);
GoodsList* search_item(GoodsList *L, char* goods_id);
bool change_item(GoodsList *L, char* goods_id, GoodsInfo new_info);
void bubble_sort(GoodsList *L);
int read_line(char str[], int n);

该部分声明已包含在 “lab52.h”中。

#include "lab52.h" // 请不要删除本行头文件,否则检查不通过
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x, y;
    char goods_id[MAX_ID_LEN];
    GoodsList* p;
    GoodsInfo h1, h2;
    init_list(&p);
    printf("1.显示所有商品的信息\n");
    printf("2.修改某个商品的信息\n");
    printf("3.插入某个商品的信息\n");
    printf("4.删除某个商品的信息\n");
    printf(" 5.查找某个商品的信息\n");
    printf(" 6.商品存盘并退出系统\n");
    printf("7.对商品价格进行排序\n");
    printf(" 8.(慎用)删除所有内容\n");
    printf("存盘并退出系统\n");

    while (1) {
        printf("please enter a number:");
        scanf("%d", &x);
        switch (x) {
        case 1:
            output_all_items(p);
            break;
        case 2:
            read_line(goods_id, MAX_ID_LEN);
            h1 = read_goods_info();
            change_item(p, goods_id, h1);
            if (change_item(p, goods_id, h1))
                printf("修改成功\n");
            break;
        case 3:
            h2 = read_goods_info();
            printf("enter your choice:\n");
            scanf("%d", &y);
            insert_item(p, h2, y);
            if (insert_item(p, h2, y))
                printf("插入成功\n");
            break;
        case 4:
            read_line(goods_id, MAX_ID_LEN);
            delete_item(p, goods_id);
            if (delete_item(p, goods_id))
                printf("删除成功\n");
            break;
        case 5:
            read_line(goods_id, MAX_ID_LEN);
            search_item(p, goods_id);
            break;
        case 6:
            save_to_file(p);
            exit(0);
            break;
        case 7:
            bubble_sort(p);
            break;
        case 8:
            destory_list_and_file(&p);
            break;
        default:
            destory_list(&p);
            exit(0);
        }
    }
    return 0;
}

02-初始化
函数原型:VOID INIT_LIST(GOODSLIST **L) 其中 *L 为指向链表头结点的指针,L为指向链表头结点指针的地址,INIT_LIST首先创建链表的头结点,之后读取GOODSINFO.TXT(示例文件下载)中的商品信息,并初始化链表,函数执行后,需确保 *L 为指向链表头结点的指针。

init_list 实现商品信息链表的初始化,函数从已有的商品信息文件goodsinfo.txt(示例文件下载)中读入商品信息,并且分配内存保存至链表中。
为了方便在表头插入和删除结点的操作,经常在表头结点(存储第一个元素的结点)的前面增加一个结点,称之为头结点或表头附加结点。这样原来的表头指针由指向第一个元素的结点改为指向头结点,头结点的数据域为空,头结点的指针域指向第一个元素的结点。

#include "lab52.h" // 请不要删除本行头文件,否则检查不通过
#include <stdio.h>
#include <stdlib.h>

extern int CurrentCnt; // 请不要删除本行的全局变量声明,否则检查不通过
void init_list(GoodsList** L)
{
    GoodsList* p;
    int i = 0;
    *L = (GoodsList*)malloc(sizeof(GoodsList));
    if (*L == NULL) {
        return;
    }
    GoodsList* hpead = *L;
    GoodsInfo gi;
    FILE* fp = fopen("goodsinfo.txt", "a+");
    if (fp == NULL) {
        perror("fopen()");
        return;
    }
    while (fscanf(fp, "%s %s %d %s %d %d\n", gi.goods_id, gi.goods_name, &gi.goods_price, gi.goods_discount, &gi.goods_amount, &gi.goods_remain) != EOF) {
        GoodsList* cur = (GoodsList*)malloc(sizeof(GoodsList));
        if (cur == NULL) {
            continue;
        }
        cur->data = gi;
        hpead->next = cur;
        hpead = hpead->next;
        i++;
    }
    printf("目前有 %d个商品信息 ", i);
}

链表:03-插入
函数原型:BOOL INSERT_ITEM(GOODSLIST *L, GOODSINFO GOODSINFO, INT CHOICE), L为指向链表头结点的指针,函数根据CHOISE的值,将GOODSINFO插入到链表的指定位置,如果成功插入,函数返回TRUE,如果未插入,函数返回FALSE。

分别实现头插法、尾插法,中间位置插入三种:
用户输入0,将商品信息插入到链表尾部;
用户输入1,将商品信息插入到链表头部;
用户输入其它正整数i,将商品信息插入到链表中间第i号位置, 例如:输入3,应该在第二个节点后插入新节点

#include "lab52.h" // 请不要删除本行头文件,否则检查不通过
#include <stdio.h>
#include <stdlib.h>

extern int CurrentCnt; // 请不要删除本行的全局变量声明,否则检查不通过

bool insert_item(GoodsList* L, GoodsInfo goodsInfo, int choice)
{

    GoodsList *cur, *new_node;
    new_node = (GoodsList*)malloc(sizeof(GoodsList));
    if (new_node == NULL) {
        return false;
    }
    new_node->data = goodsInfo;
    new_node->next = NULL;
    cur = L->next;
    int i = 2;
    switch (choice) {
    case 0: //尾插
        if (cur == NULL) {
            L->next = new_node;
            CurrentCnt++;
            break;
        } else {
            while (cur->next) {
                cur = cur->next;
            }
        }
        cur->next = new_node;
        CurrentCnt++;
        break;
    case 1: //头插
        if (cur == NULL) {
            L->next = new_node;
            CurrentCnt++;
        } else {
            new_node->next = L->next;
            L->next = new_node;
            CurrentCnt++;
        }
        break;
    default: //任意插
        while (cur) {
            if (i == choice) {
                new_node->next = cur->next;
                cur->next = new_node;
                CurrentCnt++;
                return true;
            }
            cur = cur->next;
            i++;
        }
        return false;
        break;
    }
    return true;
}

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值