#include<stdio.h> int main() { int a[8], *p, *n1, *n2;
n1 = n2 = 0; //初始化正数和负数个数的指针

printf("请输入8个整数:\n");
for(p=a;p<a+8;p++) //指针遍历数组
{
    scanf("%d",p);
    if(*p>0) //统计正数个数
        (*n1)++;
    else if(*p<0) //统计负数个数
        (*n2)++;
    else //将0视为正数
        (*n1)++;
}
printf("正数:%d\n负数:%d\n", *n1, *n2);

for(p=a;p<a+8;p++) //再次遍历数组输出结果
{
    if(*p<0)
        *p = 0;
    printf("%d ",*p);
}
return 0;

}

c语言利用指针统计数组中正数、负数的个数利用指针实现如下功能:输入8个数将期中所有负数置0输出结果数组。【样例输入】4 -1 -3 5 0 8 -2 -6【样例输出】正数:3负数:4#includestdiohint main int a8 p n1 n2; printf正数:dn负数:d n1 n2; return 0;

原文地址: https://www.cveoy.top/t/topic/fnzm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录