[DebugForTheBook]清单2.2使用cout的例子有错误 (C/C++学习)
发布于 2006-01-22 15:46 阅读:17,081 评论:0 标签:

  在书第13页,清单2.2使用cout的例子第6行为:“std::cout<<"The manipulator std::endl";”书出给出的输出为:“The manipulator endl”。我百思不得其解,“std::endl”的输出怎么是“endl”呢。

  今天把Dev-C++装好后,一运行,嘿嘿,书上错了。不知道是原作者还是翻译有问题。

  输出应该为:The manipulator std::endl。

附程序:

/*********************************************
*Developer:                     yayu;                          *
*My email:                     xieyayu@163.com                *
*Development environment:   Dev-C++ 4.9.9.0;                 *
*********************************************/

#include <iostream>
#include <stdlib.h>

//using namespace std;

int main(int argc, char *argv[])
{
 
  std::cout<<"Hello there.\n"; 
  std::cout<<"Here is 5:" << 5 << "\n";
  std::cout<<"The manipulator std::endl";
  std::cout<<"Writes a new line to the screen.";
  std::cout<<std::endl;
  std::cout<<"Here is a very big number:\t" << 70000;
  std::cout<<std::endl;
  std::cout<<"Here is the sum of 8 and 5 :\t";
  std::cout<<8+5 << std::endl;
  std::cout<<"Here is a fraction:\t\t";
  std::cout<<(float) 5/8 <<std::endl;
  std::cout<<"And a very very big number:\t";
  std::cout<<(float) 7000*7000 <<std::endl;
  std::cout<<"Yayu is a C++ programer!\n";
 
  return 0;
}

展开全文  
收起全文  
《21天学通C++》栏目说明 (C/C++学习)
发布于 2006-01-22 15:37 4 阅读:13,052 评论:4 标签:

  寒假前一天在李马推荐下买了本《21天学通C++》。希望以此为今后的学习打下基础。这里记录学习的体会吧!

  本书作者为:Jesse Liberty[美]。由康博工作室翻译。人民邮电出版社2002年3月出版,2005年4月北京第8次印刷。原书为第四版。

展开全文  
收起全文  
什么是MFC (C/C++学习)
发布于 2005-11-30 16:34 18 阅读:31,136 评论:18 标签: C++ MFC

     MFC,微软基础类(Microsoft Foundation Classes),实际上是微软提供的,用于在C++环境下编写应用程序的一个框架和引擎,VC++是WinOS下开发人员使用的专业C++ SDK(SDK,Standard SoftWare Develop Kit,专业软件开发平台),MFC就是挂在它之上的一个输助软件开发包,MFC作为与VC++血肉相连的部分(注意C++和VC++的区别:C++是一种程序设计语言,是一种大家都承认的软件编制的通用规范,而VC++只是一个编译器,或者说是一种编译器+源程序编辑器的IDE,WS,PlatForm,这跟Pascal和Dephi的关系一个道理,Pascal是Dephi的语言基础,Dephi使用Pascal规范来进行Win下应用程序的开发和编译,却不同于Basic语言和VB的关系,Basic语言在VB开发出来被应用的年代已经成了Basic语言的新规范,VB新加的Basic语言要素,如面对对象程序设计的要素,是一种性质上的飞跃,使VB既是一个IDE,又成长成一个新的程序设计语言),MFC同BC++集成的VCL一样是一个非外挂式的软件包,类库,只不过MFC类是微软为VC++专配的..

     MFC是Win API与C++的结合,API,即微软提供的WinOS下应用程序的编程语言接口,是一种软件编程的规范,但不是一种程序开发语言本身,可以允许用户使用各种各样的第三方(如我是一方,微软是一方,Borland就是第三方)的编程语言来进行对Win OS下应用程序的开发,使这些被开发出来的应用程序能在WinOS下运行,比如VB,VC++,Java,Dehpi编程语言函数本质上全部源于API,因此用它们开发出来的应用程序都能工作在WinOS的消息机制和绘图里,遵守WinOS作为一个操作系统的内部实现,这其实也是一种必要,微软如果不提供API,这个世上对Win编程的工作就不会存在,微软的产品就会迅速从时尚变成垃圾,上面说到MFC是微软对API函数的专用C++封装,这种结合一方面让用户使用微软的专业C++ SDK来进行Win下应用程序的开发变得容易,因为MFC是对API的封装,微软做了大量的工作,隐藏了好多内节程序开发人员在Win下用C++ & MFC编制软件时的大量内节,如应用程序实现消息的处理,设备环境绘图,这种结合是以方便为目的的,必定要付出一定代价(这是微软的一向作风),因此就造成了MFC对类封装中的一定程度的的冗余和迂回,但这是可以接受的..

     最后要明白MFC不只是一个功能单纯的界面开发系统,它提供的类绝大部分用来进行界面开发,关联一个窗口的动作,但它提供的类中有好多类不与一个窗口关联,即类的作用不是一个界面类,不实现对一个窗口对象的控制(如创建,销毁),而是一些在WinOS(用MFC编写的程序绝大部分都在WinOS中运行)中实现内部处理的类,如数据库的管理类等,学习中最应花费时间的是消息和设备环境,对C++和MFC的学习中最难的部分是指针,C++面向对像程序设计的其它部分,如数据类型,流程控制都不难,建议学习数据结构C++版..

展开全文  
收起全文  
二叉树全集 (C/C++学习)
发布于 2005-11-09 15:25 阅读:10,431 评论:0 标签: 二叉树

#include
#include
#include
 
#define  MaxSize 20

typedef struct BiTNode
{
 int data;
 struct BiTNode *lchild, *rchild;
}BiTNode,*BiTree;

//建立二叉树
void CreateBiTree(BiTree *T)
{
 char ch;
 scanf("%c",&ch);
 getchar();
 if(ch==' ')
 {
  printf("不产生子树。\n");
  *T=NULL;
 }
 else
 {
  if(!(*T=(BiTNode *)malloc(sizeof(BiTNode))))
  {
   printf("分配空间失败");
   return;
  }//生成一个新节点
  (*T)->data = ch;
  printf("产生左右子树。\n");
  CreateBiTree(&(*T)->lchild);                          
  CreateBiTree(&(*T)->rchild);                           
 }
}

//递归前序遍历
void Preorder(BiTNode *T)
 {
  if(T)
  {
   printf("%c ",T->data);
   Preorder(T->lchild);                               
   Preorder(T->rchild);                             
  }
}

//递归中序遍历
void Inorder(BiTNode *T)
{
  if(T)
  {
   Inorder(T->lchild);
   printf("%c ",T->data);
   Inorder(T->rchild);
  }
}

//递归后序遍历
void Postorder(BiTNode *T)
{
 if(T)
 {
  Postorder(T->lchild);
  Postorder(T->rchild);
  printf("%c ",T->data);
 }
}

//非递归前序遍历
void NPreorder(BiTNode *T)
 {
 BiTNode *stack[MaxSize],*p;
 int top=-1;
  if(T)
  {
   top++;
   stack[top]=T;    //根节点进栈
   while(top>-1)   //栈不为空时循环
   {
    p=stack[top];  //退栈并访问该节点
    top--;
    printf("%c ",p->data);
    if(p->rchild)     //右孩子进栈
    {
     top++;
     stack[top]=p->rchild;
    }
    if(p->lchild)    //左孩子进栈
    {
     top++;
     stack[top]=p->lchild;
    }
   }                         
  }
}

//非递归中序遍历
void NInorder(BiTNode *T)
{
 BiTNode *stack[MaxSize],*p;
 int top=-1;
 p=T;
 while(p||top!=-1)
 {
  if(p)
  {
   top++;
   stack[top]=p;
   p=p->lchild;
  }  //根节点进栈,遍历左子树
  else  //根节点退栈,访问根节点,遍历右子树
  {
   p=stack[top];
   top--;
   printf("%c ",p->data);
   p=p->rchild;
  }
 }
}

//非递归后序遍历
void NPostorder(BiTNode *T)
{
 BiTNode *stack[MaxSize],*p;
 int flag,top=-1;
 do
 {
  while(T)
  {
   top++;
   stack[top]=T;
   T=T->lchild;
  } //所有左节点进栈
  p=NULL;  //p总是指向当前节点的前一个已经访问过的节点
  flag=1;  //flag为1表示当前节点已经访问过了
  while(top!=-1 && flag)
  {
   T=stack[top];
   if(T->rchild==p) //右子树不存在或者已经被访问过时
   {
    printf("%c ",T->data);
    top--;
    p=T;        //调整p指针
   }
   else
   {
    T=T->rchild;
    flag=0; //调整访问标志
   }
  }
 } while(top!=-1);
}

//层次遍历二叉树
void Translever(BiTNode *T)
{
 struct node
 {
  BiTNode *vec[MaxSize];
  int f,r;    //r为队尾,f为队头
 }queue;
 BiTNode *p;
 p=T;
 queue.f=0;
 queue.r=0;
 if(T)
  printf("%c ", p->data);
 queue.vec[queue.r]=p;
 queue.r=queue.r+1;
 while(queue.flchild)
  {
   printf("%c ",p->lchild->data);
   queue.vec[queue.r]=p->lchild;
   queue.r=queue.r+1;
  }
  if(p->rchild)
  {
   printf("%c ",p->rchild->data);
   queue.vec[queue.r]=p->rchild;
   queue.r=queue.r+1;
  }
 }
 printf("\n");
}

//求二叉树的深度
int Depth(BiTNode *T)
{
 int dep1,dep2;
 if(T==NULL)
  return(0);
 else
 {
  dep1=Depth(T->lchild);
  dep2=Depth(T->rchild);
  if(dep1>dep2)
   return(dep1+1);
  else
   return(dep2+1);
 }
}

//输出二叉树
void Disptree(BiTNode *T)
{
 if(T)
 {
  printf("%c",T->data);
        if(T->lchild || T->rchild)
  {
   printf("(");
   Disptree(T->lchild);
   if(T->rchild)
    printf(",");
   Disptree(T->rchild);
   printf(")");
  }
 }
}
 
void main()
{
 BiTree T=NULL;
 char j;
 int sign = 1;

 printf("本程序可以进行建立二叉树、递归与非递归先序、中序、后序
遍历二叉树、层次遍历二叉树、输出二叉树的扩展序列的操作。\n");
    printf("请将二叉树的先序序列输入以建立二叉树,叶子节点用
          空格代替。\n");
 printf("您必须一个一个地输入字符。\n");
 while(sign)
 {
  printf("请选择: \n");
  printf("0.生成二叉树   1.求二叉树的深度\n");
  printf("2.递归先序遍历  3.非递归先序遍历\n");
  printf("4.递归中序遍历  5.非递归中序遍历\n");
  printf("6.递归后序遍历  7.非递归后序遍历\n");
  printf("8.层次遍历  9.输出二叉树的广义表形式\n");
  printf("q.退出程序\n");
  scanf("%c",&j);
  getchar();
  switch(j)
  {
  case '0':
   printf("生成二叉树:");
   CreateBiTree(&T);
   printf("\n");
   printf("\n");
   break;
  case '1':
   if(T)
   {
    printf("此二叉树的深度为:");
    printf("%d",Depth(T));
    printf("\n");
    printf("\n");
   }
   else printf("二叉树为空!\n");
   break;
  case '2':
   if(T)
   {
    printf("递归先序遍历二叉树:");
    Preorder(T);
    printf("\n");
    printf("\n");
   }
   else
    printf("二叉树为空!\n");
   break;
        case '3':
   if(T)
   {
    printf("非递归先序遍历二叉树:");
    NPreorder(T);
    printf("\n");
    printf("\n");
   }
   else
    printf("二叉树为空!\n");
   break;
  case '4':
   if(T)
   {
    printf("递归中序遍历二叉树:");
    Inorder(T);
    printf("\n");
    printf("\n");
   }
   else printf("二叉树为空!\n");
   break;
  case '5':
   if(T)
   {
    printf("非递归中序遍历二叉树:");
    NInorder(T);
    printf("\n");
    printf("\n");
   }
   else printf("二叉树为空!\n");
   break;
  case '6':
   if(T)
   {
    printf("递归后序遍历二叉树:");
    Postorder(T);
    printf("\n");
    printf("\n");
   }
   else printf("二叉树为空!\n");
   break;
  case '7':
   if(T)
   {
    printf("非递归后序遍历二叉树:");
    NPostorder(T);
    printf("\n");
    printf("\n");
   }
   else printf("二叉树为空!\n");
   break;
        case '8':
   if(T)
   {
    printf("层次遍历二叉树:");
    Translever(T);
    printf("\n");
    printf("\n");
   }
   else printf("二叉树为空!\n");
   break;
  case '9':
   if(T)
   {
    printf("输出二叉树:");
    Disptree(T);
    printf("\n");
    printf("\n");
   }
   else printf("二叉树为空!\n");
   break;
  default:
   sign=0;
   printf("程序运行结束,按任意键退出!\n");
  }
 }
}

展开全文  
收起全文