69,371
社区成员
发帖
与我相关
我的任务
分享
int main()
{
Invrec *new_rec;
int n_parts;
int cost,supplier;
n_parts =8;
cost = 88;
supplier =9;
/*new_rec = create_subassy_record(n_parts);
if(new_rec == NULL)
{
printf("error!\n");
return 0;
}
printf("n_parts=%d,%3type=%d",new_rec->info.subassy->n_parts,(int)(new_rec->type));
discard_inventory_record(new_rec);*/
new_rec = create_part_record(cost,supplier);
if(new_rec == NULL)
{
printf("error!\n");
return 0;
}
printf("costs %d dollars the supplier is %d\n",new_rec->info.part->cost,new_rec->info.part->supplier);
discard_inventory_record(new_rec);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int cost;
int supplier;
}Partinfo;
typedef struct
{
int n_parts;
struct SUBASSYPART
{
char partno[10];
short quan;
}*part;
}Subassyinfo;
typedef struct
{
char partno[10];
int quan;
enum {PART,SUBASSY} type;
union
{
Partinfo *part;
Subassyinfo *subassy;
}info;
}Invrec;
Invrec *create_subassy_record(int n_parts);
Invrec *create_part_record(int cost,int supplier);
void discard_inventory_record(Invrec *record);
#include "inventor.h"
Invrec *create_subassy_record(int n_parts)
{
Invrec *new_rec;
new_rec = malloc(sizeof(new_rec));
if(new_rec != NULL)
{
new_rec->info.subassy=malloc(sizeof( Subassyinfo ));
if(new_rec->info.subassy != NULL)
{
new_rec->info.subassy->part=malloc(sizeof(struct SUBASSYPART)*n_parts);
if(new_rec->info.subassy->part != NULL)
{
new_rec->type = SUBASSY;
new_rec->info.subassy->n_parts = n_parts;
return new_rec;
}
free(new_rec->info.subassy);
}
free(new_rec);
}
return NULL;
}
Invrec *create_part_record(int cost,int supplier)
{
Invrec *new_rec;
new_rec = malloc(sizeof(new_rec));
if(new_rec != NULL)
{
new_rec->info.part=malloc(sizeof(Partinfo));
if(new_rec->info.part !=NULL)
{
new_rec->type=PART;
new_rec->info.part->cost=cost;
new_rec->info.part->supplier=supplier;
return new_rec;
}
free(new_rec);
}
return NULL;
}
void discard_inventory_record(Invrec *record)
{
if(record->type == SUBASSY)
{
free(record->info.subassy->part);
free(record->info.subassy);
}
else
{
free(record->info.part);
}
free(record);
}