C++ 反转数字C++;

C++ 反转数字C++;,c++,C++,在这里寻找一些关于我做错了什么的建议。我的一切都应该是好的,保持不变。我的问题在于我的反向功能。它在cout语句“the number is”之前打印反向的数字,而不是在应该的位置下方。我花了一段时间试图修复,但没有找到解决方案 #include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> using namespace std; const in

在这里寻找一些关于我做错了什么的建议。我的一切都应该是好的,保持不变。我的问题在于我的反向功能。它在cout语句“the number is”之前打印反向的数字,而不是在应该的位置下方。我花了一段时间试图修复,但没有找到解决方案

 #include <iostream>
 #include <iomanip>
 #include <cstdlib>
 #include <ctime>
 using namespace std;

 const int NUM_VALS = 10;   //the maximum number of values to use

 int reverse(int num);
 bool isPrime(int num);

 int main()
  {
  int number,  //Holds the random number that is manipulated and tested
    loopCnt; //Controls the loop

  //set the seed value for the random number generator
  //Note: a value of 1 will generate the same sequence of "random" numbers     every
  //      time the program is executed
  srand(1);

  //Generate 10 random numbers to be manipulated and tested
  for( loopCnt = 1; loopCnt <= NUM_VALS; loopCnt++ )
       {
    //Get a random number
    number = rand();

   //Display the sum of adding up the digits in the random number, the reversed
   //random number, and whether or not the number is palindromic or a prime number

    cout << "The number is " << number << endl
         << "----------------------------------------" << endl
 //     << "Adding the digits result" << setw(16) << sumDigits( number ) << endl
        << "Reversing the digits result" << setw(13) << reverse(number) << endl
 //    << "Is the number a palindrome?" << setw(13) << (isPalindrome(number)? "Yes" : "No") << endl
 //     << "Is the number prime?" << setw(20) << (isPrime(number)? "Yes" : "No") << endl
        << endl << endl;
  }

  return 0;
}

int reverse(int num)
{
int quo, rem;
quo = num;  
while (quo != 0)
{
    rem = quo % 10;
    cout << rem;
    quo /= 10;
}

}

bool isPrime(int num)
{
int i;

if (num % 2 == 0)
    return false;
for (i = 3; i*i <= num; i+=2)
    {
        if (num % i == 0)
            return false;
    }
return true;
  }
#包括
#包括
#包括
#包括
使用名称空间std;
const int NUM_VALS=10//要使用的最大值数
int反转(int num);
bool-isPrime(int-num);
int main()
{
int number,//保存被操纵和测试的随机数
loopCnt;//控制循环
//设置随机数生成器的种子值
//注:值为1时,将每隔一天生成相同的“随机”数序列
//执行程序的时间
srand(1);
//生成10个随机数进行操作和测试

对于(loopCnt=1;loopCnt您需要让
reverse
函数将数字返回为reversed,因为返回值在
main
中使用

通过将“反转”值乘以10,然后将余数相加,可以生成反转数:

int reverse(int num)
{
  int reversed = 0;
  int quo, rem;
  quo = num;  
  while (quo != 0)
  {
    rem = quo % 10;
    reversed = reversed * 10 + rem;
    quo /= 10;
  }
  return reversed;
}

您需要让
reverse
函数将数字返回为反向,因为返回值在
main
中使用

通过将“反转”值乘以10,然后将余数相加,可以生成反转数:

int reverse(int num)
{
  int reversed = 0;
  int quo, rem;
  quo = num;  
  while (quo != 0)
  {
    rem = quo % 10;
    reversed = reversed * 10 + rem;
    quo /= 10;
  }
  return reversed;
}

您还可以使用此方法通过获取字符串输入来反转数字,然后将其反转并转换为int

#include <iostream>
#include<string>

using namespace std;

int reverse_num(string a)
{
        string s;
        for(int i=a.length()-1;i>=0;i--)
        {
                s+=a[i];
        }
        int n;
        n=stoi(s);
        return n;
}
int main()
{
    
    string a;
    cin>> a;
    cout<<reverse_num(a);        
    return 0;
}
#包括
#包括
使用名称空间std;
int reverse_num(字符串a)
{
字符串s;
对于(int i=a.length()-1;i>=0;i--)
{
s+=a[i];
}
int n;
n=stoi(s);
返回n;
}
int main()
{
字符串a;
cin>>a;

cout您还可以使用此方法通过获取字符串输入来反转数字,然后将其反转并转换为int

#include <iostream>
#include<string>

using namespace std;

int reverse_num(string a)
{
        string s;
        for(int i=a.length()-1;i>=0;i--)
        {
                s+=a[i];
        }
        int n;
        n=stoi(s);
        return n;
}
int main()
{
    
    string a;
    cin>> a;
    cout<<reverse_num(a);        
    return 0;
}
#包括
#包括
使用名称空间std;
int reverse_num(字符串a)
{
字符串s;
对于(int i=a.length()-1;i>=0;i--)
{
s+=a[i];
}
int n;
n=stoi(s);
返回n;
}
int main()
{
字符串a;
cin>>a;

coutYour
reverse
函数说它返回一个
int
,但我没有看到任何
return
语句。要么返回一个值,要么将返回类型更改为
void
。你的
reverse
函数说它返回一个
int
,但我没有看到任何
return
语句。要么返回一个值或者将返回类型更改为
void
。谢谢!我有点猜会是这样的。我很接近,我有一个“反向”val,但遗漏了*10部分。再次,非常感谢!谢谢!我有点猜会是这样的。我很接近,我有一个“反向”valval有一次漏掉了*10部分。再次感谢你!