简体   繁体   English

C++,计算正负数并计算数字的平均值)编写一个程序,读取未指定数量的整数

[英]C++ ,Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers

"Count positive and negative numbers and compute the average of numbers Write a program that reads an unspecified number of integers , determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a double. where did i go wrong?? “计算正数和负数并计算数字的平均值编写一个程序,读取未指定数量的整数,确定读取了多少正负值,并计算输入值的总数和平均值(不计算零)。你的程序以输入 0 结束。将平均值显示为双精度。我哪里出错了??

#include <iostream>
using namespace std;
int main()

{
            int num= 0;
            int sum=0;
            int pos=0;
            int neg=0;
            double ave=0;
            cout << "Enter an integer, the input ends if it is 0: " ;
            cin >> num ;
            if (num%10==10) {
                while (num!=0) {
                    num/=10;
                    if (num%10>0) {
                        pos++;
                    }
            else if (num%10<0) {
                neg++;
            }
            sum+=num;
                }
            ave= (double)sum/(pos+neg);
            }
            cout <<"The number of positives are " << pos <<endl;
            cout <<"The number of negatives are " << neg <<endl;
            cout <<"The total is " << sum << endl;
            cout <<"The average is "<< ave << endl;
            return 0;

 }

You can use char[] to read input.您可以使用char[]来读取输入。 I have modified your program as follows;我已将您的程序修改如下;

int main()
{
    int sum=0;
    int pos=0;
    int neg=0;
    double ave=0;
    char arr[100] = {'\0',};

    std::cout << "Enter an integer, the input ends if it is 0: " ;  
    gets(arr);

    int index = 0;
    char ch[1];
    bool negativeNumber = false;

    while(true)
    {               
        ch[0] = arr[index++];
        if(ch[0] == ' ') // Check space and continue;
        {
            continue;
        }
        else if(ch[0] == '0' || ch[0] == '\0') // check for 0 or NULL and break;
        {
            break;
        }
        if(ch[0] == '-') // Set flag if "-ve"
        {           
            negativeNumber = true;
            continue;
        }

        int digit = atoi(ch);
        if(negativeNumber)
        {
            digit *= -1;
            negativeNumber = false;
        }
        if(digit > 0)
        {
            pos++;
        }
        else if(digit < 0)
        {
            neg++;
        }
        sum += digit;
    }
    ave= (double)sum/(pos+neg);

    cout <<"The number of positives are " << pos <<endl;
    cout <<"The number of negatives are " << neg <<endl;
    cout <<"The total is " << sum << endl;
    cout <<"The sverage is "<< ave << endl;

    return 0;
 }

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM