C language statistics sorted

IN.DAT known data file in there 200 four-digit number, and has called the read function readDat () array into a number of these in, please prepare a function jsVal (), which is: in order from the array a remove a four-digit, five digit number if the continuous and smaller than the digit after the number is an even number (hereinafter the four digit number less than five, no statistics), then the statistics satisfy this condition cnt four-digit number and put them in ascending order into array b, after calling the write function writeDat () the output array b cnt and qualified four digits to OUT.DAT file.
#include <stdio.h>

#define MAX 200

int a[MAX], b[MAX], cnt = 0 ;

void jsVal()

{int i,j,flag;

for(i=0;i<MAX-5;i++)

{for(j=i+1;j<=i+5;j++)

if(a[i]<a[j]&&a[i]%2==0) flag=1;

else { flag=0; break;}

if(flag==1) b[cnt++]=a[i];

}

for(i=0;i<cnt-1;i++)

for(j=i+1;j<cnt;j++)

if(b[i]>b[j]) {flag=b[i];b[i]=b[j];b[j]=flag;}

}

void readDat()

{

int i ;

FILE *fp ;

fp = fopen(“in.dat”, “r”) ;

for(i = 0 ; i < MAX ; i++) fscanf(fp, “%d”, &a[i]) ;

fclose(fp) ;

}

void main()

{

int i ;

readDat() ;

jsVal ();

printf ( "satisfying the condition number =% d \ n", cnt);

for(i = 0 ; i < cnt ; i++)

printf("%d “, b[i]) ; printf(”\n") ;

writeDat() ;

}

writeDat()

{

FILE *fp ;

int i ;

fp = fopen(“out.dat”, “w”) ;

fprintf(fp, “%d\n”, cnt) ;

for(i = 0 ; i < cnt ; i++) fprintf(fp, “%d\n”, b[i]);

fclose(fp) ;

}

Published 239 original articles · won praise 3 · Views 3163

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105164220