| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1702 人关注过本帖
标题:[求助]应该如何做?
只看楼主 加入收藏
chzh
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2006-10-24
收藏
得分:0 
不用數據庫連接的``
祇要簡單的控制台就行了```

為了要遇見妳``我連呼吸都反複練習`
2006-10-25 17:05
lw817810
Rank: 1
等 级:新手上路
帖 子:79
专家分:7
注 册:2006-4-4
收藏
得分:0 
该上课了,有空再写。

2006-10-25 17:11
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
复数的要用+,-重载吗?

2006-10-25 17:20
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication9
{
public class complex
{
double realPart=0;
double immaginaryPart=0;

public double RealPart
{
get
{
return realPart;
}
set
{

realPart = value;

}
}

public double ImmaginaryPart
{
get
{
return immaginaryPart;
}
set
{

immaginaryPart = value;

}
}

public complex(double r ,double i)
{
realPart = r;
immaginaryPart = i;
}

public complex()
{
realPart = 7;
immaginaryPart = 77;
}

public void add(complex ctemp)
{
double r;
double i;
r = this.realPart + ctemp.realPart;
i = this.immaginaryPart + ctemp.immaginaryPart;
Console.WriteLine("参加加法运算的两个复数为");
PrintComplex(ctemp);
PrintComplex(this);
complex c = new complex(r,i);
Console.WriteLine("加法结果为");
PrintComplex(c);
}

public void Sub(complex ctemp)
{
double r;
double i;
r = this.realPart -ctemp.realPart;
i = this.immaginaryPart - ctemp.immaginaryPart;
Console.WriteLine("参加减法运算的两个复数为");
Console.Write("被减数: ");
PrintComplex(this);
Console.Write("减数: ");
PrintComplex(ctemp);

complex c = new complex(r, i);
Console.WriteLine("减法结果为");
PrintComplex(c);
}

public void PrintComplex(complex ctemp)
{
Console.WriteLine(ctemp.realPart.ToString()+"+"+"("+ctemp.immaginaryPart.ToString()+")"+"i");
}

static void Main()
{
complex c = new complex(1.5,2.6);
complex c1 = new complex(3.6,4.6);
c.add(c1);
c.Sub(c1);
}
}


}

2006-10-25 17:44
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
运行结果:
参加加法运算的两个复数为
3.6+(4.6)i
1.5+(2.6)i
加法结果为
5.1+(7.2)i
参加减法运算的两个复数为
被减数: 1.5+(2.6)i
减数: 3.6+(4.6)i
减法结果为
-2.1+(-2)i

2006-10-25 17:44
jingzhao22visa
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2006-8-10
收藏
得分:0 
重载的忘记了。。。。。。。


2006-10-25 17:45
chzh
Rank: 1
等 级:新手上路
帖 子:127
专家分:0
注 册:2006-10-24
收藏
得分:0 
謝謝咯```呵呵``!

為了要遇見妳``我連呼吸都反複練習`
2006-10-25 19:13
shen2006
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2006-4-20
收藏
得分:0 

using System;

namespace 重载_复数加减
{
public struct Complex
{
public int realpart;
public int imaginarypart;
public Complex(int realpart, int imaginarypart)
{
this.realpart = realpart;
this.imaginarypart = imaginarypart;
}
// Declare which operator to overload (+), the types
// that can be added (two Complex objects), and the
// return type (Complex):
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.realpart + c2.realpart, c1.imaginarypart + c2.imaginarypart);
}
// Override the ToString method to display an complex number in the suitable format:
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex (c1.realpart - c2.realpart ,c1.imaginarypart - c2.imaginarypart );
}
public override string ToString()
{
return(String.Format("{0} + {1}i", realpart, imaginarypart));
}
public static void Main()
{
Complex num1 = new Complex(2,3);
Complex num2 = new Complex(3,4);
// Add two Complex objects (num1 and num2) through the
// overloaded plus operator:
Complex sum = num1 + num2;
Complex sub = num1 - num2;
// Print the numbers and the sum using the overriden ToString method:
Console.WriteLine("First complex number: {0}",num1);
Console.WriteLine("Second complex number: {0}",num2);
Console.WriteLine("The sum of the two numbers: {0}",sum);
if( sub.imaginarypart <0 )
{
Console.WriteLine("The sub of the two numbers: {0}{1}i",sub.realpart ,sub.imaginarypart );
}
else
Console.WriteLine("The sub of the two numbers: {0}",sub);
Console.ReadLine ();
}
}

}


2006-10-25 21:23
shen2006
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2006-4-20
收藏
得分:0 
可在这里面修改初值:
Complex num1 = new Complex(2,3);
Complex num2 = new Complex(3,4);

2006-10-25 21:25
skyland84
Rank: 2
等 级:新手上路
威 望:4
帖 子:544
专家分:0
注 册:2006-10-9
收藏
得分:0 
哈哈~!写 得这么快~!

决定人生~
2006-10-26 09:54
快速回复:[求助]应该如何做?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034325 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved