40 Recurve cpp include iostream h include iomanip

  • Slides: 8
Download presentation

第四章 递归 4、0 递归运算(栈的应用) //递归运算(栈的应用)Recurve. cpp #include <iostream. h> #include <iomanip. h> class list;

第四章 递归 4、0 递归运算(栈的应用) //递归运算(栈的应用)Recurve. cpp #include <iostream. h> #include <iomanip. h> class list; class listnode{ friend class list; int data; listnode *link; listnode(int value, listnode *zz): data(value), link(zz) {} }; class list{ listnode *first; //累加和 long sum(listnode *p); //计数器 long count(listnode *p);

public: list(): first(NULL){} //生成栈 void add(int value); //显示栈中数据 void display(); //获取累加和 long get_sum(){return sum(first);

public: list(): first(NULL){} //生成栈 void add(int value); //显示栈中数据 void display(); //获取累加和 long get_sum(){return sum(first); } //获取数据个数 long get_count(){return count(first); } //求平均 float get_avg() {return (float)sum(first)/count(first); } }; void list: : add(int value){ listnode *p=new listnode(value, first); if(p!=NULL) first=p; else cerr <<"Memory Error!" <<endl; }

void list: : display(){ cout<<"数据域的各个值: "; for(listnode *p=first; p!=NULL; p=p->link) cout<<setw(2)<<p->data; cout <<endl; }

void list: : display(){ cout<<"数据域的各个值: "; for(listnode *p=first; p!=NULL; p=p->link) cout<<setw(2)<<p->data; cout <<endl; } long list: : sum(listnode *p){ if(p->link==NULL) return p->data; else return p->data+sum(p->link); } long list: : count(listnode *p){ long n=1; if(p->link==NULL){return 1; } else{return count(p->link)+n; } }

¡ void main() ¡ {cout<<"Recurve. cpp运行结果: n"; ¡ list a; ¡ a. add(1); ¡

¡ void main() ¡ {cout<<"Recurve. cpp运行结果: n"; ¡ list a; ¡ a. add(1); ¡ a. add(2); ¡ a. add(3); ¡ a. add(4); ¡ a. display(); ¡ cout<<"累加和sum="<<a. get_sum()<<endl; ¡ cout<<"数据个数count="<<a. get_count()<<endl; ¡ cout<<"平均值avg="<<a. get_avg()<<endl; ¡ cin. get(); } 单击此处运行程序

4、1 使用回溯法求解迷宫问题 //使用回溯法求解迷宫问题migong. cpp #include<iostream. h> #include<iomanip. h> #include<stdlib. h> #include<fstream. h> //路口的结构体定义 typedef

4、1 使用回溯法求解迷宫问题 //使用回溯法求解迷宫问题migong. cpp #include<iostream. h> #include<iomanip. h> #include<stdlib. h> #include<fstream. h> //路口的结构体定义 typedef struct {int left; int forward; int right; }Inter. S; //迷宫类定义与实现 class Maze {private: int maze. Size; //路口个数 int Exit; //出口 Inter. S *int. Sec; //路口集合 public:

//构造函数 Maze(char *filename); //搜索函数 int Trav. Maze(int int. Sec. V); }; Maze: : Maze(char

//构造函数 Maze(char *filename); //搜索函数 int Trav. Maze(int int. Sec. V); }; Maze: : Maze(char *filename) {ifstream fin; fin. open(". \Maze 1. dat", ios: : in); //打开文件 if(!fin) {cerr<<"数据文件无法打开!n"; exit(1); } fin>>maze. Size; //读入路口个数 int. Sec=new Inter. S[maze. Size+1]; //建立路口集合数组 for(int i=1; i<=maze. Size; i++)//读入所有路口的结构体数值 fin>>int. Sec[i]. left>>int. Sec[i]. forward>>int. Sec[i]. right; fin>>Exit; //读入出口号码 fin. close(); //关闭文件 } int Maze: : Trav. Maze(int int. Sec. V) {if(int. Sec. V>0) {if(int. Sec. V==Exit)//到达出口

¡ ¡ ¡ ¡ ¡ ¡ {cout<<int. Sec. V<<"<=="; //输出路口号码 return 1; } else

¡ ¡ ¡ ¡ ¡ ¡ {cout<<int. Sec. V<<"<=="; //输出路口号码 return 1; } else if(Trav. Maze(int. Sec[int. Sec. V]. left))//向左搜索 {cout<<int. Sec. V<<"<=="; //输出路口号码 return 1; } else if(Trav. Maze(int. Sec[int. Sec. V]. forward))//向前搜索 {cout<<int. Sec. V<<"<=="; return 1; } else if(Trav. Maze(int. Sec[int. Sec. V]. right))//向右搜索 {cout<<int. Sec. V<<"<=="; return 1; }} return 0; } //迷宫类的测试 void main() {cout<<"migong. cpp运行结果: n"; cout<<"求解迷宫问题: n"; char file. Name[20]={". \Maze 1. dat"}; Maze m(file. Name); int start=1; if(m. Trav. Maze(start)) cout<<endl<<"此迷宫的一条通路如上输出所示!n"; else cout<<"此迷宫无通路!n"; cin. get(); }