Динамическое формирование и преобразование списков и структур

Курсовой проект - Компьютеры, программирование

Другие курсовые по предмету Компьютеры, программирование

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Схема 6 - Функция save

 

  1. Текст программы

 

#include

#include

#include

#include

#include

#include

#define mg struct mg

#define ad struct ad

#define pr struct pr

pr {

char name[20];

int price;

char time[20];

};

ad {

char city[20];

char str[20];

int dom;

};

mg {

int num;

ad adr;

pr pro[5];

mg *prev, *next;

};

///////////////////////////OPEN LIST FROM FILE///////////////////////////////

mg *open (mg *last) {

mg *p, *start;

FILE *f;

int n;

char filename[50];

start=NULL;

last=NULL;

clrscr();

printf("Enter path to file to open (ex. C:\\file.txt):\n");

scanf("%s", filename);

if((f=fopen(filename, "r"))==NULL) {

printf("(!) Invalid filename or file is missing! Press any key to return to menu.");

getch();

} else

if(filelength(fileno(f))==0) {

clrscr();

printf("File is empty! Press any key...");

getch();

goto end;

}

else {

while (!feof(f)) {

p=(mg*)malloc(sizeof(mg));

scanf(f, "%d", &(p->num));

adr.city);">fscanf(f, "%s", p->adr.city);

adr.str);">fscanf(f, "%s", p->adr.str);

adr.dom));">fscanf(f, "%d", &(p->adr.dom));

for(n=0;n<5;n++) {

fscanf(f, "%s", p->pro[n].name);

fscanf(f, "%d", &(p->pro[n].price));

fscanf(f, "%s", p->pro[n].time);

}

p->prev=last;

p->next=NULL;

if(last!=NULL)

last->next=p;

last=p;

if(start==NULL) start=last;

}

fclose(f);

printf("File readed succesfully! Press any key...");

getch();

}

end:

return start;

}

//////////////////////PRINT OLD LIST ON SCREEN/////////////////////////////

void *print (mg *start) {

int n;

mg *p=start;

while(p->next!=NULL) {

clrscr();

printf("\nMAGAZINE #%d", p->num);

printf("\n Address");

adr.city);">printf("\n City: %s", p->adr.city);

adr.str);">printf("\n Street: %s", p->adr.str);

adr.dom);">printf("\n House: %d", p->adr.dom);

printf("\nProducts");

for(n=0;n<5;n++) {

printf("\n %d)", n+1);

printf("\n Name: %s", p->pro[n].name);

printf("\n Price: %d", p->pro[n].price);

printf("\n Time: %s", p->pro[n].time);

printf("\n");

}

printf("\n\nAny key to next record...");

getch();

p=p->next;

}

return start;

}

///////////////////////////PRINT NEW LIST ON SCREEN//////////////////////////

void *printnew (mg *start2) {

int n;

mg *p2=start2;

clrscr();

while(p2!=NULL) {

clrscr();

printf("\nMAGAZINE #%d", p2->num);

printf("\n Address");

adr.city);">printf("\n City: %s", p2->adr.city);

adr.str);">printf("\n Street: %s", p2->adr.str);

adr.dom);">printf("\n House: %d", p2->adr.dom);

printf("\nProducts");

for(n=0;n<5;n++) {

printf("\n %d)", n+1);

rintf("\n Name: %s", p2->pro[n].name);

printf("\n Price: %d", p2->pro[n].price);

printf("\n Time: %s", p2->pro[n].time);

printf("\n");

}

printf("\n\nAny key to continue...");

getch();

p2=p2->next;

}

return start2;

}

//////////////////////////SAVE NEW LIST//////////////////////////////////

void save (mg *start, mg *start2) {

mg *p, *p2;

FILE *f;

int n;

char filename[50];

clrscr();

printf("Enter path to file to write (ex. C:\\file.txt):\n");

scanf("%s",filename);

if((f=fopen(filename, "w"))==NULL) {

clrscr();

printf("Invalid filename or file is corrupt! Press any key...");

getch();

goto end;

}

p=start;

while(p->next!=NULL) {

clrscr();

fprintf(f, "%d\n", p->num);

adr.city);">fprintf(f, "%s\n", p->adr.city);

adr.str);">fprintf(f, "%s\n", p->adr.str);

adr.dom);">fprintf(f, "%d\n", p->adr.dom);

for(n=0;n<5;n++) {

fprintf(f, "%s\n", p->pro[n].name);

printf(f, "%d\n", p->pro[n].price);

fprintf(f, "%s\n", p->pro[n].time);

}

p=p->next;

}

fprintf(f, "---------------\n");

p2=start2;

while(p2!=NULL) {

clrscr();

fprintf(f, "%d\n", p2->num);

adr.city);">fprintf(f, "%s\n", p2->adr.city);

adr.str);">fprintf(f, "%s\n", p2->adr.str);

adr.dom);">fprintf(f, "%d\n", p2->adr.dom);

for(n=0;n<5;n++) {

fprintf(f, "%s\n", p2->pro[n].name);

fprintf(f, "%d\n", p2->pro[n].price);

fprintf(f, "%s\n", p2->pro[n].time);

}

p2=p2->next;

}

fclose(f);

printf("File created succesfully! Press any key...");

getch();

end:

}

////////////////////////////CREATING NEW LIST//////////////////////////////

mg *newlist (mg *start) {

mg *p, *p2, *start2, *last2;

FILE *f;

char prod[20];

int np, k=0, sump=0, n, count=0;

 

p2=last2=start2=NULL;

clrscr();

printf("Enter products name: ");

scanf("%s", prod);

p=start;

while(p->next!=NULL) {

count++;

for(n=0;n<5;n++) {

if (strcmp(p->pro[n].name, prod)==0) {

np=n;

sump=p->pro[n].price+sump;

}

else { k++; }

}

if (k==5*count) {

printf("There is no such product. Press any key...");

getch();

goto end;

}

p=p->next;

}

p=start;

while(p->next!=NULL) {

if(p->pro[np].price > (sump/count))

if ((p->pro[np].price - (sump/count)) > ((sump/count)/10)) {

p2=(mg*)malloc(sizeof(mg));

p2->num=p->num;

adr.city,p->adr.city);">strcpy(p2->adr.city,p->adr.city);

adr.str,p->adr.str);">strcpy(p2->adr.str, p->adr.str);

adr.dom=p->adr.dom;">p2->adr.dom=p->adr.dom;

for(n=0;n<5;n++) {

strcpy(p2->pro[n].name, p->pro[n].name);

p2->pro[n].price=p->pro[n].price;

strcpy(p2->pro[n].time,p->pro[n].time);

}

p2->prev=last2;

p2->next=NULL;

if(last2!=NULL)

last2->next=p2;

last2=p2;

if(start2==NULL) start2=last2;

p2=p2->next;

}

p=p->next;

}

printf("New list created succesfully! Press any key...");

getch();

end:

return start2;

}

/////////////////////////MAIN FUNCTION////////////////////////////////

void main() {

mg *start, *start2, *last;

int i,n, newn,t=0;

char ans;

clrscr();

for (i=1;i<2;) {

clrscr();

printf("====================MENU=====================\n");

printf("1. Open file\n");

printf("2. Print list of markets\n");

printf("3. Create a new list\n");

printf("4. Print new list of markets\n");

printf("5. Save new list\n");

printf("6. Exit\n\n");

printf("Press 1-6 key. Any other key will exit program");

gotoxy(1, 20);

printf("===============COURSE PROJECT=================\nStudent: Saushkin Pavel Nikolaevich. Group: CBC-31. MIEM 2008");

ans=getch();

switch(ans) {

case 1: {

start=open(last);

if(start!=NULL) t=1;

else if(start->num > 1000 || start->num < 1) {

clrscr();

printf("(!) Error of reading file! Incapable data type! Press any key...");

getch();

break;

}

break;

}

case 2: {

if(t==0) goto err1;

print(start);

break;

}

case 3: {

if(t==0) goto err1;

start2=newlist(start);

t=2;

break;

}

case 4: {

if(t==0) goto err1; else if (t==1) goto err2;

printnew(start2);

break; }

case 5: {

if(t==0) {

err1:

clrscr();

printf("You must open a file! Press any key...");

getch();

break;

} else