创建一个程序,该程序将一串数字作为输入,并输出偶数和奇数的总和
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
创建一个程序,将一串数字和输出作为输入 偶数的总和和奇数的总和。 该程序在 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];
}
- 如何从Windows应用程序输出到标准?
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 而循环:简单的除法程序输出零,不明白为什么
- Qt creator 4.11,在应用程序输出面板中创建一个链接
- 使用复制构造函数的程序输出错误
- 为什么Qt Creator的应用程序输出不能从spdlog记录器打印
- 为什么在 Direct X 11 中没有我的程序输出?
- 我怎样才能阻止我的程序输出时间,它使我的程序难以阅读
- 程序输出奇怪的字符
- 程序输出"nan"
- 为什么我的C++程序输出不同的数字来将米转换为英尺?
- 3D 纹理大小会影响程序输出,而不会引发错误
- C++,程序输出无效输入,但继续通过循环
- 如何连接C++和Excel(从C++程序输出到Excel电子表格)
- 为什么我的C 程序输出乱码代码
- QT小部件应用程序输出到控制台
- 混淆了具有虚拟继承的程序输出
- 为什么我的C++程序输出这个
- 为什么我的程序输出不断变化?EAX寄存器不断更改值