Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2571715
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: 项目管理

2012-09-30 14:07:44

/**
* @brief 这里重载了string类,调用了静态库!listring.a
*/

prototype.h
 

点击(此处)折叠或打开

  1. /*
  2.  * prototype.h
  3.  *
  4.  * Created on: Sep 24, 2012
  5.  * Author: hl
  6.  */

  7. #ifndef _PROTOTYPE_H_
  8. #define _PROTOTYPE_H_
  9. #include <string>
  10. using namespace std;

  11. class Mail;

  12. /**
  13.  * @brief 广告模板
  14.  */
  15. class AdvTemplate
  16. {
  17. public:
  18.     AdvTemplate();
  19.     AdvTemplate(string sub, string adv);
  20. public:
  21.     string getAdvSub();    /// < 获得广告主题
  22.     string getAdvMsg();    /// < 获得广告内容
  23. private:
  24.     string advSubject;    /// < 广告主题
  25.     string advContext;    /// < 广告内容
  26. };

  27. /**
  28.  * @brief 对象克隆
  29.  */
  30. class Clone
  31. {
  32. public:
  33.     virtual Mail * getClone() = 0;
  34. };

  35. /**
  36.  * 邮件类
  37.  */
  38. class Mail : public Clone
  39. {
  40. public:
  41.     Mail(AdvTemplate adv);    /// < 获取邮件主题和内容
  42. public:
  43.     void setRec(string rec);    /// < 设置收件人
  44.     void setAppe(string appe);    /// < 设置名称
  45.     void setTail(string tai);    /// < 设置结尾

  46.     string getSub();
  47.     string getContext();
  48.     string getRec();
  49.     string getAppe();
  50.     string getTail();

  51.     Mail * getClone();
  52. private:
  53.     string receiver;    /// < 接收者
  54.     string appellation;    /// < 名称
  55.     string tail;        /// < 结尾

  56.     string subject;        /// < 主题
  57.     string context;        /// < 内容
  58. };

  59. #endif /* _PROTOTYPE_H_ */
prototype.cpp

点击(此处)折叠或打开

  1. //============================================================================
  2. // Name : prototype.cpp
  3. // Author : hl
  4. // Version :
  5. // Copyright : Copyright (c) 2012 Tiros
  6. // Description : Prototype Model in C++, Ansi-style
  7. //============================================================================
  8. #include "prototype.h"
  9. #include "sendmail.h"
  10. #include "string.h"
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <iostream>
  14. #include <string.h>
  15. #include <stdio.h>
  16. using namespace std;

  17. #define random(x) (rand()%x + 123)

  18. AdvTemplate::AdvTemplate()
  19. {

  20. }
  21. AdvTemplate::AdvTemplate(string sub, string adv)
  22. {
  23.     advSubject = sub;
  24.     advContext = adv;
  25. }

  26. /**
  27.  * @brief 获得广告内容
  28.  */
  29. string AdvTemplate::getAdvMsg()
  30. {
  31.     return this->advContext;
  32. }

  33. /**
  34.  * @brief 获得广告主题
  35.  */
  36. string AdvTemplate::getAdvSub()
  37. {
  38.     return this->advSubject;
  39. }

  40. /**
  41.  * 邮件类
  42.  */
  43. Mail::Mail(AdvTemplate adv)
  44. {
  45.     subject = adv.getAdvSub();
  46.     context = adv.getAdvMsg();
  47. }

  48. void Mail::setRec(string rec)
  49. {
  50.     receiver = rec;
  51. }

  52. void Mail::setAppe(string appe)
  53. {
  54.     appellation = appe;
  55. }

  56. void Mail::setTail(string tai)
  57. {
  58.     tail = tai;
  59. }

  60. string Mail::getSub()
  61. {
  62.     return subject;

  63. }

  64. string Mail::getContext()
  65. {
  66.     return context;
  67. }

  68. string Mail::getRec()
  69. {
  70.     return receiver;
  71. }

  72. string Mail::getAppe()
  73. {
  74.     return appellation;
  75. }

  76. string Mail::getTail()
  77. {
  78.     return tail;
  79. }

  80. Mail * Mail::getClone()
  81. {
  82.     Mail * pMail = new Mail(*this);

  83.     return pMail;
  84. }

  85. /**
  86.  * @brief 整数转换为字符串
  87.  */
  88. char *myitoa(int num, char *str, int radix)
  89. {
  90.     /* 索引表 */
  91.     char index[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  92.     unsigned unum;
  93.     int i = 0, j, k;
  94.     if (radix == 10 && num < 0) /* 十进制负数 */
  95.     {
  96.         unum = (unsigned) -num;
  97.         str[i++] = '-';
  98.     }
  99.     else
  100.         unum = (unsigned) num; /* 其他情况 */
  101.     /* 逆序 */
  102.     do
  103.     {
  104.         str[i++] = index[unum % (unsigned) radix];
  105.         unum /= radix;
  106.     } while (unum);
  107.     str[i] = '\0';
  108.     /* 转换 */
  109.     if (str[0] == '-')
  110.         k = 1; /* 十进制负数 */
  111.     else
  112.         k = 0;
  113.     return str;
  114. }

  115. int main()
  116. {
  117.     /// < 创建邮件
  118.     Mail mal(*(new AdvTemplate("幸运大奖", "犬友头像征集活动开始了,欢迎到家踊跃报名!")));

  119.     char buf[128];
  120.     srand(time(0));
  121.     for (int i = 0; i < 5; i++)
  122.     {
  123.         memset(buf, 0, 128);
  124.         myitoa(random(123456), buf, 10);

  125.         /// < 自己封装了一个String函数,这样省去了以上的字符串操作!不过需要將string函数替换!不过我再修改
  126.         /// < 下库,提供一个获取字符串的函数!
  127.         String b = "@qq.com";
  128.         String d = buf;
  129.         String c = d + b;

  130.         char * abc = c.getStr();

  131.         Mail * pMail = mal.getClone();    /// < 得到clone版本,这样再执行多线程操作才不会影响结果!省去了我们自己控制线程pthread_create!
  132.         cout << "address1 ---------- "<< pMail << endl;    /// <
  133.         pMail->setRec(abc);
  134.         delete abc;
  135.         pMail->setAppe("xiaodou");
  136.         pMail->setTail("lego公司");

  137.         SendMail::sendMail(*pMail);
  138.         delete pMail;    /// < 执行了delete之后,可能每次分配的地址都相同!所以每次你看到address结果都可能相同!
  139.     }


  140.     return 0;
  141. }
sendmail.h

点击(此处)折叠或打开

  1. /*
  2.  * sendmail.h
  3.  *
  4.  * Created on: Sep 24, 2012
  5.  * Author: hl
  6.  */

  7. #ifndef _SENDMAIL_H_
  8. #define _SENDMAIL_H_
  9. #include "prototype.h"
  10. /**
  11.  * @brief 写信的过程 - 写信的内容、把信放到信封、把信送到邮局、邮局送信
  12.  */
  13. class SendMail
  14. {
  15. public:
  16.     static void sendMail(Mail mal);
  17. };

  18. #endif /* _SENDMAIL_H_ */
sendmail.cpp

点击(此处)折叠或打开

  1. /*
  2.  * sendmail.cpp
  3.  *
  4.  * Created on: Sep 24, 2012
  5.  * Author: hl
  6.  */
  7. #include "sendmail.h"
  8. #include <iostream>
  9. using namespace std;

  10. void SendMail::sendMail(Mail mal)
  11. {
  12.     cout << "姓名 " << mal.getAppe() << "\n内容 " << mal.getContext() << "\n收信人 " << mal.getRec()
  13.             << "\n主题 " << mal.getSub() << "\n结尾 " << mal.getTail() << endl;
  14. }
string.h

点击(此处)折叠或打开

  1. /*
  2.  * string.h
  3.  *
  4.  * Created on: Sep 24, 2012
  5.  * Author: hl
  6.  */

  7. #ifndef _STRING_H_
  8. #define _STRING_H_

  9. /**
  10.  * @brief 字符串操作符重载 - 实现+
  11.  * 注意:
  12.  *     s大小128,针对具体的硬件平台可适当修改!
  13.  * 使用示例:
  14.  *     String a = "hello";
  15.  *     String b = "kitty";
  16.  *     String c;
  17.  *     c = a + b + " " + "$" + f + ".com";
  18.  */
  19. class String
  20. {
  21. public:
  22.     String();
  23.     String(char * str);
  24. public:
  25.     char * getStr();
  26.     void display();
  27.     String operator+(String b);
  28. private:
  29.     char s[128];
  30. };

  31. #endif /* _STRING_H_ */

阅读(982) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~