C语言中任意输入一组数,并判断正负数的个数?

没有规定输入的个数,但也可以有上限,最好能分别运用scanf、getchar、gets函数来实现(即3个程序),因为我对这几个函数分不清怎么运用,谢谢啊,我想了几天了,都快晕了%>_<%

#include<stdio.h>
#include<stdlib.h>
main()//scanf的
{
int plus=0,negative=0;
double s;
printf("请输入一组数(以0结束输入):\n");
while(1)
{
scanf("%lf",&s);
if(s>0)
plus++;
else if(s<0)
negative++;
else
break;
}
printf("共有正数%d个\n",plus);
printf("共有负数%d个\n",negative);
system("pause");
}
main()//gets的
{
int plus=0,negative=0;
char s[100];
printf("请输入一组数(以0结束输入):\n");
while(1)
{
gets(s);
if(s[0]=='0')
break;
else if(s[0]=='-')
negative++;
else
plus++;
}
printf("共有正数%d个\n",plus);
printf("共有负数%d个\n",negative);
system("pause");
}
getchar???这个只能输入一个字符!!比如:23。已经是两个字符了!!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-17
#include <iostream>
using namespace std;
void main()
{
int i = 0, j = 0;
double N = 0;
while(cin >> N) //输入非数字结束循环
{
if(N > 0) i++;
else if(N < 0) j++;
}
cout << "正数个数:" << i << endl
<< "负数个数:" << j << endl;
system("pause");
}
第2个回答  2013-04-17
我只写主要部分,前面的要求的头文件等等的你自己加。
int a[20]; //设置输入20个数
int i,j,n;
for(n=0;n<20;n++)
{
scanf("%d",&a[n]);
}
for(n=0;n<20;n++)
{
if(a[n]<0) i++;
else j++;
}
printf("%d,%d\n",i,j);

说说三者区别,getchar()只能获取一个字符,所以不可能实现你要求的输入一组数,比如你要输入12和-1,但是实际上获取的是四个字符'1','2','-','1';

gets()获取键盘输入的一个字符串,比如
char a[];
gets(a);
键盘输入“asdef"时,这个字符串就被赋给数组a;
第3个回答  2013-04-17
#include <stdio.h>
void main()
{
double a=0;
int plusCount = 0; //正数个数
int negativeCount =0;//负数个数
while(true)
{
scanf("%lf",&a);

if(a>0)
{
plusCount++;
}
else if(a<0)
{
negativeCount++;
}
else
{
break;//a = 0结束输入
}
}
printf("result:\n");
printf("plusCount=%d ;",plusCount);
printf(" negativeCount=%d",negativeCount);
}
第4个回答  2020-01-11
#include<stdio.h>
#include<stdlib.h>
main()//scanf的
{
int
plus=0,negative=0;
double
s;
printf("请输入一组数(以0结束输入):\n");
while(1)
{
scanf("%lf",&s);
if(s>0)
plus++;
else
if(s<0)
negative++;
else
break;
}
printf("共有正数%d个\n",plus);
printf("共有负数%d个\n",negative);
system("pause");
}
main()//gets的
{
int
plus=0,negative=0;
char
s[100];
printf("请输入一组数(以0结束输入):\n");
while(1)
{
gets(s);
if(s[0]=='0')
break;
else
if(s[0]=='-')
negative++;
else
plus++;
}
printf("共有正数%d个\n",plus);
printf("共有负数%d个\n",negative);
system("pause");
}
getchar???这个只能输入一个字符!!比如:23。已经是两个字符了!!

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网