import java.util.Map;
import java.util.HashMap;
import java.util.Scanner;
public class triangleTest {
public static void main(String[] args) {
System.out.println("==== 余弦定理 解三角形 ====");
System.out.println();
String calType = "calType";
double num1 = 0;
double num2 = 0;
double num3 = 0;
Scanner input = new Scanner(System.in);
if(input.hasNext()){
calType = input.next();
num1 = input.nextDouble();
num2 = input.nextDouble();
num3 = input.nextDouble();
}
System.out.println("你输入了计算类型:"+calType);
if(calType.equals("sss")){
System.out.println("你输入了边长a: a="+num1);
System.out.println("你输入了边长b: b="+num2);
System.out.println("你输入了边长c: c="+num3);
Map<String,Double> triangleMap = triangleSSS(num1,num2,num3);
System.out.println();
System.out.println("====== 经计算:========");
String A = String.format("%.2f",triangleMap.get("A"));
String B = String.format("%.2f",triangleMap.get("B"));
String C = String.format("%.2f",triangleMap.get("C"));
System.out.println("角A为:A="+A+"度");
System.out.println("角B为:B="+B+"度");
System.out.println("角C为:C="+C+"度");
}else if(calType.equals("sas")){
System.out.println("你输入了边长b: b="+num1);
System.out.println("你输入了边长c: c="+num2);
System.out.println("你输入了 角A: A="+num3);
Map<String,Double> triangleMap = triangleSAS(num1,num2,num3);
System.out.println();
System.out.println("====== 经计算:========");
System.out.println("第三边a长度为:a="+triangleMap.get("a"));
System.out.println("角B为:B="+triangleMap.get("B"));
System.out.println("角C为:C="+triangleMap.get("C"));
}else{
System.out.println("请正确输入你想要计算的类型:sss 或 sas ");
}
}
public static Map<String,Double> triangleSAS(double b,double c, double A){
Map<String,Double> triangleMap = new HashMap<String,Double>();
double radiansA = Math.toRadians(A);
double a = Math.sqrt(b*b+c*c - 2*b*c*Math.cos(radiansA));
triangleMap.put("a",a);
double fenzi = c*c + a*a - b*b;
double fenmu = 2*c*a;
double cosB = fenzi / fenmu;
double B = Math.toDegrees(Math.acos(cosB));
triangleMap.put("B",B);
triangleMap.put("C",180-B-A);
return triangleMap;
}
public static Map<String,Double> triangleSSS(double a,double b,double c){
Map<String,Double> triangleMap = new HashMap<String,Double>();
double fenziA = c*c + b*b - a*a;
double fenmuA = 2*b*c;
double cosA = fenziA / fenmuA;
double A = Math.toDegrees(Math.acos(cosA));
double fenziB = c*c + a*a - b*b;
double fenmuB = 2*c*a;
double cosB = fenziB / fenmuB;
double B = Math.toDegrees(Math.acos(cosB));
double fenziC = a*a + b*b - c*c;
double fenmuC = 2*a*b;
double cosC = fenziC / fenmuC;
double C = Math.toDegrees(Math.acos(cosC));
triangleMap.put("A",A);
triangleMap.put("B",B);
triangleMap.put("C",C);
return triangleMap;
}
}