我创建了一个程序来显示总和并显示一个人输入的反转数字。 sum函数有效,但反转功能不起作用。任何人都可以给我任何关于如何解决它的提示。 我创建了一个程序来显示总和并显示一个人输入的反转数字。 sum函数有效,但反转功能不起作用。任何人都可以给我任何关于如何解决它的提示。
#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 :(得分: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;
}