用java定义一个复数类,能实现加减,获得实部和虚部,将复数转换成字符串这几个方法
2个回答
展开全部
楼上大哥那是C++啊。
这是我以前写的,可以实现加减
不过什么叫复数转换成字符串啊?
import java.io.*;
class complex{
//复数类
public float realPart;
public float imagPart;
}
public class Example extends complex{
public static String readString(){
//从键盘读取字符串
BufferedReader br=new BufferedReader(new InputStreamReader(System.in),1);
String s1="";
try{
s1=br.readLine();
}catch(IOException ex){
System.out.println(ex);
}
return s1;
}
public static float readFloat(){
//字符串转换为浮点数
return Float.parseFloat(readString());
}
public static char plus(float r1,float r2,float i1,float i2){
//实现复数相加并输出
complex sum=new complex();
sum.imagPart=i1+i2;
sum.realPart=r1+r2;
if(sum.imagPart<0){
//解决虚部为负时同时显示+ -号问题
sum.imagPart=-sum.imagPart;
System.out.println("两数相加="+sum.realPart+"-"+sum.imagPart+"i");
return 'f';
}
System.out.println("两数相加="+sum.realPart+"+"+sum.imagPart+"i");
return 'z';
}
public static char minus(float r1,float r2,float i1,float i2){
//实现复数相减并输出
complex cha=new complex();
cha.imagPart=i1-i2;
cha.realPart=r1-r2;
if(cha.imagPart<0){
//解决虚部为负时同时显示+ -号问题
cha.imagPart=-cha.imagPart;
System.out.println("两数相减="+cha.realPart+"-"+cha.imagPart+"i");
return 'f';
}
System.out.println("两数相减="+cha.realPart+"+"+cha.imagPart+"i");
return 'z';
}
public static void in(complex x){//实现从键盘输入的过程
System.out.print("\n实部=");
x.realPart=Example.readFloat();
System.out.print("虚部=");
x.imagPart=Example.readFloat();
}
public static void main(String[] args) {
//主方法
complex a=new complex();
complex b=new complex();
System.out.println("输入第一个复数...");
in(a);
System.out.println("\n输入第二个复数...");
in(b);
System.out.println("\n");
plus(a.realPart,b.realPart,a.imagPart,b.imagPart);
minus(a.realPart,b.realPart,a.imagPart,b.imagPart);
}
}
这是我以前写的,可以实现加减
不过什么叫复数转换成字符串啊?
import java.io.*;
class complex{
//复数类
public float realPart;
public float imagPart;
}
public class Example extends complex{
public static String readString(){
//从键盘读取字符串
BufferedReader br=new BufferedReader(new InputStreamReader(System.in),1);
String s1="";
try{
s1=br.readLine();
}catch(IOException ex){
System.out.println(ex);
}
return s1;
}
public static float readFloat(){
//字符串转换为浮点数
return Float.parseFloat(readString());
}
public static char plus(float r1,float r2,float i1,float i2){
//实现复数相加并输出
complex sum=new complex();
sum.imagPart=i1+i2;
sum.realPart=r1+r2;
if(sum.imagPart<0){
//解决虚部为负时同时显示+ -号问题
sum.imagPart=-sum.imagPart;
System.out.println("两数相加="+sum.realPart+"-"+sum.imagPart+"i");
return 'f';
}
System.out.println("两数相加="+sum.realPart+"+"+sum.imagPart+"i");
return 'z';
}
public static char minus(float r1,float r2,float i1,float i2){
//实现复数相减并输出
complex cha=new complex();
cha.imagPart=i1-i2;
cha.realPart=r1-r2;
if(cha.imagPart<0){
//解决虚部为负时同时显示+ -号问题
cha.imagPart=-cha.imagPart;
System.out.println("两数相减="+cha.realPart+"-"+cha.imagPart+"i");
return 'f';
}
System.out.println("两数相减="+cha.realPart+"+"+cha.imagPart+"i");
return 'z';
}
public static void in(complex x){//实现从键盘输入的过程
System.out.print("\n实部=");
x.realPart=Example.readFloat();
System.out.print("虚部=");
x.imagPart=Example.readFloat();
}
public static void main(String[] args) {
//主方法
complex a=new complex();
complex b=new complex();
System.out.println("输入第一个复数...");
in(a);
System.out.println("\n输入第二个复数...");
in(b);
System.out.println("\n");
plus(a.realPart,b.realPart,a.imagPart,b.imagPart);
minus(a.realPart,b.realPart,a.imagPart,b.imagPart);
}
}
展开全部
easy~~
#include<iostream>
#include<math.h>
using namespace std;
class Complex
{
private:
double real;
double image;
public:
Complex(double real,double image)
{
this-> real=real;
this->image=image;
}
float modulo ()
void display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
};
int main()
{
cout<<"enter the real and the image"<<endl;
double A,B;
cout<<"输入数的实部:";
cin>>A;
cout<<endl<<"输入数的虚部:";
cin>>B;
Complex C (A,B);
C.display();
cout<<"此复数的模为:"<<C.modulo();
}
#include<iostream>
#include<math.h>
using namespace std;
class Complex
{
private:
double real;
double image;
public:
Complex(double real,double image)
{
this-> real=real;
this->image=image;
}
float modulo ()
void display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
};
int main()
{
cout<<"enter the real and the image"<<endl;
double A,B;
cout<<"输入数的实部:";
cin>>A;
cout<<endl<<"输入数的虚部:";
cin>>B;
Complex C (A,B);
C.display();
cout<<"此复数的模为:"<<C.modulo();
}
追问
加法和减法呢??
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询