C++ 如何强制C++;仅读取以结尾的输入#
我试图在谷歌上找到答案,但什么也没找到, 因此,我们的任务是创建一个程序,该程序将读取输入(一个数字字符串),最后只带#(“1234#”),并计算奇数和偶数 我通过以下方式与counter合作:C++ 如何强制C++;仅读取以结尾的输入#,c++,C++,我试图在谷歌上找到答案,但什么也没找到, 因此,我们的任务是创建一个程序,该程序将读取输入(一个数字字符串),最后只带#(“1234#”),并计算奇数和偶数 我通过以下方式与counter合作: char ch; int odd_number, even_number; odd_number=even_number = 0; printf("To leave print % : "); while((ch=getchar()) !='%') { switch
char ch;
int odd_number,
even_number;
odd_number=even_number = 0;
printf("To leave print % : ");
while((ch=getchar()) !='%')
{
switch (ch)
{
case '1':
odd_number++;
break;
case '2':
even_number++;
break;
但现在我不知道如何让它只读取带有#结尾的数字字符串您需要做的是:
- 在循环中执行所有操作,直到用户输入包含“%”
- 通知用户要做什么
- 从用户处读取完整的输入行
- 如果这是一个非空字符串,请选中,该字符串由digity组成,并在末尾有一个#
- 如果该行不适合请求的格式,则忽略它
- 计算赔率。even数是字符串的其余部分(不含#)
- 向用户显示结果
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
int main() {
// Stay in program, until input string conatains a %
for (std::string input{}; std::none_of(input.begin(), input.end(), [](const char c) {return c == '%'; });) {
// Give instruction to the user
std::cout << "Enter a number followed by a # (a '%' in the string will end the program):\n-> ";
// Get user input and check, if it is a valid numberdfgh
if (std::getline(std::cin, input) and
not input.empty() and
input.back() == '#' and
std::all_of(input.begin(), std::prev(input.end()), isdigit)) {
// Count odd numbers
size_t countOfOdds = std::count_if(input.begin(), std::prev(input.end()), [](const char c) {return c & 1; });
// Calculate even numbers. That is the rest (without the #)
size_t countOfEvens = input.size() - countOfOdds - 1;
//Show result 0 user
std::cout << "\nNumber contains " << countOfOdds << " odds and " << countOfEvens << " evens\n\n";
}
}
return 0;
}
#包括
#包括
#包括
#包括
int main(){
//保持在程序中,直到输入字符串conatains a%
对于(std::string input{};std::none_of(input.begin(),input.end(),[](const char c){return c='%';});){
//指导用户
你需要做的是:
- 在循环中执行所有操作,直到用户输入包含“%”
- 通知用户要做什么
- 从用户处读取完整的输入行
- 如果这是一个非空字符串,请选中,该字符串由digity组成,并在末尾有一个#
- 如果该行不适合请求的格式,则忽略它
- 计算赔率。偶数是字符串的其余部分(不包括#)
- 向用户显示结果
现在,在代码中翻译这个设计思想
您必须开发自己的代码
无论如何,我会提供一些你可以尝试理解的示例代码,并确保老师不相信这是你的解决方案
请参阅:
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
int main() {
// Stay in program, until input string conatains a %
for (std::string input{}; std::none_of(input.begin(), input.end(), [](const char c) {return c == '%'; });) {
// Give instruction to the user
std::cout << "Enter a number followed by a # (a '%' in the string will end the program):\n-> ";
// Get user input and check, if it is a valid numberdfgh
if (std::getline(std::cin, input) and
not input.empty() and
input.back() == '#' and
std::all_of(input.begin(), std::prev(input.end()), isdigit)) {
// Count odd numbers
size_t countOfOdds = std::count_if(input.begin(), std::prev(input.end()), [](const char c) {return c & 1; });
// Calculate even numbers. That is the rest (without the #)
size_t countOfEvens = input.size() - countOfOdds - 1;
//Show result 0 user
std::cout << "\nNumber contains " << countOfOdds << " odds and " << countOfEvens << " evens\n\n";
}
}
return 0;
}
#包括
#包括
#包括
#包括
int main(){
//保持在程序中,直到输入字符串conatains a%
对于(std::string input{};std::none_of(input.begin(),input.end(),[](const char c){return c='%';});){
//指导用户
STD::如果你想这样做,那么首先使用C++而不是C语言。然后在循环中用“代码> STD::GETLION/CODEL”读取完整的行。如果它包含一个“Y”,则使用该行,否则丢弃/忽略它。如果它包含一个“Y”,则可以计算奇数和偶数字符。取整个字符串并找到其大小。现在检查最后的字符。字符(大小1)如果你想这样做,那么先在循环中使用,然后使用C++,而不是C语言。然后在循环中用“代码> STD::GETLION/CODE”读取完整的行。如果它包含一个“Y”,则使用该行,否则丢弃/忽略它。如果包含一个“Y”,则可以计算奇数和偶数字符。现在检查最后一个字符(大小1)是否为#。在循环中执行此操作