022
06.06.2005, 23:24 Uhr
Pler
Einer von Vielen (Operator)
|
store.cpp ( ist eigentlich alles Standard C )
C++: |
#include "store.h"
struct list* Alloc_List(void) { struct list *elem;
elem=(struct list* )malloc(sizeof(struct list)*1);
return elem; }
struct articles* Alloc_Articles(void) { struct articles *article;
article=(struct articles* )malloc(sizeof(struct articles)*1);
return article; }
void Inc_List(struct list** head) // Da Inc_List nur von Add_To_List gebraucht wird // waere es besser sie in eine Funk. zu tun // vorallem muss man dann die Liste nicht zwei mal durchlaufen // Urspruenglich sollte Inc_List aber oefter verwendet werden { struct list *inc, *act;
inc= Alloc_List();
inc->next=NULL; inc->article=Alloc_Articles();
if(!(*head)) // Neue Liste anlagen { *head=inc; } else // Bestehende Liste erweitern { for(act=*head;act->next!=NULL;act=act->next); /* For-Schleife durchlaueft die Liste */ act->next=inc; } }
void Add_To_List(struct list** head, struct articles* new_article) { struct list *act;
Inc_List(head); for(act=*head;act->next!=NULL;act=act->next); *(act->article)=*new_article; act->next=NULL; }
void Read_New_Article(struct list** head) { struct articles article;
printf("\nBitte die Produkt-ID eingeben:\n"); scanf("%d", &article.pid);
printf("\nBitte den Namen des Getraeks eingeben:\n"); fflush(stdin); gets( article.name);
printf("\nBitte den Alkoholgehalt des Getraenks eingeben:\n"); scanf("%lg", &article.alk);
printf("\nBitte den Koeffizienten eingeben:\n"); scanf("%d", &article.coef);
article.quan=0;
Add_To_List(head, &article); }
int Load_List(struct list** head) { FILE *stream;
struct articles *article;
if(!head) return -2;
if(!(stream=fopen(FILENAME,"rb"))) return -1;
article=Alloc_Articles();
while(!feof(stream)) { if(fread(article, sizeof(struct articles), 1, stream)==1) Add_To_List(head, article); }
fclose(stream);
return 0; }
int Save_List(struct list *head) // Der Head-Zeiger wird nur in der Funktion ver�dert { // Er bleibt dem Hauptprogramm erhalten FILE *stream; if(!(stream=fopen(FILENAME,"wb"))) return -1;
while(head) { fwrite(head->article, sizeof(struct articles),1,stream); head=head->next; }
fclose(stream);
return 0; }
void Print_Article(struct articles* article) { printf("\n%d\n", article->pid); printf("%s\n", article->name); printf("%lg\n", article->alk); printf("%d\n", article->coef); printf("%d\n", article->quan); }
void Print_List(struct list* head) { if(!head) return;
while(head) { Print_Article(head->article); // Der Head-Zeiger wird nur in der Funktion ver�dert head=head->next; // Er bleibt dem Hauptprogramm erhalten } }
int Sub_From_List(struct list** head,int pid) { struct list *act, *temp;
if(!head || !(*head)) return -2;
act=*head;
if(pid==act->article->pid) // Wenn erstes Listenelement geloescht werden soll { if(act->article->quan) return -3; temp=*head; *head=act->next; free(temp); }
else // Nicht das erste Listenelement // Unterscheidung notwendig, da nicht vor dem ersten Listenelement stehen geblieben werden kann { for(act=*head;act->next && pid!=act->next->article->pid;act=act->next); /* For-Schleife durchlaueft die Liste */ if(!(act->next)) return -1;
if((act->next->article->quan)) return -3;
temp=act->next; act->next=act->next->next;
free(temp); }
return 0; }
void Free_List(struct list* head) { struct list *ptr;
if(!head) return;
do{ ptr=head; head=head->next; free(ptr->article); free(ptr); }while(head); }
struct boxes* Alloc_Box(void) { struct boxes *box;
box=(struct boxes* )malloc(sizeof(struct boxes)*1);
return box; }
struct boxes* Load_Box(void) { FILE *stream;
struct boxes *box;
if(!(stream=fopen(FILENAME2, "rb"))) return NULL;
box=Alloc_Box();
fread(box, sizeof(struct boxes), 1, stream);
fclose(stream);
return box; }
int Save_Box(struct boxes* box) { FILE *stream; if(!(stream=fopen(FILENAME2, "wb"))) return -1;
fwrite(box, sizeof(struct boxes), 1, stream);
fclose(stream);
return 0; }
void Free_Box(struct boxes* box) { free(box); }
struct list* Pass_List(struct list* head, int pid) { struct list *act;
for(act=head;act && pid!=act->article->pid;act=act->next); return act; }
int Add_Bottle(struct list* head, struct boxes* box, int pid, int loc) { if(!(head=Pass_List(head, pid))) return -1;
(head->article->quan)+=1;
box->loc[loc]=pid;
return 0; }
void Sub_Bottle(struct list* head, struct boxes* box, int pid, int loc) { head=Pass_List(head, pid);
head->article->quan-=1;
box->loc[loc]=0; }
int End_Store(struct list* head, struct boxes* box) { int return1, return2;
return1=Save_List(head);
return2=Save_Box(box);
if(return1==-1 || return2==-1) return -1;
if(return1==-2 || return2==-2) return -2; Free_Box(box);
Free_List(head);
return 0; }
int List_Size(struct list* head) { struct list *act; int count;
count=0; for(act=head;act;act=act->next) count++;
return count; }
|
store.h:
C++: |
#include <stdio.h> #include <stdlib.h> #include <string.h>
#define NAME_LENG 20 #define MAX_LOCS 20 #define FILENAME "store1.dat" #define FILENAME2 "store2.dat"
struct articles{ int pid; /* Produkt-ID */ char name[NAME_LENG+1]; /* Name des Getraenks */ double alk; /* Alkoholgehalt des G. */ int coef; /* Einschenkkoeffizient */ int quan; /* Anzahl des G. */ };
struct list{ struct articles *article; struct list *next; /*Zeiger auf das naechste Listenelement */ };
struct boxes{ int loc[MAX_LOCS]; /* Plaetze im Kasten */ };
void Inc_List(struct list** );
struct articles* Alloc_Articles(void);
struct list* Alloc_List(void);
void Read_New_Article(struct list** );
void Print_Article(struct articles* );
void Print_List(struct list* );
void Add_To_List(struct list** , struct articles* );
int Load_List(struct list** );
int Save_List(struct list* );
int Sub_From_List(struct list** , int);
void Free_List(struct list* );
struct boxes* Alloc_Box(void);
struct boxes* Load_Box(void);
int Save_Box(struct boxes* );
void Free_Box(struct boxes* );
struct list* Pass_List(struct list* , int);
void Sub_Bottle(struct list* , struct boxes* , int, int);
int Add_Bottle(struct list* , struct boxes* , int, int);
int End_Store(struct list* , struct boxes* );
int List_Size(struct list* );
void Pic_Name(char* , int);
|
|