SPL Programming Exercise - Chapter 2 Doing Judgment

 

2.1 Logic operation

1.Determine whether the following expressions are true or false, and run them in SPL to see the results.

A
1 =50>1 && 5-3<1
2 =50>1 || 5-3<1
3 =!(50<1)
4 =!100 && 5>1 && 9/4>2
5 =10\3==3 || 10%3!=3 && !10
6 =(10\3==3 || 10%3!=3) && !10

2.Programming Exercises

(1) Enter any natural number to determine whether it is odd or even.

(2) A certain qualification exam has three subjects, and passing any two subjects (>=60 points) is considered qualified.

(3) Enter any number and determine if it is positive, negative, or zero.

(4) Enter a positive integer to determine if it is a two-digit number.

(5) Enter two numbers and output them in descending order.

2.2 Branching structure

1.Determine code blocks for A1, B2, B4, A5, and A6 based on grid layout.

A B C D
1 X X
2 X X
3 X X
4 X X
5 X X
6 X X

2.Programming Exercises

(1) Using branch structure to implement the segmented function:

imagepng

(2) Develop a student performance system, output A if the score is greater than 80 points, output B if the score is greater than 70 points but less than 80 points, output C if the score is greater than 60 points but less than 70 points, and output D if the score is less than 60 points.

(3) Given three sides, determine if a triangle can be formed, and if so, calculate the perimeter and area.

Tip: The formula for calculating the area of a triangle using its side length is Helen’s formula

imagepng, p is half of the perimeter

(4) Enter 3 numbers and output them in descending order.

(5) Enter any year and month to determine how many days there are in that year and month (Tip: Calculation method for leap years: years can be divided by 4 and cannot be divided by 100; or integer years can be divided by 400).

2.3 Comments and jumps

1.What are the values of A1 after running the following two pieces of code? When A1 inputs 6, What are they respectively?

A B C
1 2
2 if A1>5 >A1=A1+4 >A1=A1+5
3 else goto C2
A B
1 2
2 if A1>5 goto A4
3 >A1=A1+5
4 >A1=A1+4

2.Programming exercise: Using if and goto statements to calculate the sum of 1+2+3+…+100

Suggestedanswers:

2.1 Logic operation

2.

(1)

A
1 6
2 =if(A1%2==0,“even”,“odd”)

(2)

A
1 95 /Subject1
2 83 / Subject2
3 76 / Subject3
4 =if((A1>=60 && A2>=60)||(A1>=60 && A3>=60)||(A2>=60 && A3>=60),“pass”,“fail”)

(3)

A
1 3
2 =if(A1>0:“Positive”,A1<0:“Negative”,A1==0:“Zero”)

(4)

A
1 15
2 =if(A1>=10 && A1<100, output(“Yes”), output(“No”))

(5)

A B
1 15 23
2 =if(A1>=B1, output(A1,B1),output(B1,A1))

2.2 Branching structure

1.

The code block for A1 is B1: D4

The code block for B2 is C2: D3

The code block for B4 is C4: D4

The code block for A5 is B5: D5

The code block for A6 is B6: D6

2.

(1)

A B
1 5
2 if A1>1 >B1=3*A1-5
3 else if A1>=-1 && A1<=1 >B1=A1+2
4 else >B1=5*A1+3

A1 Input the value of x

Store the calculation result of B1 function in B1

(2)

A B C
1 90 /Input score
2 if A1>=0 && A1<=100 if A1>=80 >output(“A”)
3 else if A1>=70 >output(“B”)
4 else if A1>=60 >output(“C”)
5 else >output(“D”)
6 else >output(“Illegal score”)

(3)

A B C
1 3 4 5
2 if A1 + B1 > C1 && A1 + C1 > B1 && B1 + C1 > A1 =A1+B1+C1 =B2/2
3 =sqrt(C2*(C2-A1)*(C2-B1)*(C2-C1))
4 >output(B2,B3)
5 else >output(“This side length group does not form a triangle”)

(4)

A B C
1 23 62 15
2 if A1<B1 =A1
3 >A1=B1
4 >B1=B2
5 if A1<C1 =A1
6 >A1=C1
7 >C1=B5
8 if B1<C1 =B1
9 >B1=C1
10 >C1=B8
11 >output(A1,B1,C1)

(5)

A B C
1 2023 /Input year
2 2 /Input month
3 if A2==2 if A1%4==0 && A1%100!=0 || A1%400==0 >output(29)
4 else >output(28)
5 else if A2==1 || A2==3 || A2==5 || A2==7 || A2==8 || A2==10 || A2==12 >output(31)
6 else >output(30)

2.3 Comments and jumps

2.

A B C D
1 1
2 if A1<=100 >B1=B1+A1 >A1=A1+1 goto A2