第16週課堂補充(程式題題庫解析)

Download Report

Transcript 第16週課堂補充(程式題題庫解析)

16週
觀念題與程式題
題庫解析
982程式設計題-1
• 試撰寫一完整C++
程式,可以從鍵
盤讀取一系列整
數,將這些整數
依其奇偶數之別
放入odd.txt與
even.txt二個檔
案中。
#include <iostream>
#include <fstream> //檔案的寫入參考課本-12頁
using namespace std;
int main()
{
int num;
fstream odd; odd.open("odd.txt", ios::out);
//指定odd檔案串流,開啟odd.txt,模式為輸出
fstream even;even.open("even.txt", ios::out);
//指定even檔案串流,開啟even.txt,模式為輸出
cout << "輸入一個整數(999停止輸入):"; cin >> num;
while (num != 999){
if (num % 2 == 0) {//偶數
odd << num << "\t" ; //"\t"為送出定位點,即鍵盤的tab鍵
} else {
even << num << "\t";
}
cout << "輸入一個整數(999停止輸入):"; cin >> num;
}
odd.close();
even.close();
//輸出檔案務必執行close,否則有可能有部份結果尚未寫入檔案
system("pause");
return 0;
}
982程式設計題-2
•
試撰寫一完整C++程式,可以從鍵盤讀取一系列整數,將這些整數排列後,
放入sorted.txt檔案中。
#include <iostream>
#include <fstream>
//檔案的寫入參考課本-12頁
using namespace std;
void bubbleSort(int data[], int amount){
//氣泡排序
int k,times,i,temp;
k = amount - 1;
while(k != 0){
times=0;
for(i=0; i <= k-1; i++){
if(data[i] > data[i+1]){
temp = data[i];
data[i] = data[i+1];
data[i+1] = temp;
times=I;
}
}
k=times;
}
}
int main()
{
int num;
int data[1000];
int index = -1;
fstream sorted; sorted.open("sorted.txt", ios::out);
cout << "輸入一個整數(999停止輸入):"; cin >> num;
while (num != 999){
index++;
data[index] = num;
cout << "輸入一個整數(999停止輸入):"; cin >> num;
}
bubbleSort(data, index+1);
for (int i = 0; i<=index; i++) sorted << data[i] <<
" " ;
sorted << endl;
sorted.close();
system("pause");
return 0;
}
• 試寫一個程式,依據下表規格建立矩形類別(Rectangle)
,程式將要求使用者輸入長與寬,以建立矩形物件,並計
算該矩形物件的面積與周長,且將結果予以輸出。
資料成員
成員函數
•
執行例:
輸入資料
請輸入矩形的長度:10
請輸入矩形的寬度:5
輸出結果
矩形的長度為:10
矩形的寬度為:5
矩形的面積為:50
矩形的周長為:30
height (高), float
私有(private)
width (寬), float
私有(private)
area (面積),float
私有(private)
perimeter (周長), float
私有(private)
setHeight(設定高)
setWidth(設定寬)
公有(public)
公有(public)
getHeight(取得高)
公有(public)
getWidth(取得寬)
公有(public)
getArea (取得面積)
公有(public)
getPerimeter (取得周長)
公有(public)
題庫觀念題-1
• 執行下列程式後,最後印出
的結果為何? 並會出呼叫
圖
int f1 (int n){
if (n > 2)
return n * f1 (n - 1);
else
return 2;
}
int main( ){
cout << f1 (8);
system("pause");
}
//Java動態劇場
import jeliot.io.*;
public class MyClass {
static int f1 (int n){
if (n > 2)
return n * f1 (n - 1);
else
return 2;
}
public static void main() {
System.out.println(f1(8));
}
}
題庫觀念題-2
•
執行下列程式後,最後印出的結果為
何? 並會出呼叫圖
int f2 (int n, int n2){
if (n >= n2){
cout<<n <<endl;
f2 (n-1, n2);
}
}
int f3 (int n, int n2) {
if (n >= n2){
f2 (n, n2);
cout <<endl;
f3 (n-1, n2);
}
}
int main( ){
f3 (6, 2);
system("pause");
}
//Java動態劇場
import jeliot.io.*;
public class MyClass {
static int f2 (int n, int n2){
if (n >= n2){
System.out.println(n);
f2 (n-1, n2);
}
}
static int f3 (int n, int n2) {
if (n >= n2){
f2 (n, n2);
System.out.println();
f3 (n-1, n2);
}
}
public static void main() {
f3(6,2);
}
}
//此題有間接遞迴,只跑第一回結果,其他回參考第一
回來看
題庫觀念題-3
•
執行下列程式後,最後印出的
結果為何? 並會出呼叫圖
void m(int a, int b, int *c){
int d;
if (b > 0) {
m(a, b - 1, &d);
*c = a * d;
}
else *c = 2;
}
int main(){
int a = 3, b = 2, c ;
m(b, a, &c);
cout << c;
}
//此題有指標, Java沒有指標,所以此題不用
Java動態劇場來看
題庫觀念題-4
• 執行下列程式後,最後印
出的結果為何? 並會出
呼叫圖
int m(int a, int b){
if (b > 0)
return( a + m(a, b-3));
else return (0);
}
int main()
{
int a = 7;
cout << m(a, a-1);
}
//Java動態劇場
import jeliot.io.*;
public class MyClass {
static int m(int a, int b){
if (b > 0) return( a + m(a, b-3));
else return (0);
}
public static void main() {
int a = 7;
System.out.println(m(a, a-1));
}
}