第一次博客作业

一、前言

这三次的作业主要是考察了对代码输入与输出的理解与实践;题量上不多不少基本合适;难度对于java刚入门的我来说还是偏难的。(~ ̄▽ ̄)~

二、设计与分析

题目集一   程序7-8

import java.util.*;
public class Work78
{
    public static void main(String args[])
    {
        Scanner input=new Scanner(System.in);
        double a=input.nextDouble();
        double b=input.nextDouble();
        double c=input.nextDouble();
        double 误差=0.000001;                                   
        if(a<1||b<1||c<1||a>200||b>200||c>200)
        {
            System.out.println("Wrong Format");                 
        }
        else if(a+b<c+误差||a+c<b+误差||b+c<a+误差)
        {
            System.out.println("Not a triangle");           
            return;
        }
        else if(a==b||a==c||b==c)
        {
            if(a==b&&a==c)
            {
                System.out.println("Equilateral triangle");
            }
            else if((a*a+b*b<c*c+误差&&a*a+b*b>c*c-误差)||(a*a+c*c<b*b+误差&&a*a+c*c>b*b-误差)||(c*c+b*b<a*a+误差&&c*c+b*b>a*a-误差))
            {
                System.out.println("Isosceles right-angled triangle");
                return;
            }
            else
            {
                System.out.println("Isosceles triangle");
                return;
            }
        }
        else if((a*a+b*b<c*c+误差&&a*a+b*b>c*c-误差)||(a*a+c*c<b*b+误差&&a*a+c*c>b*b-误差)||(c*c+b*b<a*a+误差&&c*c+b*b>a*a-误差))
        {
            System.out.println("Right-angled triangle");
            return;
        }
        else
        {
            System.out.println("General triangle");
            return;
        }
    }
}

输出结果

1.
Equilateral triangle
2. Isosceles triangle
3. Wrong Format

对于本题的编写思路:是要先去确定这个三角形的最长那条边;然后再去判断程序的输入是否在合法的区间内;再去判断这三条边是否能够构成一个三角形;最后再做出等边三角形,等腰直角三角形,等腰三角形,直角三角形与普通三角形的判断条件。

程序编写的过程中还要注意一些容易发生错误的地方,比如先要理清逻辑顺序,哪个条件先进入判断;其次就是三角形中最长边的交换不能直接进行,要用中间的条件进行间接的交换;再者就是注意编写上的一些小问题。

 

题目集二   程序7-4

import java.util.Scanner;

public class Main {
    
    public static boolean isLeapYear(int year) {
        boolean isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0;
        return isLeapYear;
    }
    
    public static boolean checkInputValidity(int year,int month,int day) {
            int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
            if(isLeapYear(year))
                aa[2] = 29;
            boolean checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=aa[month]&&day>0);
                return checkInputValidity;
    }
    
    public static void nextDate(int year,int month,int day) {
        int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            aa[2] = 29;
        int a = 0,b = 0,c = 0;
        if(checkInputValidity(year,month,day)) {
            if(month==12) {
                if(day==aa[month]) {
                a = year+1;
                b = 1;
                c = 1;}
            if(day>0&&day<aa[month]) 
                    {a = year;
                    b = month;
                    c =day +1;
                    }
            }
            if(month<12) {
                if(day==aa[month]) {
                    a = year;
                    b = month + 1;
                    c = 1;}
                if(day>0&&day<aa[month]) 
                        {a = year;
                        b = month;
                        c = day+1;
                        }
            }
            System.out.println("Next date is:"+a+"-"+b+"-"+c);
            }
        else System.out.println("Wrong Format");
    }

    public static void main(String[] args) {
        Scanner LJY = new Scanner(System.in);
        Main ljy = new Main();
        int year = LJY.nextInt();
        int month = LJY.nextInt();
        int day = LJY.nextInt();
        ljy.nextDate(year,month,day);

    }

}

输出结果

1.
Next date is:2020-3-11
2. Wrong Format

本题的要求如下:

输入年月日的值(均为整型数),输出该日期的下一天。其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

当输入数据非法及输入日期不存在时,输出“Wrong Format”;

当输入日期合法,输出下一天,格式如下:Next date is:年-月-日。

在编写本题的思路上,第一要先去判断输入年份是否为闰年,同时要注意是否会进入到下一年。

要注意闰年的条件,以及闰年的二月天数与平时年份不相同;同时要注意每年的年底下一天是第二年的第一天;其中要重点注意的是闰年年份并不仅是能被4整除。

以下是本题程序的圈复杂度:

 

 

 

题目集二   程序7-5

import java.util.Scanner;

public class Main {
    
    public static boolean isLeapYear(int year) {
        boolean isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0;
        return isLeapYear;
    }
    
    public static boolean checkInputValidity(int year,int month,int day,int n) {
            int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
            if(isLeapYear(year))
                aa[2] = 29;
            boolean checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=aa[month]&&day>0&&Math.abs(n)<=10&&Math.abs(n)>=0);
                return checkInputValidity;
    }
    
    public static void nextDate(int year,int month,int day,int n) {
        int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            aa[2] = 29;
        int a = 0,b = 0,c = 0;
        if(checkInputValidity(year,month,day,n)) {
            if (n==0) {
                a = year;
                b = month;
                c = day;
            }
            if(month==12&&n>0) {
                if(day-n<=aa[month]&&day-n>0) {
                a = year;
                b = month;
                c = day-n;}
                if(day-n<=aa[month]&&day-n<0) {
                    a = year;
                    b = month-1;
                    c = aa[b]-n+day;
                }
                if(day-n<=aa[month]&&day-n==0) {
                    a = year;
                    b = month-1;
                    c = aa[b]-n+day;
                }
            }
            if(month==12&&n<0) {
                if(day-n>aa[month]) {
                    a = year +1;
                    b = 1;
                    c =day - n -aa[month];
                }
                if(day-n<=aa[month]) {
                    a = year;
                    b = month;
                    c =day - n;
                }
            }
            if(month>1&&month<12&&n>0) {
                if(day-n<=aa[month]&&day-n>0) {
                a = year;
                b = month;
                c = day-n;}
                if(day-n<=aa[month]&&day-n<0) {
                    a = year;
                    b = month-1;
                    c = aa[b]-n+day;
                }
                if(day-n<=aa[month]&&day-n==0) {
                    a = year;
                    b = month-1;
                    c = aa[b]-n+day;
                }
            }
            if(month<12&&month>1&&n<0) {
                if(day-n>aa[month]) {
                    a = year;
                    b = month+1;
                    c =day - n -aa[month];
                }
                if(day-n<=aa[month]) {
                    a = year;
                    b = month;
                    c =day - n;
                }
            }
            if(month == 1&&n>0) {
                if(day-n<=aa[month]&&day-n>0) {
                    a = year;
                    b = month;
                    c = day-n;
                }
                if(day-n<=aa[month]&&day-n<0) {
                    a = year-1;
                    b = 12;
                    c = aa[b]-n+day;
                }
                if(day-n<=aa[month]&&day-n==0) {
                    a = year-1;
                    b = 12;
                    c = aa[b]-n+day;
                }
            }
            if(month==1&&n<0) {
                if(day-n>aa[month]) {
                    a = year;
                    b = month+1;
                    c =day - n -aa[month];
                }
                if(day-n<=aa[month]) {
                    a = year;
                    b = month;
                    c =day - n;
                }
            }
            System.out.println(n+" days ago is:"+a+"-"+b+"-"+c);
            }
        else System.out.println("Wrong Format");
    }

    public static void main(String[] args) {
        Scanner LJY = new Scanner(System.in);
        Main ljy = new Main();
        int year = LJY.nextInt();
        int month = LJY.nextInt();
        int day = LJY.nextInt();
        int n = LJY.nextInt();
        ljy.nextDate(year,month,day,n);

    }

}

输出结果

1.
8 days ago is:2018-6-11
2. -8 days ago is:2018-6-27

题目要求:

输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。

输入格式:

在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。

输出格式:

当输入的年、月、日以及n的值非法时,输出“Wrong Format”;

当输入数据合法时,输出“n days ago is:年-月-日”。

本题思路与上一题基本相同,因此不再赘述。

以下是本题的圈复杂度:

 

 

 

题目集三   程序7-2

import java.util.Scanner;

public class Main{
    public static void main(String[]args) {
        Scanner in=new Scanner(System.in);
        Date a=new Date();
        a.year=in.nextInt();
        a.month=in.nextInt();
        a.day=in.nextInt();
        a.getNextDate(a.getYear(),a.getMonth(),a.getDay());
    }
}

class Date
{
    int year;
    int month;
    int day;
    int[] maxnum=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};

    public void setYear(int year)
    {
        this.year=year;
    }
    public int getMonth() {
        return month;
    }
    public Date(int year,int month,int day) {
        this.year=year;
        this.month=month;
        this.day=day;
    }
    public Date() {
        int year,month,day;
    }
    public int getYear() {
        return year;
    }
    public void setMonth(int month)
    {
        this.month=month;
    }
    public int getDay()
    {
        return day;
    }
    public void setDay(int day) {
        this.day=day;
    }
    public boolean isLeapYear(int year)
    {
        boolean isLeapYear;
        isLeapYear=(year % 4 == 0 && year % 100 !=0 ||year % 400 == 0);
        return isLeapYear;
    }
    public boolean checkInputValidity(int year,int month,int day)
    {
        boolean checkInputValidity;
        int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year)) {
            a[2]=29;
        }
        checkInputValidity = (year>=1900&&year<=2000&&month>0&&month<=12&&day<=a[month]&&day>0);
        return checkInputValidity;
    }
    public void getNextDate(int year,int month,int day)
    {
        int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
        int d=0,m=0;
        if(isLeapYear(year))
        {
            a[2]=29;
        }
        
            if(checkInputValidity(year,month,day))
            {
                if(month==12) {
                if(day==a[month]) {
                    year = year+1;
                    m = 1;
                    d=1;
                }
                else{
                    m=month;
                    d =day +1;
                }
            }
             else {
                    if(day==a[month]) {
                        m = month + 1;
                        d = 1;
                    }
                    else{
                        m=month;
                        d = day+1;
                    }
             }
            System.out.println("Next day is:"+year+"-"+m+"-"+d);
    }
    
         else if(checkInputValidity(year,month,day)==false)
                System.out.println("Date Format is Wrong");
    }
}

输出结果

1.
Next day is:1912-12-26

2.
Date Format is Wrong

题目要求如下:

定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;

当输入日期合法,输出下一天,格式如下:Next day is:年-月-日。

本题的编写思路如下:第一是要判断闰年,再是判断输入的合法性,再是定义一个类为date,在main中new出一个类为date,最后得到下一天的日期。

需要注意的地方还是关于闰年的那一些东西;在对日期的判断前要先去判断输入数据的合法性。

本题的一些参数如下:

 

 

题目集三   程序7-3

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

public class work73  {
    
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        String temp=in.nextLine();
        
        DvForString dv=new DvForString();
        dv.setPolynthic(temp);//预处理输入的字符串
        dv.print(); //根据合法性作出不同输出
//        System.out.println();
//        dv.printEveryElement();
    }
}
class DvForString{
    private static LinkedList<String> item;//存储各项,此题并不需要
    private static String sign,number,regex,regex1,regex2,regex3,concat,concatend,specialnum,end;//各项正则
//静态区,字符串
 static {
          //静态区只在类被加载时被执行一次
         item=new LinkedList<>();
         sign="(?:[+|-])";
         number="(?:([1-9](\\s*\\d*\\s*)*))";
         //全是常数项时的特判
         specialnum="((?:(\\s*([+|-]?[1-9]+\\s*\\d*)\\s*))*)";
         //带x项,允许系数中间内有空格
         //有不为0的系数和指数
         regex="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))";
         //x只有指数
         regex1="(?:(x\\s*\\^\\s*[+|-]?\\s*([1-9](\\s*\\d*\\s*)*)))";
         //x只有系数
         regex2="(?:(([1-9](\\s*\\d*\\s*)*)\\s*\\*\\s*x))";
         //x前面系数,指数都没有
         regex3="(?:x)";
         concat="("+regex+"|"+regex1+"|"+regex2+"|"+regex3+")";
         //数字和带x项或在一起构成多项式中的一项
         concatend="("+concat+"|"+number+")";
         //多项式表达式 ,首项可以无+,-,  后续项必须有
         end="(?:("+"\\s*"+sign+"?\\s*"+concatend+"(?:\\s*"+sign+"\\s*"+concatend+"\\s*)*"+"))";
    }
    //Polynthic 多项式
    private String Polynthic=null;
    
    public LinkedList<String> getItem() {
        return item;
    }
    //无参构造
    public DvForString(){}
    //有参构造,可以直接通过字符串数组名赋值另一个相同大小和类型的数组
    public DvForString(String polynthic) {
        this.Polynthic = polynthic;
    }
    //对私有变量获取和,赋值的方法
    public String getPolynthic() {
        return Polynthic;
    }
    public void setPolynthic(String polynthic) {
        this.Polynthic = polynthic;
    }
    //合法性检验
    private boolean isLegal() {
        boolean flag=true;
    
        String s=Polynthic;
        //全常数检验
        if(s.matches(specialnum)) 
            return true;
        //复杂多项式检验
        if(!s.matches(end)) flag=false;
        return flag;
    }
    //打印合法输入中每一项
    public  void printEveryElement() {
        System.out.println();
        if(isLegal()) { for(String e: item) System.out.println(e);}
        else System.out.println("Wrong Format");
    }
    //打印
    public void print() {
        if(isLegal()) printLegal();
        else  printNotLegal();//不合法结果打印
    }
    //打印不合法输出
    private void printNotLegal() {
        System.out.println("Wrong Format");
    }
    //打印合法输出
    private  void printLegal() {
        //拷贝一份给字符串s
        String s=Polynthic;
        s=s.replace(" ","");

        char[] t=s.toCharArray();
        for(int i=0;i<s.length()-1;i++) 
            if(t[i]=='^'&&t[i+1]=='-') t[i+1]='#';
    
        //直接通过tostring转换字符数组会出错,所以一个个字符拼接
        String s1="";
        for(int i=0;i<t.length;i++) 
            s1=s1+t[i];
        //再来整体替换-
        s1=s1.replace("^+","^");
        s1=s1.replace("-","+-");
        
        //如果+出现在第一个位置会报异常,要用\\+
        String [] id=s1.split("\\+");
        
        //加号输出标记
        int flag=0;
        //无项时输出标记,标记若最终为0,最后输出0
        int lazy=0;
        //所有常数直接打印不需要特意放+,带x项,对于大于0项,前面有其他项要打印出来
    
        for(String e :id) {
            //切开后对每个元素进行分析处理,#要替换成-
            e=e.replace("#","-");
            if(e!=null) item.addLast(e);
            
            int start=e.indexOf("x");
            int mid=e.indexOf("*");
            int end=e.indexOf("^");
            //x,^都有
            if(start!=-1&&end!=-1) {
                //系数不为1,存在*
                if(mid!=-1) {
                    BigInteger pro=new BigInteger(e.substring(0,mid));
                    BigInteger pos=new BigInteger(e.substring(end+1,e.length()));
                    BigInteger xishu=pro.multiply(pos);//乘法
                    BigInteger zhishu=pos.subtract(new BigInteger(1+""));//减法
                    
                    if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu);
                    else {
                        if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu);
                        else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu);
                        else {
                            if(    xishu.compareTo(new BigInteger(0+""))>0) 
                                if(flag==1) System.out.print("+");
                            System.out.print(xishu+"*x^"+zhishu);
                        }
                    }
                     lazy=1;
                     if(flag==0) flag=1;
                }
                //没有*,系数为负
                else {
                    if(e.charAt(0)=='-') {
                        BigInteger pos=new BigInteger(e.substring(end+1,e.length()));
                        BigInteger xishu=pos.multiply(new BigInteger(-1+""));//乘法
                        BigInteger zhishu=pos.subtract(new BigInteger(1+""));//减法
                        if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu);
                        else {
                            if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu);
                            else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu);
                            else {
                                if(    xishu.compareTo(new BigInteger(0+""))>0) 
                                    if(flag==1) System.out.print("+");
                                System.out.print(xishu+"*x^"+zhishu);
                            }
                        }
                    }
                    //没*,系数不为负
                    else {
                        BigInteger pos=new BigInteger(e.substring(end+1,e.length()));
                        BigInteger xishu=pos.multiply(new BigInteger(1+""));//乘法
                        BigInteger zhishu=pos.subtract(new BigInteger(1+""));//减法
                        if(zhishu.equals(new BigInteger(0+""))) System.out.print(xishu);
                        else {
                            if(xishu.equals(new BigInteger(1+""))) System.out.print("x^"+zhishu);
                            else if(xishu.equals(new BigInteger(-1+"")))System.out.print("-x^"+zhishu);
                            else {
                                if(    xishu.compareTo(new BigInteger(0+""))>0) 
                                    if(flag==1) System.out.print("+");
                                System.out.print(xishu+"*x^"+zhishu);
                            }
                        }
                    }
                    lazy=1;
                    if(flag==0) flag=1;
                }
            }//^,x都存在的分界线
            //有x,没有指数
            else if(start!=-1) {
                //有*
                if(mid!=-1) { 
                    BigInteger num=new BigInteger(e.substring(0, mid));
//                    if(num.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+");
                    System.out.print(num);                
                }
                //没有*
                else {
                    BigInteger num=null;
                    if(e.charAt(0)=='x') num=new BigInteger("1");
                    else if(e.charAt(0)=='-') num=new BigInteger("-1");
                    else 
                        num=new BigInteger(e.substring(0, start));
//                    if(num.compareTo(new BigInteger(0+""))>0) if(flag==1) System.out.print("+");
                    System.out.print(num);
                }
                lazy=1;
                if(flag==0) flag=1;
            }
            //常数
            else 
                System.out.print("");
        }//大for结尾
        if(lazy==0) System.out.println(0);
    }
}

输出结果:

1.
4*x^-3+60*x^11-4

2.
Wrong Format

本题的一些要求如下:

输入格式:

在一行内输入一个待计算导函数的表达式,以回车符结束。

输出格式:

如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”;

如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;

当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;

当输出结果第一项系数符号为“+”时,不输出“+”;

当指数符号为“+”时,不输出“+”;

当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。

以下是编写本题的一些思路与心得:静态区只能在类被加载时被执行一次;直接通过tostring转换字符数组会出错,所以一个个字符拼接;所有常数直接打印不需要特意放+,带x项,对于大于0项,前面有其他项要打印出来;无项时输出标记,标记若最终为0,最后输出0;有参构造,可以直接通过字符串数组名赋值另一个相同大小和类型的数组。

三、心得总结

从刚开始Java的入门到现在已经有一个多月了,在编程的思路方面我觉得我已经有了一些改变。首先是从学习c语言时期的面向过程的思维在慢慢向面向对象的思维改变,体会到了使用类的方便与实用之处。但还是有很多的方面都存在着不足,比如编写程序时首先想到的还是整块的main一main到底,在完善改进的时候才会发现这些错误。

希望在作业中能够多多锻炼自己的面向对象思维,在这一学期的学习中能收获更多的知识提升自己代码编写的综合水平。

posted on 2021-10-15 22:30  Rajay_Liu  阅读(39)  评论(0编辑  收藏  举报

导航