Write a commodity shopping cash register system in C language, and count the total price of purchased commodities

Flowers will bloom again, people will never be young again, what we are going to learn today is how to write a cash register shopping system, keep learning and struggle

To make a "system", you have to first realize the small functions one by one, and then integrate them. We only need to write them into functions one by one in C language, and we can go directly to the topic. We list the headers we need today. files, a total of five

header function

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "windows.h"

Let's write the main function first, or that int main(){ }

Then let's think about what we need to open a store. It must be purchased. If there are no products, there will be no business, right? Then we must not buy the products we buy. We must first count the content we need, and build a structure first. Body, save multiple data

commodity structure

struct commodity
{ /*结点类型声明 */
    char name[30]; /*商品名 */
    char num[10]; /*编号 */
    float cost; /*单价 */
    float sell;/*进价*/
    int total;/*库存*/
    struct commodity* next; /*链接指针定义 */
};

Adding goods

The next step is the key step, adding our products into the system, here we use the linked list method

void add()
{ /*添加新结点 */
    //char sth[100]; /*预设一个储存输入字符串的数组 */
    newnode = (struct commodity*)malloc(sizeof(struct commdity*));    /*申请新结点的存储空间 */
    if (head == NULL)
        head = newnode; /*原商品为空,新结点就是头结点 */
    else
    { /*原商品不为空 */

        currnode = head; /*当前结点为头结点 */
        while (currnode->next != NULL) /*从当前开始找到尾结点 */
            currnode = currnode->next;
        currnode->next = newnode;
    }
    
    /*将新结点链接到链表尾结点成为新的尾结点 */
    currnode = newnode; /*指针指向新结点,进行数据输入 */
    {
        printf("\n 输入商品名:");
        scanf("%s", currnode->name);
        printf("\n 输入编号:");
        scanf("%s", currnode->num);
        printf("\n 输入单价:");
        scanf("%f", &currnode->cost);
        printf("\n 输入进价:");
        scanf("%f", &currnode->sell);
        while (1)
        {
            printf("\n 输入商品库存量:");
            scanf("%d", &currnode->total);
            if ((currnode->total) < 0) { /*判断库存量是否出错 */
                printf("库存量出错!");
                fflush(stdin);
                continue;
            }
            else
                break;
        }
    
        printf("\n 成功添加!");
        currnode->next = NULL; /*使新结点成为尾结点 */
    }
     head  =  newnode; // p和plist是指向一段相同地址空间的两个不同指针。
 while (head  != NULL)
 {
    int q;
    q=head->next;
    free(head);
    head=q;
}
newnode = NULL; // 只是释放了其内存空间,指向链表的指针值并没有被置为空
}

password authentication system

At this time, after writing a function, we write it into the main function. The function is realized, but there is no special feature. Can the system be opened casually? Of course not, what should we do? We write a system password authentication login Interface, here we use the judgment statement, use the defined password to save the input password, use if to compare with the pw password set by the system, and then the judgment can be realized.

void checkLogin()   //密码检测登录
{
    int n;
    int PW = 123456;        //密码

    printf("请输入系统密码:");
     
    for(cnt=0;cnt!=3;)
    {
        scanf("%d",&password);
        if(password==PW)
        {
            n=1;
            printf("登录成功,3秒后自动进入系统\n");
            Sleep(1000);
            
            printf("登录成功,2秒后自动进入系统\n");
            Sleep(1000);
            
            printf("登录成功,1秒后自动进入系统\n");
            Sleep(1000);
            system("cls");
            break;
        }
        else
        {
            cnt++;
            while(cnt==3)
        {
            printf("系统锁定,无法输入密码,3秒后系统自动关闭\n");
            Sleep(2000);
            system("cls");
            printf("系统锁定,无法输入密码,2秒后系统自动关闭\n");
            Sleep(1200);
            system("cls");
            printf("系统锁定,无法输入密码,1秒后系统自动关闭\n");
            Sleep(1200);
            system("cls");
            exit(0);
        }
            printf("密码错误,再次输入密码(你只有三次机会):\n");
        }
     
    }

} 

main interface menu

The login interface has been written, let’s go in and take a look, hey, it’s empty inside, we need to add some features to it, prefabricate the interface we want to set, and remind us what functions we haven’t written

printf("----------------------------------------商品销售系统--------------------------------------");
    char ch;
    int flag = 1;
    while (flag)
    {
        printf("\n--------------------------");
        printf("\n[1]添加商品\n[2]查找商品\n[3]删除商品\n[4]修改\n[5]排序\n[6]统计\n[7]显示当前在售商品\n[8]退出\n");
        fflush(stdin); /*刷新缓冲区,过滤回车 */
        printf("-----------------------------------------------------------------------------------------------------\n");
        printf("请输入");

From the picture, we revealed to you a total of eight functions, and now we have implemented one to add products. It is very interesting that the eighth one, if you have entered the system, you must be able to exit it. You can’t buy and sell by force, right? .

At the same time, if we want to choose a function, we must use a loop body, that is our switch-case, set multiple target functions, input a corresponding number, and then enter the corresponding function to realize its function

scanf("%c", &ch);
        switch (ch)
        {
        case '1':
            /*输入1为添加商品*/
            add();
            break;
        case '2':
            search();
            break;
        case '3':
            Delete();
            break;
        case '4':
            modify();
            break;
        case '5':
            order();
            break;
        case '6':
            statistic();
            break;
        case '7':
            show();
            break;
        default:
            flag = 0; /*其他按键结束操作 */
        }
        getchar();

The next step is to implement the delete function. After all, if the product is sold out accidentally, this product becomes out of print, so there is no need to store this product in the system. Let’s clear it from the system

delete item

void Delete() {
    char name[100];
    printf("请输入你要删除的商品的名称: ");
    scanf("%s", name);
    prenode = NULL;
    currnode = head; /*当前结点为头结点 */
    while (currnode != NULL) {
        if (strcmp(name, currnode->name) == 0) {
            if (prenode == NULL) {
                printf("删除成功!\n");
                head = head->next;
                return;
            }
            else {
                prenode->next = currnode->next;
                printf("删除成功\n");
                return;
            }
        }
    }
    printf("不存在!\n");
}

modify product

To modify a product is to search for the product in the linked list through traversal, and operate when it is found, and change the value of each product by modifying the pointer data. If it cannot be found, it is natural to exit the operation of modifying the product and give a prompt

void modify()
{
    int t;
    char i[10];
    printf("请输入你要修改的商品的编号:\n");
    scanf("%s", &i);

    currnode = head;  /*以头结点为当前节点*/
    while (currnode != NULL)
    {
        if (strcmp(currnode->num,i)== 0)
        {
            printf("商品名:%s\n    编号:%s\n        单价:%f\n    售价:%f\n  \n", currnode->name, currnode->num, currnode->cost,
                currnode->sell);
            printf("确定要修改吗?确定请输入1,取消请输入0\n");
            scanf("%d", &t);
            if (t)
            {
                printf("\n 输入修改后的商品名:");
                scanf("%s", currnode->name);
                printf("\n 输入修改后的编号:");
                scanf("%s", currnode->num);
                printf("\n 输入修改后的单价:");
                scanf("%f", &currnode->cost);
                printf("\n 输入修改后的进价");
                scanf("%f", &currnode->sell);
            }
            break;
        }
        else
            currnode = currnode->next;/*以下一节点为当前节点*/
    }
    printf("修改成功");

}

to sort

In the sorting section, we can set two options, one is to sort by the number when adding the product, and the other is to sort by the price more intuitively.

void order()
{
    struct commodity* p, t;
    if (head == NULL)
        printf("暂时无相关信息!\n");
    int n;
    printf("请选择排序方式:\n1.按编号排序\n2.按价格排序");
    scanf("%d", &n);
    currnode = head;
    switch (n)
    {
    case 1:
    {
        while (currnode->next != NULL)//将所有商品编号从小到大排序 
        {
            p = currnode->next;
            while (p != NULL)                  //每一个p与currnode比较从中逐个进行排序 
            {
                if (currnode->num > p->num)
                {
                    t = *currnode;  //交换结构体 
                    *currnode = *p;
                    *p = t;
                    t.next = currnode->next; //重新排列链表 
                    currnode->next = p->next;
                    p->next = t.next;
                }
                p = p->next;
            }
            currnode = currnode->next;
        }
    }
    break;
    case 2: {
        while (currnode->next != NULL) {   //将所有的价格从小到大排序并且逐个排序 
            p = currnode->next;
            while (p != NULL)      //每一个p与currnode比较从中逐个进行排序 
            {
                if (currnode->cost > p->cost)
                {
                    t = *currnode;  //交换结构体 
                    *currnode = *p;
                    *p = t;
                    t.next = currnode->next; //重新排列链表 
                    currnode->next = p->next;
                    p->next = t.next;
                }
                p = p->next;
            }
            currnode = currnode->next;
        }
    }
          break;
    }
    printf("排序结果如下:\n");
    while (currnode != NULL)
    {
        printf("商品名:%s\n 编号:%s \n    价格:%f 数量:%d ", currnode->name, currnode->num, currnode->cost, currnode->total);
        currnode = currnode->next;
    }
        printf("商品名:%s\n 编号:%s \n    价格:%f 数量:%d ", currnode->name, currnode->num, currnode->cost, currnode->total);
}

statistics

void statistic() {
    int total = 0;
    int totalPrice = 0;
    int totalsell = 0;
    int profit = 0;
    int totalprofit = 0;
    currnode = head; /*当前结点为头结点 */
    while (currnode != NULL) {
        total+=currnode->total;
        totalPrice += currnode->cost*currnode->total;
        totalsell += currnode->sell*currnode->total;
        profit = currnode->cost-currnode->sell;
        totalprofit += profit*currnode->total;
        currnode = currnode->next;
    }
    printf("商品数量为:%d\n", total);
    printf("商品总价为:%d\n", totalPrice);
    printf("商品总进价为:%d\n",totalsell);
    printf("商品总利润为:%d\n", totalprofit);
}

Show what's on sale

void search()

{
    printf("请输入所查商品的信息关键词\n");
    printf("若按商品名查找请按1\n");
    printf("若按编号查找请按2\n");
    printf("若按单价查找请按3\n");
    printf("若按库存查找请按4\n");

    int a;
    printf("从键盘上输入所选编号:");
    scanf("%d", &a);                  //从键盘上输入所选编号 
    
    char name[50];
    float cost = 0;
    int num = 0, total = 0;
    //定义查询信息时的变量 
    
    if (a >= 1 && a <= 4)
    {
        switch (a)                     //根据编号确定所查信息 
        {
        case 1: {printf("请输入:");scanf("%s", &name);break; }
        case 2: {printf("请输入:");scanf("%d", &num);break;}
        case 3: {printf("请输入:");scanf("%f", &cost);break;}
        case 4: {printf("请输入:");scanf("%d", &total);break;}
        }
    }
    else
        printf("所输入的编号错误,请重新输入\n");
    int i = 0;
    currnode = head;
    
    while (currnode != NULL)
    {
    
        if (strcmp(currnode->name, name) == 0 || currnode->num || currnode->cost == cost || currnode->total == total)
            printf("\n\n\n\n您要寻找的信息如下\n商品名:%s\n 编号:%s \n   价格:%f \n  库存:%d", currnode->name, currnode->num, currnode->cost, currnode->total);
        currnode = currnode->next;
    }

}

all codes

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "windows.h"

struct commodity
{ /*结点类型声明 */
    char name[30]; /*商品名 */
    char num[10]; /*编号 */
    float cost; /*单价 */
    float sell;/*进价*/
    int total;/*库存*/
    struct commodity* next; /*链接指针定义 */
};
/*函数声明 */
void add(); 
void search();
void Delete();
void statistic();
void modify();
void order();
void show();
void checkLogin();
int cnt = 0;
int password;

struct commodity* head = NULL, * currnode, * newnode, * prenode;
/*指向结点的指针声明 */




 //主函数 
int main()
{
     checkLogin();
    printf("----------------------------------------商品销售系统--------------------------------------");
    char ch;
    int flag = 1;
    while (flag)
    {
        printf("\n--------------------------");
        printf("\n[1]添加商品\n[2]查找商品\n[3]删除商品\n[4]修改\n[5]排序\n[6]统计\n[7]显示当前在售商品\n[8]退出\n");
        fflush(stdin); /*刷新缓冲区,过滤回车 */
        printf("-----------------------------------------------------------------------------------------------------\n");
        printf("请输入");
        scanf("%c", &ch);
        switch (ch)
        {
        case '1':
            /*输入1为添加商品*/
            add();
            break;
        case '2':
            search();
            break;
        case '3':
            Delete();
            break;
        case '4':
            modify();
            break;
        case '5':
            order();
            break;
        case '6':
            statistic();
            break;
        case '7':
            show();
            break;
        default:
            flag = 0; /*其他按键结束操作 */
        }
        getchar();
    }
}

void add()
{ /*添加新结点 */
    //char sth[100]; /*预设一个储存输入字符串的数组 */
    newnode = (struct commodity*)malloc(sizeof(struct commdity*));    /*申请新结点的存储空间 */
    if (head == NULL)
        head = newnode; /*原商品为空,新结点就是头结点 */
    else
    { /*原商品不为空 */

        currnode = head; /*当前结点为头结点 */
        while (currnode->next != NULL) /*从当前开始找到尾结点 */
            currnode = currnode->next;
        currnode->next = newnode;
    }
    
    /*将新结点链接到链表尾结点成为新的尾结点 */
    currnode = newnode; /*指针指向新结点,进行数据输入 */
    {
        printf("\n 输入商品名:");
        scanf("%s", currnode->name);
        printf("\n 输入编号:");
        scanf("%s", currnode->num);
        printf("\n 输入单价:");
        scanf("%f", &currnode->cost);
        printf("\n 输入进价:");
        scanf("%f", &currnode->sell);
        while (1)
        {
            printf("\n 输入商品库存量:");
            scanf("%d", &currnode->total);
            if ((currnode->total) < 0) { /*判断库存量是否出错 */
                printf("库存量出错!");
                fflush(stdin);
                continue;
            }
            else
                break;
        }
    
        printf("\n 成功添加!");
        currnode->next = NULL; /*使新结点成为尾结点 */
    }
     head  =  newnode; // p和plist是指向一段相同地址空间的两个不同指针。
 while (head  != NULL)
 {
    int q;
    q=head->next;
    free(head);
    head=q;
}
newnode = NULL; // 只是释放了其内存空间,指向链表的指针值并没有被置为空
}

//统计商品总量 
void statistic() {
    int total = 0;
    int totalPrice = 0;
    int totalsell = 0;
    int profit = 0;
    int totalprofit = 0;
    currnode = head; /*当前结点为头结点 */
    while (currnode != NULL) {
        total+=currnode->total;
        totalPrice += currnode->cost*currnode->total;
        totalsell += currnode->sell*currnode->total;
        profit = currnode->cost-currnode->sell;
        totalprofit += profit*currnode->total;
        currnode = currnode->next;
    }
    printf("商品数量为:%d\n", total);
    printf("商品总价为:%d\n", totalPrice);
    printf("商品总进价为:%d\n",totalsell);
    printf("商品总利润为:%d\n", totalprofit);
}



//删除功能 
void Delete() {
    char name[100];
    printf("请输入你要删除的商品的名称: ");
    scanf("%s", name);
    prenode = NULL;
    currnode = head; /*当前结点为头结点 */
    while (currnode != NULL) {
        if (strcmp(name, currnode->name) == 0) {
            if (prenode == NULL) {
                printf("删除成功!\n");
                head = head->next;
                return;
            }
            else {
                prenode->next = currnode->next;
                printf("删除成功\n");
                return;
            }
        }
    }
    printf("不存在!\n");
}




//修改功能 
void modify()
{
    int t;
    char i[10];
    printf("请输入你要修改的商品的编号:\n");
    scanf("%s", &i);

    currnode = head;  /*以头结点为当前节点*/
    while (currnode != NULL)
    {
        if (strcmp(currnode->num,i)== 0)
        {
            printf("商品名:%s\n    编号:%s\n        单价:%f\n    售价:%f\n  \n", currnode->name, currnode->num, currnode->cost,
                currnode->sell);
            printf("确定要修改吗?确定请输入1,取消请输入0\n");
            scanf("%d", &t);
            if (t)
            {
                printf("\n 输入修改后的商品名:");
                scanf("%s", currnode->name);
                printf("\n 输入修改后的编号:");
                scanf("%s", currnode->num);
                printf("\n 输入修改后的单价:");
                scanf("%f", &currnode->cost);
                printf("\n 输入修改后的进价");
                scanf("%f", &currnode->sell);
            }
            break;
        }
        else
            currnode = currnode->next;/*以下一节点为当前节点*/
    }
    printf("修改成功");

}


//查找功能
void search()

{
    printf("请输入所查商品的信息关键词\n");
    printf("若按商品名查找请按1\n");
    printf("若按编号查找请按2\n");
    printf("若按单价查找请按3\n");
    printf("若按库存查找请按4\n");

    int a;
    printf("从键盘上输入所选编号:");
    scanf("%d", &a);                  //从键盘上输入所选编号 
    
    char name[50];
    float cost = 0;
    int num = 0, total = 0;
    //定义查询信息时的变量 
    
    if (a >= 1 && a <= 4)
    {
        switch (a)                     //根据编号确定所查信息 
        {
        case 1: {printf("请输入:");scanf("%s", &name);break; }
        case 2: {printf("请输入:");scanf("%d", &num);break;}
        case 3: {printf("请输入:");scanf("%f", &cost);break;}
        case 4: {printf("请输入:");scanf("%d", &total);break;}
        }
    }
    else
        printf("所输入的编号错误,请重新输入\n");
    int i = 0;
    currnode = head;
    
    while (currnode != NULL)
    {
    
        if (strcmp(currnode->name, name) == 0 || currnode->num || currnode->cost == cost || currnode->total == total)
            printf("\n\n\n\n您要寻找的信息如下\n商品名:%s\n 编号:%s \n   价格:%f \n  库存:%d", currnode->name, currnode->num, currnode->cost, currnode->total);
        currnode = currnode->next;
    }

}




//排序
void order()
{
    struct commodity* p, t;
    if (head == NULL)
        printf("暂时无相关信息!\n");
    int n;
    printf("请选择排序方式:\n1.按编号排序\n2.按价格排序");
    scanf("%d", &n);
    currnode = head;
    switch (n)
    {
    case 1:
    {
        while (currnode->next != NULL)//将所有商品编号从小到大排序 
        {
            p = currnode->next;
            while (p != NULL)                  //每一个p与currnode比较从中逐个进行排序 
            {
                if (currnode->num > p->num)
                {
                    t = *currnode;  //交换结构体 
                    *currnode = *p;
                    *p = t;
                    t.next = currnode->next; //重新排列链表 
                    currnode->next = p->next;
                    p->next = t.next;
                }
                p = p->next;
            }
            currnode = currnode->next;
        }
    }
    break;
    case 2: {
        while (currnode->next != NULL) {   //将所有的价格从小到大排序并且逐个排序 
            p = currnode->next;
            while (p != NULL)      //每一个p与currnode比较从中逐个进行排序 
            {
                if (currnode->cost > p->cost)
                {
                    t = *currnode;  //交换结构体 
                    *currnode = *p;
                    *p = t;
                    t.next = currnode->next; //重新排列链表 
                    currnode->next = p->next;
                    p->next = t.next;
                }
                p = p->next;
            }
            currnode = currnode->next;
        }
    }
          break;
    }
    printf("排序结果如下:\n");
    while (currnode != NULL)
    {
        printf("商品名:%s\n 编号:%s \n    价格:%f 数量:%d ", currnode->name, currnode->num, currnode->cost, currnode->total);
        currnode = currnode->next;
    }
        printf("商品名:%s\n 编号:%s \n    价格:%f 数量:%d ", currnode->name, currnode->num, currnode->cost, currnode->total);
}

void show()//显示商品信息功能 
{
        currnode = head;
    while (currnode != NULL)
    {
    printf("商品名:%s\n 编号:%s \n价格:%f \n数量:%d ", currnode->name, currnode->num, currnode->cost, currnode->total);
        currnode = currnode->next;
    }
}

void checkLogin()   //密码检测登录
{
    int n;
    int PW = 123456;        //密码

    printf("请输入系统密码:");
     
    for(cnt=0;cnt!=3;)
    {
        scanf("%d",&password);
        if(password==PW)
        {
            n=1;
            printf("登录成功,3秒后自动进入系统\n");
            Sleep(1000);
            
            printf("登录成功,2秒后自动进入系统\n");
            Sleep(1000);
            
            printf("登录成功,1秒后自动进入系统\n");
            Sleep(1000);
            system("cls");
            break;
        }
        else
        {
            cnt++;
            while(cnt==3)
        {
            printf("系统锁定,无法输入密码,3秒后系统自动关闭\n");
            Sleep(2000);
            system("cls");
            printf("系统锁定,无法输入密码,2秒后系统自动关闭\n");
            Sleep(1200);
            system("cls");
            printf("系统锁定,无法输入密码,1秒后系统自动关闭\n");
            Sleep(1200);
            system("cls");
            exit(0);
        }
            printf("密码错误,再次输入密码(你只有三次机会):\n");
        }
     
    }

} 

Guess you like

Origin blog.csdn.net/neadsc/article/details/128742716