#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SNUM 3
//Inventory型の宣言
typedef struct{
int volume;
char date[7];
} Inventory;
//Product型の宣言
typedef struct product{
int code,
price;
char name[30];
Inventory info;
struct product *next; //次のノードへのポインタ
} Product;
//List型の宣言(リスト制御ブロック)
typedef struct{
Product *head; //先頭ノードを指すポインタ
Product *tail; //末尾ノードを指すポインタ
} List;
//関数のプロトタイプ宣言
Product *Alloc_Node(void);
void Init_List(List *list),
load_file_data(List *list),
print_list(List *list),
print_node(Product *pt);
int Inp_Data(Product *pt);
Product *Search(List *list,char key);
int main(void)
{
List shop_A; //リスト制御ブロックの宣言
char key[20]; //検索キーの値
int retry; //再実行判別フラグ(0:再実行しない 1:再実行)
Init_List(&shop_A); //空の線形リストの作成
while(load_file_data(&shop_A)!=EOF){} //線形リストの作成
print_list(&shop_A); //リスト上の全ノードの表示
do{
printf("\n検索キー:");
scanf("%s",&key); //品名の入力
print_node(Search(&shop_A,key));
printf("もう一度?0-いいえ/1-はい:");
scanf("%d",&retry); //再実行判定フラグretryの値の入力
}while(retry==1); //retryが1の間は実行を繰り返す
return 0;
}
void Init_List(List *list)
{
list->head=list->tail=Alloc_Node(); //ダミーノードの作成
}
Product *Alloc_Node(void)
{
Product *pt;
pt=(Product*)calloc(1,sizeof(Product)); //Product型1個分動的に確保
if(pt==NULL){
printf("記憶領域の確保に失敗しました\n");
exit(1);
}
return pt;
}
void load_file_data(List *list)
{
Product *pt,*ip;
pt=Alloc_Node();
if(Inp_Data(pt)==EOF) return EOF;
//データを商品コード番順に追加
//商品コード番号が現在のデータより大で次のデータより小
for(ip=list->head;ip->next!=NULL;ip=ip->next){
if(pt->code<ip->code){ //規データ数1の場合で
pt->next=ip;
list->head=pt;
printf("0\n");
return 1;
}
if(pt->code<ip->next->code){
pt->next=ip->next;
ip->next=pt;
printf("1\n");
return 1;
}
}
if(ip->next==NULL){ //商品コード番号が最大の場合
*list->tail=*pt; //ptの中身をダミーノードにコピー
list->tail->next=pt; //データを末尾に追加
list->tail=pt;
printf("2\n");
return 1;
}
else return EOF;
}
int Inp_Data(Product *pt)
{
int i;
FILE *fp; //ファイルポインタ
//入力ファイルのオープン(読み込みモード)
fp=fopen("product.txt","r");
if(fp==NULL){
printf("入力ファイルのオープンに失敗しました\n");
exit(1);
}
//入力ファイルからデータの読み込み
for(i=0;i<SNUM;i++){
fscanf(fp,"%d%s%d%d%s",
&pt[i].code,
&pt[i].price,
&pt[i].name,
&pt[i].info.volume,
&pt[i].info.date);
}
fclose(fp); //入力ファイルのクローズ
return 1;
}
void print_list(List *list)
{
Product *pt;
pt=list->head;
printf("****商品情報の表示****\n");
while(pt!=list->tail){
printf("%d\t",pt->code);
printf("%s\t",pt->name);
printf("%d\t",pt->price);
printf("%d\t",pt->info.volume);
printf("%s\n",pt->info.date);
pt=pt->next;
}
}
void print_node(Product *pt)
{
printf("%d\t",pt->code);
printf("%s\t",pt->name);
printf("%d\t",pt->price);
printf("%d\t",pt->info.volume);
printf("%s\n",pt->info.date);
}
Product *Search(List *list,char key)
{
Product *ip=list->head;
while(ip!=list->tail){
strcmp
ip=ip->next;
}
return NULL;
}
//product.txt
/*
1001 六角ナット 6 200 090211
1002 ナベ小ネジ 22 100 090210
1003 Oリング 155 25 090301
*/