簡體   English   中英

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 ,找到正,負數和零的計數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM