#include
#include
#include
#include
#define MAXSIZE 100
typedef struct NODE
{
char name[MAXSIZE];
int p_num;
double p_price;
char kind[MAXSIZE];
struct NODE next;
}node;
//下面这部分是啥???
typedef struct
{
char name[MAXSIZE];
int p_num;
double p_price;
}str;
#if 0
void create(node *head); /创建货物 main 194line*/
void del(node head); /删除指定货物*/
void find(node head) /查找货物*/
void all_delete(node head); /清空全部货物*/
void change(node head); /修改货物信息*/
int display() /*菜单*/
#endif
void create(node head) /创建货物 main 194line*/
{
node p; /判断货物是否存在的指针变量*/
int i = 1;
char name[MAXSIZE];
int p_num;
double p_price;
char kind[MAXSIZE];
printf("*****************请输入货物名称:************\n");
scanf("%s",name);
//getchar();
p = head->next;
while(p != NULL)
{
if(strcmp(p->name,name) == 0)
{
printf("*********货物已存在************\n");
p = NULL;
i = 0; /* 货物已存在,不执行输入货物命令*/
}
else
{
p = p->next;
}
}
while( 1 == i)
{
node *q;
printf("**************请输入货物单位价格:**************\n");
scanf("%lf",&p_price);
printf("**************请输入货物数量:**************\n");
scanf("%d",&p_num);
printf("*****************请输入货物所属类型:************\n");
scanf("%s",kind);
q = (node*)malloc(sizeof(node));
if(NULL == q)
{
printf("error2\n");
}
strcpy(q->name,name);
//strcpy(q->p_num,p_num);
q->p_num=p_num;
q->p_price=p_price;
strcpy(q->kind,kind);
q->next = head->next;
head->next = q; /*头插法*/
i = 0;
}
}
//我的代码跟这个的区别:没有在函数内重新定义变量 然后使用strcpy这些赋值
void order(node head,int len1) //给货物排序
{
node *p; /用来排序的变量*/
node q; /同上*/
p = head->next;
while(len1 > 0) /*实现货物按字母排列*/
{
while(p != NULL && p->next!= NULL)
{
char tempname[MAXSIZE]; /*相当于中间变量用来存储货物信息*/
int tempp_num;
double tempp_price;
char tempkind[MAXSIZE];
if(strcmp(p->name,p->next->name) > 0)
{
strcpy(tempname, p->next->name);
tempp_price=p->next->p_price;
tempp_num=p->next->p_num;
strcpy(tempkind,p->next->kind);
strcpy(p->next->name,p->name);
p->next->p_price=p->p_price;
p->next->p_num=p->p_num;
strcpy(p->next->kind,tempkind);
strcpy(p->name,tempname);
p->p_price=tempp_price;
p->p_num=tempp_num;
strcpy(p->kind,tempkind);
}
else
{
p=p->next;
}
}
len1--;
}
}
void print(node head) /打印联系人的函数*/
{
int i = 1;
node *p;
p = head->next;
printf("现在货物为:\n");
printf("编号 名称 价格 数量 所属类型\n");
while(p != NULL)
{
printf("%d %s %.2lf %d %s\n",
i,p->name,p->p_price,p->p_num,p->kind);
p = p->next;
i++;
}
}
void del(node head) /删除指定货物函数*/
{
node *p;
node *q;
q = head;
p = q->next;
char name[MAXSIZE];
printf("************请输入要删除的货物名称:*********\n");
scanf("%s",name);
while((p != NULL) && (strcmp(p->name,name) != 0))
{
q = p;
p = p->next;
}
if(NULL == p)
{
printf("************未查找到***************\n");
}
else
{
q->next = p->next;
free(p);
printf("***********删除成功****************\n");
}
}
void find(node head) //查找货物
{
node *p;
char name[MAXSIZE];
printf("******请输入要查找货物名称:**********\n");
scanf("%s",name);
p = head->next;
while(p != NULL && strcmp(p->name,name) != 0)
{
p = p->next;//为什么del函数里面还定义了一个指针q
}
if(NULL == p)
{
printf("********没有这个货物*********\n");
}
else
{
printf(" 名称:%s\n 价格:%.2lf 数量:%d 所属类型:%s",
p->name,p->p_price,p->p_num,p->kind);
}
}
void all_delete(node head) //清空全部货物
{
while(head->next != NULL)
{
node *q;
q = head->next;
head->next = q->next;
free(q);
}
if(NULL == head->next)
{
printf("********清除所有货物成功*********\n");
}
}
void change(node *head) //修改货物信息
{
node *p;
char name[MAXSIZE];
double p_price;
int p_num;
char kind[MAXSIZE];
p = head->next;
printf("请输入要修改的货物名称\n");
scanf("%s",name);
while( p != NULL)
{
if(strcmp(p->name,name) == 0)
{
printf("请输入要修改的货物价格\n");
scanf("%lf",p_price);
p->p_price=p_price;
printf("修改成功\n");
break;
}
else
{
p = p->next;
}
}
if(p == NULL)
{
printf("未查找到此联系人!\n");
}
}
int sum(node *head) //求链表中有多少货物
{
int count_node = 0;
node *p;
p = head->next;
if(p != NULL)
{
count_node++;
p = p->next;
}
return count_node;
}
/*void write_to_file(node *head,int len) //链表中的货物信息写到文件
{
int i;
str string[100];
FILE *fp = fopen("a2","wb");
if(NULL == fp)
{
printf("open error\n");
exit(1);
}
//printf("1111\n");
while( head->next != NULL)
{
for(i = 0;i < len;i++)
{
strcpy(string[i].name,head->next->name);
strcpy(string[i].p_num,head->next->p_num);
fwrite(&string[i],sizeof(str),1,fp); //依次将结构体数组写到文件
head = head->next;
}
}
fclose(fp);
}
void read_to_linklist(node *head) //将文件中的数据读到链表中
{
int i;
int m;
int j = 0;
node *p;
node *q;
p = head;
FILE *fp;
fp = fopen("a2","rb");
fseek(fp,0,SEEK_END);
i = ftell(fp);
fseek(fp,0,SEEK_SET);
m = (i/(sizeof(str))); // m :文件中有多少个联系人
str string[100]; //结构体数组,存放所有联系人
fread(string,sizeof(str),m,fp);
while(m > 0)
{
q = (node*)malloc(sizeof(node));
strcpy(q->name,string[j].name);
strcpy(q->p_num,string[j].p_num);
m--;
j++;
p->next = q;
p = q;
}
fclose(fp);
}*/
int display() //菜单
{
printf( "\n**************请选择要进行的操作***********\n");
printf("-----------------------------------------------\n");
printf("*|************* 1 .添加货物******************|*\n");
printf("*|************* 2 .删除货物******************|*\n");
printf("*|**************3 .查找货物******************|*\n");
printf("*|**************4 .显示货物******************|*\n");
printf("*|**************5 .修改货物******************|*\n");
printf("*|**************6 .清空货物******************|*\n");
printf("*|**************7 .退 出******************|*\n");
printf("-----------------------------------------------");
}
int main()
{
int a;
int n = 1;
node *head;
node *q;
head = (node*)malloc(sizeof(node));
if(NULL == head)
{
printf("error\n");
}
q = head;
head->next = NULL;
//read_to_linklist(q);
while(n == 1)
{
printf("\n",display());
printf("\n**********************请输入要进行的操作**********\n\n");
scanf("%d",&a);
switch(a)
{
case 1:
while(getchar() != '\n');
create(q);
order(q,sum(q));
a = 0;
break;
case 2:
while(getchar() != '\n');
del(q);
getchar();
print(q);
break;
case 3:
find(q);
break;
case 4:
while(getchar() != '\n');
print(q);
break;
case 5:
while(getchar() != '\n');
change(q);
break;
case 6:
all_delete(q);
break;
case 7:
//write_to_file(q,sum(q));
n = 0;
break;
default:
{
printf("*****输入错误,请重新选择指令:******\n");
}
while(getchar() != '\n');
break;
}
}
free(head);
}