Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
struct f1rec
{
char scnmbr[13];
char psnmbr[11];
char FIO[61];
};
struct f2rec
{
char scnmbr[13];
char bnknmbr[31];
char depnm[21];
char prc[11];
char voldep[21];
};
struct f3rec
{
char depnm[21];
char FIO[61];
char voldep[21];
};
int vscnf(char *fmt, ...)
{
va_list argptr;
int cnt;
va_start(argptr, fmt);
cnt = vscanf(fmt, argptr);
va_end(argptr);
return(cnt);
}
int isint(char *str)
{
int ans=1;
for (int i=0;i<strlen(str);i++)
if (!isdigit(str[i])) ans=0;
return(ans);
}
int isstr(char *str)
{
int ans=1;
int t;
for (int i=0;i<strlen(str);i++)
{
t=str[i];
if (!isalpha(str[i])&&(t!=44)) ans=0;
}
return(ans);
}
int isfloat(char *str)
{
int ans=1;
int t;
for (int i=0;i<strlen(str);i++)
{
t=str[i];
if (!isdigit(str[i])&&(t!=46)) ans=0;
}
return(ans);
}
int N1=0,N2=0;
f1rec *f1correct(char *flnm)
{
f1rec *ret;
ret=NULL;
FILE *f1;
char *str;
str=flnm;
if ((f1 = fopen(str, "r"))== NULL)
{
printf("Fatal error can't open f1 file.\n");
getch();
fclose(f1);
return(ret);
}
int stop=0;
int c=0;
while ((stop!=1)&&(!feof(f1)))
{
strcpy(str,"");
fscanf(f1,"%s",str);
if (!isint(str))
{
printf("Warning. Error in %i record in f1\n",c+1);
printf("Social number:%s\n",str);
}
if (strlen(str)!=12)
{
stop=1;
printf("Fatal error in %i record in f1\n",c);
printf("Social number:%s\n",str);
getch();
fclose(f1);
return(ret);
}
strcpy(str,"");
fscanf(f1,"%s",str);
if (!isint(str))
{
printf("Warning. Error in %i record in f1\n",c+1);
printf("Passport number:%s\n",str);
}
if (strlen(str)!=10)
{
stop=1;
printf("Fatal error in %i record in f1\n",c);
printf("Passport number:%s\n",str);
getch();
fclose(f1);
return(ret);
}
strcpy(str,"");
fscanf(f1,"%s",str);
if (!isstr(str))
{
printf("Warning. Error in %i record in f1\n",c+1);
printf("FIO:%s\n",str);
}
if ((strlen(str)>60)||(strlen(str)==0))
{
stop=1;
printf("Fatal error in %i record in f1\n",c);
printf("Social number:%s\n",str);
getch();
fclose(f1);
return(ret);
}
c++;
}
N1 = c;
rewind(f1);
f1rec *F1 = new f1rec[N1+1];
for(int i=0;i<=N1;i++)
{
fscanf(f1,"%s",F1[i].scnmbr);
fscanf(f1,"%s",F1[i].psnmbr);
fscanf(f1,"%s",F1[i].FIO);
}
for(i=0;i<=N1;i++)
for(int j=i+1;j<N1;j++)
{
if(strcmp(F1[i].scnmbr,F1[j].scnmbr)==0)
{
printf("Fatal error. Social Number in f1 in %i record is not unic\n",j+1);
printf("Social number %s\n",F1[i].scnmbr);
getch();
fclose(f1);
return(ret);
}
}
/*
for(int i=0;i<=N1;i++)
for(int j=i+1;j<N1;j++)
{
if(strcmp(F1[i].psnmbr,F1[j].psnmbr)==0)
printf("Warning. Pasport number in %i record in f1 is not unic\n",j);
}
*/
fclose(f1);
return(F1);
}
f2rec *f2correct(char *flnm)
{
f2rec *ret;
ret=NULL;
FILE *f2;
char *str;
str=flnm;
if ((f2=fopen(str, "r"))== NULL)
{
printf("Fatal error can't open f2 file.\n");
getch();
fclose(f2);
return(ret);
}
int stop=0;
int c=0;
while ((stop!=1)&&(!feof(f2)))
{
strcpy(str,"");
fscanf(f2,"%s",str);
if (!isint(str))
{
printf("Warning. Error in %i record in f2\n",c+1);
printf("Social number:%s\n",str);
}
if (strlen(str)!=12)
{
stop=1;
printf("Fatal error in %i record in f2\n",c);
printf("Social number:%s\n",str);
getch();
fclose(f2);
return(ret);
}
strcpy(str,"");
fscanf(f2,"%s",str);
if (!isint(str))
{
printf("Warning. Error in %i record in f2\n",c+1);
printf("Deposit number:%s\n",str);
}
if (strlen(str)!=30)
{
stop=1;
printf("Fatal error in %i record in f2\n",c);
printf("Deposit number:%s\n",str);
getch();
fclose(f2);
return(ret);
}
strcpy(str,"");
fscanf(f2,"%s",str);
if (!isstr(str))
{
printf("Warning. Error in %i record in f2\n",c+1);
printf("Deposit name:%s\n",str);
}
if ((strlen(str)>20)||(strlen(str)==0))
{
stop=1;
printf("Fatal error in %i record in f2\n",c);
printf("Deposit name:%s\n",str);
getch();
fclose(f2);
return(ret);
}
strcpy(str,"");
fscanf(f2,"%s",str);
if (!isfloat(str))
{
printf("Warning. Error in %i record in f2\n",c+1);
printf("Percents:%s\n",str);
}
if ((strlen(str)>10)||(strlen(str)==0))
{
stop=1;
printf("Fatal error in %i record in f2\n",c);
printf("Percents:%s\n",str);
getch();
fclose(f2);
return(ret);
}
strcpy(str,"");
fscanf(f2,"%s",str);
if (!isfloat(str))
{
printf("Warning. Error in %i record in f2\n",c+1);
printf("Volume:%s\n",str);
}
if ((strlen(str)>20)||(strlen(str)==0))
{
stop=1;
printf("Fatal error in %i record in f2\n",c);
printf("Percents:%s\n",str);
getch();
fclose(f2);
return(ret);
}
c++;
}
/* if (stop==2)
{
printf("Fatal error in %i record in f2\n",c);
getch();
fclose(f2);
return(ret);
}
*/ N2=c;
rewind(f2);
f2rec *F2 = new f2rec[N2+1];
for(int i=0;i<=N2;i++)
{
fscanf(f2,"%s",F2[i].scnmbr);
fscanf(f2,"%s",F2[i].bnknmbr);
fscanf(f2,"%s",F2[i].depnm);
fscanf(f2,"%s",F2[i].prc);
fscanf(f2,"%s",F2[i].voldep);
}
/*
for(int i=0;i<=N1;i++)
for(int j=i+1;j<N1;j++)
{
if(strcmp(F2[i].bnknmbr,F2[j].bnknmbr)==0)
printf("Warning. Deposit number in %i record in f2 is not unic\n",j);
}
*/
fclose(f2);
return(F2);
}
void main()
{
char *str="";
FILE *f3;
printf("Enter f1 file name\n");
vscnf("%s",str);
f1rec *F1=f1correct(str);
if(F1==NULL)
{
printf("Fatal Error.\n");
getch();
exit(0);
}
printf("Enter f2 file name\n");
vscnf("%s",str);
f2rec *F2=f2correct(str);
if(F2==NULL)
{
printf("Fatal Error.\n");
getch();
exit(0);
}
printf("Enter f3 output file name\n");
vscnf("%s",str);
if ((f3= fopen(str, "w"))== NULL)
{
printf("Fatal error can't create f3 file.\n");
getch();
exit(0);
}
//Обработка основной задачи
for (int i=0;i<N2;i++)
{
int wr=0;
for(int j=0;j<i;j++)
if (strcmp(F2[i].depnm,F2[j].depnm)==0) wr=1;
if (!wr)
{
float DepSum=0;
int smflg=0;
for(int j=i;j<N2;j++)
if (strcmp(F2[j].depnm,F2[i].depnm)==0)
{
int fnd=0;
for(int k=0;k<N1;k++)
if(strcmp(F2[j].scnmbr,F1[k].scnmbr)==0)
{
fprintf(f3,"\n%s %s %s",F2[j].depnm,F1[k].FIO,F2[j].voldep);
DepSum+=atof(F2[j].voldep);
smflg=1;
fnd=1;
}
else
{
if ((k==N1-1)&&(!fnd))
{
printf("\nWarning. Social number of record in f2 don't exist in f1\nSocial number is %s",F2[j].scnmbr );
fprintf(f3,"\n%s %s %s",F2[j].depnm,"Unknown",F2[j].voldep);
DepSum+=atof(F2[j].voldep);
smflg=1;
}
}
}
if (smflg)fprintf(f3,"\n%f",DepSum);
}
}
fclose(f3);
printf("\nDone..");
getch();
}
Соседние файлы в папке Cwork