递归分配没有给出正确的输出
Recursion assignment not giving correct output
我有一个作业,我们要用递归执行 a + b 操作。如果这是一个超级简单的修复,请多多包涵,因为我是新手。最近我一直在犯简单的错误,只是没有看到它们。
我们必须使用:
BASE CASE: if ( a == 0 ) return( b ); BASE CASE: if ( b == 0 ) return( a );
RECURSIVE CASE: else return( RecursiveAPlusB( a - 1, b - 1 ) + 2 );
我的header是:
#ifndef Adder_h
#define Adder_h
#include <iostream>
using namespace std;
class Adder{
public:
Adder( int a, int b );
int getA( ) const;
int getB( ) const;
int RecursiveAPlusB( ) const;
int IterativeAPlusB( ) const;
private:
int myValueofA;
int myValueofB;
};
#endif /* Adder_h */
我的 driver 是:
#include "Adder.h"
Adder::Adder(int a, int b){
myValueofA = a;
myValueofB = b;
}
int Adder::getA( ) const{
return myValueofA;}
int Adder::getB() const{
return myValueofB;
}
int Adder::IterativeAPlusB( ) const{
if(myValueofA==0)
return myValueofB;
else if(myValueofB==0)
return myValueofA;
else
return RecursiveAPlusB();
}
int Adder::RecursiveAPlusB() const{
return ((myValueofA - 1, myValueofB - 1 ) + 2) ;
}
我的主要是(作业要求):
#include "Adder.h"
#include <iostream>
using namespace std;
int main() {
Adder ten( 6, 4 );
// All these calls should produce the
// exact same answer...
// namely, the number 10!
cout << ten.RecursiveAPlusB( ) << endl;
cout << ten.IterativeAPlusB( ) << endl;
cout << ten.RecursiveAPlusB( ) << endl;
Adder tenagain( 2, 8 );
cout << tenagain.RecursiveAPlusB( ) << endl;
cout << tenagain.IterativeAPlusB( ) << endl;
cout << tenagain.RecursiveAPlusB( ) << endl;
return 0;
}
请告诉我我的数学哪里出了问题?!输出是 5 5 5 9 9 9,但它们应该都是 10。谢谢!!!
在你的函数中你使用 comma operator
:
int Adder::RecursiveAPlusB() const{
return ((myValueofA - 1, myValueofB - 1 ) + 2) ;
}
(6 - 1, 4 - 1) = 3
的结果。
顺便一提 - 将 RecursiveAPlusB() 更改为:
int Adder::RecursiveAPlusB() const
{
return (Adder(myValueofA - 1, myValueofB - 1 ).IterativeAPlusB() + 2) ;
}
在我看来,根据作业的描述,您应该按以下方式定义函数。首先,您需要向 class
添加一个成员函数
#ifndef Adder_h
#define Adder_h
#include <iostream>
using namespace std;
class Adder{
public:
Adder( int a, int b );
int getA( ) const;
int getB( ) const;
int RecursiveAPlusB( ) const;
int IterativeAPlusB( ) const;
private:
int RecursiveAPlusB( int a, int b ) const;
private:
int myValueofA;
int myValueofB;
};
#endif /* Adder_h */
函数定义看起来像
int Adder::RecursiveAPlusB() const
{
return RecursiveAPlusB( getA(), getB() );
}
int Adder::RecursiveAPlusB( int a, int b ) const
{
if ( a == 0 ) return b;
else if ( b == 0 ) return a;
else return 2 + RecursiveAPlusB( a - 1, b - 1 ) ;
}
和
int Adder::IterativeAPlusB( ) const{
{
int sum = 0;
int a = getA(), b = getB();
for ( ; a != 0 && b != 0; a -= 1, b -=1 )
{
sum += 2;
}
if ( a == 0 ) sum += b;
else sum += a;
return sum;
}
请注意,函数仅在值为非负数的情况下才有效。所以最好将它们声明为 unsigned int
.
类型
我有一个作业,我们要用递归执行 a + b 操作。如果这是一个超级简单的修复,请多多包涵,因为我是新手。最近我一直在犯简单的错误,只是没有看到它们。
我们必须使用:
BASE CASE: if ( a == 0 ) return( b ); BASE CASE: if ( b == 0 ) return( a );
RECURSIVE CASE: else return( RecursiveAPlusB( a - 1, b - 1 ) + 2 );
我的header是:
#ifndef Adder_h #define Adder_h #include <iostream> using namespace std; class Adder{ public: Adder( int a, int b ); int getA( ) const; int getB( ) const; int RecursiveAPlusB( ) const; int IterativeAPlusB( ) const; private: int myValueofA; int myValueofB; }; #endif /* Adder_h */
我的 driver 是:
#include "Adder.h" Adder::Adder(int a, int b){ myValueofA = a; myValueofB = b; } int Adder::getA( ) const{ return myValueofA;} int Adder::getB() const{ return myValueofB; } int Adder::IterativeAPlusB( ) const{ if(myValueofA==0) return myValueofB; else if(myValueofB==0) return myValueofA; else return RecursiveAPlusB(); } int Adder::RecursiveAPlusB() const{ return ((myValueofA - 1, myValueofB - 1 ) + 2) ; }
我的主要是(作业要求):
#include "Adder.h" #include <iostream> using namespace std; int main() { Adder ten( 6, 4 ); // All these calls should produce the // exact same answer... // namely, the number 10! cout << ten.RecursiveAPlusB( ) << endl; cout << ten.IterativeAPlusB( ) << endl; cout << ten.RecursiveAPlusB( ) << endl; Adder tenagain( 2, 8 ); cout << tenagain.RecursiveAPlusB( ) << endl; cout << tenagain.IterativeAPlusB( ) << endl; cout << tenagain.RecursiveAPlusB( ) << endl; return 0;
}
请告诉我我的数学哪里出了问题?!输出是 5 5 5 9 9 9,但它们应该都是 10。谢谢!!!
在你的函数中你使用 comma operator
:
int Adder::RecursiveAPlusB() const{
return ((myValueofA - 1, myValueofB - 1 ) + 2) ;
}
(6 - 1, 4 - 1) = 3
的结果。
顺便一提 - 将 RecursiveAPlusB() 更改为:
int Adder::RecursiveAPlusB() const
{
return (Adder(myValueofA - 1, myValueofB - 1 ).IterativeAPlusB() + 2) ;
}
在我看来,根据作业的描述,您应该按以下方式定义函数。首先,您需要向 class
添加一个成员函数#ifndef Adder_h
#define Adder_h
#include <iostream>
using namespace std;
class Adder{
public:
Adder( int a, int b );
int getA( ) const;
int getB( ) const;
int RecursiveAPlusB( ) const;
int IterativeAPlusB( ) const;
private:
int RecursiveAPlusB( int a, int b ) const;
private:
int myValueofA;
int myValueofB;
};
#endif /* Adder_h */
函数定义看起来像
int Adder::RecursiveAPlusB() const
{
return RecursiveAPlusB( getA(), getB() );
}
int Adder::RecursiveAPlusB( int a, int b ) const
{
if ( a == 0 ) return b;
else if ( b == 0 ) return a;
else return 2 + RecursiveAPlusB( a - 1, b - 1 ) ;
}
和
int Adder::IterativeAPlusB( ) const{
{
int sum = 0;
int a = getA(), b = getB();
for ( ; a != 0 && b != 0; a -= 1, b -=1 )
{
sum += 2;
}
if ( a == 0 ) sum += b;
else sum += a;
return sum;
}
请注意,函数仅在值为非负数的情况下才有效。所以最好将它们声明为 unsigned int
.