2016蓝桥杯假期任务之《复数运算》
2016-01-31 11:39
483 查看
编程实现两个复数的运算。设有两个复数 和 ,则他们的运算公式为:
要求:(1)定义一个结构体类型来描述复数。
(2)复数之间的加法、减法、乘法和除法分别用不用的函数来实现。
(3)必须使用结构体指针的方法把函数的计算结果返回。
说明:用户输入:运算符号(+,-,*,/) a b c d.
输出:a+bi,输出时不管a,b是小于0或等于0都按该格式输出,输出时a,b都保留两位。
输入:
- 2.5 3.6 1.5 4.9
输出:
1.00+-1.30i
代码:
运行结果:
- 2.5 3.6 1.5 4.9
1.00+-1.30i
JAVA输入单个字符的方法:
char s = 0;
try {
s = (char)System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
要求:(1)定义一个结构体类型来描述复数。
(2)复数之间的加法、减法、乘法和除法分别用不用的函数来实现。
(3)必须使用结构体指针的方法把函数的计算结果返回。
说明:用户输入:运算符号(+,-,*,/) a b c d.
输出:a+bi,输出时不管a,b是小于0或等于0都按该格式输出,输出时a,b都保留两位。
输入:
- 2.5 3.6 1.5 4.9
输出:
1.00+-1.30i
代码:
import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) { class Complex { double real; double image; Complex() { } Complex(double real, double image) { this.real = real; this.image = image; } private void Complex(double real, double image) { // TODO Auto-generated method stub this.real = real; this.image = image; } public double getReal() { return real; } public void setReal() { this.real = real; } public double getImage() { return image; } public void setImage() { this.image = image; } Complex add(Complex a) { double real1 = a.getReal(); double image1 = a.getImage(); double newreal = real + real1; double newimage = image + image1; Complex newcomplex = new Complex(newreal, newimage); return newcomplex; } Complex sub(Complex a) { double real1 = a.getReal(); double image1 = a.getImage(); double newreal = real - real1; double newimage = image - image1; Complex newcomplex = new Complex(newreal, newimage); return newcomplex; } Complex mul(Complex a) { double real1 = a.getReal(); double image1 = a.getImage(); double newreal = real * real1 - image * image1; double newimage = image * real1 + real * image1; Complex newcomplex = new Complex(newreal, newimage); return newcomplex; } Complex div(Complex a) { double real1 = a.getReal(); double image1 = a.getImage(); double newreal = (real * real1 + image * image1) / (real1 * real1 + image1 * image1); double newimage = (image * real1 - real * image1) / (real1 * real1 + image1 * image1); Complex newcomplex = new Complex(newreal, newimage); return newcomplex; } public void print() { System.out.printf("%.2f+%.2fi",real,image); } } char s = 0; try { s = (char)System.in.read(); } catch (IOException e) { e.printStackTrace(); } Scanner sc = new Scanner(System.in); double real = sc.nextDouble(); double image = sc.nextDouble(); double real1 = sc.nextDouble(); double image1 = sc.nextDouble(); Complex data1 = new Complex(real,image); Complex data2 = new Complex(real1,image1); switch (s) { case '+': Complex result = data1.add(data2); result.print(); break; case '-': Complex result1 = data1.sub(data2); result1.print(); break; case '*': Complex result2 = data1.mul(data2); result2.print(); break; case '/': Complex result3 = data1.div(data2); result3.print(); break; default: break; } } }
运行结果:
- 2.5 3.6 1.5 4.9
1.00+-1.30i
JAVA输入单个字符的方法:
char s = 0;
try {
s = (char)System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树