Data structure (C language) course 5: drug sales statistics system of drugstores

Data structure (C language) course 5: drug sales statistics system of drugstores

Title Description:

You work as a warehouse keeper in an L drug store, and you will go home for the new year's holiday soon. L drug store has made statistics on the sales of this year, but it seems to be too messy. So you need to sort out the data so that they don't look that bad. "Oh, my God, who put amoxinlin here? It should be over there! It's better to wash your hands with tihuazhixiu before moving, which will be more solemn. So you need to sort them so they look good. However, store manager L is an indifferent person. He said "from small to large" and left without saying what attributes to sort. (Tut, what a bother!) This means that you need to sort each attribute from small to large. These attributes are "drug number" (num), "drug name" (name), "drug unit price" (price), "drug sales quantity" (count), "drug sales" (sale=price*count).

The following are five sorting algorithms:
  • Insertion sort
  • Selection sort
  • Bubble sort
  • Heap sort
  • Quick sort
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define MAX 100
typedef struct node
{
    char Num[5];//number
    char Name[11];//Name
    double Price;//Unit Price
    int Count;//Sales volumes
    double sale;
}Medicine;
typedef struct
{
    Medicine y[MAX];
    int length;
}SequenList;

void cinInfo(SequenList &S, int n)//Input information
{
    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)//Insert sort number
{
    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];//Assign S.y[i] to S.y[0] as a surveillance post
            S.y[i] = S.y[i - 1];//S. If y [i] is less than S.y[i-1], move the larger backward after comparison
            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)//Choose a name
{
    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)//Point k to the smallest
            {
                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)//Bubbling unit price
{
    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];//Save root information
    for(int j = 2*s; j <= k; j *= 2)//Filter down the higher value child nodes
    {
        if(j < k && S.y[j].Count < S.y[j + 1].Count) j++;
        if(m.Count >= S.y[j].Count) break;//If the root node value is greater than the child node, the root node should be the value of s
        S.y[s] = S.y[j];//Otherwise, the root node should always be updated on j,
        s = j;
    }
    S.y[s] = m;
}
void CreateHeap(SequenList &S)//Initial construction of large pile
{
    int n = S.length;
    for(int i = n/2; i > 0; i--)
    {
        HeapAdjust(S, i, n);
    }
}
void HeapSort(SequenList &S)//Number of stacks
{
    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) {   //Fast sorting algorithm -- drug sales 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);     //number
    SelectSort(S);      //Name
    BubbleSort(S);      //Unit Price
    HeapSort(S);        //Number
    QuickSort(S, 1, n); //Sales volume
    print(S);
    return 0;
}

Published 7 original articles, praised 0 and visited 12
Private letter follow

Tags: Attribute less C

Posted on Sun, 12 Jan 2020 08:07:37 -0500 by adeelzia