完善一个初学者的程序 1.实现add()方法,将两个complex对象相加后存在一个complex对象中。//这条起码不符合要求你的add()方法只实现了两个int型数据相加的功能,并不是两个complex对象相加 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 add()方法是一个运算符重载,楼主装初学而已。 经过重新了解我做了修改但好像SUM有错大家帮我看看还有就是怎么做到第五点。大家快帮帮忙呀!!!using System; public struct Complex { public int real; public int imaginary; public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } public static Complex operator +(Complex c1, Complex c2) { return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary); } public override string ToString() { return(String.Format( "{0} + {1}i ", real, imaginary)); } public static void Main() { Complex c1 = new Complex(); Complex c2 = new Complex(); Complex sum = c1 + c2; Console.WriteLine ("please enter the real1:"); c1.real=Int32.Parse (Console.ReadLine ()); Console.WriteLine( "Real1: {0} ",c1.real); Console.WriteLine ("please enter the imaginary1:"); c1.imaginary=Int32.Parse (Console.ReadLine ()); Console.WriteLine( "Imaginary1: {0}i ",c1.imaginary); Console.WriteLine( "First complex number: {0} ",c1); Console.WriteLine ("please enter the real2:"); c2.real=Int32.Parse (Console.ReadLine ()); Console.WriteLine( "Real2: {0} ",c2.real); Console.WriteLine ("please enter the imaginary2:"); c2.imaginary=Int32.Parse (Console.ReadLine ()); Console.WriteLine( "Imaginary2: {0}i ",c2.imaginary); Console.WriteLine( "Second complex number: {0} ",c2); Console.WriteLine( "The sum of the two numbers: {0} ",sum); } } using System; public class Complex { public int real; public int imaginary; public Complex() { real = 1; imaginary = 1; } public Complex(int real, int imaginary) { this.real = real; this.imaginary = imaginary; } public static Complex operator +(Complex c1, Complex c2) { return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary); } public override string ToString() { string outPut; outPut = (real!=0 ? (real.ToString() + " "+ (imaginary != 0?((imaginary > 0?("+ " + (imaginary!=1?(imaginary.ToString()):"")):("- " + (imaginary!=-1?((-1) * imaginary).ToString():"") )) + "i"):"")) : ((imaginary != 0?((imaginary > 0?((imaginary!=1?(imaginary.ToString()):"")):("- " + (imaginary!=-1?((-1) * imaginary).ToString():"" ))) + "i"):""))); if(outPut.Trim() == "") { outPut = "NULL"; } return outPut; } public static void Main() { Complex c1 = new Complex(); Complex c2 = new Complex(); Console.WriteLine ("please enter the real1:"); c1.real=Int32.Parse (Console.ReadLine ()); Console.WriteLine( "Real1: {0} ",c1.real); Console.WriteLine ("please enter the imaginary1:"); c1.imaginary=Int32.Parse (Console.ReadLine ()); Console.WriteLine( "Imaginary1: {0}i ",c1.imaginary); Console.WriteLine( "First complex number: {0} ",c1); Console.WriteLine ("please enter the real2:"); c2.real=Int32.Parse (Console.ReadLine ()); Console.WriteLine( "Real2: {0} ",c2.real); Console.WriteLine ("please enter the imaginary2:"); c2.imaginary=Int32.Parse (Console.ReadLine ()); Console.WriteLine( "Imaginary2: {0}i ",c2.imaginary); Console.WriteLine( "Second complex number: {0} ",c2); Complex sum = c1 + c2; Console.WriteLine( "The sum of the two numbers: {0} ",sum); Console.ReadLine(); } } 嘿嘿,对括号把眼睛都对花,kao 这个ToString重载,真是厉害,活活 求解决C#中listview高亮显示鼠标停留行时候的重绘问题 WPF DispatcherTimer经典异步问题 C# 做个简单的劲舞团 恳请各位朋友给推荐本C#入门书籍 【在线观望】if----else if----else 的小小问题 如何在load时打开一网站页面 关于连接数据,判断数据问题。 c#里面如何定义一个带单个双引号的字符串,注意是单个双引号,而且在字符串尾部 winform:很奇怪的问题,高手来看这是什么原因 eval http://www.netyi.net/in.asp?id=lglchf :大家帮助大家 请问 关于 c# 的文件操作~~~~~~急
using System;
public struct Complex
{
public int real;
public int imaginary;
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public override string ToString()
{
return(String.Format( "{0} + {1}i ", real, imaginary));
}
public static void Main()
{
Complex c1 = new Complex();
Complex c2 = new Complex();
Complex sum = c1 + c2;
Console.WriteLine ("please enter the real1:");
c1.real=Int32.Parse (Console.ReadLine ());
Console.WriteLine( "Real1: {0} ",c1.real); Console.WriteLine ("please enter the imaginary1:");
c1.imaginary=Int32.Parse (Console.ReadLine ());
Console.WriteLine( "Imaginary1: {0}i ",c1.imaginary); Console.WriteLine( "First complex number: {0} ",c1); Console.WriteLine ("please enter the real2:");
c2.real=Int32.Parse (Console.ReadLine ());
Console.WriteLine( "Real2: {0} ",c2.real);
Console.WriteLine ("please enter the imaginary2:");
c2.imaginary=Int32.Parse (Console.ReadLine ());
Console.WriteLine( "Imaginary2: {0}i ",c2.imaginary);
Console.WriteLine( "Second complex number: {0} ",c2);
Console.WriteLine( "The sum of the two numbers: {0} ",sum);
}
}
public class Complex
{
public int real;
public int imaginary;
public Complex()
{
real = 1;
imaginary = 1;
}
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public override string ToString()
{
string outPut; outPut = (real!=0 ?
(real.ToString() + " "+ (imaginary != 0?((imaginary > 0?("+ " + (imaginary!=1?(imaginary.ToString()):"")):("- " + (imaginary!=-1?((-1) * imaginary).ToString():"") )) + "i"):""))
: ((imaginary != 0?((imaginary > 0?((imaginary!=1?(imaginary.ToString()):"")):("- " + (imaginary!=-1?((-1) * imaginary).ToString():"" ))) + "i"):"")));
if(outPut.Trim() == "")
{
outPut = "NULL";
}
return outPut;
}
public static void Main()
{
Complex c1 = new Complex();
Complex c2 = new Complex();
Console.WriteLine ("please enter the real1:");
c1.real=Int32.Parse (Console.ReadLine ());
Console.WriteLine( "Real1: {0} ",c1.real); Console.WriteLine ("please enter the imaginary1:");
c1.imaginary=Int32.Parse (Console.ReadLine ());
Console.WriteLine( "Imaginary1: {0}i ",c1.imaginary); Console.WriteLine( "First complex number: {0} ",c1); Console.WriteLine ("please enter the real2:");
c2.real=Int32.Parse (Console.ReadLine ());
Console.WriteLine( "Real2: {0} ",c2.real);
Console.WriteLine ("please enter the imaginary2:");
c2.imaginary=Int32.Parse (Console.ReadLine ());
Console.WriteLine( "Imaginary2: {0}i ",c2.imaginary);
Console.WriteLine( "Second complex number: {0} ",c2);
Complex sum = c1 + c2;
Console.WriteLine( "The sum of the two numbers: {0} ",sum);
Console.ReadLine();
}
}
嘿嘿,对括号把眼睛都对花,kao