⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 C语言写的商品信息管理系统 学习数据结构
💻 C
📖 第 1 页 / 共 2 页
字号:
#include<stdio.h> 
#include "ShangPinManage.h"
#include "userinfo.h"
#include "zhuce.h"
#include "denglu.h"           
#include <stdlib.h>
#include <stdlib.h>
#include<string.h>
#include "hh.h"                        //定义表 
#include "jiancebianhao.h"             //检测编号
#include "dengji.h"                    //登记商品信息                               1
#include "chaxunshuoyou.h"             //查询所有商品信息                           2
#include "charu.h"                     //插入商品信息                               3
#include "shanchu.h"                   //删除商品信息                               4
#include "chaxundingkucun.h"           //查询给定库存的商品信息                     5
#include "chaxundaxiaokucun.h"         //查询大于或小于给定库存的商品信息           6
#include "chaxunduankucun.h"           //查询给定数值段中库存最高的商品信息         7
#include "chaxundayudingjiage.h"       //输出价格高于某个值的所有商品按价格排序输出 8 
#include "chaxunxiaoyudingkucun.h"     //输出库存小于某个值的所有商品信息并排序输出 9
#include "chaxunzhidingkucun.h"        //任意指定对商品进行库存的排序               10
#include "chaxunzhidingjiage.h"        //任意指定对商品进行价格排序                 11
#include "savefile.h"                  // 保存所有商品信息                          12
#include "openfile.h"                  // 打开所有商品信息                          13

struct shangpin
{
	int id;              //商品编号
	char spname[30];   //商品名称
	float price;         //商品价格
	int kucun;         //商品库存
};
struct userinfo
{
	char username[30];
	char passwd[30];
};

///////////////////////程序入口 
int main()
{
	struct userinfo user[0];
	struct userinfo userlogin[0];
	userlogin[0].username[0]=' ';
	int choice=-1;
	int checkdenglu=-1;
	while(1)
	{
		///////////////////////
		printf("\n\n\n\n\n\n");
		
		if(userlogin[0].username[0]==' ' || checkdenglu==-1)
		{
            printf("\t\t 商品信息管理系统   状态->未登陆");
            printf("\n\t\t __________________________________________________");
            printf("\n\n\t\t    1  -> 用户登录");
        }
        else
        {
		    printf("\t\t||-->>商品信息管理系统<<--||   状态->%s 登陆到系统\n",userlogin[0].username);
            printf("\n\t\t __________________________________________________");
            printf("\n\n\t\t    1  -> 进入商品信息管理系统");
        }
		printf("\n\n\t\t    2  -> 注册用户");
		printf("\n\n\t\t    0  -> 退出系统\n");
		printf("\n\t\t __________________________________________________");
		printf("\n\n\t\t 请选择菜单号(0-10):");
		scanf("%d",&choice);getchar();
		////////////////////////
		if(choice==1)                     ///// 1 -- 登陆
		{
            if(checkdenglu==-1)
            {
                checkdenglu=denglu(checkdenglu,userlogin);
                system("cls");
            }
            else
            {
				system("cls"); 
                ShangPinManage(userlogin[0].username);                //进入商品信息管理系统...
            }
		}
		else if(choice==2)               ///// 2 -- 注册用户信息
		{
             if(checkdenglu==1)
             {
                  zhuce(user);
             }
             else
             {
                 printf("\n\t\t 对不起你没有权限,请先登陆!任意键继续...");
                 getchar();
             }
             system("cls"); 
		}
		else if(choice==0)                 //// 0  -- 退出系统 
		{
		    printf("\n\t\t 任意键退出...");
		    getchar();
		    exit(0);
        }
		else
		{
            printf("\n\t\t 您的选择有错,请重新选择!任意键继续...");
            getchar();
            system("cls");
        }
	}
	return 0;
}


int ShangPinManage(char username[])
{
	struct shangpin shangp[MAXLENTH];
	int count=0;
	int choice=-1;
	while(1)
	{
		printf("\t\t 商品信息管理系统   当前用户-> %s",username);
		printf("\n\t\t __________________________________________________");
		/////////////////////////////////////////////////////// 商品信息管理
		printf("\n\t\t     ____________________________");
		printf("\n\t\t        1   ->  登记商品信息");
		printf("\n\t\t        2   ->  查询所有商品信息");
		printf("\n\t\t        3   ->  插入一个商品信息");
		printf("\n\t\t        4   ->  删除一个商品信息");
		printf("\n\t\t     ____________________________");
		/////////////////////////////////////////////////////// 删除商品信息
		printf("\n\t\t        5   ->  查询一个定库存的商品信息 ");
		printf("\n\t\t        6   ->  查询大于或者小于给定库存的商品信息");
		printf("\n\t\t        7   ->  查询给定库存段中库存最高的商品信息");
		printf("\n\t\t     ____________________________");
		/////////////////////////////////////////////////////// 商品信息排序
		printf("\n\t\t        8   ->  价格高于某值的所有商品信息按价格排序输出");
		printf("\n\t\t        9   ->  库存不足某值的所有商品信息按库存排序输出");
		printf("\n\t\t        10  ->  任意指定库存段对商品信息进行库存排序输出");
		printf("\n\t\t        11  ->  任意指定价格段对商品信息进行价格排序输出");
		printf("\n\t\t     ____________________________");
		/////////////////////////////////////////////////////// 商品信息永久保存 
		printf("\n\t\t        12  ->  保存所有商品信息");
		printf("\n\t\t        13  ->  导入所有商品信息 ");
		printf("\n\t\t        0   ->  退出管理");
		printf("\n\t\t     ____________________________");
		printf("\n\t\t __________________________________________________");
		printf("\n\t\t 请选择菜单号(0-10):");
		scanf("%d",&choice);getchar();
		////////////////////////
		if(choice==1)                     ///// 1 -- 登记商品信息
		{
			count=0;
			count=dengji(shangp,count);
			system("cls");
		}
		else if(choice==2)               ///// 2 -- 查询商品信息
		{
			chaxunshuoyou(shangp,count);
			system("cls");
		}
		else if(choice==3)               ///// 3 -- 插入商品信息
		{
			count=charu(shangp,count);
		    system("cls");
		}
		else if(choice==4)               ///// 4 -- 删除商品信息
		{
			count=shanchu(shangp,count);
			system("cls");
		}
		else if(choice==5)               ///// 5  -- 查询给定库存的商品信息
		{
		    chaxundingkucun(shangp,count);
		    system("cls");
		}
		else if(choice==6)             ///// 6  -- 查询大于或小于给定库存的商品信息
	    {
		    chaxundaxiaokucun(shangp,count);
		    system("cls");
		}
		else if(choice==7)                 //// 7  -- 查询给定数值段中库存最高的商品信息
		{
	     	chaxunduankucun(shangp,count);
			system("cls");
	    }
		else if(choice==8)                 //// 8  -- 输出价格高于某个值的所有商品排序输出
		{
	     	chaxundayudingjiage(shangp,count);
            system("cls");
		}
		else if(choice==9)                 //// 9  -- 输出库存不足某个值的所有商品信息并排序输出
		{
	     	chaxunxiaoyudingkucun(shangp,count);
	     	system("cls");
       }
		else if(choice==10)                //// 10 -- 任意指定对商品进行库存排序
		{
	     	chaxunzhidingkucun(shangp,count);
			system("cls");
        }
        else if(choice==11)                //// 11 -- 任意指定对商品进行价格排序
        {
	     	chaxunzhidingjiage(shangp,count);
			system("cls");
        }
        else if(choice==12)                //// 12 -- 保存所有商品信息 
        {
            savefile(shangp,count);
			system("cls");
        }
        else if(choice==13)                //// 13 -- 任打开所有商品信息
        {
            count=0;
            count=openfile(shangp,count);
			system("cls");
        }
		else if(choice==0)                 //// 0  -- 退出系统 
		{
		    printf("\n\t\t 任意键退出...");
		    getchar();
		    exit(0);
        }
		else
		{
            printf("\n\t\t 您的选择有错,请重新选择!任意键继续...");
            getchar();
            system("cls");
        }
	}
	return 0;
}
//////////////////////////////// 保存所有商品信息  
void savefile(struct shangpin shangp[],int count)
{
	char filepath[30];
	printf("\n\t\t 请输入要保存的文件名:");
	scanf("%s",&filepath);getchar();
    FILE *fp; 
    if((fp=fopen(filepath,"wb+"))==NULL)
    {
        printf("Cannot open file strike any key exit!");
        getch();
    }
    fwrite(shangp,sizeof(struct shangpin),count,fp);
    fclose(fp);
    printf("\n\t\t 保存成功!任意键继续...");
    getchar();
}
///////////////     检测是否存在相同的商品编号
int jiancebianhao(struct shangpin shangp[],int id,int count)
{
	int i;
	int bianhao=1;
	for(i=0;i<count;i++)
	{
		if(shangp[i].id==id)
		  bianhao=-1;
	}
	return bianhao;       //相等的返回-1 不相等,就返回 1 
}
///////////////删除商品信息 4
int shanchu(struct shangpin shangp[],int count)
{
	int i=0;
	int bianhaochecked=-1; //是否检测编号 ,当编号为1时, 即进得删除操作 避免用二个循环 
	int id=0;
	printf("\n\t\t 提示:编号为-1时,退出删除\n");
	printf("\n\t\t 请输入要删除商品编号:");
	scanf("%d",&id);getchar();
	if(id==-1)
	{
		printf("\n\t\t 已退出删除操作,按任意键继续其他操作...");
		getchar();
		return count;
	}
	else
	{
		for(i=0;i<count;i++)
	    {
			if(shangp[i].id==id)
			{
				bianhaochecked=1;
			}
			if(bianhaochecked==1)
		        shangp[i]=shangp[i+1];   //当编号为1时, 即进得删除操作
        }
        if(bianhaochecked==1)
        {
			count--;
	        printf("\n\t\t 删除完成,按任意键继续其他操作..."); 
	        getchar();
	        return count;
		}
		else
		{
			printf("\n\t\t 没有找到编号,删除失败,按任意键继续其他操作...");
			getchar();
		    return count;
		}
	}
}
/////////////////////  打开所有商品信息 
int openfile(struct shangpin shangp[],int count)
{
	char filepath[30];
	printf("\n\t\t 请输入打开的文件名:"); 
	scanf("%s",&filepath);getchar();
    FILE *fp;
    if((fp=fopen(filepath,"r"))==NULL)
    {
        printf("\n\t\t 导入出错!数据被破坏/没有保存过数据.任意键继续...");
        getchar();
    }
    else
    {
        while(!feof(fp))
        {
            count=count+1;
            fread(&shangp[count-1],sizeof(struct shangpin),1,fp);
        }
        printf("\n\t\t 导入成功!任意键继续...");
    }
    fclose(fp);
    getchar();
    return count-1;
}
///////////////登记商品信息 1
int dengji(struct shangpin shangp[],int count)
{
	int bianhao=1;
	printf("\n\t\t 提示:编号为0时,退出登记\n");
	while(1)
	{
	mylabel1:
		printf("\n\t\t 请输入商品编号:");
		scanf("%d",&shangp[count].id);getchar();
		bianhao=jiancebianhao(shangp,shangp[count].id,count);//     检测是否存在相同的商品 
		if(bianhao==-1)                        
		{
			printf("\n\t\t 该编号已经存在!!\n");
	goto mylabel1;
		}
		if(shangp[count].id==0)                              //     按0退出登记 
		    break;
		printf("\n\t\t 请输入商品名:");
		scanf("%s",&shangp[count].spname);getchar();
	mylabel2:
		printf("\n\t\t 请输入商品单价(元):");
		scanf("%f",&shangp[count].price);getchar();
		if(shangp[count].price<=0)
		{
			printf("\n\t\t 单价不可以小于等于0");
	goto mylabel2;
	    }
	mylabel3:
		printf("\n\t\t 请输入商品库存:");
		scanf("%d",&shangp[count].kucun);getchar();
	    if(shangp[count].kucun<0)
	    {
			printf("\n\t\t 库存不可以小于0");
	goto mylabel3;
	    }
		count++;
		printf("\n\t\t 已登记了%d个\n",count);
	}
	printf("\n\t\t 登记完成,按任意键继续其他操作...."); 
	getchar();
	return count;
}
/////////////////////    用户登陆
int denglu(int checkdenglu,struct userinfo userlogin[])
{
    char username[30];
	char passwd[30];
    printf("\n\t\t 用户名:");
    scanf("%s",&userlogin[0].username);getchar();
    printf("\n\t\t 密  码:");
    scanf("%s",&userlogin[0].passwd);getchar();
    
    FILE *fp; 
    struct userinfo user;
    if((fp=fopen("UserInfo.txt","r"))==NULL)
    {
        printf("Cannot open file strike any key exit!");
        getch();
    }
    else
    {
        while(!feof(fp))
        {
            fread(&user,sizeof(struct userinfo),1,fp);
            if(strcmp(userlogin[0].username,user.username)==0 && strcmp(userlogin[0].passwd,user.passwd)==0)
            {
                checkdenglu=1;
            }
        }
    }
    fclose(fp);
    if(checkdenglu==1)
    {
        printf("\n\t\t 登陆成功!任意键继续...");
    }
    else
    {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -