Здравствуйте ....
я вот тут решил разобрать в С....
ну и вот и дошел до структур и есть такое задани создать базу данных..... чтобы в ней мона было смореть изменять данные ну и писать их файлы ..... и я застрял......=(
ну вот у мну просьба.... как я понимаю ет не очень сложная прога....
ну вот напишите плиз текст этой проги а я по нему потом разберусь надеюсь=)
заранее спасибо....
P.S. если мона чтоб в борланде С работала а то у мну пока такой тока.....
SoS
Модераторы: Хыиуду, MOTOCoder, Medved, dr.Jekill
Писал в институте такую вещь, щас найду...
Вот
Вот
Код: Выделить всё
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define input_file "input.txt"
#define output_file "output.txt"
#define N 300
int b[N],b1[N],nn=0;
struct shop
{
char name[20];
char street[20];
char cc[10];
char item[20];
} shop_list[N];
int read()
{
int i,j,s=0;
char *c=new char[500],*c1;
FILE *rf=fopen(input_file,"r");
if (rf==NULL)
{
printf("\nCant open the input file (%s)!",input_file);
getche();
exit(0);
}
while (!feof(rf))
{
fgets(c,500,rf);
c1=strtok(c,";");
for (i=0;i<strlen(c1);i++)
shop_list[s].name[i]=c1[i];
c1=strtok(NULL,";");
for (i=0;i<strlen(c1);i++)
shop_list[s].street[i]=c1[i];
c1=strtok(NULL,";");
for (i=0;i<strlen(c1);i++)
shop_list[s].cc[i]=c1[i];
c1=strtok(NULL,";");
for (i=0;i<strlen(c1)-1;i++)
shop_list[s].item[i]=c1[i];
s++;
}
delete[] c;
fclose(rf);
return s;
}
int srav(char s1[],char s2[]) // Sravninaet stroki s1 & s2.
{ // s1 > s2 => return 1;
int i=0,n,r=-1; // r=-1 => s1=s2
n=strlen(s1);
if (strlen(s2)<n)
n=strlen(s2);
if (s1[i] > s2[i])
return 1;
if (s1[i] < s2[i])
return 0;
while (i<=n)
{
i++;
if (s1[i] == s2[i])
continue;
if (s1[i] > s2[i])
return 1;
if (s1[i] < s2[i])
return 0;
}
return r;
}
void indexmas(int w,int j1)
{
int i,a,j;
if (!j1)
{
for (i=0;i<nn;i++)
b[i]=i;
for (i=0;i<nn;i++)
for (j=nn-1;j>i;j--)
switch (w)
{
case 1:
if (srav(shop_list[b[j-1]].name,shop_list[b[j]].name) == 1)
{
a=b[j];
b[j]=b[j-1];
b[j-1]=a;
}
break;
case 2:
if (srav(shop_list[b[j-1]].street,shop_list[b[j]].street) == 1)
{
a=b[j];
b[j]=b[j-1];
b[j-1]=a;
}
break;
case 3:
if (srav(shop_list[b[j-1]].cc,shop_list[b[j]].cc) == 1)
{
a=b[j];
b[j]=b[j-1];
b[j-1]=a;
}
break;
case 4:
if (srav(shop_list[b[j-1]].item,shop_list[b[j]].item) == 1)
{
a=b[j];
b[j]=b[j-1];
b[j-1]=a;
}
break;
}
}
else
{
for (i=0;i<j1;i++)
for (j=j1-1;j>i;j--)
switch (w)
{
case 1:
if (srav(shop_list[b1[j-1]].name,shop_list[b1[j]].name) == 1)
{
a=b1[j];
b1[j]=b1[j-1];
b1[j-1]=a;
}
break;
case 2:
if (srav(shop_list[b1[j-1]].street,shop_list[b1[j]].street) == 1)
{
a=b1[j];
b1[j]=b1[j-1];
b1[j-1]=a;
}
break;
case 3:
if (srav(shop_list[b1[j-1]].cc,shop_list[b1[j]].cc) == 1)
{
a=b1[j];
b1[j]=b1[j-1];
b1[j-1]=a;
}
break;
case 4:
if (srav(shop_list[b1[j-1]].item,shop_list[b1[j]].item) == 1)
{
a=b1[j];
b1[j]=b1[j-1];
b1[j-1]=a;
}
break;
}
}
}
int write(char l,int search,char *s1,int j,char l1,int k)
{
int s=0,i;
FILE *wf;
if (k)
wf=fopen(output_file,"w+");
else
wf=fopen(input_file,"w+");
if (wf==NULL)
{
printf("\nCant open the output file (%s)!",output_file);
getche();
exit(0);
}
if (k)
{
fputs(" NAME | ADRESS | CRED.CARD | ITEM |\n",wf);
fputs("--------------|-------------------|-----------|--------------|\n",wf);
if (!search)
for (i=0;i<nn;i++)
fprintf(wf,"%13s | %17s | %9s | %12s |\n",shop_list[b[i]].name,shop_list[b[i]].street,shop_list[b[i]].cc,shop_list[b[i]].item);
else
{
for (i=0;i<j;i++)
fprintf(wf,"%13s | %17s | %9s | %12s |\n",shop_list[b1[i]].name,shop_list[b1[i]].street,shop_list[b1[i]].cc,shop_list[b1[i]].item);
fputs("\n\nZadany parametry poiska:\n",wf);
}
if (search)
switch(l1)
{
case '2':
fprintf(wf,"Iskat' v \"STREET\" znachenie \"%s\"\n\n",s1);
break;
case '3':
fprintf(wf,"Iskat' v \"CRED.CARD\" znachenie \"%s\"\n\n",s1);
break;
case '4':
fprintf(wf,"Iskat' v \"ITEM\" znachenie \"%s\"\n\n",s1);
break;
default:
fprintf(wf,"Iskat' v \"NAME\" znachenie \"%s\"\n\n",s1);
break;
}
switch(l)
{
case '2':
fputs("\nOtsortirovano po adresu",wf);
break;
case '3':
fputs("\nOtsortirovano po nomeru cred.card",wf);
break;
case '4':
fputs("\nOtsortirovano po pokupke",wf);
break;
default:
fputs("\nOtsortirovano po imeni",wf);
break;
}
}
else
{
for (i=0;i<nn;i++)
if (i!=nn-1)
fprintf(wf,"%s;%s;%s;%s\n",shop_list[i].name,shop_list[i].street,shop_list[i].cc,shop_list[i].item);
else
fprintf(wf,"%s;%s;%s;%s",shop_list[i].name,shop_list[i].street,shop_list[i].cc,shop_list[i].item);
}
fclose (wf);
return s;
}
Даже самое длинное путешествие начинается с первого шага.
Продолжение...
Пример входного файла input.txtПример выходного файла output.txt
Надеюсь поможет
Код: Выделить всё
main()
{
char l,yn,s,*s1=new char[20];
int i,k,j=0,z=1,i1;
nn=read();
do
{
clrscr();
printf("\nSchitano %d strok iz faila %s\n\n",nn,input_file);
printf("Po kakomu polu sortirovat'? (1-4)\n");
printf("1 - name; 2 - adress; 3 - cred.card; 4 - item. ");
l=getche();
switch (l)
{
case '2':
indexmas(2,0);
break;
case '3':
indexmas(3,0);
break;
case '4':
indexmas(4,0);
break;
default:
indexmas(1,0);
}
printf("\nBudem vypolnyat' poisk? (y/n) ");
yn=getch();
while (1)
{
if (yn=='y' || yn=='Y' || yn=='n' || yn=='N')
break;
yn=getch();
}
if (yn=='n' || yn=='N')
{
printf("\n\n ID | NAME | ADRESS | CRED.CARD | ITEM |\n");
printf("----|---------------|-------------------|-----------|---------------|\n");
for (i=0;i<nn;i++)
printf(" %2d |%14s | %17s | %9s | %13s |\n",b[i],shop_list[b[i]].name,shop_list[b[i]].street,shop_list[b[i]].cc,shop_list[b[i]].item);
write(l,0,0,0,0,1);
}
else
{
printf("\nV kakom pole iskat'? (1-4)\n");
printf("1 - name; 2 - adress; 3 - cred.card; 4 - item. ");
s=getche();
printf("\nVvedite stroku dlya poiska: ");
for (i=0;i<nn;i++)
{
s1[i]=getche();
if (s1[i] == 13)
break;
}
s1[i]=0;
switch(s)
{
case '2':
for (i=0;i<nn;i++)
{
if (strstr(shop_list[i].street,s1))
{
b1[j]=i;
j++;
}
}
break;
case '3':
for (i=0;i<nn;i++)
{
if (strstr(shop_list[i].cc,s1))
{
b1[j]=i;
j++;
}
}
break;
case '4':
for (i=0;i<nn;i++)
{
if (strstr(shop_list[i].item,s1))
{
b1[j]=i;
j++;
}
}
break;
default:
for (i=0;i<nn;i++)
{
if (strstr(shop_list[i].name,s1))
{
b1[j]=i;
j++;
}
}
break;
}
switch (l)
{
case '2':
indexmas(2,j);
break;
case '3':
indexmas(3,j);
break;
case '4':
indexmas(4,j);
break;
default:
indexmas(1,j);
}
printf("\n\n ID | NAME | ADRESS | CRED.CARD | ITEM |\n");
printf("----|---------------|-------------------|-----------|---------------|\n");
for (i=0;i<j;i++)
printf(" %2d |%14s | %17s | %9s | %13s |\n",b1[i],shop_list[b1[i]].name,shop_list[b1[i]].street,shop_list[b1[i]].cc,shop_list[b1[i]].item);
write(l,1,s1,j,s,1);
}
printf("\nOtsortirovannyi spisok zapisan v %s",output_file);
printf("\n\nVyberite operaciu:\n1-dobavit' zapis', 2-udalit' zapis', 3-na4at' zanovo, Esc-exit.");
s=getch();
switch(s)
{
case '1':
nn++;
printf("\nNAME: ");
scanf("%s",&shop_list[nn-1].name);
printf("STREET: ");
scanf("%s",&shop_list[nn-1].street);
printf("CARD: ");
scanf("%s",&shop_list[nn-1].cc);
printf("ITEM: ");
scanf("%s",&shop_list[nn-1].item);
if (yn=='n' || yn=='N')
{
write(l,0,0,0,0,0);
}
else
{
write(l,1,s1,j,s,0);
}
break;
case '2':
printf("\nVvedite ID stroki dlya udaleniya: ");
scanf("%d",&i);
shop_list[i].name[0] = 0;
shop_list[i].street[0] = 0;
shop_list[i].cc[0] = 0;
shop_list[i].item[0] = 0;
nn--;
for (i1=i;i1<nn;i1++)
{
for (i=0;i<20;i++)
{
shop_list[i1].name[i] = shop_list[i1+1].name[i];
shop_list[i1].street[i] = shop_list[i1+1].street[i];
shop_list[i1].item[i] = shop_list[i1+1].item[i];
}
for (i=0;i<10;i++)
shop_list[i1].cc[i] = shop_list[i1+1].cc[i];
}
break;
case '3':
z=1;
break;
default:
z=0;
break;
}
} while (z);
if (yn=='n' || yn=='N')
{
write(l,0,0,0,0,0);
}
else
{
write(l,1,s1,j,s,0);
}
delete[] s1;
return 1;
}
Код: Выделить всё
Vova;Respubliki;123456;TV
Vasya;Main;654321;Stereo
Код: Выделить всё
NAME | ADRESS | CRED.CARD | ITEM |
--------------|-------------------|-----------|--------------|
Vasya | Main | 654321 | Stereo |
Vova | Respubliki | 123456 | TV |
Otsortirovano po imeni
Даже самое длинное путешествие начинается с первого шага.