两个复数的加减运算

  1. 2014软件技术1班
  2. 作    者:A19秦嘉琪   
  3.  完成日期:2014年 12 月 14日  
  4.  问题描述:两个复数的加减运算
  5. 输入描述:m,n,x,y四个数 
  6. 程序输出:两个复数的运算值


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication31
{
    class Program
    {
        static void Main(string[] args)
        {
      
            double m, n, x, y;




            Console.WriteLine("第1个数的实数部分");
           m = Convert.ToDouble(Console.ReadLine());
           
          
            Console.WriteLine("第2个数的实数部分");
           n = Convert.ToDouble(Console.ReadLine());
           
            Console.WriteLine("第1个数的虚数部分");
           x = Convert.ToDouble(Console.ReadLine());
           
            Console.WriteLine("第2个数的虚数部分");
           y = Convert.ToDouble(Console.ReadLine());
           
            Complex num = new Complex();
            num.m = 1;


            num.n = 3;


            num.x = 2;


            num.y = 4;


            Console.WriteLine("复数相减等于{0}+{1}", num.SubStract(), num.substract());
            Console.WriteLine("复数相加等于{0}+{1}", num.Add(), num.add());
            
            Console.Read();
        }


    }
    class Complex
    {
        public double m, n, x, y;
       


        public double SubStract()
        {
            return m - n;
        }
       
        public string substract()
        {
            return (x - y) + "i";
        }
        
        public double Add()
        {
            return m + n;
        }
       
        public string add()
        {
            return (x + y ) + "i";
        }
        
    }
}



小结:将实数的运算进一步扩展为复数运算


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,复数可以使用结构体或者类来表示。以下是使用结构体实现复数加减运算的示例代码: ```c++ #include <iostream> using namespace std; struct Complex { double real; double imag; }; Complex add(Complex c1, Complex c2) { Complex result; result.real = c1.real + c2.real; result.imag = c1.imag + c2.imag; return result; } Complex sub(Complex c1, Complex c2) { Complex result; result.real = c1.real - c2.real; result.imag = c1.imag - c2.imag; return result; } int main() { Complex c1, c2, res; cout << "Enter the real and imaginary parts of first complex number: "; cin >> c1.real >> c1.imag; cout << "Enter the real and imaginary parts of second complex number: "; cin >> c2.real >> c2.imag; res = add(c1, c2); cout << "Addition: " << res.real << " + " << res.imag << "i" << endl; res = sub(c1, c2); cout << "Subtraction: " << res.real << " + " << res.imag << "i" << endl; return 0; } ``` 在上面的代码中,我们定义了一个复数结构体`Complex`,并实现了两个函数`add`和`sub`,分别用于计算复数的加法和减法。在`main`函数中,我们通过用户输入两个复数,调用`add`和`sub`函数进行计算,并输出结果。 另外,如果使用类来实现复数,可以将加法和减法运算符重载,使得代码更加简洁易读。以下是使用类实现复数加减运算的示例代码: ```c++ #include <iostream> using namespace std; class Complex { public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} Complex operator+(Complex c2) { return Complex(real + c2.real, imag + c2.imag); } Complex operator-(Complex c2) { return Complex(real - c2.real, imag - c2.imag); } friend ostream& operator<<(ostream& os, Complex c) { os << c.real << " + " << c.imag << "i"; return os; } private: double real; double imag; }; int main() { Complex c1, c2, res; cout << "Enter the real and imaginary parts of first complex number: "; cin >> c1; cout << "Enter the real and imaginary parts of second complex number: "; cin >> c2; res = c1 + c2; cout << "Addition: " << res << endl; res = c1 - c2; cout << "Subtraction: " << res << endl; return 0; } ``` 在上面的代码中,我们定义了一个复数类`Complex`,其中重载了加法和减法运算符,并定义了一个友元函数重载输出运算符。在`main`函数中,我们通过用户输入两个复数,直接使用重载的运算符进行计算,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值