2013-10-19 2 views
-4

나는 합계를 보여주고 사람이 입력 한 반대 숫자를 보여주는 프로그램을 만들었습니다. sum 함수는 작동하지만 reverse 함수는 작동하지 않습니다. 아무도 날 고칠 수있는 방법에 대한 조언을 줄 수 있습니다. 저는 합계를 보여주고 사람이 입력 한 반대 숫자를 보여주는 프로그램을 만들었습니다. sum 함수는 작동하지만 reverse 함수는 작동하지 않습니다. 아무도 날 고칠 수있는 방법에 대한 조언을 줄 수 있습니다. 주석에서 언급 한 바와 같이 다음 코드에서C++에서 숫자 반전하기

#include<iostream> 
    #include<iomanip> 

    using namespace std; 

    void printSum(int n, bool reverse); 
    int sm(int n); 
    int reverseInt(int n); 
    void printAddTable(int n); 
    int main() 
    { 
    int reverse; 
     int sum=0; 
      int n; 
     cout<<"Enter N value:"<<endl; 
     cin>>n; 

     if(n>0) 
     { 
      reverse = true; 
      printSum(n, reverse); // calls the printSum Method 
     } 
     else 
     { 
      //cout<<"enter positive Number only:"<<endl; 
     } 
      sum = sm(n);  //err // calls the sum Method 
     reverse = reverseInt(n); // calls the reverseInt Method 
     cout<<"The reverse value is:"<<reverse; 
     printAddTable(n);   // calls the printAddTable Method 
     //getch() 
     } 
     //end of main() 
    void printSum(int n, bool reverse) 
    { 

    int sum=0;    
         // print sum of reverse numbers 
    for(int i=n; i>=1; i--) 
    { 
    sum=sum+i; 
    cout<<i<< " "<<"+"<<" "; 

    } 
    cout<<sum; 
    } 

    int sm(int n) 
    {int sum =0; 
    for(int i=1; i<=n; i++) 
    { 
     sum = sum + i ; 
    cout << endl; 

    cout<<i<<" "<<"+"<<" "<<endl;   // print n positive integers 
    cout << endl; 
    } 
    cout<< "Are " <<n<< " positive integers"<<endl; 
    cout<< "Sum is "<<sum <<endl; 





    return sum; 
    } 

    int reverseInt(int n) 
    { 
    int reminder=0; 

    int sum =0;  

    while(n<=0) 
    { 

    reminder = n/10; 
    sum = (sum * 10) + reminder;  // it returns the reverse number 
    n = n % 10; 
    } 
    return sum; 

    } 
    void printAddTable(int n) 
    { 
    for(int i=1; i<=n; i++) 
    { 
    cout<<i<<endl; 
    for(int j=1; j<=n; j++)   // print n X n add table 
    { 
    cout<<i+j<<endl;  

    } 
    } 
    } 
    { 

답변

0

reverseInt 기능에서 변경 될 것입니다.

while(n<0) 
{ 

//reminder = n/10; should be 
reminder = n%10; 
sum = (sum * 10) + reminder;  // it returns the reverse number 
//n = n % 10; 
//Should be 
n = n/10; //or n /= 10; 
} 
+1

테스트는 while (n <= 0)에서 while (n> 0)으로 변경해야합니다. – mattnewport