shpi0 » 21 дек 2006, 14:39
Писал в институте такую вещь, щас найду...
Вот
Код: Выделить всё
#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;
}
Писал в институте такую вещь, щас найду...
Вот[code]#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;
}
[/code]