20届icoding实验六(10~13)

代码仅供参考,望多多思考

#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);

链表:10-释放链表

链表:10-释放链表并删除文件
函数原型:void destory_list_and_file(GoodsList **L), 该函数调用destory_list释放包括头结点在内的所有结点,将指向链表头结点的指针为NULL,最后删除存储商品信息的文件goodinfo.txt。

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

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

void destory_list_and_file(GoodsList** L)
{
    destory_list(L);
    remove("goodsinfo.txt");
}

链表:11-保存文件

链表:11-保存文件
函数原型:int save_to_file(GoodsList *L) 将当前链表所有的商品信息保存到文件 goodsinfo.txt 中,其中L为指向链表头结点的指针,保存成功后,该函数返回商品的数量。

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

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

int save_to_file(GoodsList* L)
{
    GoodsList* gl;
    gl = L->next;
    int i = 0;
    FILE* fp = fopen("goodsinfo.txt", "w+");
    if (fp == NULL)
        perror("fopen()");
    while (gl) {
        GoodsInfo gi = gl->data;
        fprintf(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);
        gl = gl->next;
        i++;
    }
    fclose(fp);
    return i;
}

链表:12-排序

链表:12-排序
函数原型:void bubble_sort(GoodsList *L) L为指向链表头结点的指针,该函数利用冒泡排序算法,按价格从低到高对商品进行排序。

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

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

void bubble_sort(GoodsList* L)
{
    GoodsList* p;
    p = L;
    int n, h, j;
    n = 0;
    if (!p)
        return;

    while (p) {
        n++;
        p = p->next;
    }
    if (n == 1)
        return;
    for (j = 0; j < n - 1; j++) {
        p = L;
        for (h = 0; h < n - 1 - j; h++) {
            if (p->data.goods_price > p->next->data.goods_price) {
                GoodsInfo temp;
                temp = p->next->data;
                p->next->data = p->data;
                p->data = temp;
            }
            p = p->next;
        }
    }
}

链表:13-读商品信

链表:13-读商品信息
函数原型:GoodsInfo read_goods_info() 该函数调用read_line及scanf等函数,按“商品ID、商品名称、商品价格、商品折扣、商品数量、商品剩余数量”的顺序让用户输入,并将这些信息保存到 GoodsInfo 结构体中,函数最后返回该结构体。


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

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

GoodsInfo read_goods_info()
{
    GoodsInfo t;
    printf("商品ID:");
    read_line(t.goods_id, MAX_ID_LEN);
    printf("商品名称:");
    read_line(t.goods_name, MAX_NAME_LEN);
    printf("商品价格:");
    scanf("%d", &t.goods_price);
    printf("商品折扣:");
    read_line(t.goods_discount, MAX_DISCOUNT_LEN);
    printf("商品数量:");
    scanf("%d", &t.goods_amount);
    printf("商品剩余数量:");
    scanf("%d", &t.goods_remain);
    return t;
}

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值