64,654
社区成员
发帖
与我相关
我的任务
分享
void ashow(MyStruct * p)
{
if (p != nullptr)
{
ashow(p->pLeft);
ashow(p->pRight);
cout << p->Nodedata << endl;
}
}
#include <iostream>
using namespace std;
struct MyStruct {
int Nodedata;
MyStruct *pLeft;
MyStruct *pRight;
} s1,s2,s3,s4,s5,s6,s7,s8;
void ashow(MyStruct * p) {
if (p != nullptr) {
ashow(p->pLeft);
ashow(p->pRight);
cout << p->Nodedata << endl;
if (p->Nodedata == 4) {
throw 1;//退出递归.直接返回.
}
}
}
int main() {
MyStruct *pRoot;//根
pRoot = &s1;
s1.Nodedata = 1;
s2.Nodedata = 2;
s3.Nodedata = 3;
s4.Nodedata = 4;
s5.Nodedata = 5;
s6.Nodedata = 6;
s7.Nodedata = 7;
s8.Nodedata = 8;
s1.pLeft = &s2;
s1.pRight = &s3;
s2.pLeft = &s4;
s2.pRight = &s5;
s3.pLeft = &s6;
s3.pRight = &s7;
try {
ashow(pRoot);
} catch (int err) {
cout<<"catch err "<<err<<endl;
}
cin.get();
return 0;
}
//4
//catch err 1
//
//
static bool bContinue = true;
void ashow(MyStruct * p)
{
if (p != NULL && bContinue)
{
if (p->Nodedata != 4)
{
//退出递归.直接返回.
ashow(p->pLeft);
ashow(p->pRight);
}else{
cout << p->Nodedata << endl;
bContinue = false;
}
}
}
如果是个终止条件,比如遍历这棵树,直到 Nodedata == 4,那就使用我前面说的方法就行了。
static bool bContinue = true;
void ashow(MyStruct * p)
{
if (p != NULL && bContinue)
{
if (p->Nodedata != 4)
{
//退出递归.直接返回.
ashow(p->pLeft);
ashow(p->pRight);
}
bContinue = false;
cout << p->Nodedata << endl;
}
}
如果你想要输出序列跟查找序列相同,应使用先根遍历:
下面代码输出1 2 4
static bool bContinue = true;
void ashow(MyStruct * p)
{
if (p != NULL && bContinue)
{
cout << p->Nodedata << endl;
if (p->Nodedata != 4)
{
//退出递归.直接返回.
ashow(p->pLeft);
ashow(p->pRight);
}else{
bContinue = false;
}
}
}
#include <iostream.h>
void MyFunc( void );
class CTest
{
public:
CTest(){};
~CTest(){};
const char *ShowReason() const { return "Exception in CTest class."; }
};
class CDtorDemo
{
public:
CDtorDemo();
~CDtorDemo();
};
CDtorDemo::CDtorDemo()
{
cout << "Constructing CDtorDemo." << endl;
}
CDtorDemo::~CDtorDemo()
{
cout << "Destructing CDtorDemo." << endl;
}
void MyFunc()
{
CDtorDemo D;
cout<< "In MyFunc(). Throwing CTest exception." << endl;
throw CTest();
}
int main()
{
cout << "In main." << endl;
try
{
cout << "In try block, calling MyFunc()." << endl;
MyFunc();
}
catch( CTest E )
{
cout << "In catch handler." << endl;
cout << "Caught CTest exception type: ";
cout << E.ShowReason() << endl;
}
catch( char *str )
{
cout << "Caught some other exception: " << str << endl;
}
cout << "Back in main. Execution resumes here." << endl;
return 0;
}
If a matching catch handler is found, and it catches by value, its formal parameter is initialized by copying the exception object. If it catches by reference, the parameter is initialized to refer to the exception object. After the formal parameter is initialized, the process of “unwinding the stack” begins. This involves the destruction of all automatic objects that were constructed (but not yet destructed) between the beginning of the try block associated with the catch handler and the exception’s throw site. Destruction occurs in reverse order of construction. The catch handler is executed and the program resumes execution following the last handler (that is, the first statement or construct that is not a catch handler).
This is the output from the preceding example:
In main.
In try block, calling MyFunc().
Constructing CDtorDemo.
In MyFunc(). Throwing CTest exception.
Destructing CDtorDemo.
In catch handler.
Caught CTest exception type: Exception in CTest class.
Back in main. Execution resumes here.
Note the declaration of the exception parameter in both catch handlers:
catch( CTest E )
{ // ... }
catch( char *str )
{ // ... }
You do not need to declare this parameter; in many cases it may be sufficient to notify the handler that a particular type of exception has occurred. However, if you do not declare an exception object in the exception declaration, you will not have access to the object in the catch handler clause. For example:
catch( CTest )
{
// No access to a CTest exception object in this handler.
}
A throw expression with no operand re-throws the exception currently being handled. Such an expression should appear only in a catch handler or in a function called from within a catch handler. The re-thrown exception object is the original exception object (not a copy). For example:
try
{
throw CSomeOtherException();
}
catch(...) // Handle all exceptions
{
// Respond (perhaps only partially) to exception
//...
throw; // Pass exception to some other handler
}int flag = 1;
void ashow(MyStruct * p)
{
if (p != nullptr && flag)
{
ashow(p->pLeft);
ashow(p->pRight);
cout << p->Nodedata << endl;
if (p->Nodedata == 4)
{
//退出递归.直接返回.
flag = 0;
}
}
}
#include <iostream>
using namespace std;
#include <setjmp.h>
jmp_buf mark;
struct MyStruct {
int Nodedata;
MyStruct *pLeft;
MyStruct *pRight;
} s1,s2,s3,s4,s5,s6,s7,s8;
void ashow(MyStruct * p) {
if (p != nullptr) {
ashow(p->pLeft);
ashow(p->pRight);
cout << p->Nodedata << endl;
if (p->Nodedata == 4) {
longjmp( mark, 1 );//退出递归.直接返回.
}
}
}
int main() {
MyStruct *pRoot;//根
pRoot = &s1;
s1.Nodedata = 1;
s2.Nodedata = 2;
s3.Nodedata = 3;
s4.Nodedata = 4;
s5.Nodedata = 5;
s6.Nodedata = 6;
s7.Nodedata = 7;
s8.Nodedata = 8;
s1.pLeft = &s2;
s1.pRight = &s3;
s2.pLeft = &s4;
s2.pRight = &s5;
s3.pLeft = &s6;
s3.pRight = &s7;
int jmpret;
jmpret = setjmp( mark );
if (jmpret==0) ashow(pRoot);
cin.get();
return 0;
}
//4
//
//
#include<iostream>
#include <string>
#include <stack>
using namespace std;
struct MyStruct
{
int Nodedata = 0;
MyStruct *pLeft = nullptr;
MyStruct *pRight = nullptr;
}BTree, *pBTree;
void ashow(MyStruct * p)
{
if (p != nullptr)
{
ashow(p->pLeft);
ashow(p->pRight);
cout << p->Nodedata << endl;
if (p->Nodedata == 4)
{
//退出递归.直接返回.
}
}
}
int main()
{
MyStruct *pRoot;//根
MyStruct s1;
MyStruct s2;
MyStruct s3;
MyStruct s4;
MyStruct s5;
MyStruct s6;
MyStruct s7;
MyStruct s8;
pRoot = &s1;
s1.Nodedata = 1;
s2.Nodedata = 2;
s3.Nodedata = 3;
s4.Nodedata = 4;
s5.Nodedata = 5;
s6.Nodedata = 6;
s7.Nodedata = 7;
s8.Nodedata = 8;
s1.pLeft = &s2;
s1.pRight = &s3;
s2.pLeft = &s4;
s2.pRight = &s5;
s3.pLeft = &s6;
s3.pRight = &s7;
ashow(pRoot);
cin.get();
return 0;
}
我这个递归是后序遍历.我本来的结果是4526731
我想要的结果是 4 后面的526731不显示.