ch27-9.c

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

C
58
字号
#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,data;
   for(i=0;i<MAXNUM;i++)
      if(xx[i]>0) 
      {
         totNum++;
         data=xx[i]>>1;
         if(data%2==0)
         {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("请输入 %d 个四位的整数:\n", 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 + -
显示快捷键?