做商品管理系统
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include<math.h>
//商品类别目录
struct list
{
int number;//类别编号
char name[30];//名称
int sum;//类别种数
int log;//读入标记
struct list* next;
struct node* child;
};
//子目录
struct node
{
int number;//类别编号
char id[20];//商品编号
char name[30];//商品或分类的名称
double price;//商品价格
struct node* next;//用单链表记录当前结点的所有不为空的子结点
};
int logo;//页数标记
int choice1;//main函数里的全局选择变量
char *filename_list;//商品类别目录所在的路径
char *filename_goods;//商品信息目录所在的路径
void function(struct list* h);//总功能大厅函数
void get_filename(char *name);//获取文件所在的路径函数
struct list* input();//读取商品类别的目录函数
void input_goods(struct list* h);//读取商品信息函数
void add(struct list* h);//添加函数
void add_one(struct list* h);//添加一个商品函数
void add_kind(struct list* h);//添加一个商品类别函数
void Delete(struct list* h);//删除函数
void Delete_one(struct list* h);//删除一个商品函数
void Delete_kind(struct list* h);//删除一个商品类别函数
void search(struct list* h);//查找函数
void change(struct list* h);//修改函数
void change_one(struct list* h);//修改商品函数
void change_kind(struct list* h);//修改目录函数
void display(struct list* h);//查阅商品函数
void display_all(struct list* h);//查阅全部
void display_one(struct list* h);//翻页查阅
void display_book(struct list* h);//目录式查阅
void display_goods(struct list* h, int number);//展示一个商品函数
void check_all(struct list* h, struct list* t,int *m,char *s);//检查修改时数据有没有重复函数
void check_one(struct list* t, struct node* m, char* s);//检查修改商品时商品数据有误冲突
void close(struct list* h);//关掉系统
int main(int argc,char *argv[])
{
struct list* h=NULL;
get_filename(argv[0]);
h=input();
char password[10]={0};
int i=0;
printf("欢迎使用简单商品分类管理系统!\n");
printf("请输入密码:\n");
for(i=0;i<3;i++)
{
scanf("%s",password);
if(strcmp(password,"123456")==0)
{
printf("登陆成功!");
system("pause");
function(h);
break;
}
else
{
printf("登陆失败,请重新输入密码!\n");
}
}
if(i==3){
printf("登陆失败,退出程序!");
}
system("pause");
return 0;
}
void function(struct list* h)
{
while(1){
system("cls");
printf("\n");
printf("================欢迎使用简单商品分类管理系统===================\n");
printf("= =\n");
printf("= 1---查看目录 2---添加 =\n");
printf("= =\n");
printf("= 3---删除 4---查找商品 =\n");
printf("= =\n");
printf("= 5---修改 0---退出系统 =\n");
printf("= =\n");
printf("===============================================================\n");
printf("请输入你的选择:");
scanf("%d",&choice1);
while(choice1!=1&&choice1!=2&&choice1!=3&&choice1!=4&&choice1!=5&&choice1!=0){
printf("输入错误,请重新输入!");
scanf("%d",&choice1);
}
switch(choice1){
case 1://查看目录
display(h);
break;
case 2://添加商品
add(h);
break;
case 3://删除商品
Delete(h);
break;
case 4://查找商品
break;
case 5://修改商品
change(h);
break;
case 0:
close(h);
return 0;
}
}
}
void get_filename(char *name)
{
char *nowpath;//当前文件的路径,包括文件名
char *path1,*path2;//获取当前文件所在的路径,且不包括文件名
char *filename;//文件名
int i=0,j=0,k=0,p,length;//length为文件名下标
int count1=0,count2=0,count3=0;//计数器,count1记录文件路径中与文件名称相同的个数,count2记录 \\ 的个数,count3记录文件名称和后缀的长度
nowpath=(char *)malloc(strlen(name)+1);//文件路径长度+结束符
strcpy(nowpath,name);//复制
for(i=0;i<strlen(nowpath);i++)
{
if(nowpath[i]=="\\") count2++;//统计斜杠个数
}
for (i=strlen(nowpath)-1;nowpath[i]!='\\';i--) count3++;//统计文件名称+后缀长度
path1=(char *)malloc(strlen(nowpath)-count3+count2+30);//计算文件名称长度
path2=(char *)malloc(strlen(nowpath)-count3+count2+31);
filename=(char *)malloc(count3-4+1);//减去后缀名,加上结束符
//获取文件名称
for(i=strlen(nowpath);i>0;i--)
{
//自减使指针指向 . 后面的一位
if(nowpath[i--]=='.')
{
//.往左到第一个\为文件名称
for(j=0;nowpath[i]!='\\';j++,i--)
{
filename[j]=nowpath[i];
}
filename[j]='\0';//文件名之后接结束符
break;
}
}
length=strlen(filename)-1;//文件名称数组下标=文件名称的长度-1
//检测文件路径中有无与文件名称相同的字符串,并记录
for(i=0;i<strlen(nowpath);i++)
{
if(nowpath[i]==filename[length])
{
for(k=i+1,p=length-1;k<i+length+1;k++,p--)
{
if (nowpath[k]==filename[p]) {
if (k==i+length) count1++;//当k==i+length时到文件名最后一个字符,相同时+1
}
else break;
}
}
}
for (i=0,j=0;i<strlen(nowpath);i++,j++)
{
path1[j] = nowpath[i];
if (nowpath[i]=='\\') path1[++j]='\\';
if (nowpath[i] == filename[length])
{
for (k=i+1,p=length-1;k<i+length+1;k++,p--)
{
if (nowpath[k]==filename[p])
{
if (k==i+length&&count1>0) count1--;//到文件名最后一个字符,文件名称相同时计数器减1
}
else
{
break;
}
}
}
//计数为0则已读到商品名称,在商品名称前设置结束符并退出循环
if (count1==0) {
path1[j]='\0';
break;
}
}
strcpy(path2,path1);
strcat(path1,"\\\\list.txt");
filename_list=(char*)malloc(strlen(path1)+1);
strcpy(filename_list, path1);
strcat(path2, "\\\\goods.txt");
filename_goods=(char*)malloc(strlen(path2)+1);
strcpy(filename_goods,path2);
free(nowpath);
free(path1);
free(path2);
free(filename);
//释放各个指针内存
}
//获取商品类别目录
struct list* input()
{
struct list* head=NULL,*p1,*p2=NULL;
FILE *fp;
logo=0;//页数设初值0;
if ((fp = fopen(filename_list, "a+"))==NULL)//打开文本文件进行追加
{
printf("1不能打开该文件!\n");
exit(0);
}
if ((p1=(struct list*)malloc(sizeof(struct list)))==NULL)//开辟新结点p1
{
printf("分类1不能成功分配内存储块!");
exit(0);
}
//令新结点指针域为NULL
p1->next = NULL;
p1->child = NULL;
while((fscanf(fp,"%d %s",&p1->number,p1->name))!=EOF)//从fp指向的文件中读取格式化数据
{
if(head==NULL) head=p1;
else p2->next=p1;//表尾链入新结点
p2=p1;//p2指向新的表尾结点
if ((p1=(struct list*)malloc(sizeof(struct list)))==NULL)
{
printf("分类2不能成功分配内存储块!");
exit(0);
}
p1->next=NULL;
p1->child=NULL;//类别下面的商品的链表头
}
input_goods(head);//将商品信息添加到商品类别目录中
fclose(fp);
return head;//返回链表头指针
}
void input_goods(struct list* h)
{
struct list* i=NULL;
struct node* head=NULL,*p1,*p2=NULL;
FILE *fp;
logo=0;//页数设初值0;
if ((fp=fopen(filename_goods, "a+"))==NULL)//打开文本文件进行追加
{
printf("1不能打开该文件!\n");
exit(0);
}
if ((p1=(struct list*)malloc(sizeof(struct list)))==NULL)//开辟新结点p1
{
printf("分类1不能成功分配内存储块!");
exit(0);
}
p1->next = NULL;
for (i=h;i!=NULL;i=i->next) {
while ((fscanf(fp,"%d %s %s %lf",&p1->number,&p1->id,p1->name,&p1->price))!=EOF) {
if (i->number==p1->number) {
i->sum++;
if (i->child==NULL) i->child=p1;
else p2->next=p1;
p2=p1;
if ((p1=(struct node*)malloc(sizeof(struct node)))==NULL) {
printf("商品不能成功分配内存储块!");
exit(0);
}
p1->next=NULL;
}
}
rewind(fp);//将文件内部的位置指针重新指向一个文件的开头
}
fclose(fp);
}
void add(struct list* h)//系统的添加功能
{
int choice;
while(1){
system("cls");
printf("\n");
printf("===================欢迎使用系统的添加功能======================\n");
printf("= =\n");
printf("= 1---添加目录 =\n");
printf("= =\n");
printf("= 2---添加商品 =\n");
printf("= =\n");
printf("= 0---退出该功能 =\n");//返回上一目录
printf("= =\n");
printf("===============================================================\n");
printf("请输入你的选择:");
scanf("%d",&choice);
while(choice!=1&&choice!=2&&choice!=0){
printf("输入错误,请重新输入!");
scanf("%d",&choice);
}
switch(choice){
case 1://添加目录
add_kind(h);
system("pause");
break;
case 2://添加商品
system("pause");
break;
case 0://退出该功能
return 0;
}
}
}
//添加一个商品类别
void add_kind(struct list* h)
{
struct list* i=NULL;
int number;
if(choice1==2)
{
system("cls");
printf("\n 类别编号 类别名称 类别数目 \n");
printf("================================\n");
for (i=h;i!=NULL;i=i->next) {
printf("= %-6d %-8s %-7d =\n", i->number, i->name, i->sum);
}
printf("================================\n");
}
printf("请输入所需要创建商品类别的编号:\n");
scanf("%d",&number);
i=(struct list*)malloc(sizeof(struct list));
i->next=NULL;
i->child=NULL;
i->number=number;
printf("请输入所需要创建商品类别的名称:\n");
scanf("%s",i->name);
i->sum=0;//商品初始种数为0
i->log=++logo;
i->next=h->next;
h->next=i;
printf("添加成功!\n");
}
void close(struct list* h)
{
FILE* fp;
struct list* i=h;
struct node* j=NULL;
if ((fp=fopen(filename_list,"w+"))==NULL)
{
printf("不能打开该文件!\n");
exit(0);
}
while (i!=NULL){
fprintf(fp,"%d %s\n",i->number,i->name);
i=i->next;
}
if (fclose(fp)){
printf("不能正常关闭文件!");
exit(0);
}
if ((fp = fopen(filename_goods, "w+"))==NULL) {
printf("不能打开该文件!\n");
exit(0);
}
for (i=h;i!=NULL;i=i->next)
{
for (j=i->child;j!=NULL;j=j->next)
{
fprintf(fp, "%d %s %s %.2lf\n",j->number,j->id,j->name,j->price);
}
}
if (fclose(fp))
{
printf(" 不能正常关闭文件!");
exit(0);
}
free(h);
if (choice1 == 0)
{
free(filename_list);
free(filename_goods);
}
}
```c
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/992789092736173.png "#left")
我 尝试了添加商品的路径
最下面显示添加成功