zoukankan      html  css  js  c++  java
  • Visual C++ 2008入门经典 第九章练习题 简单

    // 第九章练习题.cpp: 主项目文件。
    
    //第一题:
    //1 没有定义析构函数,没有销毁p指针
    //2 默认构造函数没有初始化p指针
    //看到答案以后,自己还是想得太少了
    /*#include <iostream>
    using namespace System;
    using namespace std;
    class CBadClass
    {
    private:
    	int len;
    	char* p;
    public:
    	CBadClass(const char* str = 0):len(0)
    	{
    		if(p) //判断是否存在p指针
    		{
    		    len = strlen(str); //开度
    			p = new char[len+1]; //创建指针
    			strcpy_s(p, len+1, str); //复制内容到新的指针上
    		}
    	}
    };
    int main(array<System::String ^> ^args)
    {
    
    	CBadClass pClass("Xlc");
    
        system("pause");
    	return 0;
    }
    */
    
    //第二题:
    //这是一个可以合理的CHawk类可以从CBird继承,但是COStrich不行,因为鸵鸟不能飞
    //一个更好的类层次结构为代表鸟,让让鸵鸟,企鹅不会飞的鸟
    //一个单独的类来表示,下面的层次结构允许
    /*class CBird
    {
    protected:
    	int wingSpan; //翼展的类型
    	int eggSize;  //蛋的大小
    };
    //不会飞类型的飞的类
    class CFilightlessBird : public CBird
    {
    };
    //代表可以飞的鸟类基类也是从CBird继承的
    class CFlyingBird : public CBird
    {
    protected:
    	int airSpeed;
    	int altitued;
    public:
    	virtual void fly(){
    	    altitued = 100;
    	}
    };*/
    
    
    
    //第三题
    //抽像类,因为virtual void Print() const =0,为一个纯虚函数
    /*#include <iostream>
    using namespace System;
    using namespace std;
    class CBase
    {
    protected:
    	int m_anInt;
    public:
    	CBase(int n):m_anInt(n){ cout<<"Base constructor"<<endl; }
    	virtual void Print() const = 0;
    	virtual ~CBase(){
    	    cout<<"CBase 析构函数"<<endl;
    	}
    };
    
    class MCBase : public CBase
    {
    private:
    	char* p;
    	int len;
    public:
    	MCBase(char* str, int n):CBase(n), len(0)
    	{
    		if(p){
    		    len = strlen(str);
    			p = new char[len+1];
    			strcpy_s(p, len+1, str);
    		}
    	}
    
    	virtual void Print() const
    	{
    	    cout<<"m_anInt:"<<m_anInt<<endl;
    		cout<<"p:"<<*p<<endl;
    	}
    	~MCBase(){
    		cout<<"MCBase 析构函数"<<endl;
    	    delete p;
    	}
    };
    
    int main(array<System::String ^> ^args)
    {
    	//MCBase mcbase = new MCBase("xlc", 55);
    	MCBase* pbase;
    	MCBase mcbase("Xlc", 55);
    	mcbase.Print();
    	pbase = &mcbase;
    	pbase->Print();
    
        system("pause");
    	return 0;
    }*/
    
    
    //第四题
    /*#include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>  //For rand_s function
    using namespace System;
    using namespace std;
    class CBinaryTree
    {
    private:
    	//定义一个嵌套类
    	class CNode
    	{		
    	public:
    		//构造函数
    		CNode(int n):value(0),m_pLeft(0), m_pRight(0){}
    
    		//指出节点的值
    		void listNode()
    		{
    			//如果左边的指针不为空值
    			if(m_pLeft != 0){
    			    m_pLeft->listNode(); //传到的值中去
    			}
    			cout<<setw(12)<<endl; //显示12空格
    			if(++listCount % 5 == 0){
    			     cout<<endl; //显示一换行
    			}
    			if(m_pRight != 0){ //右边指针
    			     m_pRight->listNode();
    			}		    
    		}		
    		int value;     //值
    		CNode* m_pLeft;//左边的指针
    		CNode* m_pRight;//右边的指针
    		static int listCount; //一个静态函数
    	};
    	CNode* m_pRoot; //定义一个首指针CNode
    public:
    	//构造函数
    	CBinaryTree() : m_pRoot(0){}
    	//声明添加函数
    	void add(int n); 
    	//两个参数的添加函数
    	//相对于一个给定的节点添加一个值
    	void add(int n, CNode* pNode); 
    
    	//列出所有列表
    	void listNodes()
    	{
    	     CNode::listCount = 0;
    		 if(m_pRoot == 0)
    		      cout<<"Binary tree is empty"<<endl;
    		 else
    			   m_pRoot->listNode();
    	}
    };
    //定义静态变量的值
    int CBinaryTree::CNode::listCount = 0;
    
    void CBinaryTree::add(int n)
    {
    	if(m_pRoot == 0){
    	    m_pRoot = new CNode(n);
    	}else{
    	    add(n, m_pRoot);
    	}
    };
    //相对于一个给定的节点添加一个值
    void CBinaryTree::add(int n, CNode* pNode)
    {
    	//如果值相等
    	if(n == pNode->value){ //那么将值放到当前pNode的左边
    		//定义一个新的CNode指针
    	    CNode* pNewNode = new CNode(n);
    		CNode* pTemp = pNode->m_pLeft;
    		pNode->m_pLeft = pNewNode;
    		pNewNode->m_pLeft = pTemp;
    
        //如果n大于当前指针的值
    	}else if(n > pNode->value)
    	{
    		//先判断当前指针的右值是否为0
    		//那么值接为当前指针的左值创建一个指针
    		//否则就是在前m_pRight上面添加值
    		if(pNode->m_pRight == 0){
    		    pNode->m_pRight = new CNode(n);
    		}else{
    		    add(n, pNode->m_pRight); //这里直到找到等于或者他的值或者右边值为空的性情况才会被插入
    		}
    	}else{
            //应该只有一种情况了,那就是n < pNode->value
    		//跟在插入右边值的一样
    		if(pNode->m_pLeft ==0){
    		     pNode->m_pLeft = new CNode(n);
    		}else{
    		     add(n, pNode->m_pLeft);
    		}
    	}
    }
    
    
    
    
    int main(array<System::String ^> ^args)
    {
    
    	CBinaryTree tree;
    	unsigned int number = 0; //无符号的整型
    
    	double max = 1000000.0;
    	for(int i=0; i<100; i++){
    		//取一个随机数放入number中
    		if(rand_s(&number) != 0) //成功是返回0???
    			cout<<"随机数生成器失败!"<<endl;
    		number = static_cast<unsigned int>(static_cast<double>(number)/UINT_AMX*max)+1;
    		tree.add(number); //插入tree
    		cout<<setw(12)<<endl;
    
    		//换行
    		if((i+1) % 5 ==0){
    		   cout<<endl;
    		}
    
    		cout<<"树的输出:"<<endl;
    		tree.listNodes();
    	}
        system("pause");
    	return 0;
    }*/
    
    //第五题
    /*#include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>  //For rand_s function
    using namespace System;
    using namespace std;
    ref class CBinaryTree
    {
    private:
    	//定义一个嵌套类
    	ref class CNode
    	{		
    	public:
    		//构造函数
    		CNode(int n):value(n),m_pLeft(nullptr), m_pRight(nullptr){}
    
    		//指出节点的值
    		void listNode()
    		{
    			//如果左边的指针不为空值
    			if(m_pLeft != nullptr){
    			    m_pLeft->listNode(); //传到的值中去
    			}
    			//cout<<setw(12)<<value; //显示12空格
    			Console::Write(L"{0, 12}", value);
    			if(++listCount % 5 == 0){
    			     cout<<endl; //显示一换行
    			}
    			if(m_pRight != nullptr){ //右边指针
    			     m_pRight->listNode();
    			}		    
    		}		
    		int value;     //值
    		CNode^ m_pLeft;//左边的指针
    		CNode^ m_pRight;//右边的指针
    		static int listCount; //一个静态函数
    	};
    	CNode^ m_pRoot; //定义一个首指针CNode
    public:
    	//构造函数
    	CBinaryTree() : m_pRoot(nullptr){}
    	//声明添加函数
    	void add(int n); 
    	//两个参数的添加函数
    	//相对于一个给定的节点添加一个值
    	void add(int n, CNode^ pNode); 
    
    	//列出所有列表
    	void listNodes()
    	{
    	     CNode::listCount = 0;
    		 if(m_pRoot == nullptr)
    		      cout<<"Binary tree is empty"<<endl;
    		 else
    			   m_pRoot->listNode();
    	}
    };
    //定义静态变量的值
    //int CBinaryTree::CNode::listCount = 0;
    
    void CBinaryTree::add(int n)
    {
    	if(m_pRoot == nullptr){
    	    m_pRoot = gcnew CNode(n);
    	}else{
    	    add(n, m_pRoot);
    	}
    };
    //相对于一个给定的节点添加一个值
    void CBinaryTree::add(int n, CNode^ pNode)
    {
    	//如果值相等
    	if(n == pNode->value){ //那么将值放到当前pNode的左边
    		//定义一个新的CNode指针
    	    CNode^ pNewNode = gcnew CNode(n);
    		CNode^ pTemp = pNode->m_pLeft;
    		pNode->m_pLeft = pNewNode;
    		pNewNode->m_pLeft = pTemp;
    
        //如果n大于当前指针的值
    	}else if(n > pNode->value)
    	{
    		//先判断当前指针的右值是否为0
    		//那么值接为当前指针的左值创建一个指针
    		//否则就是在前m_pRight上面添加值
    		if(pNode->m_pRight == nullptr){
    		    pNode->m_pRight = gcnew CNode(n);
    		}else{
    		    add(n, pNode->m_pRight); //这里直到找到等于或者他的值或者右边值为空的性情况才会被插入
    		}
    	}else{
            //应该只有一种情况了,那就是n < pNode->value
    		//跟在插入右边值的一样
    		if(pNode->m_pLeft ==nullptr){
    		     pNode->m_pLeft = gcnew CNode(n);
    		}else{
    		     add(n, pNode->m_pLeft);
    		}
    	}
    }
    
    
    int main(array<System::String ^> ^args)
    {
    
    
    	CBinaryTree^ tree = gcnew CBinaryTree;
    	unsigned int number = 0; //无符号的整型
    
    	Random^ generator = gcnew Random;
    
    	for(int i=0; i<20; i++)
    	{
    	    number = generator->Next(1, 1000000);
    		tree->add(number);
    		Console::Write(L"{0,12}", number);
    		if((i+1) % 5 == 0){
    		    Console::WriteLine();
    		}
    
    		cout<<"现在开始循环树:"<<endl;
    		tree->listNodes();
    	}
    	system("pause");
    	return 0;
    }*/
    
    
    //第六题
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>  //For rand_s function
    using namespace System;
    using namespace std;
    
    //定义一个通用类,前面说的已经记不起了where T:IComparable???
    generic<typename T> where T:IComparable ref class BinaryTree
    {
    private:
    	ref class Node
    	{
    	public:
    		Node(T n):value(n), left(nullptr), right(nullptr){}
    		void listNode()
    		{
    			if(left != nullptr){
    			    left->listNode();
    			}		
    			Console::WriteLine(format, value);
    			if(++listCount % 5 ==0){
    			     Console::WriteLine();
    			}
    			if(right != nullptr){
    			    right->listNode();
    			}
    		}
    		T value;
    		Node^ left;
    		Node^ right;
    		static int listCount;
    		static String^ format;
    	};
    
    	Node^ root;
    	static int width;
    
    public:
    	BinaryTree() : root(nullptr){}
    
    	void add(T o);
    	void add(T o, Node^ node);
    
    	void listNodes()
    	{
    		Node::format = L"{0, "+(width+2)+L"}"; //定义显示的空格数
    		Node::listCount = 0;
    		if(root ==nullptr){
    		    Console::WriteLine(L"列表显示为空!");
    		}else{
    		    root->listNode();
    		}
    	}
    };
    
    //定义添加方法
    generic<typename T>
    void BinaryTree<T>::add(T o)
    {
    	if(root == nullptr){
    	    root = gcnew Node(o);
    	}else{
    	    add(o, root);
    	}
    
    	width = System::Math::Max(width, o->ToString()->Length);
    };
    
    generic<typename T>
    void BinaryTree<T>::add(T o, Node^ node)
    {
    	if(o == node->value){
    	   Node^ newNode = gcnew Node(o);
    	   Node^ temp = node->left;
    	   node->left = newNode;
    	   newNode->left = temp;
    	}else if(o->CompareTo(node->value) > 0){
    		if(node->right == nullptr){
    		     node->right = gcnew Node(o);
    		}else{
    		     add(o, node->right);
    		}
    	}else{
    		if(node->left == nullptr){
    		     node->left = gcnew Node(o);
    		}else{
    		     add(o, node->left);
    		}
    	}
    };
    
    
    int main(array<System::String ^> ^args)
    {
    	BinaryTree<int>^ tree = gcnew BinaryTree<int>;
        unsigned int number = 0;
    	Random^ generator = gcnew Random;
    
    	for(int i=0; i<100; i++){
    	    number = generator->Next(1, 1000000);
    		tree->add(number);
    		Console::Write(L"{0,12}", number);
    		if((i+1) % 5 == 0){
    		    Console::WriteLine();
    		}
    	}
    	Console::WriteLine(L"Output from the tree is:");
    	tree->listNodes();
    
    	array<String^>^ words = {L"Success", L"is", L"the", L"ability", L"to",
    	                         L"go", L"from", L"one", L"failure", L"to", 
    	                         L"another", L"with", L"no", L"loss", L"of",
    	                         L"enthusiasm"};
    	BinaryTree<String^>^ strTree = gcnew BinaryTree<String^>;
    	int lineCount = 0;
    	for each(String^ str in words){
    	     strTree->add(str);
    		 Console::Write(L"{0,12}", str);
    		 if(++lineCount % 5 == 0){
    		     Console::WriteLine();
    		 }
    	}
    
    	Console::WriteLine(L"\n\nOutput from the tree is:");
    	strTree->listNodes();
    	Console::WriteLine();
    
        system("pause");
    	return 0;
    }
    
    
    /*#include <iostream>
    using namespace System;
    using namespace std;
    int main(array<System::String ^> ^args)
    {
    
    
    
        system("pause");
    	return 0;
    }*/
    

      

  • 相关阅读:
    迭代合并排序算法
    appendChild和insertBefore的区别
    使用定时器处理数组
    正则表达式 删除string首尾的空白
    图片滚动
    数组合并法(IE7性能优化)
    赋值取值+arguments
    条件预加载(conditional advanceloading)
    Just a Note~
    腾讯马拉松复赛第一场
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2764600.html
Copyright © 2011-2022 走看看