C++ 创建一个以一串数字作为输入并输出偶数和奇数之和的程序
创建一个以一串数字和输出作为输入的程序 偶数和奇数之和。这个节目正在播出 C++ 注意:建议使用“for”循环在 手指刺痛。提示:使用模数运算符确定偶数或偶数 奇数 将“char”转换为“int”的巧妙方法如下:C++ 创建一个以一串数字作为输入并输出偶数和奇数之和的程序,c++,string,input,C++,String,Input,创建一个以一串数字和输出作为输入的程序 偶数和奇数之和。这个节目正在播出 C++ 注意:建议使用“for”循环在 手指刺痛。提示:使用模数运算符确定偶数或偶数 奇数 将“char”转换为“int”的巧妙方法如下: char a = '4'; int ia = a - '0'; 上面的代码利用了角色在 ASCII表将其转换为整数 以下是我目前掌握的代码: int main() { int sumOdd = 0; // For accumulating odd numbers, init
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”包含字符。
偶数数字之和为:-380436870
奇数数字之和为:-997720815
预期产量
请输入仅由数字组成的字符串:
数字串“1234567890”包含10个字符。
偶数数字之和为:20
奇数位数之和为:25
总的来说,我在计算输入和获得正确的偶数和奇数公式时遇到了困难。非常感谢您的帮助,非常感谢 一个简单的方法是将数字保持为文本形式:
std::string number_as_text;
cout << "Enter number: ";
cin >> number_as_text;
一个重要的注意事项是,数字的文本表示与数字的内部数字表示之间存在差异
在char
类型上编辑1:%
char
数据类型是一个整数。余数运算符%
,用于整数类型。因此,您可以在char
类型上使用%
注意:此操作假定
'0'
的字符映射为偶数整数,其他数字的值是连续的 从逻辑上讲,这个应该可以工作
for(int i=0; i<digits.lenght; i++){
if(digits[i] % 2 == 0)
sumEven+=digits[i];
else
sumOdd+=digits[i];
}
for(int i=0;i读取字符串字段中的输入,而不是作为-
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;
}
对于(int i=0;i你搞错了,你应该做1+3+5+7+9和2+4+6+8+0的和。你要做的是所有小于1234567890的偶数和小于1234567890的奇数之和。
可以考虑用“代码> CIN >数字< /代码>来代替字符串中的所有数字。
检查此代码,它从输入中读取字符并将其吐回
#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;
int main(){
不能使用std::getline
将所有数字读入std::string
:
std::cout << "Enter string of numbers: " << std::endl;
if (std::string numbers; std::getline(std::cin, numbers)) {
/* ... */
}
顺便说一句,您不需要将字符数字转换为数字数字来测试偶数或奇数。您应该使用std::string
来输入数字字符串。解决此类问题的正确工具是调试器。在询问堆栈溢出之前,您应该逐行检查代码。有关更多帮助,请阅读。至少,您可以你的问题应该包括一个重现你的问题的例子,以及你在调试器中所做的观察。while(数字)我投票将这个问题作为离题题题来结束,因为代码中充满了误解和逻辑错误。解决所有这些问题可能会填补一个空白(初学者)C++编写的。你应该尝试编译你提交的代码作为答案。这个例子不会编译。另外,关于非功能代码的答案预计包含解释解决方案如何解决问题或原始问题中的错误。@ StAdGuURR,因为字符是整数。ainder运算符“%”,适用于整数,也可以应用于字符。我更喜欢for(char-digit:number\u as_-text){isdigit…}
我不明白它看起来有点干净。在您的示例中没有奇偶校验。另外,为什么要限制(硬编码)到5?请看我的答案。这显然是学校作业,所以我不会通过发布解决方案来破坏它。:-)尝试运行示例,但不要将数字从字符转换为整数。将余数运算符直接应用于字符。那么我将如何将它们相加?或者我遗漏了什么?
// 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;
}
#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::cout << "Enter string of numbers: " << std::endl;
if (std::string numbers; std::getline(std::cin, numbers)) {
/* ... */
}
std::pair odd_even{0, 0};
for (auto c : numbers) {
(c % 2 ? std::get<0>(odd_even) : std::get<1>(odd_even)) += c - '0';
}