15-算法训练 P1103
2024-08-27 13:09:27
算法训练 P1103
时间限制:1.0s 内存限制:256.0MB
编程实现两个复数的运算。设有两个复数 和 ,则他们的运算公式为:
要求:(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
思路:
首先在java里面用类代替结构体,注意不要用public class, 且将成员变量设置为public便于使用,否则要写set,get函数,很麻烦;
格式输出注意:用system.out.printf()或者String.format()都可以,注意double 是%f 而不是lf.
import java.util.Scanner; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in);
char fh = cin.next().charAt(0); // 注意这里不用nextLine
fushu t1 = new fushu();
t1.shi = cin.nextDouble();
t1.xu = cin.nextDouble();
fushu t2 = new fushu();
t2.shi = cin.nextDouble();
t2.xu = cin.nextDouble();
if(fh == '+') {
t1.add(t2);
}
else if(fh == '-'){
t1.jian(t2);
}
else if(fh == '*') {
t1.chen(t2);
}
else {
t1.chu(t2);
} // System.out.println(t1.shi + "+" + t1.xu + "i");
// System.out.println(String.format("%.2f+%.2fi", t1.shi, t1.xu));
System.out.printf("%.2f+%.2fi", t1.shi, t1.xu);
} } class fushu{
public double shi;
public double xu;
fushu(){ }
fushu(double a, double b){
shi = a;
xu = b;
}
public void jian(fushu b) {
this.shi -= b.shi;
this.xu -= b.xu;
}
public void add(fushu b) {
this.shi += b.shi;
this.xu += b.xu;
}
public void chen(fushu b) {
double shi = this.shi;
double xu = this.xu;
this.shi = shi * b.shi - xu * b.xu;
this.xu = shi * b.xu + xu * b.shi;
}
public void chu(fushu b) {
double shi = this.shi;
double xu = this.xu;
this.shi = (shi * b.shi + xu * b.xu) / (b.shi * b.shi + b.xu * b.xu);
this.xu = (xu * b.shi - shi * b.xu) / (b.shi * b.shi + b.xu * b.xu);
}
}
最新文章
- 【JAVA面试题系列一】面试题总汇--JAVA基础部分
- 激活神器 KMSAuto Net 2015 v1.3.8
- Oracle数据库,join多表关联方式、union结果集合并
- java集合——Collection接口
- sdut 2482 二叉排序树
- C#使用Sockets操作FTP【转载】
- ubuntu 14.04 GDAL
- 分页(将数据库中的多条数据一页一页的显示在jsp页面中)
- 【ASP.NET MVC 学习笔记】- 05 依赖注入工具Ninject
- Review: Basic Knowledge about SQL
- BATJ面试指南
- 重建整个数据库的索引(Server2000)
- RISC精简指令集系统计算机
- highChart 缺值-曲线断开问题
- [转]使用Cython来保护Python代码库
- 100道c++面试题(上)
- layui框架学习记录
- ceph 生成rpm包
- myeclipse2014配置多个同版本的Tomcat
- FatMouse's Speed (hdu 1160)
热门文章
- CAS单点登录原理解析
- C++复习:STL之算法
- centos 下修改mysql 默认字符集
- python抽象方法
- Android开发最佳实践《IT蓝豹》
- spring boot 事务支持
- Incompatible shapes during the half way training---Invalid argument: Incompatible shapes: [1,63,4] vs. [1,64,4]
- mysql查找某连续字段中断的编号
- JAVA第五周 动手动脑
- cakephp 利用Pushapi扩展 进行app 消息推送