C語言程式設計 19:40- 21:20 (100分鐘) 請儘量依序作答, 可參考講義或筆記或書本, 但請不要看別人的 :-) 10% I. 寫出以下這程式的輸出結果, 並說明原因: (注意一些常數與猜題不同喔:-) //problem1.cpp #include long y=0; /* global variable*/ long testaa(){ long x = 1; y++; return ++x; } long testbb(){ static long x = 1; y++; return x++; } int main(){ cout << testaa() << testaa() << testaa() << testaa()< int main(){ int*pp; /*修正時這statement不要改, 阿應該說不准改, 不然沒分數:-) */ *pp = 38; } (b) #include FILE * fp; char buf[999]; int main(){ if(fp = fopen("testdata.txt", "rt") !=0){ fscanf(fp, "%s", buf); printf("==%s\n", buf); } } 10% VIII. 以下三小題選一題做, 都只要說明 (不用寫程式) -- 說明要如何算出並印出 1 階乘到 52 階乘? (不用寫程式, 說明怎麼做即可) -- 說明 如何用 array 配合整數來做出 STACK (堆疊)? -- 說明 如何用 array 配合兩個整數來做出 QUEUE (佇列, 排隊)? 20%)IX. 有一種遊戲叫做 Bull & Cow (或叫幾 A 幾 B), 是由一人想好 0123 到 9876 間 的一個每位都不同的數, 另一人猜時就告訴他是幾 A 和 幾 B, 幾 A 表示有幾 位數字同且位置也同, 幾 B 是說數字同但位置不同。 Write a C++ function (因參數用 call by reference, 是 C++ 才有的) int ishit(char X[4], char Y[4], int& na, int & nb); 以便在猜中時函數傳回 1 (就是 得到 4 A 時)否則函數值傳回 0, 此外, na 是傳回幾 A, 而 nb 是傳回幾 B (注意 na, nb 都是 call by reference, 所以算 C++ 程式) For example, if X[] 是"1234" and Y[] is "1237" then na is 3 and nb is 0. If X="1734" and Y="1237" then na is 2 and nb is 1. If X="8734" and Y="8347" then na is 1 and nb is 3. (a) 寫出函數 ishit 即可, 不用寫主程式。 (b) 討論若是用 C 語言(不是 C++)則 na 和 nb 仍要能傳回, 函數中及叫用函數時須 如何寫? (重寫你的程式並說明主程式如何呼叫你的函數) 額外題: 10% X. Write a simple makefile for the following example: The makefile will generate only one executable file "myprog". (assume the link command is: g++ -o myprog aa.o bb.o cc.o dd.o) The command "make clear" will clear all obj (*.o) files. And "make clobber" will remove the executable file and all obj files. The program consists of 4 ".c" files and 5 ".h" files: (1) ac.h --- included in aa.c, cc.c (2) b.h --- included in bb.c (3) bc.h --- included in bb.c, cc.c (4) bcd.h --- included in bb.c, cc.c, dd.c (5) cd.h --- included in cc.c, dd.c