数据结构(C语言)课设5——药店的药品销售统计系统

数据结构(C语言)课设5——药店的药品销售统计系统

题目描述:

你在一个L药品店当一名仓库管理员,马上就要放寒假回家过年了,L药店对这一年的销售情况做了统计,但这看起来太杂乱无章了。因此,你需要对这些数据进行整理,使他们看起来并不那么糟糕。“哦,我的天,是谁把阿莫新林放在了这?它应该放在那边的!最好在移动前用蒂花之秀先洗洗手,这样会显得更庄重些”。所以说,你需要将它们进行排序,这样就会看起来好看。但是,L店长是个冷漠的人,他一语“从小到大”,便拂袖而去,并没有说要对什么属性进行排序。(啧,真麻烦!)这意味着你要分别按照各个属性进行从小到大排序,这些属性分别为“药品编号”(num),“药品名称”(name),“药品单价”(price),“药品销售数量”(count),“药品销售额”(sale=price*count)。

以下分别为五种排序算法:
  • 插入排序
  • 选择排序
  • 冒泡排序
  • 堆排序
  • 快速排序
#include 
#include 
#include 
using namespace std;
#define MAX 100
typedef struct node
{
    char Num[5];//编号
    char Name[11];//名称
    double Price;//单价
    int Count;//销售数量
    double sale;
}Medicine;
typedef struct
{
    Medicine y[MAX];
    int length;
}SequenList;

void cinInfo(SequenList &S, int n)//输入信息
{
    for(int i = 1; i <= n; i++)
    {
        cin>>S.y[i].Num;
        cin>>S.y[i].Name;
        cin>>S.y[i].Price;
        cin>>S.y[i].Count;
        S.y[i].sale = S.y[i].Price * S.y[i].Count;
    }
    S.length = n;
}
void print(SequenList &S)
{
    for(int i = 1; i <= S.length; i++)
    {
        printf("%s\t%s\t%.1lf\t%d\t%.1lf\n", S.y[i].Num, S.y[i].Name, S.y[i].Price, S.y[i].Count, S.y[i].sale);
    }
    cout <<endl;
}

void InsertSort(SequenList &S)//插入排序  编号
{
    int i, j;
    for(i = 2; i <= S.length; ++i)
    {
        if(strcmp(S.y[i - 1].Num, S.y[i].Num) > 0)
        {
            S.y[0] = S.y[i];//将S.y[i]赋给S.y[0] 作为监视哨
            S.y[i] = S.y[i - 1];//S.y[i]小于S.y[i-1],则将比较后较大的向后移
            for(j = i - 2; strcmp(S.y[j].Num, S.y[0].Num) > 0; --j)
                S.y[j+1] = S.y[j];
            S.y[j + 1] = S.y[0];
        }
    }
    print(S);
}
void SelectSort(SequenList &S)//选择 名称
{
    int i, j, k;
    Medicine t;
    for(i = 1; i < S.length; ++i)
    {
        k = i;
        for(j = i + 1; j <= S.length; ++j)
        {
            if(strcmp(S.y[k].Name, S.y[j].Name) > 0)//将k指向最小的
            {
                k = j;
            }

        }
        if(k != i)
        {
            t = S.y[i];
            S.y[i] = S.y[k];
            S.y[k] = t;
        }
    }
    print(S);
}
void BubbleSort(SequenList &S)//冒泡  单价
{
    Medicine t;
    int l = S.length - 1;
    bool flag = false;
    while(l > 0 && flag == false)
    {
        flag = true;
        for(int j = 1; j <= l; j++)
        {
            if(S.y[j].Price > S.y[j + 1].Price)
            {
                flag = false;
                t = S.y[j];
                S.y[j] = S.y[j + 1];
                S.y[j + 1] = t;
            }
        }
        l--;
    }
    print(S);
}
void HeapAdjust(SequenList &S, int s, int k)
{
    Medicine m  = S.y[s];//保存根结点信息
    for(int j = 2*s; j <= k; j *= 2)//沿着值较大的孩子结点向下筛选
    {
        if(j < k && S.y[j].Count < S.y[j + 1].Count) j++;
        if(m.Count >= S.y[j].Count) break;//若根结点值大于孩子结点,根结点应是s的值
        S.y[s] = S.y[j];//否则根结点应该在j上,一直更新根结点,
        s = j;
    }
    S.y[s] = m;
}
void CreateHeap(SequenList &S)//初建大根堆
{
    int n = S.length;
    for(int i = n/2; i > 0; i--)
    {
        HeapAdjust(S, i, n);
    }
}
void HeapSort(SequenList &S)//堆排  数量
{
    CreateHeap(S);
    Medicine m;
    for(int i = S.length; i > 1; i--)
    {
        m = S.y[1];
        S.y[1] = S.y[i];
        S.y[i] = m;
        HeapAdjust(S, 1, i - 1);
    }
    print(S);
}

int Partition(SequenList &S, int low, int high) {//1 n
    S.y[0] = S.y[low];
    int pivotkey = S.y[low].sale;
    while(low < high) {
        while(low < high && S.y[high].sale >= pivotkey) high--;
        S.y[low] = S.y[high];
        while(low < high && S.y[low].sale <= pivotkey) low++;
        S.y[high] = S.y[low];
    }
    S.y[low] = S.y[0];
    return low;
}
void QuickSort(SequenList &S, int low, int high) {   //快速排序算法——药品销售e
    if(low < high) {
        int pivotloc = Partition(S, low, high);
        QuickSort(S, low, pivotloc - 1);
        QuickSort(S, pivotloc + 1, high);
    }
}
int main()
{
    SequenList S;
    int n;
    cin>>n;
    cinInfo(S, n);
    InsertSort(S);     //编号
    SelectSort(S);      //名称
    BubbleSort(S);      //单价
    HeapSort(S);        //数量
    QuickSort(S, 1, n); //销售额
    print(S);
    return 0;
}

此博文只用于博主记录学习过程(有问题可以评论)

你可能感兴趣的:(C语言数据结构)