Data Structures——Chapter One Exercises(换位置+复数的加减乘除运算)
2017-12-06 17:23
363 查看
题目:
1.输入10个整数,将其中最小的属于第一个数调换,将最大的属于最后一个数对换。分析:定义三个函数,输入10个数,处理函数,输出函数
package FIRST_Chapter; import java.util.*; /* * 1.输入10个整数,将其中最小的属于第一个数调换,将最大的属于最后一个数对换。 分析:定义三个函数,输入10个数,处理函数,输出函数 */ public class IOInverse{ public static void main(String[] Args){ ArrayList str= new ArrayList(10); IOInverse ioi = new IOInverse(); ioi.input(str); ioi.reverse(str); ioi.output(str); } void input(ArrayList str){ Scanner sc = new Scanner(System.in); for(int i=0; i<10;i++){ System.out.println("please input the "+(i+1)+"th data"); int a=0; a=sc.nextInt(); str.add(i, a); } } void output(ArrayList str){ for(int i=0; i<10;i++){ System.out.println(str.get(i)); } } void reverse(ArrayList str){ int temp_max=0,index=0,temp_inverse=0; for(int j=0;j<10;j++){ if((int)(str.get(j))>=temp_max){ temp_max=(int)(str.get(j)); ind 4000 ex=j; } } System.out.println("ArrayList中最大的数是,下标为"+index+"的数"+(int)(str.get(index))); temp_inverse=(int)(str.get(9)); str.set(9, (int)(str.get(index))); str.set(index, temp_inverse); } }
输出结果:
please input the 1th data
1
please input the 2th data
1
please input the 3th data
1
please input the 4th data
1
please input the 5th data
1
please input the 6th data
3
please input the 7th data
4
please input the 8th data
1
please input the 9th data
1
please input the 10th data
1
ArrayList中最大的数是,下标为6的数4
1
1
1
1
1
3
1
1
1
4
题目
2.实现复数的加减乘除运算。第一个Java文件
package FIRST_Chapter; class Complex{ double real=0,image=0; Complex(double real,double image){ //构造函数 this.real=real; this.image=image; } Complex(){} static Complex add(Complex a,Complex b){ Complex temp; temp = new Complex(a.real+b.real,a.image+b.image); return temp; } static Complex sub(Complex a,Complex b){ Complex temp; temp = new Complex(a.real-b.real,a.image-b.image); return temp; } static Complex multiply(Complex a,Complex b){ Complex temp; temp = new Complex(a.real*b.real-a.image*b.image,a.real*b.image+a.image*b.real); return temp; } static Complex division(Complex a,Complex b){ Complex temp= new Complex(); temp.real = (a.real*b.real+a.image*b.image)/(b.real*b.real+b.image*b.image); temp.image= (a.image*b.real-a.real*b.image)/(b.real*b.real+b.image*b.image); return temp; } void show(){ if(image<0) {System.out.println("this complex is :"+real+image+"i");} else if(image>0){System.out.println("this complex is :"+real+"+"+image+"i");} else {System.out.println("this complex is :"+real);} } }
第二个Java文件(带主函数的测试文件)
package FIRST_Chapter; import java.util.Scanner; class test{ public static void main(String[] Args){ double real0,real1; double image0,image1; int i=1; Scanner sc= new Scanner(System.in); System.out.println("please input"+(i)+"th real part"); real0 = sc.nextDouble(); System.out.println("please input"+(i++)+"th imaginary part"); image0 = sc.nextDouble(); Complex a = new Complex(real0,image0); a.show(); System.out.println("please input"+(i)+"th real part"); real1 = sc.nextDouble(); System.out.println("please input"+(i++)+"th imaginary part"); image1 = sc.nextDouble(); Complex b = new Complex(real1,image1); b.show(); //sc.nextLine();//如果下面注释行想用nextLine的话,就要注意加上这句话 while(true){ System.out.println("what algorithm do you want a&b to make? add,sub,multiply,or division?"); String str=sc.next(); //注意这个地方不能用nextLine(),如果想用nextLine,上面注释语句就要转换成有效语句 if(str.equals("add")) { System.out.println("the add result is :"); Complex.add(a,b).show(); } else if(str.equals("sub")) { System.out.println("the sub result is :"); Complex.sub(a,b).show(); } else if(str.equals("multiply")) { System.out.println("the multiply result is :"); Complex.multiply(a,b).show(); } else if(str.equals("division")) { if (b.real==0&&b.image==0) { System.out.println("The divisor can't be 0"); continue;} System.out.println("the division result is :"); Complex.division(a,b).show(); } else {System.out.println("I don't know what you will do");} } } }
运行结果:
please input1th real part 1 please input1th imaginary part 1 this complex is :1.0+1.0i please input2th real part 0 please input2th imaginary part 0 this complex is :0.0 what algorithm do you want a&b to make? add,sub,multiply,or division? division The divisor can't be 0 what algorithm do you want a&b to make? add,sub,multiply,or division? add the add result is : this complex is :1.0+1.0i what algorithm do you want a&b to make? add,sub,multiply,or division?
相关文章推荐
- Summarizes chapter one of datastructures and program design in C++
- 实现复数的加减乘除四则运算的C#类(已测成功)
- 复数的加减乘除及开方乘方运算
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记二
- Data Structures with C++ Using STL Chapter 1数据结构入门---笔记
- Data Structures with C++ Using STL Chapter 3算法概述---笔记
- asp.net(c#)复数类(复数加减乘除四则运算)
- 【索引】Fundamental Data Structures:Exercises: Intermediate
- Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记四
- Data Structures and algorithm analysis—1.2.4&1.2.5Modular Arithmeti&The P Word(数据结构—模数运算&P字)
- Data Structures with C++ Using STL Chapter 2对象设计技术---笔记
- Data Structures with C++ Using STL Chapter 5指针和动态内存---笔记
- 【索引】Fundamental Data Structures:Exercises: Advanced
- 函数重载实现复数运算的加减乘除
- Data Structures with C++ Using STLChapter 1数据结构入门---作业
- Chapter 3.GDI/DirectDraw Internal Data Structures
- 【索引】Fundamental Data Structures:Exercises: Beginner
- 用java实现复数的加减乘除运算
- [翻译]Understanding Linux Network Internals--Chapter 2. Critical Data Structures
- Unable to acquire account data from realm [com.hongao.shiro.chapterOne.MyRealm2@2b80d80f]. The [org