C++ 从用户输入中计算 +VE、-VE 和零
c++ counting +ve, -ve and zeros from user's input
我想创建一个程序,要求用户输入数字。然后软件会计算出有多少个正数、负数和零。
注意,我必须使用调用函数。
这是我到目前为止所做的,但仍然不能正常工作
#include <iostream>
using namespace std;
void counting(double, double);
int main()
{
float count, P=0, i;
cout<< "how many numbers you wanna enter?" << endl;
cin >> count;
do {
cout << "enter your number: " << endl;
cin >> i;
counting(count, i);
P++;
}
while (P<count);
return 0;
}
void counting(double count, double i)
{
int positive_Count = 0;
int negative_Count = 0;
int zero_Count = 0;
int u;
for (u=0; u <= count; u++)
{
if (i > 0)
{
positive_Count++;
}
else if (i < 0)
{
negative_Count++;
}
else if (i == 0)
{
zero_Count++;
}
}
cout << "Count of positive elements = " << positive_Count << endl
<< "Count of negative elements = " << negative_Count << endl
<< "Count of zero elements = " << zero_Count << endl;
}
这是一个有严重缺陷的代码。建议-
-
先读取所有数字,然后将其复制到
dynamically allocated array
-
将该数组传递给
counting function
-
在
loop
中的函数中对该数组进行迭代,并找到正数、负数的计数&零