1. 程式人生 > 其它 >前三次題目集總結性blog

前三次題目集總結性blog

作業總結

前言:

  在前三次作業的程式碼訓練當中,整體難度逐漸偏難,第一次還好可以全部寫出來,後面兩次的作業難度直線上升(還是自己太菜了沒能掌握完全),接下來也是應老師要求對前三次作業做一個小小的總結,雖然有些作業現在還沒有完全搞明白,在這分析問題的過程當中,也希望自己可以找到新的突破吧。

  接下來就直接對三次作業分開來進行分析好了,最後再做一個大總結。

1、第一次作業(綜合應用訓練)

  本次作業偏簡單,也比較偏綜合性,在這裡也不逐個分析了,但是在題目中還是有一些小細節值得去注意的

  比如第二題在精確度的判斷中有著些許問題

7-2 長度質量計量單位換算 (5 分)  

長度、質量的計量有多重不同的計算體系,有標準的國際單位制:千克與米,也有各個國家自己的計量方法如:磅、英寸;1磅等於0.45359237千克,1英寸等於0.0254米,請編寫程式實現國際單位制與英制之間的換算。

輸入格式:

兩個浮點數,以空格分隔,第一個是質量(以千克為單位)、第二個是長度(以米為單位)。例如:0.45359237 0.0254。

輸出格式:

兩個浮點數,以空格分隔,第一個是質量(以磅為單位)、第二個是長度(以英寸為單位)。例如:1.0 1.0。

 

  程式碼以下,在這道題當中注意的是doublefloat的選取使用

        

import java.util.Scanner;



public class Main {



public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new
Scanner(System.in); float str1 = input.nextFloat(); float str2 = input.nextFloat(); str1=(float)(str1/0.45359237); str2=(float)(str2/0.0254); System.out.println(str1+" "+str2); } }

 

  只要採取float單精度就可以通過測試點

  然後就是第六題,對字串讀入的處理也是一個需要注意的點

7-6 學號識別 (10 分)  

學校的學號由8位數字組成,前兩位是入學年份(省略了20);第3、4位是學院編號,01代表材料學院,02代表機械學院,03代表外語學院,20代表軟體學院;第5、6位是學院內部班級編號,最後兩位是班級內部學號。如:18011103,入學年份是2018年,材料學院,11班,03號

輸入格式:

8位數字組成的學號。例如:18011103
注意:輸入學號不是8位或者學院編號不是01、02、03、20其中之一,屬於非法輸入

輸出格式:

學號每一項的完整說明。例如:
入學年份:2018年
學院:材料學院
班級:11
學號:03

注意:如非法輸入,輸出“Wrong Format"

  在本題當中,主要是對學號的讀入,以及在不是非法讀入的情況下對八位數字的學號進行兩兩數字的判斷

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int[] numbers = new int[8];
int x=input.nextInt();
int sum=0;
int y=x;
while(y>0)
{
y/=10;
sum++;
}
if(sum==8)
{
for(int i=7;i>=0;i--)
{
numbers[i]=x%10;
x=x/10;
}
if(numbers[2]==0 && numbers[3]>=1 &&numbers[3]<=3 || numbers[2]==2&&numbers[3]==0)
{
System.out.println("入學年份:20"+numbers[0]+numbers[1]+"年");
System.out.print("學院:");
if(numbers[2]==0)
{
if(numbers[3]==1)
System.out.println("材料學院");
if(numbers[3]==2)
System.out.println("機械學院");
if(numbers[3]==3)
System.out.println("外語學院");
}
else
System.out.println("軟體學院");
System.out.println("班級:"+numbers[4]+numbers[5]);
System.out.println("學號:"+numbers[6]+numbers[7]);
}
else
{
sum=0;
}
}
if(sum!=8)
System.out.println("Wrong Format");
}
}  

 

 

  本題先將字串讀入,然後再對字串進行初步判斷,用標記sum來判斷,若符合8位數字的標準,則進入下一步的判斷。

  再將字串儲存到數組裡面,對陣列進行分析判斷合法性,若不合法則改變標記sum,最後再輸出題目所要求的。

  本題可以簡化改進,不用多一個迴圈來判斷數字長度,直接採用x.length來判斷即可

2、第二次作業(對字串的分析應用)

  本次作業更多的是偏向對字串進行讀入並進行分析輸出,根據題目的要求對讀入的字串進行分析

  第一題是對字串進行字母數字進行轉換

  第二題是對讀入的串列埠字元進行分析

  第三題是對字串進行格式判斷及提取

  所以可以看出來,這三道題目的解題思路都要從字串入手,也按照要求重點對第二道題目進行分析

 

7-2 串列埠字元解析 (40 分)

RS232是串列埠常用的通訊協議,在非同步通訊模式下,串列埠可以一次傳送5~8位資料,收發雙方之間沒有資料傳送時線路維持高電平,相當於接收方持續收到資料“1”(稱為空閒位),傳送方有資料傳送時,會在有效資料(5~8位,具體位數由通訊雙方提前設定)前加上1位起始位“0”,在有效資料之後加上1位可選的奇偶校驗位和1位結束位“1”。請編寫程式,模擬串列埠接收處理程式,注:假定有效資料是8位,奇偶校驗位採用奇校驗。

輸入格式:

由0、1組成的二進位制資料流。例如:11110111010111111001001101111111011111111101111

輸出格式:

過濾掉空閒、起始、結束以及奇偶校驗位之後的資料,資料之前加上序號和英文冒號。
如有多個數據,每個資料單獨一行顯示。
若資料不足11位或者輸入資料全1沒有起始位,則輸出"null data",
若某個資料的結束符不為1,則輸出“validate error”。
若某個資料奇偶校驗錯誤,則輸出“parity check error”。
若資料結束符和奇偶校驗均不合格,輸出“validate error”。
如:11011或11111111111111111。
例如:
1:11101011
2:01001101
3:validate error

  從題目可以看出來輸入的是由0和1組成的串列埠字元,然後再對串列埠字元進行具體的分析,具體思路就是用string讀入串列埠字元,但是在後面的分析中當時遇到了瓶頸以及時間有限,於是只做了最簡單的錯誤判斷,並沒有把所有的都完成

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a=1;
        Scanner input = new Scanner(System.in);
        String x=input.nextLine();
        char[] sum=x.toCharArray();
        int num=sum.length;
        if(x.length()<11)
            System.out.print("null data");
        if(x.matches("^[1]*$"))
        {
            System.out.print("null data");
        }
            
    }
}

  首先先是對串列埠符進行讀入,先判斷資料不足11位或者輸入資料全1沒有起始位,然後先輸出null data,這邊可以直接對讀入的字串進行判斷,分別利用長度以及正則表示式進行判斷串列埠符的合法性

  當時到這裡就沒有往後面寫下去了,現在再順著當時的思路繼續往下分析

  由題目可知,結束符不為1並且奇偶校驗不合格輸出validate error,所以可以將這兩個點進行單獨判斷輸出結果

  然後接下來就是採用陣列的遍歷對串列埠符進行具體分析

  具體還要判斷有幾組資料,對正常值,空閒位等都要進行判斷,由於程式碼也沒有全部寫完那就直接跳下一次作業好了。

2、第三次作業(點線形系列)

  本次作業的三道題目都是循序漸進

  第一題從兩點之間的距離出發進行長度計算

  第二題從線段之間進行運算

  第三題則是對由線段組成的三角形進行判斷

  具體分析放到程式碼最後面去了

7-1 點線形系列1-計算兩點之間的距離 (10 分)  

輸入連個點的座標,計算兩點之間的距離

輸入格式:

4個double型別的實數,兩個點的x,y座標,依次是x1、y1、x2、y2,兩個點的座標之間以空格分隔,每個點的x,y座標以英文“,”分隔。例如:0,0 1,1或0.1,-0.3 +3.5,15.6。
若輸入格式非法,輸出"Wrong Format"。
若輸入格式合法但座標點的數量超過兩個,輸出“wrong number of points”。

輸出格式:

計算所得的兩點之間的距離。例如:1.4142135623730951

 

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner  = new Scanner(System.in);
        String a =  scanner.nextLine();
        char str[] = a.toCharArray();
        int len=a.length();
        int i,j;
        int sum=0;
        int flag=1;
        int point=0;
        int e=0,b=0,c=0,d=0;
        int num1=0,num2=0,num3=0,num4=0;
        double x1=0,y1=0,x2=0,y2=0;
        double result;
        for(i=0;i<len;i++)
        {
            if(str[i]==',')
                sum++;
        }
        if(sum>2)
            System.out.print("wrong number of points");
        else
        {
            for(i=0;i<len;i++)
            {
                if(str[i]=='+' || str[i]=='-')
                {
                    if(a.matches("(\\+|-)?(0|[1-9]\\d*)(\\.\\d*[1-9])?(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d*[1-9])?(\\ )(\\+|-)?(0|[1-9]\\d*)(\\.\\d*[1-9])?(\\.\\d+)?(\\,)(\\+|-)?(0|[1-9]\\d*)(\\.\\d*[1-9])?"))
                        flag=flag;
                    else
                        flag++;
                    if(str[i]=='.')
                        point++;
                    if(str[i]>='a' && str[i]<='z')
                        flag++;
                    if(str[i]>='A' && str[i]<='Z')
                        flag++;
                }
            }
            if(flag!=1)
            {
                System.out.print("Wrong Format");
            }
            else if(point==0)
            {
               for(i=0;i<len;i++)
                {
                    //System.out.println(str[i]);
                    if(point==0)
                    {
                        if(str[i]=='-')
                            e++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num1++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x1=x1*10+(str[i]-'0');
                            //System.out.println(x1);
                        }
                    }
                    
                    if(point==1)
                    {
                        if(str[i]=='-')
                            b++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num2++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y1=y1*10+(str[i]-'0');
                            //System.out.println(y1);
                        }
                    }
                    
                    if(point==2)
                    {
                        if(str[i]=='-')
                            c++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num3++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x2=x2*10+(str[i]-'0');
                            //System.out.println(x2);
                        }
                    }
                    
                    if(point==3)
                    {
                        if(str[i]=='-')
                            d=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num4++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y2=y2*10+(str[i]-'0');
                            //System.out.println(y2);
                        }
                    }
                    
                    if(str[i]==' '||str[i]==',')
                        point++;
                }
                if(e==1)
                    {
                        x1=-x1;
                    }
                if(b==1)
                    {
                        y1=-y1;
                    }
                if(c==1)
                    {
                        x2=-x2;
                        //System.out.println(x2);
                    }
                if(d==1)
                    {
                        y2=-y2;
                        //System.out.println(y2);
                    }
                if(num1!=0)
                    x1=x1/Math.pow(10,num1);
                if(num2!=0)
                    y1=y1/Math.pow(10,num2);
                if(num3!=0)
                    x2=x2/Math.pow(10,num3);
                if(num4!=0)
                    y2=y2/Math.pow(10,num4);
                    result=Math.sqrt(Math.abs(x1*x1+x2*x2-2*x1*x2)+Math.abs(y1*y1+y2*y2-2*y1*y2));
                    System.out.print(result);
                    }
        }
    }
}

 

7-2 點線形系列2-線的計算 (42 分)  

使用者輸入一組選項和資料,進行與直線有關的計算。選項包括:
1:輸入兩點座標,計算斜率,若線條垂直於X軸,輸出"Slope does not exist"。
2:輸入三個點座標,輸出第一個點與另外兩點連線的垂直距離。
3:輸入三個點座標,判斷三個點是否在一條線上,輸出true或者false。
4:輸入四個點座標,判斷前兩個點所構成的直線與後兩點構成的直線是否平行,輸出true或者false.
5:輸入四個點座標,計算輸出前兩個點所構成的直線與後兩點構成的直線的交點座標,x、y座標之間以英文分隔",",並輸出交叉點是否在兩條線段之內(不含四個端點)的判斷結果(true/false),判斷結果與座標之間以一個英文空格分隔。若兩條線平行,沒有交叉點,則輸出"is parallel lines,have no intersection point"。

輸入格式:

基本格式:選項+":"+座標x+","+座標y+" "+座標x+","+座標y。
例如:1:0,0 1,1
如果不符合基本格式,輸出"Wrong Format"。
如果符合基本格式,但輸入點的數量不符合要求,輸出"wrong number of points"。
不論哪個選項,如果格式、點數量都符合要求,但構成任一條線的兩個點座標重合,輸出"points coincide",

輸出格式:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner  = new Scanner(System.in);
        String a =  scanner.nextLine();
        char str[] = a.toCharArray();
        int len=a.length();
        int i,j;
        int sum=0;
        int flag=1;
        int point=0;
        int num1=0,num2=0,num3=0,num4=0,num5=0,num6=0;
        int e=0,b=0,c=0,d=0,f=0,g=0,h=0,k=0;
        double x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0;
        double result,result1,result2;
        for(i=0;i<len;i++)
        {
            if(str[i]==',')
                sum++;
        }
        if(sum>4||sum==1)
            System.out.print("wrong number of points");
        if(str[0]=='1')
        {
            for(i=2;i<len;i++)
            {
                if(str[i]=='+' || str[i]=='-')
                {
                    if(str[i+1]>='0' && str[i+1]<='9')
                        flag=flag;
                    else
                        flag++;
                    if(str[i]=='.')
                        point++;
                }
            }
            if(flag!=1)
            {
                System.out.print("Wrong Format");
            }
            else if(point==0)
            {
                for(i=2;i<len;i++)
                {
                    //System.out.println(str[i]);
                    if(point==0)
                    {
                        if(str[i]=='-')
                            e++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num1++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x1=x1*10+(str[i]-'0');
                        }
                    }
                    
                    if(point==1)
                    {
                        if(str[i]=='-')
                            b++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num2++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y1=y1*10+(str[i]-'0');
                        }
                    }
                    
                    if(point==2)
                    {
                        if(str[i]=='-')
                            c++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num3++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x2=x2*10+(str[i]-'0');
                        }
                    }
                    
                    if(point==3)
                    {
                        if(str[i]=='-')
                            d=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num4++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y2=y2*10+(str[i]-'0');
                        }
                    }
                    
                    if(str[i]==' '||str[i]==',')
                        point++;
                }
                if(e==1)
                        x1=-x1;
                if(b==1)
                        y1=-y1;
                if(c==1)
                        x2=-x2;
                if(d==1)
                        y2=-y2;
                if(num1!=0)
                    x1=x1/Math.pow(10,num1);
                if(num2!=0)
                    y1=y1/Math.pow(10,num2);
                if(num3!=0)
                    x2=x2/Math.pow(10,num3);
                if(num4!=0)
                    y2=y2/Math.pow(10,num4);
                result=(y1-y2)/(x1-x2);
                if(x1==x2&&y1==y2)
                    System.out.print("points coincide");
                else if(x1==x2&&y1!=y2)
                    System.out.print("Slope does not exist");
                else
                    System.out.print(result);
            }
        }
        
        if(str[0]=='2')
        {
            
        }
        if(str[0]=='3')
        {
            for(i=2;i<len;i++)
            {
                if(str[i]=='+' || str[i]=='-')
                {
                    if(str[i+1]>='0' && str[i+1]<='9')
                        flag=flag;
                    else
                        flag++;
                    if(str[i]=='.')
                        point++;
                }
            }
            if(flag!=1)
            {
                System.out.print("Wrong Format");
            }
            else if(point==0)
            {
                for(i=2;i<len;i++)
                {
                    if(point==0)
                    {
                        if(str[i]=='-')
                            e++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num1++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x1=x1*10+(str[i]-'0');
                        }
                    }
                    
                    if(point==1)
                    {
                        if(str[i]=='-')
                            b++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num2++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y1=y1*10+(str[i]-'0');
                        }
                    }
                    
                    if(point==2)
                    {
                        if(str[i]=='-')
                            c++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num3++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x2=x2*10+(str[i]-'0');
                        }
                    }
                    
                    if(point==3)
                    {
                        if(str[i]=='-')
                            d=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num4++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y2=y2*10+(str[i]-'0');
                            //System.out.println(y2);
                        }
                    }
                    if(point==4)
                    {
                        if(str[i]=='-')
                            f=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num5++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x3=x3*10+(str[i]-'0');
                        }
                    }
                    if(point==5)
                    {
                        if(str[i]=='-')
                            g=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num6++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y3=y3*10+(str[i]-'0');
                        }
                    }
                    if(str[i]==' '||str[i]==',')
                        point++;
                }
                if(e==1)
                        x1=-x1;
                if(b==1)
                        y1=-y1;
                if(c==1)
                        x2=-x2;
                if(d==1)
                        y2=-y2;
                if(f==1)
                        x3=-x3;
                if(g==1)
                        y3=-y3;
                if(num1!=0)
                    x1=x1/Math.pow(10,num1);
                if(num2!=0)
                    y1=y1/Math.pow(10,num2);
                if(num3!=0)
                    x2=x2/Math.pow(10,num3);
                if(num4!=0)
                    y2=y2/Math.pow(10,num4);
                if(num5!=0)
                    x3=x3/Math.pow(10,num1);
                if(num6!=0)
                    y3=y3/Math.pow(10,num2);
                if(x1==x2&&y1==y2 || x1==x3&&y1==y3 || x3==x2&&y3==y2)
                    System.out.println("points coincide");
                else if(x1*y2==x2*y1 && x2*y3==x3*y2 && x1*y3==x3*y1)
                    System.out.println("true");
                else
                    System.out.println("flase");
            }
        }
        if(str[0]=='4')
        {
            for(i=2;i<len;i++)
            {
                if(str[i]=='+' || str[i]=='-')
                {
                    if(str[i+1]>='0' && str[i+1]<='9')
                        flag=flag;
                    else
                        flag++;
                    if(str[i]=='.')
                        point++;
                }
            }
            if(flag!=1)
            {
                System.out.print("Wrong Format");
            }
            else if(point==0)
            {
                for(i=2;i<len;i++)
                {
                    //System.out.println(str[i]);
                    if(point==0)
                    {
                        if(str[i]=='-')
                            e++;
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x1=x1*10+(str[i]-'0');
                            //System.out.println(x1);
                        }
                    }
                    
                    if(point==1)
                    {
                        if(str[i]=='-')
                            b++;
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y1=y1*10+(str[i]-'0');
                            //System.out.println(y1);
                        }
                    }
                    
                    if(point==2)
                    {
                        if(str[i]=='-')
                            c++;
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x2=x2*10+(str[i]-'0');
                            //System.out.println(x2);
                        }
                    }
                    
                    if(point==3)
                    {
                        if(str[i]=='-')
                            d=1;
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y2=y2*10+(str[i]-'0');
                            //System.out.println(y2);
                        }
                    }
                    if(point==4)
                    {
                        if(str[i]=='-')
                            f=1;
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x3=x3*10+(str[i]-'0');
                            //System.out.println(y2);
                        }
                    }
                    if(point==5)
                    {
                        if(str[i]=='-')
                            g=1;
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y3=y3*10+(str[i]-'0');
                            //System.out.println(y2);
                        }
                    }
                    if(point==6)
                    {
                        if(str[i]=='-')
                            h=1;
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x4=x4*10+(str[i]-'0');
                            //System.out.println(y2);
                        }
                    }
                    if(point==7)
                    {
                        if(str[i]=='-')
                            k=1;
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y4=y4*10+(str[i]-'0');
                            //System.out.println(y2);
                        }
                    }
                    if(str[i]==' '||str[i]==',')
                        point++;
                }
                if(e==1)
                    {
                        x1=-x1;
                    }
                if(b==1)
                        y1=-y1;
                if(c==1)
                        x2=-x2;
                if(d==1)
                        y2=-y2;
                if(f==1)
                        x3=-x3;
                if(g==1)
                        y3=-y3;
                if(h==1)
                        x4=-x4;
                if(k==1)
                        y4=-y4;
                result1=(y1-y2)/(x1-x2);
                result2=(y3-y4)/(x3-x4);
                if(Math.abs(result1-result2)<0.0001)
                    System.out.println("true");
                else
                    System.out.println("flase");
            }
        }
        }
}
7-3 點線形系列3-三角形的計算 (48 分)  

使用者輸入一組選項和資料,進行與三角形有關的計算。選項包括:
1:輸入三個點座標,判斷是否是等腰三角形、等邊三角形,判斷結果輸出true/false,兩個結果之間以一個英文空格符分隔。
2:輸入三個點座標,輸出周長、面積、重心座標,三個引數之間以一個英文空格分隔,座標之間以英文","分隔。
3:輸入三個點座標,輸出是鈍角、直角還是銳角三角形,依次輸出三個判斷結果(true/false),以一個英文空格分隔,
4:輸入五個點座標,輸出前兩個點所在的直線與三個點所構成的三角形相交的交點數量,如果交點有兩個,則按面積大小依次輸出三角形被直線分割成兩部分的面積。若直線與三角形一條線重合,輸出"The point is on the edge of the triangle"
5:輸入四個點座標,輸出第一個是否在後三個點所構成的三角形的內部(輸出in the triangle/outof triangle)。
必須使用射線法,原理:由第一個點往任一方向做一射線,射線與三角形的邊的交點(不含點本身)數量如果為1,則在三角形內部。如果交點有兩個或0個,則在三角形之外。若點在三角形的某條邊上,輸出"on the triangle"

輸入格式:

基本格式:選項+":"+座標x+","+座標y+" "+座標x+","+座標y。點的x、y座標之間以英文","分隔,點與點之間以一個英文空格分隔。

輸出格式:

基本輸出格式見每種選項的描述。
異常情況輸出:
如果不符合基本格式,輸出"Wrong Format"。
如果符合基本格式,但輸入點的數量不符合要求,輸出"wrong number of points"。
如果輸入的三個點無法構成三角形,輸出"data error"。
注意:輸出的資料若小數點後超過6位,只保留小數點後6位,多餘部分採用四捨五入規則進到最低位。小數點後若不足6位,按原始位數顯示,不必補齊。例如:1/3的結果按格式輸出為 0.333333,1.0按格式輸出為1.0

選項4中所輸入線的兩個點座標重合,輸出"points coincide",

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner scanner  = new Scanner(System.in);
        String a =  scanner.nextLine();
        char str[] = a.toCharArray();
        int len=a.length();
        int i,j;
        int sum=0;
        int flag=1;
        int point=0;
        int e=0,b=0,c=0,d=0;
        int num1=0,num2=0,num3=0,num4=0;
        double x1=0,y1=0,x2=0,y2=0;
        double result;
        for(i=0;i<len;i++)
        {
            if(str[i]==',')
                sum++;
        }
        if(sum>2)
            System.out.print("wrong number of points");
        else
        {
            for(i=0;i<len;i++)
            {
                if(str[i]=='+' || str[i]=='-')
                {
                    if(str[i]>=0&&str[i]<=9)
                      flag=flag;
                    else
                        flag++;
                    if(str[i]=='.')
                        point++;
                }
            }
            if(flag!=1)
            {
                System.out.print("Wrong Format");
            }
            else if(point==0)
            {
               for(i=0;i<len;i++)
                {
                    //System.out.println(str[i]);
                    if(point==0)
                    {
                        if(str[i]=='-')
                            e++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num1++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x1=x1*10+(str[i]-'0');
                            //System.out.println(x1);
                        }
                    }
                    
                    if(point==1)
                    {
                        if(str[i]=='-')
                            b++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num2++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y1=y1*10+(str[i]-'0');
                            //System.out.println(y1);
                        }
                    }
                    
                    if(point==2)
                    {
                        if(str[i]=='-')
                            c++;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num3++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            x2=x2*10+(str[i]-'0');
                            //System.out.println(x2);
                        }
                    }
                    
                    if(point==3)
                    {
                        if(str[i]=='-')
                            d=1;
                        if(str[i]=='.')
                        {
                            for(j=i+1;j<len;j++)
                            {
                                if(str[j]>='0' && str[j]<='9')
                                    num4++;
                                else
                                    break;
                            }
                        }
                        if(str[i]>='0' && str[i]<='9')
                        {
                            y2=y2*10+(str[i]-'0');
                            //System.out.println(y2);
                        }
                    }
                    
                    if(str[i]==' '||str[i]==',')
                        point++;
                }
                if(e==1)
                    {
                        x1=-x1;
                    }
                if(b==1)
                    {
                        y1=-y1;
                    }
                if(c==1)
                    {
                        x2=-x2;
                        //System.out.println(x2);
                    }
                if(d==1)
                    {
                        y2=-y2;
                        //System.out.println(y2);
                    }
                if(num1!=0)
                    x1=x1/Math.pow(10,num1);
                if(num2!=0)
                    y1=y1/Math.pow(10,num2);
                if(num3!=0)
                    x2=x2/Math.pow(10,num3);
                if(num4!=0)
                    y2=y2/Math.pow(10,num4);
                    result=Math.sqrt(Math.abs(x1*x1+x2*x2-2*x1*x2)+Math.abs(y1*y1+y2*y2-2*y1*y2));
                    System.out.print(result);
                    }
        }
    }
}
View Code

  第三次作業的三個程式碼都有相關性,所以放一起來分析