用C#实现复数运算

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

namespace Sample_01_CA
{
    public class Complex
    {
        //定义复试的实部和虚部
        private int integer;
        private int fraction;
        //构造函数
        public Complex(int integer,int fraction)
        {
            this.integer = integer;
            this.fraction = fraction;
        }
        //重载运算符
        public static Complex operator +(Complex left,Complex right)
        {
            return new Complex(left.integer + right.integer, left.fraction + right.fraction);
        }
        //重写从Object继承的ToString()方法
        public override string ToString()
        {
            return integer.ToString()+"."+fraction.ToString();
        }
        static void Main(string[] args)
        {
            Complex left = new Complex(2008, 10);
            Complex right = new Complex(100, 1);
            Console.WriteLine(left.ToString() + " + " + right.ToString() + " = " + (left + right).ToString());
            Console.Read();
        }
    };
}

时间: 2024-08-28 22:24:56

用C#实现复数运算的相关文章

一些复数运算的C语言实现

很久不写博客了.第一次写博客是在04年,最近的一次还是在大学时,在学校时,甚至还有过自己去买虚拟主机搭WordPress写博客的经历.现在工作时间越长,越发现积累的重要性.那么就从这里开始吧,重新开始写博客. 最近打算写小算法,里面需要用到一些复数运算.贴一点复数运算的C语言实现代码.都是些很简单的东西. 包括以下运算: 复数加法.复数减法.复数乘法.复数除法.复数取模.复指数运算.复数取相角.模与相角合成复位.本人专业本职做硬件的,写程序没受过专业训练,勿吐槽. 1 /*file Comple

C语言中复数运算及调用blas,lapack中复数函数进行科学计算

C语言中常用的数据类型主要int, float ,double ,char 等,但在科学运算中复数扮演着重要角色.这里讲下C语言中的复数运算以及如何调用blas,lapack库中的复数函数来进行科学计算. 1.C语言中的复数运算. C语言中若要用的复数,需要包含头文件complex.h,下面看看一些基本的例子 #include <stdio.h> #include"complex.h" int main() { complex a, b, c, d, f; a = 1 +

C++复数运算 重载

近期整理下很久前写的程序,这里就把它放在博文中了,有些比较简单,但是很有学习价值. 下面就是自己很久前实现的复数重载代码,这里没有考虑特殊情况,像除法中,分母不为零情况. #include <iostream> /* #include <conio.h> #include<stdio.h> #include<iomanip> #include<string> #include<sstream> */ using namespace s

复数运算

#include<iostream> #include<cmath> using namespace std; struct node { int shi; int xu; int data; }; int main() { int shinum=0; int xunum=0; int n; cin>>n; node*num=new node[n]; char*a=new char[n-1]; for(int i=0;i<n-1;i++) { cin>>

Python 基础学习之: Python math 模块、cmath 模块 区别是 cmath 模块运算的是复数,math 模块运算的是数学运算 Python数学函数列表及解释 Python math 模块提供了许多对浮点数的数学运算函数。 Python cmath 模块包含了一些用于复数运算的函数

Python math 模块.cmath 模块 Python 中数学运算常用的函数基本都在 math 模块.cmath 模块中. Python math 模块提供了许多对浮点数的数学运算函数. Python cmath 模块包含了一些用于复数运算的函数. cmath 模块的函数跟 math 模块函数基本一致,区别是 cmath 模块运算的是复数,math 模块运算的是数学运算. 要使用 math 或 cmath 函数必须先导入: import math 查看 math 查看包中的内容: impo

c++实现复数运算(运算符重载)

#include <iostream> using namespace std; class Complex { public: //构造函数 Complex(int real=2,int image=4) :_real(real) ,_image(image) { cout<<"构造函数"<<endl; } //拷贝构造函数 Complex(Complex& c) { cout<<"拷贝构造函数"<&l

通过运算符重载实现复数运算

#include<iostream> using namespace std; class Complex { public: // 带缺省值的构造函数 Complex(double real = 0, double image = 0) { cout << "带缺省值的构造函数" << endl; _real = real; _image = image; } // 析构函数 ~Complex() { cout << "析构函

JavaScript实现以个复数类

<script type="text/javascript"> /** * 这里定义Complex类,用来描述复数 */ /** * 这个构造函数为它所创建的每个实例定义了实例字段r和i * 这两个字段分别保存复数的实部和虚部 * 他们是对象的状态 */ function Complex(real , imaginary){ if( isNaN( real ) || isNaN( imaginary )) //确保两个实参都是数字 throw new TypeError()

sdut 4-1 复数类的运算符重载

4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描述 通过本题目的练习可以掌握成员运算符重载及友元运算符重载 要求定义一个复数类,重载加法和减法运算符以适应对复数运算的要求,重载插入运算符(<<)以方便输出一个复数的要求. 输入 要求在主函数中创建对象时初始化对象的值. 输出 输出数据共有4行,分别代表a,b的值和它们求和.求差后的值 示例输入 无 示例输出 a=3.2+4.5i b=8.9+5.6i a+b=12.1+10.1i