Читайте также:
|
|
//---------------------------------------------------------------------------
/*СВБС-21, Матросов, Вар. №11*/
#include <vcl.h>
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
//---------------------------------------------------------------------------
#define magazin struct magazin
#define products struct products
#define address struct address
address
{
char gorod[20];
char ulica[20];
int nomd;
};
products
{
char name[20];
float price;
int srok;
products *next;
};
magazin
{
int nomer;
address adr;
products *prod;
magazin *next;
};
magazin* input(char *namevh,char *namevih) // schitivaem dannie iz faila
{
FILE *f,*f1;
magazin *magaz,*last=NULL; // magazini, nachalo steka
products *prd,*lastprd; // producti, nachalo steka
int temp;
if ((f=fopen(namevh,"r"))==NULL)
{
puts("Vhodnoi Fail ne naiden");
getch();
return last;
}
if ((f1=fopen(namevih,"w"))==NULL)
{
puts("Vihodnoi Fail ne naiden");
getch();
return last;
}
if ((fgetc(f))!=-1)
{
fseek(f,0,0);
puts("Ish.dannie:\nNomer, Gorod, Ulica, nomer Doma\n");
while (!feof(f))
{
magaz=(magazin*)malloc(sizeof(magazin));
fscanf(f,"%d%s%s%d",&(magaz->nomer),magaz->adr.gorod,magaz->adr.ulica,&(magaz->adr.nomd));
fprintf(f1,"%d %s %s %d\n",magaz->nomer,magaz->adr.gorod,magaz->adr.ulica,magaz->adr.nomd);
printf("%3d, %s, %s, %d\n",magaz->nomer,magaz->adr.gorod,magaz->adr.ulica,magaz->adr.nomd);
temp=1;
lastprd=NULL;
while(temp)
{
prd=(products*)malloc(sizeof(products));
fscanf(f,"%s%f%d",prd->name,&(prd->price),&(prd->srok));
fprintf(f1,"%s %.2f %d\n",prd->name,prd->price,prd->srok);
if (strcmp(prd->name,"*")==0&&prd->price==0&&prd->srok==0)
{
temp=0;
}
else
{
prd->next=lastprd;
lastprd=prd;
}
}
magaz->prod=lastprd;
magaz->next=last;
last=magaz;
}
}
else
printf("Fail pust\n");
fclose(f);
fclose(f1);
puts("------\n");
return last;
}
magazin* sort(magazin *magaz) //sortirovka dannih
{
magazin *last=NULL,*next; // nachalo steka, sled. element
products *prd; // vrem. producti
address addr; // vrem. address
int tempn; // vrem. nomer magazina
int check=1; // proverka na izmenenie
last=magaz;
while (check)
{
check=0;
while (magaz!=NULL&&magaz->next!=NULL)
{
next=magaz->next;
/*sortirovka pusirkom*/
if (strcmp(next->adr.gorod,magaz->adr.gorod)<0) // proverka po gorodu
{
check=1;
prd=magaz->prod;
addr= magaz->adr;
tempn= magaz->nomer;
magaz->nomer = next->nomer;
magaz->adr = next->adr;
magaz->prod= next->prod;
next->nomer = tempn;
next->adr = addr;
next->prod= prd;
}
if (strcmp(next->adr.gorod,magaz->adr.gorod)==0&&strcmp(next->adr.ulica,magaz->adr.ulica)<0) // proverka po ulice
{
check=1;
prd=magaz->prod;
addr= magaz->adr;
tempn= magaz->nomer;
magaz->nomer = next->nomer;
magaz->adr = next->adr;
magaz->prod= next->prod;
next->nomer = tempn;
next->adr = addr;
next->prod= prd;
}
if (strcmp(next->adr.gorod,magaz->adr.gorod)==0&&strcmp(next->adr.ulica,magaz->adr.ulica)==0&&(magaz->adr.nomd>next->adr.nomd)) // proverka po nomeru doma
{
prd=magaz->prod;
addr= magaz->adr;
tempn= magaz->nomer;
magaz->nomer = next->nomer;
magaz->adr = next->adr;
magaz->prod= next->prod;
next->nomer = tempn;
next->adr = addr;
next->prod= prd;
}
magaz=magaz->next;
}
/*sortirovka pusirkom*/
magaz=last;
}
puts("Sort dannie:\n");
while (magaz!=NULL)
{ // puts("-");
printf("%3d, %s, %s, %d\n",magaz->nomer,magaz->adr.gorod,magaz->adr.ulica,magaz->adr.nomd);
/* prd=magaz->prod;
while (prd!=NULL)
{
printf("%s: %.2f$. %dd\n",prd->name,prd->price,prd->srok);
prd=prd->next;
}
*/
magaz=magaz->next;
}
puts("------\n");
return last;
}
int output(magazin *last, char *namevih) //zapis' v fail
{
FILE *f;
magazin *magaz;
products *prd,*tp;
if ((f=fopen(namevih,"a"))==NULL)
{
puts("Error mag2");
getch();
return 0;
}
fputs("-----------------------------------------------\n",f);
puts("Itog:\n");
while (last!=NULL)
{
printf("%3d, %s, %s, %d\n",last->nomer,last->adr.gorod,last->adr.ulica,last->adr.nomd);
fprintf(f,"%d %s %s %d\n",last->nomer,last->adr.gorod,last->adr.ulica,last->adr.nomd);
prd=last->prod;
while (prd!=NULL)
{
fprintf(f,"%s %.2f %d\n",prd->name,prd->price,prd->srok);
// printf("%s: %.2f$. %dd\n",prd->name,prd->price,prd->srok);
tp=prd;
prd=prd->next;
free(tp);
}
magaz=last;
last=last->next;
free(magaz); // osvobozhdenie pamayati pod otrabotannimi dannimi
fputs("* 0 0\n",f);
}
puts("\nInfo zapisana v file!");
fclose(f);
return 0;
}
void main()
{
magazin *magaz,*temp,*last,*prev; // magazini, dobavlenie, nachalo steka, dlya dobaleniya (predidushi element)
products *prd,*lastprd; // producti, nachalo steka
char namevh[20];
char namevih[20];
puts("File s informaciei: ");scanf("%s",namevh);
puts("File dlay vivoda: ");scanf("%s",namevih);
magaz=input(namevh,namevih); // chtenie iz faila
if (magaz!=NULL)
{
magaz=sort(magaz); // sortirovka dannih
char ch=NULL; // soglasie na prodolzhenie vvoda, proverka dobavili ili net zapis'
last=magaz;
printf("VVodim novie magazini (D)? ");
while (ch=='\0'||ch=='\n')
ch=getchar();
while (ch=='d'||ch=='D')
{
// clrscr();
/*Vvod Novogo magazina*/
temp=(magazin*)malloc(sizeof(magazin));
puts("VVod magazina: \nVvedite address:\nNomer, Gorod, Ulica, nomer Doma\n");
printf("Nomer magazina: ");scanf("%d",&(temp->nomer));
printf("Gorod: ");scanf("%s",temp->adr.gorod);
printf("Ulica: ");scanf("%s",temp->adr.ulica);
printf("Nomer doma: ");scanf("%d",&(temp->adr.nomd));
ch=NULL;
printf("VVodim producti (D)? ");
while (ch=='\0'||ch=='\n')
ch=getchar();
lastprd=NULL;
while (ch=='d'||ch=='D') //vvodim producti
{
prd=(products*)malloc(sizeof(products));
printf("Naimenovanie: ");scanf("%s",prd->name);
printf("Price: ");scanf("%f",&(prd->price));
printf("Srok (d): ");scanf("%d",&(prd->srok));
prd->next=lastprd;
lastprd=prd;
//--------------------------------
printf("Continue vvodit' produkti (D)? ");
ch=NULL;
while (ch=='\0'||ch=='\n')
ch=getchar();
}
temp->prod=lastprd;
/*Vvod Novogo magazina*/
//--------------------------------
prev=NULL;
ch='8';
while (magaz!=NULL&&ch!=NULL) // dobavlenie v stek // ili dobavim, ili doidem do konca
{
if (strcmp(temp->adr.gorod,magaz->adr.gorod)==0&&strcmp(temp->adr.ulica,magaz->adr.ulica)==0)
{
if (prev!=NULL)
prev->next = temp;
temp->next=magaz;
ch=NULL;
}
prev=magaz;
magaz=magaz->next;
}
if (ch!=NULL)
{
prev->next=temp;
temp->next=NULL;
}
magaz=last;
printf("Continue vvodit' magazini(D)? ");
ch=NULL;
while (ch=='\0'||ch=='\n')
ch=getchar();
}
output(magaz,namevih); // vivod dannih v fail
}
getch();
}
//---------------------------------------------------------------------------
Дата добавления: 2015-07-08; просмотров: 83 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Структура программы. | | | Введение |