楼主: fantuanxiaot
18358 95

[编程语言] [VC++原创]一个基于MFC(Visual C++)的复数计算器 [推广有奖]

回帖奖励 36 个论坛币 回复本帖可获得 3 个论坛币奖励! 每人限 3 次

Ψ▄┳一大卫卍卐席尔瓦

大师

9%

还不是VIP/贵宾

-

威望
7
论坛币
-234475 个
通用积分
124.1424
学术水平
3783 点
热心指数
3819 点
信用等级
3454 点
经验
150417 点
帖子
7616
精华
32
在线时间
1327 小时
注册时间
2013-2-3
最后登录
2022-2-24

初级学术勋章 初级热心勋章 中级热心勋章 中级学术勋章 初级信用勋章 中级信用勋章 高级热心勋章 高级学术勋章 特级学术勋章 特级热心勋章 高级信用勋章 特级信用勋章

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币


用MFC制作了一个复数计算器

    CComplex类
  1. //  复数的头文件


  2. #ifndef _COMPLEX_H_
  3. #define _COMPLEX_H_


  4. #if _MSC_VER>1000
  5. #pragma once
  6. #endif

  7. //  定义一个复数的类
  8. class CComplex
  9. {
  10. protected:

  11.         double m_dblX;
  12.         double m_dblY;

  13.         //  公共的接口函数
  14. public:
  15.         //  无参数构造函数
  16.         CComplex();


  17.         //  两个参数的构造函数
  18.         CComplex(double dblX , double dblY);

  19.         //  复制构造函数
  20.         CComplex(const CComplex &other);

  21.         //  析构函数
  22.         virtual ~CComplex(){};

  23.         //  输入与显示
  24.         void SetReal(double dblX);
  25.         void SetImag(double dblY);
  26.         double GetReal();
  27.         double GetImag();

  28.         //  将字符串转换为复数
  29.         CString ToString() const;
  30.         void FromString(CString s , const CString& sDelim = _T(" "));

  31.         //  各类的数学运算
  32.         BOOL operator==(const CComplex& cpxX) const;
  33.         BOOL operator!=(const CComplex& cpxX) const;
  34.         CComplex& operator=(const CComplex& cpxX);
  35.         //  复数的模
  36.         double Abs() const;
  37.         CComplex operator+(const CComplex& cpxX) const;
  38.         CComplex operator-(const CComplex& cpxX) const;
  39.         CComplex operator*(const CComplex& cpxX) const;
  40.         CComplex operator/(const CComplex& cpxX) const;

  41.         //  以下是一些函数的运算
  42.         //  n可能是CComplex的向量个数
  43.         void Root(CComplex cpxR[]) const;
  44.         //  实幂指数
  45.         CComplex Pow(double) const;
  46.         //  复幂指数
  47.         CComplex Pow(CComplex,int) const;
  48.         CComplex Log() const;
  49.         CComplex Sin() const;
  50.         CComplex Cos() const;
  51.         CComplex Tan() const;

  52. };

  53. #endif
复制代码

构造、析构、算法等函数:
  1. #include "stdafx.h"
  2. //  所有的文件都需要在"Complex.h"之前
  3. #include "Complex.h"
  4. #include <cmath>

  5. //  为了在debug中检查内存是否泄露
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif

  11. #define PI 3.141592653

  12. //  下面定义相应的函数
  13. CComplex::CComplex()
  14. {
  15.         m_dblX = 0.0;
  16.         m_dblY = 0.0;
  17. }

  18. CComplex::CComplex(double dblX , double dblY)
  19. {
  20.         m_dblX = dblX;
  21.         m_dblY = dblY;
  22. }

  23. //  拷贝构造函数
  24. CComplex::CComplex(const CComplex& other)
  25. {
  26.         m_dblX = other.m_dblX;
  27.         m_dblY = other.m_dblY;
  28. }

  29. //  输入和显示
  30. void CComplex::SetReal(double a)
  31. {
  32.         m_dblX = a;
  33. }

  34. void CComplex::SetImag(double b)
  35. {
  36.         m_dblY = b;
  37. }

  38. double CComplex::GetReal()
  39. {
  40.         return m_dblX;
  41. }

  42. double CComplex::GetImag()
  43. {
  44.         return m_dblY;
  45. }

  46. //  字符串的转换函数
  47. CString CComplex::ToString() const
  48. {
  49.         CString s;

  50.         if(fabs(m_dblX)>1e-10)
  51.         {
  52.                 if(m_dblY>0.0)
  53.                 {
  54.                         s.Format(_T("%.3f + %.3fj"),m_dblX,m_dblY);
  55.                 } else if (m_dblY<0.0)
  56.                 {
  57.                         s.Format(_T("%.3f - %.3fj"),m_dblX,fabs(m_dblY));
  58.                 } else
  59.                 {
  60.                         s.Format(_T("%.3f"),m_dblX);
  61.                 }
  62.         } else
  63.                 //  判断小于0的情况
  64.         {
  65.                 if(m_dblY>0.0)
  66.                 {
  67.                         s.Format(_T("%.3fj"),m_dblY);
  68.                 } else if (m_dblY<0.0)
  69.                 {
  70.                         s.Format(_T("-%.3f"),fabs(m_dblY));
  71.                 } else
  72.                 {
  73.                         s.Format(_T("%.3fj"),m_dblX);
  74.                 }
  75.         }

  76.         return s;
  77. }

  78. void CComplex::FromString(CString s , const CString& sDelim)
  79. {
  80.         int nPos = s.Find(sDelim);

  81.         if (nPos == -1)
  82.         {
  83.                 s.TrimLeft();
  84.                 s.TrimRight();
  85.                 m_dblX = _wtof(s);
  86.                 m_dblY = 0;
  87.         } else
  88.         {
  89.                 int len = s.GetLength();
  90.                 CString sLeft = s.Left(len);
  91.                 CString sRight = s.Right(nPos - len - 1);

  92.                 sLeft.TrimLeft();
  93.                 sRight.TrimRight();

  94.                 m_dblX = _wtof(sLeft);
  95.                 m_dblY = _wtof(sRight);
  96.         }

  97. }

  98. //  相应的重载运算符
  99. BOOL CComplex::operator==(const CComplex& cpxX) const
  100. {
  101.         return(m_dblX == cpxX.m_dblX && m_dblY == cpxX.m_dblY);
  102. }

  103. BOOL CComplex::operator!=(const CComplex& cpxX) const
  104. {
  105.         return(m_dblX != cpxX.m_dblX || m_dblY != cpxX.m_dblY);
  106. }

  107. CComplex& CComplex::operator=(const CComplex& cpxX)
  108. {
  109.         m_dblX = cpxX.m_dblX;
  110.         m_dblY = cpxX.m_dblY;
  111.         return *this;
  112. }

  113. CComplex CComplex::operator+(const CComplex& cpxX) const
  114. {
  115.         double x = m_dblX + cpxX.m_dblX;
  116.         double y = m_dblY + cpxX.m_dblY;

  117.         return CComplex(x,y);
  118. }

  119. CComplex CComplex::operator-(const CComplex& cpxX) const
  120. {
  121.         double x = m_dblX - cpxX.m_dblX;
  122.         double y = m_dblY - cpxX.m_dblY;

  123.         return CComplex(x,y);
  124. }

  125. //  乘法
  126. CComplex CComplex::operator*(const CComplex& cpxX) const
  127. {
  128.         double a = m_dblX;
  129.         double b = m_dblY;
  130.         double c = cpxX.m_dblX;
  131.         double d = cpxX.m_dblY;

  132.         return(CComplex(a*c - b*d,a*d+b*c));
  133. }

  134. //  除法
  135. CComplex CComplex::operator/(const CComplex& cpxX) const
  136. {
  137.         double a = m_dblX;
  138.         double b = m_dblY;
  139.         double c = cpxX.m_dblX;
  140.         double d = cpxX.m_dblY;

  141.         double x,y;
  142.         x = (a*c + b*d)/(pow(c,2)+pow(d,2));
  143.         y = (b*c - a*d)/(pow(c,2)+pow(d,2));

  144.         return CComplex(x,y);

  145. }

  146. //  复数的模
  147. double CComplex::Abs() const
  148. {
  149.         double a,b,c;
  150.         a = pow(m_dblX,2);
  151.         b = pow(m_dblY,2);
  152.         c = sqrt(a+b);

  153.         return c;
  154. }

  155. //  复数的根
  156. void CComplex::Root(CComplex cpxR[]) const
  157. {

  158.         double q = atan2(m_dblX , m_dblY);
  159.         double r = sqrt(pow(m_dblX,2) + pow(m_dblY,2));

  160.         r = pow(r,0.5);
  161.         double t = 0.0;

  162.         for(int k = 0;k<2;k++)
  163.         {
  164.                 t = (2.0*k*PI + q)/2;
  165.                 cpxR[k].m_dblX = r*cos(t);
  166.                 cpxR[k].m_dblY = r*sin(t);
  167.         }

  168. }

  169. //  实幂指数
  170. CComplex CComplex::Pow(double dblW) const
  171. {
  172.         // 局部变量
  173.         double r, t;
  174.    
  175.     // 特殊值处理
  176.     if ((m_dblX == 0) && (m_dblY == 0))
  177.                 return CComplex(0, 0);
  178.    
  179.     // 幂运算公式中的三角函数运算
  180.     if (m_dblX == 0)
  181.         {
  182.         if (m_dblY > 0)
  183.             t = 1.5707963268;
  184.         else
  185.             t = -1.5707963268;
  186.         }
  187.     else
  188.         {
  189.         if (m_dblX > 0)
  190.             t = atan2(m_dblY, m_dblX);
  191.         else
  192.         {
  193.                         if (m_dblY >= 0)
  194.                 t = atan2(m_dblY, m_dblX) + PI;
  195.             else
  196.                 t = atan2(m_dblY, m_dblX) - PI;
  197.                 }
  198.     }
  199.    
  200.     // 模的幂
  201.     r = exp(dblW * log(sqrt(m_dblX * m_dblX + m_dblY * m_dblY)));
  202.    
  203.     // 复数的实幂指数
  204.     return CComplex(r * cos(dblW * t), r * sin(dblW * t));
  205. }

  206. //  复幂指数
  207. CComplex CComplex::Pow(CComplex cpxW,int n = 0) const
  208. {
  209.         // 局部变量
  210.     double r, s, u, v;
  211.    
  212.     // 特殊值处理
  213.     if (m_dblX == 0)
  214.         {
  215.         if (m_dblY == 0)
  216.                         return CComplex(0, 0);
  217.             
  218.         s = 1.5707963268 * (fabs(m_dblY) / m_dblY + 4 * n);
  219.         }
  220.     else
  221.         {
  222.         s = 2 * PI * n + atan2(m_dblY, m_dblX);
  223.         
  224.         if (m_dblX < 0)
  225.                 {
  226.             if (m_dblY > 0)
  227.                 s = s + PI;
  228.             else
  229.                 s = s - PI;
  230.         }
  231.     }
  232.    
  233.     // 求幂运算公式
  234.     r = 0.5 * log(m_dblX * m_dblX + m_dblY * m_dblY);
  235.     v = cpxW.m_dblX * r + cpxW.m_dblY * s;
  236.     u = exp(cpxW.m_dblX * r - cpxW.m_dblY * s);

  237.     return CComplex(u * cos(v), u * sin(v));
  238. }

  239. //  求对数
  240. CComplex CComplex::Log() const
  241. {
  242.         double p = log(sqrt(pow(m_dblX,2.0) +pow(m_dblY,2.0)));
  243.         return CComplex(p,atan2(m_dblY,m_dblX));
  244. }

  245. //  求解正弦
  246. CComplex CComplex::Sin() const
  247. {
  248.     int i;
  249.     double x, y, y1, br, b1, b2, c[6];
  250.    
  251.     // 切比雪夫公式的常数系数
  252.     c[0] = 1.13031820798497;
  253.     c[1] = 0.04433684984866;
  254.     c[2] = 0.00054292631191;
  255.     c[3] = 0.00000319843646;
  256.     c[4] = 0.00000001103607;
  257.     c[5] = 0.00000000002498;
  258.    
  259.     y1 = exp(m_dblY);
  260.     x = 0.5 * (y1 + 1 / y1);
  261.     if (fabs(m_dblY) >= 1)
  262.         y = 0.5 * (y1 - 1 / y1);
  263.     else
  264.     {
  265.                 b1 = 0;
  266.         b2 = 0;
  267.         y1 = 2 * (2 * m_dblY * m_dblY - 1);
  268.         for (i = 5; i >=0; --i)
  269.                 {
  270.             br = y1 * b1 - b2 - c[i];
  271.             if (i != 0)
  272.                         {
  273.                 b2 = b1;
  274.                 b1 = br;
  275.             }
  276.         }
  277.         
  278.         y = m_dblY * (br - b1);
  279.     }
  280.    
  281.     // 组合计算结果
  282.     x = x * sin(m_dblX);
  283.     y = y * cos(m_dblX);

  284.         return CComplex(x, y);
  285. }

  286. //  计算余弦
  287. CComplex CComplex::Cos() const
  288. {
  289.     int i;
  290.     double x, y, y1, br, b1, b2, c[6];
  291.    
  292.     // 切比雪夫公式的常数系数
  293.     c[0] = 1.13031820798497;
  294.     c[1] = 0.04433684984866;
  295.     c[2] = 0.00054292631191;
  296.     c[3] = 0.00000319843646;
  297.     c[4] = 0.00000001103607;
  298.     c[5] = 0.00000000002498;
  299.    
  300.     y1 = exp(m_dblY);
  301.     x = 0.5 * (y1 + 1 / y1);
  302.     if (fabs(m_dblY) >= 1)
  303.         y = 0.5 * (y1 - 1 / y1);
  304.     else
  305.     {
  306.                 b1 = 0;
  307.         b2 = 0;
  308.         y1 = 2 * (2 * m_dblY * m_dblY - 1);
  309.         for (i=5 ; i>=0; --i)
  310.                 {
  311.             br = y1 * b1 - b2 - c[i];
  312.             if (i != 0)
  313.             {
  314.                                 b2 = b1;
  315.                 b1 = br;
  316.             }
  317.         }
  318.         
  319.         y = m_dblY * (br - b1);
  320.     }
  321.    
  322.     // 组合计算结果
  323.     x = x * cos(m_dblX);
  324.         y = -y * sin(m_dblX);

  325.         return CComplex(x, y);
  326. }

  327. //  计算正切算法
  328. CComplex CComplex::Tan() const
  329. {
  330.         //  类内调用
  331.         return Sin()/Cos();
  332. }
复制代码

    如图:

1.jpg



2.jpg


3.jpg



所有源码:


本帖隐藏的内容

ComplexCaculator.rar (1.94 MB)



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Visual UAL MFC 计算器 operator 计算器

已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 100 + 100 + 5 + 5 + 5 精彩帖子
niuniuyiwan + 100 + 100 + 5 + 5 + 5 精彩帖子

总评分: 经验 + 200  论坛币 + 200  学术水平 + 10  热心指数 + 10  信用等级 + 10   查看全部评分

本帖被以下文库推荐

沙发
BlackHawk123 在职认证  发表于 2015-10-5 08:28:03 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

好厉害,棒棒哒~~~
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 3   查看全部评分

使用道具

藤椅
niuniuyiwan 在职认证  发表于 2015-10-5 09:09:02 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

发帖不易,用心良苦,感谢fantuanxiaot师兄,感谢分享。
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 30 + 10 + 1 + 1 + 1 精彩帖子
fantuanxiaot + 50 + 2 + 2 + 2 精彩帖子

总评分: 经验 + 80  论坛币 + 10  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

使用道具

板凳
Crsky7 发表于 2015-10-5 11:19:19 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

MFC比C#有什么优势?
已有 2 人评分经验 论坛币 热心指数 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论
niuniuyiwan + 1 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 3  热心指数 + 1   查看全部评分

使用道具

报纸
Crsky7 发表于 2015-10-5 11:20:25 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

现在都是C#写的程序
已有 2 人评分经验 论坛币 热心指数 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论
niuniuyiwan + 5 + 1 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 8  热心指数 + 1   查看全部评分

使用道具

地板
Crsky7 发表于 2015-10-5 11:26:12 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

现在Windows平台被C#垄断,Android平台被Java垄断,iOS平台被Swift垄断。至于C++估计只能回到DOS平台吧,俗称“躲死”,真是死无葬身之地啊,未来一定是属于Swift的。
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 20 + 10 + 1 + 1 + 1 精彩帖子
niuniuyiwan + 5 + 1 观点有启发

总评分: 经验 + 20  论坛币 + 15  学术水平 + 2  热心指数 + 1  信用等级 + 1   查看全部评分

使用道具

7
fantuanxiaot 发表于 2015-10-5 13:58:45 |只看作者 |坛友微信交流群
Crsky7 发表于 2015-10-5 11:26
现在Windows平台被C#垄断,Android平台被Java垄断,iOS平台被Swift垄断。至于C++估计只能回到DOS平台吧,俗 ...
确实 C#比C++更方便  不受各类指针的困扰
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 10 + 10 + 1 + 1 + 1 精彩帖子
niuniuyiwan + 10 + 1 观点有启发

总评分: 经验 + 10  论坛币 + 20  学术水平 + 1  热心指数 + 2  信用等级 + 1   查看全部评分

使用道具

8
fantuanxiaot 发表于 2015-10-5 13:59:21 |只看作者 |坛友微信交流群
Crsky7 发表于 2015-10-5 11:19
MFC比C#有什么优势?
mfc是c++做开发用的
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
xddlovejiao1314 + 10 + 10 + 1 + 1 + 1 精彩帖子
niuniuyiwan + 10 + 1 精彩帖子

总评分: 经验 + 10  论坛币 + 20  学术水平 + 2  热心指数 + 1  信用等级 + 1   查看全部评分

使用道具

9
ydb8848 发表于 2015-10-9 18:43:11 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

使用道具

10
ydb8848 发表于 2015-10-9 18:59:42 |只看作者 |坛友微信交流群

回帖奖励 +3 个论坛币

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 09:35