2021年3月30日 星期二

C++ Lesson 4 Assignment

 

學習目標:

1.      複習所有曾學習的C++指令,包括cin, cout, if..else, for loop及變量的設定

2.      應用以上的指令。

 

參考文件: 所需語法

 

Assignment 4-1

Calculate the area of a square

-          Let the user enter the length and then output the area of the square.

The output is as follow:



 

Assignment 4-2

Calculate the area of a circle

-          Let the user enter the radius and then output the area of the circle



 

Assignment 4-3

To judge whether a mark means passed or failed.

-          Let the user enter a mark. If the mark is larger than 60, output “passed. If not, output “failed”.





 

Assignment 4-4

To judge whether a number can be divided by 3.

-          Let the user enter a number, if the number can be divided by 3, output “ this number can be divided by 3. If not, output “this number cannot be divided by 3”.



Assignment 4-5

Enter a number and then output the same number of “*” in one line.



Assignment 4-6

Calculate the sum of all the numbers between two numbers.

-          Enter two number, output the sum of the all the numbers between these two numbers.


Assignment 4-7

Output a square made of “*”

-          Enter a number, and output a square with the same number of “* at each side.



Assignment 4-8

Output a right-angled triangle

-          Enter the base length of a right-angled triangle and then output the right-angled triangle



Assignment 4-9

Use loop function to output a 9x9 Table


Assignment 4-10

To judge a number whether it is a prime number

-          Enter a number and then judge whether it is a prime number.



Assignment 4-11

Output all the prime numbers between 1 to 100


Assignment 4-12
Let the user to enter a number, and then output all the prime numbers between 1 and the entered number.




C++ 運算符

算術運算子

運算

C++運算子

使用語法

結果

+

10+7

17

-

10-7

3

*

10*7

70

(取商數)

/

10/7

1

(取餘數)

%

10/7

3

正數

+

+10

+10

負數

-

-10

-10

 

關係運算子

關係運算子

功能

用法

大於

a>b

小於

a<b

>=

大於或等於

a>=b

<=

小於或等於

A<=b

==

等於

A==b

!=

不等於

a!=b

邏輯運算子

運算子

 

用法

&&

AND

a>b && a<c

II

OR

a>b II a<c

!

NOT

! (a>b)

 

2021年3月24日 星期三

C++ 第三課練習說明

 學習目標:

1.      算術運算

2.      條件式

 

Exercise 3-1

留意不同算術運術符的真正輸出。

 


學習重點:

1.      其實也大概可看到輸出結果,祗是數目字連在一起,較難查看。

2.      留意跟數學的算術運算符跟C++電腦語言的差異:

乘是 * 而不是 x  (於電腦上這幾乎是通用的)

除是 / 而不是 ÷ ,不過作用不完全一樣,/ 祇輸出商的整數部分,沒有小數點。

取餘數是 %5%2 的餘數是1

3.      留意,除了+ , - * 相對標準之外,其他的算術運算符,不同的電腦語言可能不同。

 

## 如何可把算式及結果於同一行輸出。例如: 5+2=7 ?

 

Exercise 3-2

輸入一個數目字,程式判斷是否少於某一數值,然後輸出結果。

例如: 輸入 5,輸出 5 is smaller than 10

複習: 輸入所需的指令。


 

學習重點:

1.      判斷語句(if)的指令語言,如下:

if (判斷條件)

{

   指令1(如符合以上條件的話,便執行)

   }

  ## 留意,符合相關條件下要執行的指令都要於{ } 內撰寫。

 

Exercise 3-3


 

 

學習要點:

1.      學習如果(符合某條件)…(做某些事),否則…(便做其他事) C++ 指令語法。

if (條件式) {   } else {  }

分行撰寫的話,如下:

if (條件式){

   指令

}

Else

{

    指令

}

 

Exercise 3-4

輸入一個數字,程式判斷這數字是否能否3整除,如可以的話,便輸出can be divided by 3,如不可以的話,便輸出cannot be divided by 3


 

學習要點:

1.      這練習其實是要綜合使用算術運算符號。如何知道是否能被3整除? 能整除的話,除以3後應沒有餘數,即是0。如有餘數,即不是0

a%3 的作用是把a除以3,得出餘數。

 

 

Exercise 3-5 

簡化if …else 的語法


 

學習要點:

1.      如果符合條件後,祇有一包指令的話,那可以省略{ }

2.      不過,強烈建議縮排處理(如上),可以更易看清楚程式。

3.      不用省略方法也可以,以免自己忘記了正常是應該有{ } 的。


如何於C++產生亂數?

 如 何於 C++ 產生亂數 ?   以下是一個例子,輸出一個由 0-4 的亂數。留意,行號 2,3,8,9,10 及 11 是需要的。 備註 : 如要輸出 0-9 的話,把行 11 的 rand()%5 改成 rand()%10 便可以。   -----...