ch27-20.c

来自「C语言程序设计上机指导与练习 冶金工业出版社 刘怀亮」· C语言 代码 · 共 56 行

C
56
字号
#include <stdio.h>
#include <conio.h>
#define MAXNUM 20

int xx[MAXNUM];
int totNum=0; /*共有多少个正整数*/
int totCnt=0; /*符合条件的正整数的个数*/
double totPjz=0.0;   /*平均值*/

void ReadDat(void);
void writeDat(void);

void CalValue(void)
{
   int i,qw,bw,sw,gw;
   for(i=0;i<MAXNUM;i++)
      if(xx[i]>0) 
      {  
         totNum++;
         qw=xx[i]/1000;   
         bw=xx[i]/100%10;
         sw=xx[i]%100/10; 
         gw=xx[i]%10;
         if((qw+bw+sw+gw)%2)
         { totCnt++; totPjz+=xx[i];}
      }
      if(totCnt==0) totPjz=0;
      else totPjz/=totCnt;
   }
void main()
{
   int i;
   clrscr();
   for(i=0;i<MAXNUM;i++)xx[i]=0;
   ReadDat();
   CalValue();
   printf("共有正整数=%d个\n",totNum);
   printf("符合条件的正整数的个数=%d个\n",totCnt);
   printf("平均值=%.2f\n",totPjz);
   writeDat();
}
void ReadDat(void)
{
   int i=0;
   printf("\n请输入%d个四位的整数",MAXNUM);
   for(i=0;i< MAXNUM;i++)
      scanf("%d,",&xx[i++]);
}
void writeDat(void)
{
   FILE *fp;
   fp=fopen("OUT.DAT","w");
   fprintf(fp,"%d\n%d\n%6.2f\n",totNum,totCnt,totPjz);
   fclose(fp);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?