244 lines
5.7 KiB
C++
244 lines
5.7 KiB
C++
#include<stdio.h>
|
||
#include<string.h>
|
||
#include<stdlib.h>
|
||
typedef struct peoples{
|
||
int year;
|
||
int month;
|
||
int day;
|
||
char gender[10];
|
||
int births;
|
||
} Peo;
|
||
typedef struct people{
|
||
int year;
|
||
int F;
|
||
int M;
|
||
int ALL;
|
||
int cha;
|
||
} Peop;
|
||
typedef struct years{
|
||
int year;
|
||
int births;
|
||
} Year;
|
||
void clean(int i,Peo *peo){
|
||
if(peo[i].year%4!=0&&peo[i].year%100==0||peo[i].year%400!=0){
|
||
if(peo[i].month==2&&peo[i].day==29){
|
||
peo[i].births=0;} }
|
||
if((peo[i].month==2&&peo[i].day==30)||(peo[i].month==2&&peo[i].day==31)){
|
||
peo[i].births=0;}
|
||
if(peo[i].month==4&&peo[i].day==31){
|
||
peo[i].births=0;}
|
||
if(peo[i].month==6&&peo[i].day==31){
|
||
peo[i].births=0;}
|
||
if(peo[i].month==9&&peo[i].day==31){
|
||
peo[i].births=0;}
|
||
if(peo[i].month==11&&peo[i].day==31){
|
||
peo[i].births=0;}
|
||
}
|
||
void init(Peo *peo,char *b,int i){
|
||
char s[5]=",";
|
||
peo[i].year=atoi(strtok(b,s));
|
||
peo[i].month=atoi(strtok(NULL, s));
|
||
peo[i].day=atoi(strtok(NULL, s));
|
||
strcpy(peo[i].gender,strtok(NULL,s));
|
||
peo[i].births=atoi(strtok(NULL,s));
|
||
}
|
||
Peo *read(char *t){
|
||
FILE *fp;
|
||
fp=fopen(t,"r");
|
||
char buff[500];
|
||
int q=15547;
|
||
Peo *peo;
|
||
peo=(Peo*)malloc(sizeof(Peo)*(q));
|
||
fgets(buff,500,fp);
|
||
int i;
|
||
for(i=0;i<15547;i++){
|
||
fgets(buff,500,fp);
|
||
init(peo,buff,i);
|
||
}
|
||
fclose(fp);
|
||
return peo;
|
||
}
|
||
void display(Peo *peo){
|
||
int i;
|
||
printf("年份\t月份\t天份\t性别\t出生人数\n");
|
||
for(i=0;i<15547;i++){
|
||
printf("%d\t%d\t%d\t%s\t%d\n",peo[i].year,peo[i].month,peo[i].day,peo[i].gender,peo[i].births);
|
||
}
|
||
}
|
||
void form(Peo *peo,Peop *peop,int a[]){
|
||
int i,e,j,k;
|
||
a[0]=0;
|
||
printf("year\tF\t\tM\t\tALL\t\tF-M\n");
|
||
peop=(Peop*)malloc(sizeof(Peop)*(5));
|
||
peop[0].year=1960;
|
||
for( i=0;i<15547;i++){
|
||
if(peo[i].year==1969){
|
||
a[1]=i+1;
|
||
peop[1].year=peo[i+1].year;
|
||
}
|
||
if(peo[i].year==1979){
|
||
a[2]=i+1;
|
||
peop[2].year=peo[i+1].year;
|
||
}
|
||
if(peo[i].year==1989){
|
||
a[3]=i+1;
|
||
peop[3].year=peo[i+1].year;
|
||
}
|
||
if(peo[i].year==1999){
|
||
a[4]=i+1;
|
||
peop[4].year=peo[i+1].year;
|
||
}
|
||
if(peo[i].year==2008){
|
||
a[5]=i+1;
|
||
peop[5].year=peo[i+1].year;
|
||
}
|
||
}
|
||
int sumC=0,sum=0,sumf=0,summ=0;
|
||
for( i=0;i<5;i++){
|
||
for( j=a[i];j<a[i+1];j++){
|
||
clean(j,peo);
|
||
sum=sum+peo[j].births;
|
||
peop[i].ALL=sum;
|
||
if(strcmp(peo[j].gender,"F")==0){
|
||
sumf=sumf+peo[j].births;
|
||
peop[i].F=sumf;
|
||
}
|
||
if(strcmp(peo[j].gender,"M")==0){
|
||
summ=summ+peo[j].births;
|
||
peop[i].M=summ;
|
||
}
|
||
sumC=sumf-summ;
|
||
peop[i].cha=sumC;
|
||
}
|
||
}
|
||
int all=0,all01=0,all02=0,allcha=0;
|
||
for(int e=0;e<5;e++){
|
||
all=all+peop[e].ALL;
|
||
all01=all01+peop[e].F;
|
||
all02=all02+peop[e].M;
|
||
allcha=allcha+peop[e].cha;
|
||
}
|
||
for(k=0;k<5;k++){
|
||
printf("%d\t%10d\t%10d\t%10d\t%10d\n",peop[k].year,peop[k].F,peop[k].M,peop[k].ALL,peop[k].cha);
|
||
}
|
||
printf("all\t%10d\t%10d\t%10d\t%10d\n",all01,all02,all,allcha);
|
||
}
|
||
void month(Peo *peo){
|
||
double z,sum0;
|
||
for(int h=0;h<15547;h++){
|
||
clean(h,peo);
|
||
sum0=sum0+peo[h].births;
|
||
}
|
||
z=sum0/480;
|
||
printf("平均每月出生%0.2f人\n",z);
|
||
}
|
||
void day(Peo *peo,int d[]){
|
||
int count=0,i,h;
|
||
double sum0,z;
|
||
for(i=0;i<40;i++){
|
||
d[i]=i+1969;
|
||
if(d[i]%4==0&&d[i]%100!=0||d[i]%400==0){
|
||
count=count+1;
|
||
}
|
||
}
|
||
for(h=0;h<15547;h++){
|
||
clean(h,peo);
|
||
sum0=sum0+peo[h].births;
|
||
}
|
||
z=sum0/(((2008-1969)*365)+count);
|
||
printf("平均每天出生%0.2f人\n",z);
|
||
}
|
||
int cmp(const void *a,const void *b){
|
||
return (*(Year*)a).births>(*(Year*)b).births?1:-1;
|
||
}
|
||
void sort(Peo *peo,int d[],Year *year,int c[]){
|
||
int j,h,q,u,e,w;
|
||
year=(Year*)malloc(sizeof(Year)*(40));
|
||
for( j=0;j<40;j++){
|
||
for( h=0;h<15547;h++){
|
||
if(peo[h].year==d[j]){
|
||
c[j]=h;
|
||
}
|
||
}
|
||
}
|
||
int sum=0,sum1=0;
|
||
for(j=0;j<c[0];j++){
|
||
clean(j,peo);
|
||
sum=sum+peo[j].births;
|
||
year[0].births=sum;
|
||
year[0].year=d[0];
|
||
}
|
||
for(u=1;u<40;u++){
|
||
sum1=0;
|
||
for( e=c[u-1];e<c[u];e++){
|
||
clean(e,peo);
|
||
sum1=sum1+peo[e].births;
|
||
year[u].births=sum1;
|
||
year[u].year=d[u];
|
||
}
|
||
}
|
||
qsort(year,40,sizeof(Year),cmp);
|
||
// printf("年份\t出生人数\n");
|
||
// for( w=0;w<40;w++){
|
||
// printf("%d\t%10d\n",year[w].year,year[w].births);
|
||
// }
|
||
int p;
|
||
p=((2008-1969+1)/2);
|
||
printf("排序后出生人数最多的是%d年,人数为%d\n排序后出生人数最少的是%d年,人数为%d\n",year[39].year,year[39].births,year[0].year,year[0].births);
|
||
printf("排序后的中位数对应年份是%d和%d\n",year[p].year,year[p+1].year);
|
||
}
|
||
typedef struct last{
|
||
int month;
|
||
int day;
|
||
int births;
|
||
} Last;
|
||
void qilin(Peo *peo,int c[],Last *las){
|
||
las=(Last*)malloc(sizeof(Last)*(800));
|
||
int j,w,a,b;
|
||
int t[800]={0};
|
||
for(j=c[0]+1;j<c[1]+1;j=j+2){
|
||
if(peo[j].day==99||peo[j+1].day==99){
|
||
peo[j].births=0;
|
||
peo[j+1].births=0;
|
||
}
|
||
a=c[0];
|
||
b=c[0]-1;
|
||
las[j-a].births=peo[j].births+peo[j+1].births;
|
||
las[j-a].day=peo[j].day;
|
||
las[j-a].month=peo[j].month;
|
||
las[j-b].births=0;
|
||
las[j-b].day=0;
|
||
las[j-b].month=0;
|
||
}
|
||
int max=0,month01,day01,min=9999999,month02,day02;
|
||
for(int u=0;u<766;u++){
|
||
if(las[u].births>max){
|
||
max=las[u].births;
|
||
month01=las[u].month;
|
||
day01=las[u].day;
|
||
}
|
||
if(las[u].births!=0&&las[u].births<min){
|
||
min=las[u].births;
|
||
month02=las[u].month;
|
||
day02=las[u].day;
|
||
}
|
||
}
|
||
printf("1970年出生人数最多的是%d月%d日,出生人数为%d\n1970年出生人数最少的是%d月%d日,出生人数为%d\n",month01,day01,max,month02,day02,min);
|
||
}
|
||
int main(){
|
||
int a[10]={0};
|
||
int d[100]={0};
|
||
int c[100]={0};
|
||
Peo *peo=read("births.csv");
|
||
Peop *peop;
|
||
Year *year;
|
||
Last *las;
|
||
display(peo);
|
||
form(peo,peop,a);
|
||
month(peo);
|
||
day(peo,d);
|
||
sort(peo,d,year,c);
|
||
qilin(peo,c,las);
|
||
return 0;
|
||
}
|