该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
struct node* list();
struct node* head();
struct Node* colour_list();
struct node* output(struct node* p);
struct node
{
char contractnumber[20];//合同号
char name[20];//产品名称
int quantity;//数量
struct Node* colour;//颜色
struct node* next;
};
struct Node
{
char colour[20];//颜色
int number;//数量
struct Node* next;
};
struct node* list()//申请新节点
{
struct node* p = (struct node*)malloc(sizeof(struct node));
p->next = NULL;
return p;
}
struct node* head()//头节点
{
struct node* p = list();
return p;
}
struct Node* colour_list()//申请颜色节点
{
struct Node* p = (struct Node*)malloc(sizeof(struct Node));
p->next = NULL;
return p;
}
void fwrite()//保存数据
{ FILE* fp = NULL;
fopen_s(&fp, "dong6.dat", "wb+");
if (!fp)
{
printf("建立文件失败!\n");
return;
}
fclose(fp);
}
void fwrite_fp(struct node* head)//保存数据
{
struct node* p = head;
FILE* fp = NULL;
fopen_s(&fp, "dong6.dat", "ab+");
if (!fp)
{
printf("打开文件失败!\n");
return;
}
else
{
if (p == NULL)
{
printf("没有数据无法保存!\n");
return;
}
else
{
while (p)
{
struct Node* p1 = p->colour;
while (p1)
{
fwrite(p1, sizeof(p1), 1, fp);
p1 = p1->next;
}
fwrite(p, sizeof(p), 1, fp);
p = p->next;
}
}
}
fclose(fp);
}
void fread_fp(struct node* head)//读取数据
{
struct node* p =head;
FILE* fp = NULL;
fopen_s(&fp, "dong6.dat", "wb");
if (!fp)
{
printf("打开文件失败!\n");
return;
}
else
{
while (feof(fp) == 0)
{
struct node* newp = list();
struct Node* p1 = colour_list();
newp->colour= p1;
while (p->next != NULL)
{
p = p->next;
}
fread(newp,sizeof(newp),1,fp);
while (p1)
{
fread(p1, sizeof(p1), 1, fp);
p1 = p1->next;
}
p->next = newp; } }
fclose(fp);
}
void input(struct node* p)
{
char c = 'c';
struct Node* p1 = colour_list();//颜色头节点
p->colour = p1;//颜色头节点赋值
printf("请输入合同号:\n");
scanf_s("%s", &p->contractnumber, 20);
printf("请输入产品名称:\n");
scanf_s("%s", &p->name, 20);
printf("请输入合同数量:\n");
scanf_s("%d", &p->quantity);
while (1)
{
struct Node* p2 = colour_list();//颜色申请新节点
while (p1->next != NULL)
{
p1 = p1->next;
}
printf("请输入颜色:\n");
scanf_s("%s", &p2->colour, 20);
printf("请输入%s色数量:\n", p2->colour);
scanf_s("%d", &p2->number); p1->next = p2;
printf("是否有其他颜色?(y/n)\n");
scanf_s(" %c", &c, 1); if (c == 'n' || c != 'y')
{
c = 'c';
break;
}
}
}
struct node* output(struct node* p)//输出单个链表
{
struct Node* p1;
printf("合同号:%s\t产品名称:%s\t数量:%d\n", p->contractnumber, p->name, p->quantity);
p1 = p->colour->next;
while (p1)
{
int i = 0;
printf("%s\t%d\t", p1->colour, p1->number);
i++;
if (i % 2 == 0)
{
printf("\n");
}
p1 = p1->next;
}
printf("\n");
return 0;
}
struct node* output_list(struct node* head)//输出链表
{
struct node* p = head->next;
while (p)
{
output(p);
p = p->next;
}
return 0;
}
struct node* colorinsert_w(struct node* head)//结尾插入
{
struct node* p=head; while (1)
{
char c = 'c';
struct node* newp = list(); if (p->next != NULL)
{
while (p->next != NULL)
{
p = p->next;
}
}
input(newp);
p->next = newp;
printf("是否有其他合同?(y/n)\n");
scanf_s(" %c", &c, 1);
if (c == 'n' || c != 'y')
{
c = 'c';
break;
}
}
return p;
}int main()
{
struct node*p= head();
colorinsert_w(p);
fwrite_fp(p);
fread_fp(p);
output_list(p);
return 0;
}