# 20230918 課堂作業
## ch03-12 各種字面常數表示法的應用
```康軒勻 1206219 資訊一乙19號
#include<iostream>
using namespace std;
int main()
{
int i; double d;
i = 074; d = 6.02e22;
cout << "i= " << i << " d= " << d << endl;
i=0xA38; d = 3.14159e-1;
cout << "i= " << i << " d= " << d << endl;
}
```

執行結果:

## ch03-18讓使用者從鍵盤輸入資料
```
#include<iostream>
using namespace std;
int main()
{
int grade;
int student_ID;
cout << "請問你是幾年級的學生:";
cin >> grade;
cout << "請問你得學號是:";
cin >> student_ID;
cout << "你是 " << grade << "年級的學生" << endl;
cout << "學號是" << student_ID;
}
```

執行結果:

## ch03-19 換匯程式
```
#include<iostream>
using namespace std;
#define US 29.5 //
#define JP 0.277 //
int main() {
double money;
cout << "請輸入要兌換的金額:";
cin >> money;
cout << "可兌換" << money / US << "美金\n";
cout << "或兌換" << money / JP << "日幣";
}
```

執行結果:

## ch03-20 交換兩變數的值
```
#include <iostream>
using namespace std;
int main()
{
int a = 10 , b = 20;
int temp;
cout << "交換前 a = " << a << "\t b = " << b << endl;
temp = a;
a=b;
b=temp;
cout << "交換後 a = "<< a << "\t b= " << b << endl;
}
```


## ch03-21 大小寫轉換
```
#include <iostream>
using namespace std;
int main()
{
char upper = 'S', lower;
lower = upper + 32 ;
cout << upper << "的小寫是" << lower << endl;
}
```

執行結果:

## 心得感想
在這次的程式實習中 我學會了許多程式的技巧及相關技能 例如: 字面常數的表示法 使用者從鍵盤輸入資料 換匯 變數值互換 大小寫字母轉換等實用程式 為我的未來學習程式之路打下了堅實的基石。
## 實作練習:
### p74-1:
```
#include<iostream>
using namespace std;
#define SIZE 10
int main()
{
cout << SIZE ;
}
```

執行結果

### p74-2:
```
#include <iostream>
using namespace std;
int main()
{
int a ;
a = 10 ;
int b ;
b = 101.7;
int c ;
c = 'c' ;
}
```

執行結果:

### p74-5:
```
#include <iostream>
using namespace std;
int main()
{
cout << "我正在學習C++語言" ;
}
```

執行結果:

### p74-7:
```
#include <iostream>
using namespace std;
int main()
{
int a = 10 , b = 20, c = 30;
int temp;
cout << "交換前 a = " << a << "\t b = " << b << "c = " << c << endl;
temp = a;
a=b;
b=temp;
temp= a;
a=c ;
c=temp;
cout << "交換後 a = "<< a << "\t b = " << b << "c = " << c << endl ;
}
```

執行結果:

# 20230925課堂作業(CH4運算子與運算式)
## CH04-05 前置與後置遞增算
程式碼:
```
#include<iostream>
int main()
{
int i = 100, j = 100, k = 100;
i++;
std::cout << "變數i的值為:"<< i;
j--;
std::cout << "\n變數j的值為:" << j;
k++;
k--;
std::cout << "\n變數 k 的值為:" << k;
}
```
程式碼實作畫面:

執行結果:

## CH04-06 關係運算子
程式碼:
```
#include<iostream>
using namespace std;
int main()
{
int i = 3, j = 3;
cout << boolalpha;
cout << "(i == j) :" << (i == j) << endl;
cout << "(i > j) :" << (i > j) << endl;
cout << "(++i > j):"<< (++i > j) << endl;
cout << "(j-- < 3):"<< (j-- < 3) << endl;
cout << "(i != j):" << (i !=j) << endl;
}
```
程式實作畫面:

執行結果:

## CH04-07邏輯運算子練習
程式碼:
```
#include<iostream>
using namespace std;
int main()
{
int i = 3;
bool b = false;
cout << boolalpha,
cout << "i = " << i << "\tb =" << b << endl;
cout <<"i && b : " << (i && b) << endl;
cout << "i || b : " << (i || b) << endl;
cout << "i &&!b: " << (i &&!b) << endl;
cout << "i ||!b:" " << (i ||!b)" << endl;
}
```
程式實作畫面:

執行結果:

## 0923實習心得
在本次的程式設計實習中,我學習到了程式中的許多運算技巧,例如邏輯運算子 關係運算子 前置與後置的遞增與遞減等等 讓我對程式的計算與比較有了初步的認識 雖說只是基礎中的基礎 但千里之行始於足下 想必這些未來業會對我很有幫助的
## ch04-08 位元運算子練習
程式碼:
```
#include<iostream>
using namespace std;
int main()
{
short i = 9, j = 25;
cout << "i >> 1 = " << (i >> 1) << endl;
cout << "i << 1 = " << (i << 1) << endl;
cout << "~i = " << (~i) << '\n' << endl;
cout << "i = " << i << "\tj =" << j << endl;
cout << "i & j = " << (i & j) << endl;
cout << "i | j = " << (i | j) << endl;
cout << "i ^ j = " << (i ^ j) << endl;
}
```
程式實作畫面:

執行結果:

計算過程:


## 實作練習
### 攝氏溫度轉華氏溫度
程式碼:
```
#include<iostream>
using namespace std;
int main()
{
double c; // 攝氏溫度
double f; // 華氏溫度
cin >> c;
f=9/5*c+32;
cout << "攝氏 " << c << " 度 = 華氏 " << f << " 度";
}
```
程式碼實作畫面:

執行結果:

### 自動販售機(幣值轉換練習)
程式碼:
```
#include <iostream>
using namespace std;
int main ()
{
int money,ten,five,one;
cout << "請輸入您的換幣金額:";
cin >> money;
ten = money / 10;
five = (money%10)/5;
one = (money%10)%5;
cout << money << "元共可兌換零錢:" << "可樂" << ten << "個"
<< "汽水" << five << "個" << "饅頭" << one << "個";
}
```
程式碼實作面:

執行結果:

## 0925實習心得
在本次的程式設計實習中 我學會了位元運算子 幣值換算 攝氏華視互相轉換等程式以及其內部的運算 除此之外 我也學會了二進制 十進制 眾多特殊符號的計算 其中有許多符號我背不起來或理解不起來 想必未來也必須多花點時間加強這一單元
# 20231016 課堂作業(CH5流程控制)
## 本章應用實例 (HW)
### 05-24電影分級制度
執行畫面:

程式碼:
```
#include<iostream>
using namespace std;
int main()
{
int age_range;
cout << "請輸入年齡 (0~120):";
cin >> age_range;
switch(age_range){
case 0 ... 5:
cout << "可看普遍級";
break;
case 6 ... 11:
cout << "可看普遍級及保護級";
break;
case 12 ... 17:
cout << "可看限制級以外的影片";
break;
case 18 ... 120:
cout << "可看各級影片";
break;
defauit:
cout << "您並未輸入正確的年齡!";
}
}
```
實作畫面:

### 05-27 猜數字遊戲
程式碼:
```
#include <iostream>
using namespace std;
int main()
{
int n, ans=7;
while (n !=ans){
cout << "猜猜看數字是多少?(1~10):";
cin >> n;
if (n < ans){
cout << "再大一點";
}
else if(n > ans){
cout << "再小一點";
}
}
cout << "猜對了! YA!";
}
```
實作畫面:

執行畫面:

### 05-28 2的N次方
執行畫面:

程式碼:
```
#include <iostream>
using namespace std;
int main()
{
int n, p = 1, i = 1;
cout << "請輸入n值以求得2的n次方值:";
cin >> n;
while (i <= n){
p = p * 2;
i++;
}
cout << "2的"<< n << "次方為:" << p;
}
```
實作畫面:

### 05-29 成績加總
執行畫面:

程式碼:
```
#include<iostream>
using namespace std;
int main()
{
int sum = 0, n = 0;
do{
sum = sum + n;
cout << "請輸入成績:";
cin >> n;
}while(n !=-1);
cout << "總分為:"<<sum;
}
```
實作畫面:

### 5-31 顯示1~50的質數
程式碼:
```
#include <iostream>
using namespace std;
int main()
{
bool a = 1;
for(int i = 2;i <= 50; i++){
for(int j=2;j <=i - 1; j++){
if(i % j==0){
a = 0;
}
}
if(a == 1){
cout << i << " ";
}
a = 1;
}
}
```
實作畫面:

執行畫面:

### 05-32 階層計算機
程式碼:
```
#include <iostream>
using namespace std;
int main()
{
while(true){
cout << "請輸入 1-170 間的整數(輸入0即結束程式):";
int num = 0;
cin >> num;
if (num == 0)
break;
cout << num << "! 等於";
double fact;
for (fact = 1; num > 0;num--)
fact = fact *num;
cout << fact << "\n\n";
}
cout << "謝謝您使用階承計算機。";
}
```
實作練習:

執行畫面:

## 課後練習
### 1.心理測驗(if...else if應用)
執行畫面:

程式碼:
```
#include<iostream>
using namespace std;
int main()
{
int chose;
cout<<"發生船難,你選擇留下什麼?\n";
cout<<"選擇(1)留下老人和孕婦\n";
cout<<"選擇(2)留下老人和嬰兒\n";
cout<<"選擇(3)留下老人或金銀財寶\n";
cout<<"選擇(4)留下孕婦和嬰兒\n";
cout<<"選澤(5)留下孕婦和金銀財寶\n";
cout<<"選擇(6)留下嬰兒和金銀財寶\n";
cin >> chose;
if (chose==1)
cout <<"你的感情很細膩,善於觀察細節,在感情中,\n相比於戀人的甜言蜜語,你更在乎對方實際的行動。\n";
else if (chose==2)
cout <<"在感情中,你不僅洞察力超好,第六感也很強,\n越是親近的人這種直覺越敏銳,所以另一半對你很難有所隱藏,因為你可以憑借著蛛絲馬跡得到你想要知道的消息。\n";
else if (chose==3)
cout <<"你是個典型的顏控,在擇偶時很注重另一半的外表和身高。\n";
else if (chose==4)
cout <<"面對感情你很承盾,一方面你很感性,渴望浪漫熱烈的愛情;\n另一方面你又很理性,明白現實的殘酷和金錢的重要性\n";
else if (chose==5)
cout <<"在感情方面你很挑剔,所以很難遇到心動的對象。不過在戀愛時你卻很專一\n,一旦喜歡上某個人,你就會全心全意的對他好,所以和你談戀愛是一件很幸福的事。\n";
else if (chose==6)
cout <<"在感情中你很缺乏安全感,有時候會忍不住通過試探或考驗的方式去確認對方是否愛你。\n";
else cout <<"亂輸入,活該一輩子舔狗命!!!";
}
```
實作畫面:

### 2. 判斷座標(x,y)在第幾象限。 (巢狀if…else 應用)
執行畫面:

程式碼:
```
#include <iostream>
using namespace std;
int main()
{
int x, y;
cout << "請輸入x值(不可輸入0):";
cin >> x;
cout << "請輸入y值(不可輸入0):";
cin >> y;
if(x > 0){
if(y > 0){
cout << "("<< x << "," << y << ")在第一象限";
}else{
cout << "(" << x << "," << y << ")在第四象限";
}
}else {
if(y>0){
cout <<"(" << x << ", " << y << ")在第二象限";
}else {
cout << "(" << x << "," << y << ")在第三象限";
}
}
}
```
實作畫面:

### 3.用*組成三角形(for迴圈的應用)
執行畫面:

程式碼:
```
#include<iostream>
using namespace std;
int main()
{
int s,t;
cout << "請輸入層數:";
cin >> s;
//正直角三角形
for(int i=1; i<=s; i++) {
for(int k=1; k<=i; k++) {
cout<<"*";
}
cout<<endl;
}
cout<<endl;
//倒直角三角形
for(int i=1; i<=s; i++) {
for(int k=1; k<=s-i+1; k++) {
cout<<"*";
}
cout<<endl;
}
cout<<endl;
//空白正直角三角形
for(int i=1; i<=s; i++) {
for(int j=1; j<=s-i; j++) {
cout<<" ";
}
for(int k=1; k<=i; k++) {
cout<<"*";
}
cout<<endl;
}
cout<<endl;
//空白倒直角三角形
for(int i=1; i<=s; i++) {
for(int j=1; j<=i-1; j++) {
cout<<" ";
}
for(int k=1; k<=s-i+1; k++) {
cout<<"*";
}
cout<<endl;
}
}
```
實作畫面:

### 4.十二生肖查詢(switch…case的應用)
執行畫面:

程式碼:
```
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "請輸入民國幾年:";
cin >> year ;
switch (year%12) {
case 1 :
cout << "鼠\n";
break;
case 2:
cout << "牛\n";
break;
case 3 :
cout << "虎\n";
break;
case 4 :
cout << "兔\n";
break;
case 5 :
cout << "龍\n";
break;
case 6 :
cout << "蛇\n";
break;
case 7 :
cout << "馬\n";
break;
case 8 :
cout << "羊\n";
break;
case 9 :
cout << "猴\n";
break;
case 10 :
cout << "雞\n";
break;
case 11 :
cout << "狗\n";
break;
case 12 :
cout << "豬\n";
break;
}
}
```
實作畫面:


### 5.閏年
執行畫面:

程式碼:
```
#include<iostream>
using namespace std;
int main() {
int year;
cout << "請輸入一個西元年";
cin >> year;
if((year%4)==0)
cout << year << "年是閏年" <<endl;
else{
cout << year << "年非閏年" <<endl;
}
}
```
實作畫面:

### 6重複計算BMI(while 迴圈)
執行畫面:

程式碼:
```
#include<iostream>
using namespace std;
int main()
{
char go_again = 'y';
float height, weight;
while(go_again == 'Y' || go_again == 'y') {
cout << "請輸入身高(公分):";
cin >> height;
cout << "請輸入體重(公斤):";
cin >> weight;
cout << "您的BMI為:" << weight / (height * height)*10000 << endl;
cout << "要繼續計算另一位嗎?" <<
"(要請輸入Y或y,若不要請輸入非Y或y的字元):";
cin >> go_again;
}
}
```
實作畫面:

### 7計算兩整數的最大公因數(while 迴圈)
執行畫面:

程式碼:
```
#include<iostream>
using namespace std;
int main()
{
int num1,num2,temp;
cout << "計算兩整數的最大公因數\n";
cout << "請輸入第1個數字: ";
cin >> num1;
cout << "請輸入第2個數字: ";
cin >> num2;
while (num2!=0) {
temp = num1%num2;
num1 = num2;
num2 = temp;
}
cout << "最大公因數是:" << num1;
}
```
實作畫面:

### 8密碼檢驗程式
執行畫面:

程式碼:
```
#include<iostream>
using namespace std;
int main()
{
int pwd;
int i=1;
do{
if (i<=3){
cout << "請輸入第" << i <<"次ATM密碼:";
cin >> pwd;}
if (i>3) {
cout << "密碼輸入錯誤3次,無法登入。";
break;}
if (pwd!=123)
cout << "密碼錯誤,請再輸入一次...\n";
i++;
}while (pwd!=123);
if (pwd==123)
cout << "歡迎進入本系統";
}
```
實作畫面:

## ch05心得感想:這一次的程式設計實習中,while跟for是我覺得最難又最重要的,首先,它們的使用方式就有一定的難度,需要複習好幾次才能懂,但我覺得它其中的邏輯才是最困難的,許多時候課本的範例我甚至不知道為什麼會那樣,然而,這部分又很重要,也就是說,我需要多複習,否則基礎不好,之後就麻煩了。
# 20231106 課堂作業(CH6函式)
## 隨堂練習
### Ch06-21 以時間亂數種子產生5個500~1000的亂數
程式碼:
```
//訊一乙 19號 康軒勻
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
srand(time(0));
for (int i = 0; i < 5; i++)
cout << (rand() %501) + 500 << endl;
}
```
程式實作畫面:

執行結果:

## 應用實例練習
### ch06-24 查詢語音費率程式
程式碼:
```
#include<iostream>
using namespace std;
void bill(int a){
switch(a){
case 196:
cout << "語音費率每秒:0.0869元";
break;
case 396:
cout << "語音費率每秒:0.0782元";
break;
case 796:
cout << "語音費率每秒:0.0696元";
break;
default:
cout << "無此費率,請重新查詢!";
}
}
int main()
{
int a;
cout << "請輸入要查詢的月租方案(196、396、796):";
cin >> a;
bill(a);
}
```
程式實作畫面:

執行結果:

### Ch06-25 猜數字遊戲
程式碼:
```
//訊一乙 19號 康軒勻
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int number(){
int num = (rand()%10)+1;
return num;
}
int main()
{
srand(time(0));
int n, ans;
ans = number();
while (n !=ans){
cout << "猜猜看數字是多少?(1~10):";
cin >> n;
if(n > ans)
cout << "猜錯了,再小一點\n";
else if(n < ans)
cout << "猜錯了,再大一點\n";
}
cout << "猜對了!YA!";
}
```
程式實作畫面:

執行結果:

### Ch06-26 猜拳遊戲
程式碼:
```
//訊一乙 19號 康軒勻
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void game(int user){
int com = (rand() % 3)+1;
if(user == com){
cout << "平手";
}else if(user == 1){
if(com ==2)
cout << "你出剪刀,電腦出石頭,你輸了";
else
cout << "你出剪刀,電腦出布,你贏了";
}else if(user == 2){
if(com == 3)
cout << "你出石頭,電腦出布,你輸了";
else
cout << "你出石頭,電腦出剪刀,你贏了";
}else if(user == 3){
if(com == 1)
cout << "你出布,電腦出剪刀,你輸了";
else
cout << "你出布,電腦出石頭,你贏了";
}
}
int main()
{
int user;
srand(time(0));
cout << "請輸入你要出的拳(1.剪刀 2.石頭 3.布):";
cin >> user;
game(user);
}
```
程式實作畫面:


執行畫面:

### ch06-27 階乘函式
程式碼:
```
#include<iostream>
using namespace std;
double factorial(int);
int main()
{
while(true){
cout << "請輸入 1-170間的整數(輸入0即結束程式):";
int num = 0;
cin >> num;
if (num == 0)
break;
cout << num << "! 等於 " << factorial(num) << endl;
}
cout << endl << "謝謝您使用階乘計算機。";
}
double factorial(int n)
{
double fact;
for(fact=1 ; n>0 ; n--)
fact = fact * n;
return fact;
}
```
實作畫面:

執行畫面:

### Ch06-28 猜點數遊戲
程式碼:
```
//訊一乙 19號 康軒勻
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int dice();
int main()
{
srand(time(0));
int num1, num2;
cout << "輸入1~6其中一個數字,猜骰子點數:";
cin >> num1;
while(num1!=0){
num2 = dice();
cout << "骰子的點數是:"<< num2 << "\n";
if (num2 == num1)
cout << "猜中了";
else
cout << "沒猜中";
cout << ",再試一次吧!\n"
<< "輸入1~6其中一個數字,猜骰子點數(或輸入0已結束遊戲):";
cin >> num1;
}
}
int dice()
{
int a;
a = (rand() %6)+1;
return a;
}
```
程式實作畫面:

執行畫面:

## 課後練習
### 1.組成三角形
程式碼:
```
#include<iostream>
#include<cmath>
using namespace std;
int triangle (double,double,double);
int main()
{
double a,b,c,t;
cout << "請輸入三角形的三個邊長:\n";
cin >> a;
cin >> b;
cin >> c;
t = triangle(a,b,c);
if(t == 1){
cout << a << "、" << b << " 、 " << c << "可組成三角形\n";
}else{
cout << a << "、" << b << " 、 " << c << "無法組成三角形\n";
}
}
int triangle (double a, double b, double c){
if ((a+b) > c &&(b+c)>a&&(a+c)>b){
return 1;
}else{
return 0;
}
}
```
實作畫面:

執行畫面:

### 2.遞迴
程式碼:
```
//資訊一乙 康軒勻
#include<iostream>
using namespace std;
int main()
{
void func();
func();
}
void func()
{
cout << "這是一種遞回函式\n";
func();
}
```
實作畫面:

執行畫面:

### 3.專案練習
程式碼:
```
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void game(int user);
int dice();
void func();
void p01();
void p02();
void p03();
void p04();
void p05();
void p06();
void p07();
int main() {
string menuItem[] = {
"[1]猜數字遊戲",
"[2]猜拳遊戲",
"[3]猜點數遊戲",
"[4]查詢語音費率函式",
"[5]階乘函式",
"[6]組成三角形",
"[7]遞迴",
"[0]離開"
};
int i, num;
int selMenu = 99;
while (selMenu != 0) {
system("chcp 950"); // 編碼
system("cls"); // 清除畫面
cout << "程式設計實習 資訊一乙 19 康軒勻\n";
cout << "-----------------------------" << endl;
int arrLength = sizeof(menuItem) / sizeof(menuItem[0]); // 讀取幾個Item
for (i = 0; i < arrLength; i++) {
cout << menuItem[i] << endl;
}
cout << "請輸入選項:";
cin >> selMenu;
switch (selMenu) {
case 1:
p01();
break;
case 2:
p02();
break;
case 3:
p03();
break;
case 4:
p04();
break;
case 5:
p05();
break;
case 6:
p06();
break;
case 7:
p07();
break;
}
system("PAUSE");
cout << "\n";
}
}
void p01() {
srand(time(0));
int n, ans;
ans = (rand() % 10) + 1;
while (n != ans) {
cout << "猜猜看數字是多少?(1~10):";
cin >> n;
if (n > ans)
cout << "猜錯了,再小一點\n";
else if (n < ans)
cout << "猜錯了,再大一點\n";
}
cout << "猜對了!YA!\n";
}
void p02() {
int user;
srand(time(0));
cout << "請輸入你要出的拳(1.剪刀 2.石頭 3.布):";
cin >> user;
game(user);
}
void game(int user) {
int com = (rand() % 3) + 1;
if (user == com) {
cout << "平手";
} else if (user == 1) {
if (com == 2)
cout << "你出剪刀, 電腦出石頭, 你輸了";
else
cout << "你出剪刀, 電腦出布, 你贏了";
} else if (user == 2) {
if (com == 3)
cout << "你出石頭, 電腦出布, 你輸了";
else
cout << "你出石頭, 電腦出剪刀, 你贏了";
} else if (user == 3) {
if (com == 1)
cout << "你出布, 電腦出剪刀, 你輸了";
else
cout << "你出布, 電腦出石頭, 你贏了";
}
}
void p03() {
srand(time(0));
int num1, num2;
cout << "輸入1~6其中一個數字,猜骰子點數:";
cin >> num1;
while (num1 != 0) {
num2 = dice();
cout << "骰子的點數是:" << num2 << "\n";
if (num2 == num1)
cout << "猜中了";
else
cout << "沒猜中";
cout << ",再試一次吧!\n"
<< "輸入1~6其中一個數字,猜骰子點數(或輸入0已結束遊戲):";
cin >> num1;
}
}
int dice() {
return (rand() % 6) + 1;
}
void bill(int a) {
switch (a) {
case 196:
cout << "語音費率每秒:0.0869元";
break;
case 396:
cout << "語音費率每秒:0.0782元";
break;
case 796:
cout << "語音費率每秒:0.0696元";
break;
default:
cout << "無此費率,請重新查詢!";
}
}
void p04() {
int a;
cout << "請輸入要查詢的月租方案(196、396、796):";
cin >> a;
bill(a);
}
double factorial(int n) {
double fact;
for (fact = 1; n > 0; n--)
fact = fact * n;
return fact;
}
void p05() {
while (true) {
cout << "請輸入 1-170間的整數(輸入0即結束程式):";
int num = 0;
cin >> num;
if (num == 0)
break;
cout << num << "! 等於 " << factorial(num) << endl;
}
cout << endl << "謝謝您使用階乘計算機。";
}
int triangle(double a, double b, double c) {
if ((a + b) > c && (b + c) > a && (a + c) > b) {
return 1;
} else {
return 0;
}
}
void p06() {
double a, b, c;
cout << "請輸入三角形的三個邊長:\n";
cin >> a;
cin >> b;
cin >> c;
if (triangle(a, b, c) == 1) {
cout << a << "、" << b << " 、 " << c << "可組成三角形\n";
} else {
cout << a << "、" << b << " 、 " << c << "無法組成三角形\n";
}
}
void p07() {
cout << "這是一種遞回函式\n";
func();
}
void func() {
cout << "這是一種遞回函式\n";
p07();
}
```
實作畫面:








執行畫面:

## 心得感想(11/06)
在本次的程式設計實習中,我認為難度頗高的,因為這章節不僅僅加了許多複雜的東西,更要背許多的函式及公式,前面的程式基礎更是要好,然而,到後面只會越來越難而已,所以我們上課更該認真專心,回家後也要花些許時間複習才能跟得上.
## 心得感想(11/20)
這是老師第一次要我們做專案練習,說實話這個東西我一開始完全不會也不懂,尤其是除錯時它顯示的錯誤訊息我也根本沒看過,導致我不知道如何修改我的程式,好在老師最後有教我們怎麼做,我們才順利把它做出來.
# 20231204 課堂作業(CH7陣列與指標)
## 12/11隨堂練習
### ch07-03 請使用者輸入8個數字,找出最大值
程式碼:
```
//資訊一乙 康軒勻
#include <iostream>
#define SIZE 8
using namespace std;
int main()
{
int numbers[SIZE];
cout << "請輸入8個數字,程式將找出最大值"<< endl;
for (int i = 0; i < SIZE;i++){
cout << "請輸入第" << (i+1) << "個數字:";
cin >> numbers[i];
}
int MAX = numbers[0];
for(int i = 1; i <SIZE;i++)
if (numbers[i] > MAX)
MAX = numbers[i];
cout << "在輸入的數字中,數值最大的是" << MAX;
}
```
實作畫面:

執行畫面:

### ch07-04氣泡排序法
程式碼:
```
#include <iostream>
#define SIZE 5
using namespace std;
int main()
{
char array[SIZE]={'r','o','g','e','r'};
cout << "排序前:";
for (int i = 0; i < SIZE; i++)
cout << array[i];
for (int i = 1; i <SIZE; i++)
for (int j = 0; j < SIZE-i; j++)
if (array[j] < array[j + 1])
{
char temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
cout << endl << "排序後:";
for (int i = 0; i < SIZE; i++)
cout << array[i];
}
```
實作畫面:

執行畫面:

### ch07-05 選擇排序法
程式碼:
```
//資訊一乙
#include<iostream>
#define SIZE 5
using namespace std;
int main()
{
int a[SIZE] = {25,12,47,18,10};
int s, temp;
cout << "排序前:";
for (int i = 0;i < SIZE; i++)
cout << a[i] << " ";
for (int i = 0; i < SIZE - 1;i++){
s = i;
for (int j = s + 1; j <=SIZE - 1; j++)
if(a[s]> a[j])
s = j;
temp = a[i];
a[i] = a[s];
a[s] = temp;
}
cout << endl << "排序後 :";
for (int i = 0; i < SIZE; i++)
cout << a[i] << " ";
}
```
實作畫面:

執行畫面:

### ch07-06 循序搜尋法
程式碼:
```
#include <iostream>
using namespace std;
int main()
{
int data[10]={2,15,3,55,46,98,54,7,6,34};
int num;
cout << "請輸入要搜尋的值:";
cin >> num;
for(int i = 0; i < 10; i++){
if(data[i]== num){
cout << "在標註" << i << "的位置找到" << num;
return 0;
}
}
cout << num << "不在陣列中";
}
```
實作畫面:
執行畫面:
### ch07-07 二元搜循法
程式碼:
```
#include<iostream>
using namespace std;
int main()
{
int a[7] = {2,16,34,47,53,67,81};
int Target, M, L = 0, R = 6;
cout << "請輸入要搜尋的值:";
cin >> Target;
while(L <= R){
M = (L + R) / 2;
if(a[M]== Target){
cout << "在標註" << M << "的位置找到" << Target;
return 0;
} else if(a[M]> Target){
R= M - 1;
}else if(a[M]< Target){
L = M+1;
}
}
cout << Target << "不在陣列中";
}
```
實作畫面:

執行畫面:

## 專案練習
程式碼:
```
#include <iostream>
using namespace std;
void p01();
void p02();
void p03();
void p04();
void p05();
int main() {
string menuItem[] = {
"[1]請使用者輸入8個數字,找出最大值",
"[2]氣泡排序法",
"[3]選擇排序法",
"[4]循序搜尋法",
"[5]二元搜尋法",
"[0]離開"
};
int i, num;
int selMenu = 99;
while (selMenu != 0) {
system("chcp 950"); //編碼
system("cls"); //清除畫面
cout << "程式設計實習 資訊一乙 19 康軒勻\n";
cout << "-----------------------------" << endl;
int arrLength = sizeof(menuItem) / sizeof(menuItem[0]); //讀取幾個Item
for (i = 0; i < arrLength; i++) {
cout << menuItem[i] << endl;
}
cout << "請輸入選項:";
cin >> selMenu;
switch (selMenu) {
case 1:
p01();
break;
case 2:
p02();
break;
case 3:
p03();
break;
case 4:
p04();
break;
case 5:
p05();
break;
}
system("PAUSE");
cout << "\n";
}
return 0;
}
void p01() {
int numbers[8];
cout << "請輸入8個數字,程式將找出最大值" << endl;
for (int i = 0; i < 8; i++) {
cout << "請輸入第" << (i + 1) << "個數字:";
cin >> numbers[i];
}
int MAX = numbers[0];
for (int i = 1; i < 8; i++)
if (numbers[i] > MAX)
MAX = numbers[i];
cout << "在輸入的數字中,數值最大的是" << MAX;
}
void p02() {
char array[5] = {'r', 'o', 'g', 'e', 'r'};
cout << "排序前:";
for (int i = 0; i < 5; i++)
cout << array[i];
for (int i = 1; i < 5; i++)
for (int j = 0; j < 5 - i; j++)
if (array[j] < array[j + 1]) {
char temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
cout << endl << "排序後:";
for (int i = 0; i < 5; i++)
cout << array[i];
}
void p03() {
int a[5] = {25, 12, 47, 18, 10};
int s, temp;
cout << "排序前:";
for (int i = 0; i < 5; i++)
cout << a[i] << " ";
for (int i = 0; i < 5 - 1; i++) {
s = i;
for (int j = s + 1; j <= 5 - 1; j++)
if (a[s] > a[j]) {
s = j;
}
temp = a[i];
a[i] = a[s];
a[s] = temp;
}
cout << endl << "排序後 :";
for (int i = 0; i < 5; i++)
cout << a[i] << " ";
}
void p04() {
int data[10] = {2, 15, 3, 55, 46, 98, 54, 7, 6, 34};
int num;
cout << "請輸入要搜尋的值:";
cin >> num;
for (int i = 0; i < 10; i++) {
if (data[i] == num) {
cout << "在標註" << i << "的位置找到" << num;
break;
}
}
cout << num << "不在陣列中";
}
void p05() {
int a[7] = {2, 16, 34, 47, 53, 67, 81};
int Target, M, L = 0, R = 6;
cout << "請輸入要搜尋的值:";
cin >> Target;
while (L <= R) {
M = (L + R) / 2;
if (a[M] == Target) {
cout << "在標註" << M << "的位置找到" << Target;
return;
} else if (a[M] > Target) {
R = M - 1;
} else if (a[M] < Target) {
L = M + 1;
}
}
cout << Target << "不在陣列中";
}
```
實作畫面:






執行畫面:

## 心得感想(100字)(12/11)
心得:
這次的程式設計實習中,不僅僅加了陣列這個新元素,還有許多以前教過的東西,例如:while迴圈、if條件判斷、運算子運算式、函式等,使這單元增加了許多難度,這告訴我,我不僅要溫故,更要知新,才有辦法跟上課程,否則最後只會越來越跟不上.
## 12/18 隨堂練習
### Ch07-08 輸出字串與字元陣列大小
程式碼:
```
#include <iostream>
using namespace std;
int main()
{
char name1[]="john smith";
char name2[]={'J','O','H','N',' ','S','M','I','T','H'};
cout << "name1[]大小為:" << sizeof(name1) << endl;
cout << "name2[]大小為:" << sizeof(name2) << endl;
cout << "name1[]:" << name1 << endl;
cout << "name2[]:" << name2 << endl;
}
```
實作畫面:

執行畫面:

### Ch07-27 以指標實作氣泡排序法
程式碼:
```
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "請輸入欲排序的數字數量:";
cin >> num;
int *ptr = new int[num];
for(int i = 0; i < num; i++){
cout << "請輸入第" << i+1 << "個數字:";
cin >> *(ptr+i);
}
for(int i =1; i < num; i++)
for(int j = 0; j < num; j++)
if(*(ptr+j)<*(ptr+j+1)){
char temp = *(ptr+j);
*(ptr+j) = *(ptr+j+1);
*(ptr+j+1) = temp;
}
cout << "排序後: ";
for(int i = 0; i < num; i++)
cout << *(ptr+i)<< '\t';
delete[]ptr;
}
```
## ch7 課後練習(12/18)
### 1.大樂透539是一種樂透型遊戲:
程式碼:
```
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main(){
int a[5];
int y=0;
srand(time(0));
int num ,i = 0;
int n[5];
while(i<5){
num = (rand() % 39) +1;
n[i] = num;
for(int j = 0; j<=i-1; j++){
if(n[j] == num)
i = i-1;
}
i++;}
cout << "請選擇六個號碼(範圍01~39):" << endl;
for (int i = 0; i < 6; ++i) {
cout << "號碼 " << i + 1 << ": ";
cin >> a[i];
}for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
if(a[i]==n[j]){
y++;
}
}
}cout << "本期彩票號碼:";
for(int i = 0; i < 5; i++){
cout << n[i] << " ";}
cout<<"中了"<<y<<"個,";
if(y==3){
cout<<"恭喜獲得普獎";
}else if(y==4){
cout<<"恭喜獲得伍獎";
}else if(y==5){
cout<<"恭喜獲得仨獎";
}else if(y==6){
cout<<"恭喜獲得頭獎";
}
}
```
實作畫面:

執行畫面:

### 2.奇數或偶數:使用亂數函數 rand() 產生 5個1~20的整數,分別對奇數的數加總以及偶數的數加總
程式碼:
```
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(static_cast<unsigned int>(time(0)));
int numbers[5], oddSum = 0, evenSum = 0;
cout << "亂數:";
for (int i = 0; i < 5; ++i) {
numbers[i] = rand() % 20 + 1;
cout << numbers[i] << " ";
(numbers[i] % 2 != 0) ? oddSum += numbers[i] : evenSum += numbers[i];
}
cout << "\n奇數加總:" << oddSum << "\n";
cout << "偶數加總:" << evenSum << "\n";
return 0;
}
```
實作畫面:

執行畫面:

### 3.在「字元陣列」中放入10個英文字母「television」,然後將字母依小到大(a到z)順序從螢幕輸出
程式碼:
```
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
char charArray[] = "television";
sort(charArray, charArray+10);
cout << "排序後的字元陣列:"<< charArray << endl;
return 0;
}
```
實作畫面:

執行畫面:

## 心得感想(100字)(12/18):
心得:這次的課程中,老師給我們許多的課後練習,要我們自己思考如何做出老師給的程式,幸好課本上都找的到相關的答案,除了樂透那題以外都可以輕鬆解決,樂透那題,需要使用較多且複雜的迴圈和if、else if,還要用到以前教過的亂數函式,就可以把它做出來了.
## ch7 課後練習(2023/12/25):
### 1.請使用二維陣列存取學生的多科成績,並輸出班級成績單
程式碼:
```
#include <iostream>
using namespace std;
int main() {
int stu;
cout << "輸入幾位學生:";
cin >> stu;
int score[stu][5];
cout << "輸入每位學生的成績(依序為國、英、數、基電、物理,以空格區隔)\n";
for (int i = 0; i < stu; i++) {
cout << i + 1 << "號學生:";
for (int j = 0; j < 5; j++) {
cin >> score[i][j];
}
}
cout << "班級成績單\n";
cout << "座號\t國文\t英文\t數學\t基電\t物理\t總分\n";
cout << "=======================================\n";
for (int i = 0; i < stu; i++) {
cout << i + 1 << "\t";
int sum = 0;
for (int j = 0; j < 5; j++) {
cout << score[i][j] << "\t";
sum += score[i][j];
}
cout << sum << endl;
}
return 0;
}
```
實作畫面:

執行畫面:

### 2.
程式碼:
```
#include <iostream>
using namespace std;
int main(){
cout<<"根據使用者輸入的數值設定表格大小(先輸入列,再輸入行)";
int row,col;
cin>>row>>col;
int iArray[row][col];
cout<<"請輸入數值填滿表格\n";
for(int i=0;i<row;i++){
for(int j=0;j<col;j++)
{
cin>>iArray[i][j];
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++)
{
cout<<"iArray["<<i<<"]["<<j<<"]="<<iArray[i][j]<<endl;
}
}
}
```
實作畫面:

執行畫面:

### 3.
程式碼:
```
#include <iostream>
using namespace std;
int main(){
int a[5],sum;
for(int i=0;i<5;i++){
cout<<"數值"<<i+1<<":";
cin>>a[i];
sum+=a[i];
}
cout<<"總和:"<<sum;
}
```
實作畫面:
執行畫面:
### 4.
程式碼:
```
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
cout<<"請輸入五個數值:";
int *p = new int[5];
for(int i = 0;i<5;i++){
cin>>p[i];
}
cout<<"指數變數p所指向的記憶起點:"<<p<<endl;
sort(p,p+5);
cout<<"最大值為"<<p[4]<<endl;;
int *a = new int(p[4]);
cout<<"最大值的記憶體位址:"<<a<<endl;
delete[] p;
}
```
實作畫面:

執行畫面:
