创建一个程序,该程序将一串数字作为输入,并输出偶数和奇数的总和

Create a program that takes as input a string of digits and outputs the sum of the even and the sum of the odd digits

本文关键字:程序 输出 输入 一串 一个 数字 创建      更新时间:2023-10-16

创建一个程序,将一串数字和输出作为输入 偶数的总和和奇数的总和。 该程序在 C++

注意:建议您使用"for"循环来迭代 手指刺痛。提示:使用模运算符确定偶数或 奇数。

将"char"转换为"int"的聪明方法如下链接:

char a = '4';
int ia = a - '0'; 

上面的代码利用了字符在 ASCII 表将其转换为整数。

以下是我到目前为止的代码:

int main() {
  int sumOdd = 0;  // For accumulating odd numbers, init to 0
  int sumEven = 0; // For accumulating even numbers, init to 0
  int digits;      // Sum from 1 to this upperbound
  // Prompt user for an upperbound
  cout << "Please enter a string comprised ONLY of digits:" << endl;
  cout << endl;
  cin >> digits;
  // Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound
  int number = 1;
  while (number <= digits) {
    if (number % 2 == 0) { // Even number
      sumEven += number;   // Add number into sumEven
    } else {               // Odd number
      sumOdd += number;    // Add number into sumOdd
    }
    ++number; // increment number by 1
  }
  // Print the results
  cout << "The string of digits "" << digits << """
       << " contained "
       << "characters." << endl;
  cout << "The sum of the even digits is: " << sumEven << endl;
  cout << "The sum of the odd digits is: " << sumOdd << endl;
  return 0;
}
这是我的输出

与我需要的输出相比

输入:

1234567890

输出:

Please enter a string comprised ONLY of digits:
The string of digits "1234567890" contained characters.
The sum of the even digits is: -380436870
The sum of the odd digits is: -997720815

预期产出

Please enter a string comprised ONLY of digits:
The string of digits "1234567890" contained 10 characters.
The sum of the even digits is: 20
The sum of the odd digits is: 25

总的来说,我在计算输入和为我的偶数和奇数获得正确的公式时遇到了麻烦。非常感谢任何帮助!

一个简单的方法是将数字保持为文本形式:

std::string number_as_text;
cout << "Enter number: ";
cin >> number_as_text;

这允许您检查每个数字的偶数或奇数:

const size_t length = number_as_text.length();
for (size_t i = 0; i < length; ++i)
{
  char digit_character = number_as_text[i];
  if (isdigit(digit_character))
  {
    if (digit_character % 2 == 0)
    {
       // digit is even
    }
    else
    {
       // digit is odd
    }
  }
}

如果您不喜欢isdigit(),可以替换为:

if ((digit_character >= '0') && (digit_character < '9'))

需要注意的是,数字的文本表示与数字的内部数字表示之间存在差异。

编辑 1:% char类型
char数据类型为整数。 余数运算符 % 适用于整数类型。 因此,您可以在char类型上使用%

注意:此操作假定'0'的字符映射是偶数,其他数字的值是连续的。

读取字符串字段中的输入,而不是整数,如 -

std::string digits; 

然后,只需运行一个有效的循环来找出偶数位和奇数位的总和 -

 for(int i=0; i<digits.length(); i++){
            int digit = digits[i]-'0';
            if(digit % 2 == 0)
                sumEven+=digit;
            else
                sumOdd+=digit;
        }

下面是一段正在运行的代码。

// Example program
#include <iostream>
#include <string>
using namespace std;
int main() {
  int sumOdd = 0;  // For accumulating odd numbers, init to 0
  int sumEven = 0; // For accumulating even numbers, init to 0
  std::string digits;      // Sum from 1 to this upperbound
  // Prompt user for an upperbound
  cout << "Please enter a string comprised ONLY of digits:" << endl;
  cout << endl;
  cin >> digits;
  // Use a while-loop to repeatedly add 1, 2, 3,..., to the upperbound
  int number = 1;
  for(int i=0; i<digits.length(); i++){
        int digit = digits[i]-'0';
        if(digit % 2 == 0)
            sumEven+=digit;
        else
            sumOdd+=digit;
    }
  // Print the results
  cout << "The string of digits "" << digits << """
       << " contained "
       << "characters." << endl;
  cout << "The sum of the even digits is: " << sumEven << endl;
  cout << "The sum of the odd digits is: " << sumOdd << endl;
  return 0;
}

你弄错了,你应该做 1+3+5+7+9 的总和和 2+4+6+8+0。相反,您正在做的是小于1234567890的所有偶数之和和小于1234567890的奇数之和。
也许考虑而不是通过cin >> digits逐个字符从输入中读取字符串中的所有数字。检查此代码,它从输入中读取字符并将其吐回。

#include<iostream>
using namespace std;

int main() {
  cout << "Please enter a string comprised ONLY of digits:" << endl;
  cout << endl;
  char one;
  while ( true ) {
      cin >> one;
      // here will come your part with deciding even / odd and counting instead of pritnting out.
      cout << " " << one;
      if ( cin.peek() == 'n' ) { break; }
  }
  return 0;
}

现在轮到你把它放在一起了。

使用 std::getline 将所有数字读入std::string

std::cout << "Enter string of numbers: " << std::endl;
if (std::string numbers; std::getline(std::cin, numbers)) {
    /* ... */
}

请注意上面即将推出的 C++1z 标准中使用的条件 if

然后,您可以利用std::pair来整齐地累积赔率和偶数,如下所示:

std::pair odd_even{0, 0};
for (auto c : numbers) {
    (c % 2 ? std::get<0>(odd_even) : std::get<1>(odd_even)) += c - '0';
}

现场示例

这个逻辑上应该工作

for(int i=0; i<digits.lenght; i++){
        if(digits[i] % 2 == 0)
            sumEven+=digits[i];
        else
            sumOdd+=digits[i];
    }