开发者

print number in reverse

im simple asking if this is ok. i was asked to do the following. Write a program that will continuously ask the user for positive integers (until the user enters a negative integer at which the program will be terminated). Every time the user inputs a positive integer the program will print out this integer in reverse. Your program should have a function that accepts and integer and returns an integer in reverse. To convert the integer to its reverse, your program will call this function. at the end of each output i keep getting 0. please explain why. also if i use void main with the function i get garbage. please explain why. thanks in advance

this is my code....

#include<iostream>
#include<cstdlib>

using namespace std;

int reverseNum(int num){
    for(int j=num; j>0; j--)
        cout<<j<<" ";
        cout<<endl;
    return false;
}

int main(){
    double enternum = 0;

    do{
        cout<<"Enter a positive number > 0, to begin countdown ";
 开发者_JAVA技巧       cin >>enternum; 
        cout<<reverseNum(enternum);
        cout<<endl;
    }
    while(enternum>0);

    if(enternum<=0)
        cout<<"Invalid entry, good bye.";
    cout<<endl;

    return 0;
}


because of this: return false; - I'll leave it to you to figure out the rest..


The function is supposed to reverse the integer and then return the result. For example, if the input is 123, then the function returns 321.

Your function outputs a count-down and returns 0 (=false).

To reverse a number, you can a) convert it to string, reverse the string, convert it back to integer; b) do it on integers directly with mathematical division / multiplication / addition / modulo operations.


In C++, you don't use void main(). A 0 because when you return false, the result of type bool is implicitly converted to an int and gets printed at the line cout<<reverseNum(enternum);

Also, In this line, double enternum = 0; you want an integer int.


From your text I thought the program was working as intended, but from reading the code I suppose it just counts down from the number. Was this what you wanted? I'd have implemented it like this (and here the function returning an integer makes sense too):

int reverseNum(int num)
{
    int reverse = 0;
    [...] // Do the actual reversing
    return reverse;
}


Your program should have a function that accepts and integer and returns an integer in reverse

your reverseNum function should return the reversed integer, not false. and it shouldn't print the number as well, it's the caller which supposed to print it.

if one does:

i = reverseNum(1234);

then i should contain 4321 as an integer (NOT string).

the reason you keep getting 0 is because false is equivalent to 0 as an integer.


You should read the C++ FAQ in its entirety. You should especially read this. You should also learn how to debug your code. If you stepped through your code in a debugger then all the answers that you have been given here will be obvious.


For good fun, I attempted a generic implementation that supports any integral or floating point type supported by your compiler.

Be warned, there are a number of issues:

  • reversing a floating point number is not well defined semantically (how to position the decimal separator? How do we handle exponential notation?)
  • floating point types are frequently inexact (at least common IEEE formats are) and hence scaling the input will introduce artificial fractional digits. I have not taken much effort to do proper rounding, so some numbers will reverse into strange things (e.g. 123.0 could reverse into 992.1 instead of 321.0 (untested for this input, try some yourself))
  • the implementation is laughably template-happy. Think of it as the instructional part of this playful answer.

Oh, uncomment the DEBUGTRACE definition to ... get debug tracing :)

See it live here [click]TM

#include <cmath>
#include <limits>
#include <iostream>

#define MAX_DECIMAL_FRACTION 5
#define DEBUGTRACE(x) // do { std::cerr << x; } while (false)

template <typename T, bool is_integer> struct reverse_impl;

template <typename T>
    struct reverse_impl<T, true>
{
    static T reverse(T input)
    {
        T output;

        for (output = 0; input; input/=10)
            output = (output * 10) + input % 10;

        return output;
    }
};

template <typename T>
    struct reverse_impl<T, false>
{
    static T reverse(T input)
    {
        if (std::abs(input) <= std::numeric_limits<T>::epsilon())
            return T(0);

        // scale input
        int log10 = (int) (std::log(input)/std::log(T(10)));
        input *= std::pow(10, MAX_DECIMAL_FRACTION);
        input = std::floor(input);
        input /= std::pow(10, log10+MAX_DECIMAL_FRACTION);

        DEBUGTRACE("debug: scaled " << input << " digits: ");

        int iteration = std::max(log10+MAX_DECIMAL_FRACTION, MAX_DECIMAL_FRACTION);

        if (std::floor(input) < 1)
        {
            input *= 10;
            iteration--;
        }

        T output;
        for (output = T(0); 
             iteration-- && std::floor(input) >= 1; 
             input-=std::floor(input), input*=T(10))
        {
            output = (output / T(10)) + std::floor(input);
            DEBUGTRACE(std::floor(input));
        }

        DEBUGTRACE(std::endl);
        return output * std::pow(10, log10);
    }
};

template <typename T>
    T reverse(T input)
{ 
    return reverse_impl<T, std::numeric_limits<T>::is_integer>::reverse(input); 
}

int main()
{
    std::cout << reverse(-123l) << std::endl;
    std::cout << reverse(123ul) << std::endl;

    std::cout << reverse(123456.0) << std::endl;
    std::cout << reverse(0.027f) << std::endl;

    return 0;
}


             ***//here is the simple solution to find reverse of a function***  
#include<iostream.h>
#include<conio.h>
void main()
{
 int n,a,c,d,b;
clrscr();
cout<<"enter five integers";

 cin>>n;
 a=n/10000;
 n=n%10000;
 b=n/1000;
 n=n%1000;
 c=n/100;
 n=n%100;
 d=n/10;
 n=n%10;
 cout<<"number in reverse order is"<<n<<d<<c<<b<<a;
 getch();
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜