2006年9月全國等級考試三級c語言上機題庫(八十一)

字號:

★☆題目81(無憂id 32 整數(shù)各位數(shù)運算題)
    已知數(shù)據(jù)文件in.dat中存有300個四位數(shù),并已調用讀函數(shù)ReadDat()把這些數(shù)存入數(shù)組a中,請編制一函數(shù)jsValue(),其功能是:求出千位數(shù)上的數(shù)加百位數(shù)等于十位數(shù)上的數(shù)加個位數(shù)上的數(shù)的個數(shù)cnt,再求出所有滿足此條件的四位數(shù)平均值pjz1,以及不滿足此條件的四位數(shù)平均值pjz2,最后調用寫函數(shù)writeDat()把結果輸出到out.dat文件。
    例如:7153,7+1=5+3,則該數(shù)滿足條件計算平均值pjz1,且個數(shù)cnt=cnt+1。8129,8+1<>2+9,則該數(shù)不滿足條件計算平均值pjz2。
    部分源程序存在文件prog1.c中。
    程序中已定義數(shù)組:a[300],已定義變量:cnt,pjz1,pjz2。
    請勿改動主函數(shù)main()、讀函數(shù)ReadDat()和寫函數(shù)writeDat()的內容。
    #include
    int a[300],cnt=0;
    double pjz1=0.0,pjz2=0.0;
    jsValue()
    {int i,thou,hun,ten,data,n=0;
    for(i=0;i<300;i++)
    {thou=a[i]/1000; hun=a[i]%1000/100;
    ten=a[i]%100/10; data=a[i]%10;
    if(thou+hun==ten+data) {cnt++;pjz1+=a[i];}
    else {n++;pjz2+=a[i];}
    }
    if(cnt==0) pjz1=0;
    else pjz1/=cnt;
    if(n==0) pjz2=0;
    else pjz2/=n;
    }
    main()
    {
    int i;
    readDat();
    jsValue();
    writeDat();
    printf("cnt=%d\n滿足條件的平均值pzj1=%7.2f\n不滿足條件的平均值pzj2=%7.2f\n",cnt,pjz1,pjz2);
    }
    readDat()
    {
    FILE *fp;
    int i;
    fp=fopen("in.dat","r");
    for(i=0;i<300;i++)fscanf(fp,"%d,",&a[i]);
    fclose(fp);
    }
    writeDat()
    {
    FILE *fp;
    int i;
    fp=fopen("out.dat","w");
    fprintf(fp,"%d\n%7.2f\n%7.2f\n",cnt,pjz1,pjz2);
    fclose(fp);
    }