设为首页 加入收藏

TOP

3.2.5 编程题
2013-10-07 16:20:16 来源: 作者: 【 】 浏览:69
Tags:3.2.5 编程

3.2.5  编程(www.cppentry.com)题

【例3.19】设计一个类Rect,要求有下列成员函数。

(1)Move():从一个位置移动到另一个位置。

(2)Size():改变矩形的大小。

(3)Where():返回矩形左上角的坐标值。

(4)Area():计算矩形面积。

解:由于Where()成员函数返回矩形左上角的坐标值,即两个参数,所以采用引用类型。本题程序如下:
 

  1. #include <iostream.h> 
  2. class Rect  
  3. {   int x,y;            //左上角坐标  
  4.     int w,h;            //宽度和高度  
  5. public:  
  6.     Rect(int x1,int y1,int w1,int h1)  
  7.     {   x=x1y=y1;  
  8.         w=w1h=h1;  
  9.     }  
  10.     void Move(int x1,int y1) {   x=x1;y=y1; }  
  11.     void Size(int w1,int h1) {   w=w1;h=h1; }  
  12.     void Where(int &x1,int &y1) {   xx1=x;yy1=y; }  
  13.     int Area() { return w*h; }  
  14. };  
  15. void main()  
  16. {   Rect r(2,3,20,10);  
  17.     int x,y;  
  18.     cout << "矩形面积:" << r.Area() << endl;  
  19.     cout << "移动到(5,4)" << endl;  
  20.     r.Move(5,4);  
  21.     cout << "改变宽为6,高为3" << endl;  
  22.     r.Size(6,3);  
  23.     r.Where(x,y);  
  24.     cout << "左上角:(" << x << "," << y << ")" << endl;  
  25.     cout << "矩形面积:"<< r.Area()<<endl;  
  26. }  

【例3.20】编写一个程序,有单链表的节点类型如下:

  1. typedef struct node  
  2. {   int no;  
  3.     struct node *next;  
  4. } Node;  

设计如下函数。

void create(Node *h):建立不带头节点的单链表h。

int len(Node *h):返回不带头节点的单链表h的长度。

void del(Node *h,int i):删除不带头节点的单链表h的第i个节点。

void disp(Node *h):输出不带头节点的单链表h的所有节点值。

解:程序如下:

  1. #include <iostream.h> 
  2. #include <malloc.h> 
  3. typedef struct node         //单链表节点类型  
  4. {   int no;  
  5.     struct node *next;  
  6. } Node;  
  7. void create(Node *&h)   //建立一个链表,h为链表的开始结点指针,一定要使用引用类型  
  8. {   Node *s,*r;  
  9.     int n;  
  10.     h=NULL;  
  11.     cout << "数序(以负数结束):";  
  12.     cin >> n;  
  13.     while (n>0)  
  14.     {   s=(Node *)malloc(sizeof(Node));  
  15.         s->nno=n;s->next=NULL;  
  16.         if (h==NULL)  
  17.         {   h=s;  
  18.             r=h;                //r指向链表的最后一个节点  
  19.         }  
  20.         else  
  21.         {   r->next=s;          //*s插入最后  
  22.             r=s;                //r指向链表的最后一个节点  
  23.         }  
  24.         cin >> n;  
  25.     }  
  26. }  
  27. int len(const Node *h)      //求链表长度  
  28. {   int i=0;  
  29.     while (h!=NULL)  
  30.     {   i++;  
  31.         hh=h->next;  
  32.     }  
  33.     return i;  
  34. }  
  35. void del(Node *&h,int i)    //删除单链表h的第i个节点  
  36. {   Node *p=h,*q;  
  37.     if (i<1 || i>len(h))    //i值错误  
  38.         return;  
  39.     if (i==1)               //删除第1个节点  
  40.     {   hh=h->next;  
  41.         free(p);  
  42.     }  
  43.     else                        //删除其他节点  
  44.     {   while (i-->2)  
  45.             pp=p->next;          //找到第i-1个节点*p  
  46.         q=p->next;          //q指向被删节点  
  47.         p->next=q->next;    //删除*q节点  
  48.         free(q);                //释放其空间  
  49.     }  
  50. }  
  51. void disp(const Node *h)    //显示单链表  
  52. {   cout << "链表:";  
  53.     while (h!=NULL)  
  54.     {   cout << h->no << " ";  
  55.         hh=h->next;  
  56.     }  
  57.     cout << endl;  
  58. }  
  59. void main()  
  60. {   Node *head;  
  61.     int i;  
  62.     create(head);  
  63.     disp(head);  
  64.     cout << "链表长度:" << len(head) << endl;  
  65.     cout << "删除第几个节点:";  
  66.     cin >> i;  
  67.     del(head,i);  
  68.     disp(head);  
  69. }  

应特别注意,del(Node *&h,int i)和create(Node *&h)这两个函数的h形参一定要用指针引用类型,否则,前者在i=1时程序运行出错,后者无法建立单链表。另外,有些函数可以使用常类型的形参,原因请读者思考。
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇3.2.4 简答题 下一篇4.1.1 友元函数

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·用 Python 进行数据 (2025-12-25 15:49:09)
·如何学习Python数据 (2025-12-25 15:49:07)
·利用Python进行数据 (2025-12-25 15:49:04)
·Java 学习线路图是怎 (2025-12-25 15:19:15)
·关于 Java 学习,有 (2025-12-25 15:19:12)