顺序表练习

请实现购物车系统,把商品[商品名称、商品属性、价格、个数]信息存储到顺序表中

  1. 在堆区申请空间

  1. 实现顺序表数据元素在表尾录入

  1. 实现顺序表输出

  1. 实现顺序表按下标插入商品信息.

  1. 实现顺序表按下标删除商品信息

  1. 查找商品名称key是否在购物车信息中出现

  1. 修改商品名称key的购买数量, 修改为n

  1. 删除最贵的商品信息

  1. 对商品总价实现升序排序

  1. 如果有两个商品名称相同的商品信息,则去重

  1. 释放堆区空间

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
 
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
 
#define MAXSIZE 20
 
typedef struct
{
    char name[MAXSIZE];
    char attribute[MAXSIZE];
    float price;
    int num;
}Goods;

typedef struct
{
    int len;
    Goods data[MAXSIZE];
}SeqList;
 
SeqList *createSpace();
void insertByRear(SeqList *s,Goods e);
void output(SeqList *s);
void insertBySub(SeqList *s,int sub,Goods e);
void deleteBySub(SeqList *s,int sub);
int searchByName(SeqList *s,char *name);
void updateByName(SeqList *s,char *name,int n);
void deleteMostExpensive(SeqList *s);
void removeSame(SeqList *s);
void sort(SeqList *s);
#endif

func.c

#include "head.h"
 
SeqList *createSpace()
{
    SeqList *s=(SeqList *)malloc(sizeof(SeqList));
    if(!s)
        return NULL;
    s->len=0;
    return s;
}
 
bool _isFull(SeqList *s)
{
    if(s->len==MAXSIZE)
    {
        return true;
    }
    return false;
}
 
bool _isEmpty(SeqList *s)
{
    if(s->len==0)
    {
        return true;
    }
    return false;
}
 
void insertByRear(SeqList *s,Goods e)
{
    if (_isFull(s))
        return ;
    s->data[s->len++]=e;
}
 
void output(SeqList *s)
{
    for (int i=0; i<s->len; i++)
    {
        printf("%d: name=%s attribute=%s price=%.2f num=%d\n",i,s->data[i].name,s->data[i].attribute,s->data[i].price,s->data[i].num);
    }
}
 
void insertBySub(SeqList *s,int sub,Goods e)
{
    if(_isFull(s)||sub<0||sub>s->len)
    {
        puts("failure");
        return ;
    }
    for (int i=s->len-1; i>=sub; i--)
    {
        s->data[i+1]=s->data[i];
    }
    s->data[sub]=e;
    s->len++;
    output(s);
}
 
void deleteBySub(SeqList *s,int sub)
{
    if(_isEmpty(s)||sub<0||sub>=s->len)
    {
        puts("failure");
        return;
    }
    for (int i=sub; i<s->len; i++)
    {
        s->data[i]=s->data[i+1];
    }
    s->len--;
    output(s);
}
 
int searchByName(SeqList *s,char *name)
{
    if(_isEmpty(s))
        return -1;
    for (int i=0; i<s->len; i++)
    {
        if(!strcmp(name,s->data[i].name))
            return i;
    }
    return -1;
}
 
void updateByName(SeqList *s,char *name,int n)
{
    int sub=searchByName(s,name);
    if(sub==-1)
    {
        puts("failure");
        return ;
    }    
    s->data[sub].num=n;
    output(s);
}

void _deleteBySub(SeqList *s,int sub)
{
    for (int i=sub; i<s->len; i++)
    {
        s->data[i]=s->data[i+1];
    }
    s->len--;
}

void deleteByName(SeqList *s,char*name)
{
    int sub=searchByName(s,name);
    if(sub==-1)
    {
        puts("failure");
        return ;
    }    
    _deleteBySub(s,sub);
}
 
void deleteMostExpensive(SeqList *s)
{
    int maxi=0;
    for (int i=0; i<s->len; i++)
    {
        if(s->data[i].price>s->data[maxi].price)
            maxi=i;
    }
    _deleteBySub(s,maxi);
}



void removeSame(SeqList *s)
{
    for (int i=0; i<s->len; i++)
    {
        for (int j=i+1; j<s->len; j++)
        {
            if(!strcmp(s->data[i].name,s->data[j].name))
            {
                _deleteBySub(s,j);
                j--;
            }
        }
    }
}
 
void sort(SeqList *s)
{
    Goods tmp;
    for (int i=1; i<s->len; i++)
    {
        for (int j=0; j<s->len-i; j++)
        {
            if(s->data[j].price>s->data[j+1].price)
            {
                tmp=s->data[j];
                s->data[j]=s->data[j+1];
                s->data[j+1]=tmp;
            }
        }
    }
}

main.c

#include "head.h"

void inputName();
 
int main(int argc, const char *argv[])
{
    SeqList *s=createSpace();
    if(!s)
        return 0;
    int n,sub;
    char name[MAXSIZE];
    Goods e;
    
    printf("insert num:");
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        inputName(&e);
        insertByRear(s,e);
    }
    puts("---output---");
    output(s);
    puts("---insertBySub---");
    printf("insert sub:");
    scanf("%d",&sub);
    inputName(&e);
    insertBySub(s,sub,e);
    puts("---deleteBySub---");
    printf("delete sub:");
    scanf("%d",&sub);
    deleteBySub(s,sub);
    puts("---searchByName---");
    printf("name:");
    scanf("%s",name);  
    if(searchByName(s,name)==-1)
        puts("failure");
    else
        printf("success\n");
    puts("---updateByName---");
    printf("key:");
    scanf("%s",name);
    printf("num:");
    scanf("%d",&n);
    updateByName(s,name,n);
    puts("---deleteMostExpensive---");
    deleteMostExpensive(s);
    output(s);
    puts("---removeSame---");
    removeSame(s);
    output(s);
    puts("---sort---");
    sort(s);
    output(s);
    free(s);
 
    return 0;
}

void inputName(Goods *e)
{
    printf("name:");
    scanf(" %s",e->name);
    printf("attribute:");
    scanf(" %s",e->attribute);
    printf("price:");
    scanf("%f",&e->price);
    printf("num:");
    scanf("%d",&e->num);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值