C++ 接收数字和数字并检查数字在数字内的次数的算法

C++ 接收数字和数字并检查数字在数字内的次数的算法,c++,function,C++,Function,编辑:现在可以工作了 我刚开始上大学的第一年,我们有一些家庭作业要做 我们被要求创建一个函数,从用户那里接收一个数字和一个数字,并打印数字在该数字中的次数。 此外,我们应该使用我的算法顶部提到的常量整数,如果数字为负数,则返回负数,如果数字为负数或大于9,则返回非法数字。 我想知道我的程序出了什么问题?不知什么原因它不起作用。有人能解释一下原因吗 非常感谢 这是: #include <iostream> using namespace std; const i

编辑:现在可以工作了

我刚开始上大学的第一年,我们有一些家庭作业要做

我们被要求创建一个函数,从用户那里接收一个数字和一个数字,并打印数字在该数字中的次数。 此外,我们应该使用我的算法顶部提到的常量整数,如果数字为负数,则返回负数,如果数字为负数或大于9,则返回非法数字。 我想知道我的程序出了什么问题?不知什么原因它不起作用。有人能解释一下原因吗

非常感谢

这是:

    #include <iostream>
    using namespace std;
    const int NEGATIVE_INPUT_NUMBER = -1;
    const int ILLEGAL_DIGIT = -2;
    int digitInNumber(int number, int digit);

    void main()
    {
        int digit,number;
        cout<< "Please insert a number and then a digit" << endl;
        cin>> number >>digit;
        cout << digitInNumber(number, digit) << endl;
    }
    int digitInNumber(int number,int digit)
    {
        int lastDig;
        int counter = 0;
        if (number < 0)
            return NEGATIVE_INPUT_NUMBER;
        if (digit > 9 || digit < 0)
            return ILLEGAL_DIGIT;
        while (number>0)
        {
            lastDig = number % 10;
            if (lastDig == digit)
                counter++;
            number = number / 10;
        }
        return counter;
#包括
使用名称空间std;
常量int负输入数=-1;
常量int非法_位=-2;
整数位数(整数,整数位数);
void main()
{
整数,数字;
密码>>数字;
cout(0)
{
lastDig=编号%10;
if(lastDig==位)
计数器++;
数字=数字/10;
}
返回计数器;

在function
digitInNumber
中,您可以在数字中注册模运算的结果,同时应将其保存在其他变量中:

int digitInNumber(int number,int digit)
{
    int counter = 0;
    int lastDigit = 0;
    if (number < 0)
        return NEGATIVE_INPUT_NUMBER;
    if (digit > 9 || digit < 1)
        return ILLEGAL_DIGIT;
    while (number>0)
    {
        lastDigit = number % 10;
        if (lastDigit == digit)
            counter++;
        number = number / 10;
    }
    return counter;
}
int-digitInNumber(int-number,int-digit)
{
int计数器=0;
整数位数=0;
如果(数字<0)
返回负的\u输入\u编号;
如果(数字>9 | |数字<1)
返回非法数字;
而(数量>0)
{
lastDigit=数字%10;
如果(最后位数==位数)
计数器++;
数字=数字/10;
}
返回计数器;
}

在循环的第一次迭代后,您的版本“忘记”了number的值。

此语句是错误的:

number = number % 10;
它会将
number
的值更改为您不想要的值。请引入一个临时局部变量
tempint
,然后改用它:

tempint = number % 10;
if (tempint == digit) {
    counter++;
}
或者只需删除该语句并将
if
条件修改为:

if (number % 10 == digit) {
    counter++;
}
另一种方法是将整数转换为字符串,将整数转换为字符,并使用以下函数:

int digitInNumber(int no, int dig) {
    std::cout << "Enter number and a digit: ";
    std::cin >> no >> dig;
    std::string number = std::to_string(no);
    char digit = dig + '0';
    int num_items1 = std::count(number.begin(), number.end(), digit);
    return num_items1;
}
int-digitInNumber(int-no,int-dig){
标准::cout>no>>挖掘;
std::string number=std::to_string(no);
字符位数=dig+'0';
int num_items1=std::count(number.begin()、number.end()、digit);
返回num_items1;
}

对于初学者,根据C++标准,没有参数的函数应该被声明为

int main()
如果数字为负数或大于9,则为非法数字

此if语句中的条件不满足要求

    if (digit > 9 || digit < 1)
        return ILLEGAL_DIGIT;
每个变量都应该在使用它的范围内声明

int lastDig;
不用于函数的外部范围
digitInNumber

不要使用幻数作为10。使用命名常量

函数可以按以下方式查看

int digitInNumber(int number, int digit)
{
    const int Base = 10;

    if (number < 0)
    {
        return NEGATIVE_INPUT_NUMBER;
    }

    if (digit >= Base || digit < 0)
    {
        return ILLEGAL_DIGIT;
    }

    int counter = 0;

    do
    {
        counter += number % Base == digit;
    } while (number /= Base);

    return counter;
}
注意这个电话

std::cout << digitInNumber(0xff, 0xf, 16) << std::endl;
std::cout或,在普通c中++

#include <iostream>
#include <string>
#include <algorithm>

// returns the number of occurrences of 'c' in 's'
size_t count_occurrences(const std::string& s, char c)
{
    size_t n = 0;
    for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
    {
        if (*it == c)
            ++n;
    }
    return n;
}

bool validate_number(const std::string& s)
{
    return s.length() && s.find_first_not_of("0123456789") == std::string::npos;
}

bool validate_digit(const std::string& s)
{
    return s.length() == 1 && isdigit(s[0]);
}

int  main()
{
    std::string number, s;
    char digit = 0;

    for(;;)
    {
        std::cout<< "Please insert a number: ";
        std::cin >> number;

        if (validate_number(number))
            break;

        std::cout<< "Invalid entry! Please try again.\n";
    }

    for (;;)
    {
        std::cout<< "Please insert a digit to look for: ";
        std::cin >> s;

        if (validate_digit(s))
        {
            digit = s[0];
            break;
        }

        std::cout<< "Invalid entry! Please try again.\n";
    }

    std::cout << "Count: " << count_occurrences(number, digit) << endl;
    return 0;
}
#包括
#包括
#包括
//返回“s”中出现“c”的次数
大小\u t计数\u出现次数(常量std::string&s,char c)
{
尺寸n=0;
对于(std::string::const_迭代器it=s.begin();it!=s.end();++it)
{
如果(*it==c)
++n;
}
返回n;
}
bool验证_编号(常量标准::字符串和s)
{
返回s.length()&s.find_first_not_of(“0123456789”)==std::string::npos;
}
布尔验证_位(常量标准::字符串和s)
{
返回s.length()==1&&isdigit(s[0]);
}
int main()
{
std::字符串编号,s;
字符位数=0;
对于(;;)
{
std::cout>number;
如果(验证_编号(编号))
打破
std::cout s;
如果(验证数字)
{
数字=s[0];
打破
}

这不是问题,但main应该返回
int
,而不是
void
“它不工作”这不是一个问题描述。为什么不呢?你会遇到什么错误?另外,你似乎在粘贴时遗漏了一些代码;你应该将其删除,以确保它们都在那里。除了宏之外,使用
all_CAPS
不被认为是好的风格,你无论如何都应该避免。谢谢。我有一些错误,我不知道它们的意思我刚刚写到它不起作用。下次我会提到它们。我编辑了代码,它现在起作用了。但我现在只缺少一个。我希望它在插入后立即返回一个错误,例如-150,而不是等待第二个输入(数字)…如果您有一个新问题,发布一个新问题,或者至少更新此问题,使其反映您的问题,我该如何做。评论不是为了提供基本信息。谢谢!我现在更改了它。我仍然收到“type int unexpected”错误嗯,那可能是因为你必须在函数的开头声明lastDigitaccordingly@Corali那是因为在
main
中有
int-digit,int-number;
。第二个
int
不应该在那里。啊哈!没错。我更改了它,现在没有错误。只有一件事:当我键入e时例如-150(数字)它也在等待数字。我怎么才能“打破”它?我的意思是,在它得到错误的输入后,它应该返回一个错误。你在使用@Corali代码的逻辑,但是为什么0应该是非法数字呢?因为这是大学一年级的练习,我想你不允许使用标准库原则上,我同意,但是,当C++用于教学C++时,并不总是这样。无论如何,OP都不会通过复制粘贴你的解决方案而学习,而你却不回答这个问题(OPS代码有什么不对?)
1
1
2
2
std::cout << digitInNumber(0xff, 0xf, 16) << std::endl;
#include <iostream>
#include <string>
#include <algorithm>

// returns the number of occurrences of 'c' in 's'
size_t count_occurrences(const std::string& s, char c)
{
    size_t n = 0;
    for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
    {
        if (*it == c)
            ++n;
    }
    return n;
}

bool validate_number(const std::string& s)
{
    return s.length() && s.find_first_not_of("0123456789") == std::string::npos;
}

bool validate_digit(const std::string& s)
{
    return s.length() == 1 && isdigit(s[0]);
}

int  main()
{
    std::string number, s;
    char digit = 0;

    for(;;)
    {
        std::cout<< "Please insert a number: ";
        std::cin >> number;

        if (validate_number(number))
            break;

        std::cout<< "Invalid entry! Please try again.\n";
    }

    for (;;)
    {
        std::cout<< "Please insert a digit to look for: ";
        std::cin >> s;

        if (validate_digit(s))
        {
            digit = s[0];
            break;
        }

        std::cout<< "Invalid entry! Please try again.\n";
    }

    std::cout << "Count: " << count_occurrences(number, digit) << endl;
    return 0;
}