icoding测试代码———实验六

本程序适用于:

 icoding的实验六并没有一个测试代码,其都是直接编写一个函数的形式,现在把实验六的原型框架代码分享。

您需要在/*补充代码*/处编写您的代码

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <stdbool.h>

#include <ctype.h>


#define MAX 100

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


//全局变量,存储当前商品的数量
int CurrentCnt;

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)
{
	int ch, i = 0;

	while (isspace(ch = getchar()))
		;
	while (ch != '\n' && ch != EOF) {
		if (i < n)
			str[i++] = ch;
		ch = getchar();
	}
	str[i] = '\0';
	return i;
}

/**********************************************************
 * main    
 **********************************************************/
int main(void)
{
    GoodsList *goodsList;
    init_list(&goodsList);
    GoodsInfo item;
    char temp_id[MAX_ID_LEN];
    while (1) {
	int choice;
	printf("超市商品管理系统\n");
	printf("********************************************\n");
	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");
	printf("********************************************\n");
	printf("输入您的选择: ");

	scanf("%d", &choice);
	switch (choice) {
	  case 1:
		 /* 补充代码*/
	  case 2:
		item = read_goods_info();
		printf("输入要修改记录的 ID:");
		 /* 补充代码read_line调用*/
		 /* 补充代码change_item调用*/
		break;
  	  case 3:
		item = read_goods_info();
		int pos;
		printf("输入数字表明你要插入的商品位置:0.商品列表尾部 1.商品列表头部 i.商品列表中间第i号位置\n");
		scanf("%d", &pos);
		 /* 补充代码insert_item调用*/
		break;
	  case 4:
		printf("输入要删除记录的 ID:");
		 /* 补充代码read_line调用*/
		 /* 补充代码delete_item调用*/
		break;
	  case 5:
		printf("输入要删除记录的 ID:");
		 /* 补充代码read_line调用*/
		 /* 补充代码search_item调用*/
		break;	
	  case 6:
		 /* 补充代码save_to_file调用*/
		printf("您已经存盘并退出超市商品管理系统!\n");
		return 0;
	  case 7:
		 /* 补充代码bubble_sort调用*/(
		break;
	  case 8:
		/* 补充代码destory_list_and_file调用*/
		printf("您已经删除商品文件内容以及链表内容!\n");
		break;
	  default:
		/* 补充代码destory_lis调用*/
		printf("您已经退出超市商品管理系统!\n");
		return 0;
	}
    }
}

/**********************************************************
 * init_list   
 **********************************************************/
void init_list(GoodsList **L) {
    FILE *fp;
    GoodsInfo goodsInfo;
    GoodsList *p, *r;

    (*L) = (GoodsList *)malloc(sizeof(GoodsList));
    r = (*L);
    if ((fp = fopen(GOODS_FILE_NAME, "r")) == NULL)
    {
        if ((fp = fopen(GOODS_FILE_NAME, "w")) == NULL)
	printf("提示:不能创建商品文件\n");
    }
    else {
        while (!feof(fp))
        {
            fscanf(fp, "%s", goodsInfo.goods_id);
            /* 仿写fscanf完成goodsInfo其他部分的内容*/
            p = (GoodsList*)malloc(sizeof(GoodsList));
            /* 完成GoodsList的构建*/
            CurrentCnt++;
        }
    }
    fclose(fp);
    r->next = NULL;
    printf("商品的链表文件已建立,有%d个商品记录\n", CurrentCnt);
}


/**********************************************************
 * insert_item  
 **********************************************************/
bool insert_item(GoodsList *L, GoodsInfo goodsInfo, int choice) {
    GoodsList *temp;
    GoodsList *pre = L, *p = L->next;
    int i;
    if (CurrentCnt >= 100) {
        printf("信息库已满,要插入请先删除一定量的商品数据!\n");
        return false;
    }

    switch (choice) {
        case 0:
                //尾插法插入新商品
                /* 补充代码*/		
                return true;
        case 1:
                //头插法插入新商品
                /* 补充代码*/		
               return true;
         default:
                //中间i号位置插入新商品,例如:输入3,应该在第二个节点后插入新节点
	// CurrentCnt 改为 CurrentCnt+1,因为当 CurrentCnt 为2时,链表中有两个记录,
	// 此时输入3,即 choise为 3,表示在第二条记录后插入数据,新记录成为第3条数据
	if (choice <= CurrentCnt+1 && choice > 0) {
	         /* 补充代码*/
	}
	else {
   	         printf("输入的位置超出当前商品列表范围\n");
                         return false;
	}
        }
}



/**********************************************************
 * delete_item  
 **********************************************************/
bool delete_item(GoodsList *L, char* goods_id) {
     GoodsList *pre = L, *p = L->next;
      /* 补充代码*/
}


/**********************************************************
 * search_item   
 **********************************************************/
GoodsList* search_item(GoodsList *L, char* goods_id) {
    GoodsList *p = L->next;
    /* 补充代码*/
}


/**********************************************************
 * change_item  
 **********************************************************/
bool change_item(GoodsList *L, char* goods_id, GoodsInfo new_info) {
     GoodsList *p = L->next;
     GoodsList *ptarget = search_item(L, goods_id);
     /* 补充代码*/
}


/**********************************************************
 *output_one_item   
 **********************************************************/
void output_one_item(GoodsList *p){
	/* 补充代码*/
}
‘
/**********************************************************
 * output_all_items   
 **********************************************************/
void output_all_items(GoodsList *L)
{
	/* 补充代码*/
}


/**********************************************************
 * destory_list
 **********************************************************/
void destory_list(GoodsList **L) {
	if (L == NULL || *L == NULL)
		return;
	GoodsList *pre = *L, *p = (*L)->next;
	/* 补充代码*/
}

/**********************************************************
 * destory_list_and_file
 **********************************************************/

void destory_list_and_file(GoodsList **L) {
	 /* 补充代码:调用destory_list*/
	remove(GOODS_FILE_NAME);
}
/**********************************************************
 * save_to_file   
 **********************************************************/
int save_to_file(GoodsList *L) {
    if(L == NULL)
        return 0;
    GoodsList *p = L->next;
    FILE *fp;
    if ((fp = fopen("goodsinfo.txt", "w")) == NULL)
    {
        printf("提示:不能打开商品文件\n");
        return 0;
    }
    int save_count = 0;
    while (p != NULL)
    {
        fprintf(fp, "%s\t", p->data.goods_id);
       /* 仿写fprintf完成goodsInfo其他部分的内容*/
        p = p->next;
        save_count++;
    }
    fclose(fp);
    return save_count;
}


/**********************************************************
 * bubble_sort  
 **********************************************************/
void bubble_sort(GoodsList *L) {
    GoodsList *p;
    GoodsInfo temp;
    int n = CurrentCnt;
    int i, j;
    
    if (L == NULL || L->next == NULL)
        printf("当前链表中没有商品\n");
    for (j = 1; j < n; ++j) {
        p = L->next;
        for (i = 0; i < n - j; ++i) {
            if (p->data.goods_price > p->next->data.goods_price) {
	/* 补充代码*/
            }
            p = p->next;
        }
    }
}


/**********************************************************
 * read_goods_info  
 **********************************************************/
GoodsInfo read_goods_info(){
	GoodsInfo goodsInfo;
	printf("输入你要插入的商品信息:\n");
	printf("商品ID:");
	 /* 补充代码read_line调用*/
	printf("商品名称:");
	 /* 补充代码read_line调用*/
	printf("商品价格:");
	scanf("%d",&goodsInfo.goods_price);
	printf("商品折扣:");
	 /* 补充代码read_line调用*/
	printf("商品数量:");
	scanf("%d", &goodsInfo.goods_amount);
	printf("商品剩余:");
	scanf("%d", &goodsInfo.goods_remain);
	return goodsInfo;
}



实验六主要考察大家的综合能力运用,涉及链表、排序、查找插入等算法,能让大家更好的理解链表这一内容。

02-初始化”“11-保存文件”题目涉及到的goodsinfo.txt的文件内容如下,请将其置于测试代码的同一文件夹下。文件夹名字不要带中文。

10	test1	101	0.9	9	380
11	test2	102	0.85	54	280
12	test3	103	0.95	190	180
13	test4	104	0.6	30	213
15	test5	105	0.9	9	33
16	test6	106	0.85	54	80
17	test7	107	0.9	9	380
18	test8	108	0.85	154	280
19	test9	109	0.95	190	180
20	test10	110	0.6	330	213
21	test11	111	0.9	9	33
22	test12	112	0.85	54	80
23	test13	113	0.6	330	213
24	test14	114	0.95	9	33
25	test15	115	0.8	64	80
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谨慎谦虚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值