顯示具有 C作業練習 標籤的文章。 顯示所有文章
顯示具有 C作業練習 標籤的文章。 顯示所有文章

2008年6月26日 星期四

C 程式設計期末作業,解題練習:解答

這個題目看起來複雜,其實不算難。有幾個關卡要解決,首先是要能讀入所有的佈雷資訊,使用二維字元陣列(亦稱為一維字串陣列)讀取檔案內容,然後逐一的判斷是要列印"*"或數字。下列的C語言程式碼逐一的檢討字元,看是要列印"*"或數字。
if (mine[i][j]=='.')
fprintf(fptr2, "%d", calcMineNumber(i,j));
else
fprintf(fptr2, "*");

這裡的calcMineNumber(i,j)是一個函數,負責計算第 i 列和第 j 行的週遭八個位置有多少是雷。其C語言完成碼如下。
int calcMineNumber(int i, int j)
{
int sum=0;
sum += addMine(i-1, j-1);
sum += addMine(i-1, j);
sum += addMine(i-1, j+1);
sum += addMine(i, j-1);
sum += addMine(i, j+1);
sum += addMine(i+1, j-1);
sum += addMine(i+1, j);
sum += addMine(i+1, j+1);
return sum;
}

這個函數的概念簡單清楚,只是加總外圍八個位置的佈雷情形。至於i,j合理與否並不考慮,這種模組式的考慮,可以將問題拆解成容易理解的程度。
這時addMine就是決定i,j是否合理的地方,其C語言程式碼如下。
int addMine(int i, int j)
{
if (i<0||j<0||i>=row||j>=column)
return 0;
if (mine[i][j]=='*')
return 1;
else
return 0;
return 0;
}

希望大家在完成這個題目後,對問題的模組化能有進一步的體會。
期末作業題目
回到作業目錄
回到首頁

2008年6月1日 星期日

C 程式設計期末作業,解題練習

作業內容:
本作業將使用C語言進行踩地雷遊戲的數字計算。

大部分的人都玩過踩地雷的遊戲,遊戲的開始是在一個M*N的陣列中佈有許多地雷,你必須將佈有地雷的地方插一個旗子,在沒有地雷的地方,填入這個格字的週遭(即上下左右斜上斜下等八個格子)佈有地雷的數字。下圖為Windows作業系統所附的遊戲畫面。在這個作業中,題目的輸入格式如下。題目中的 '*' 表示是地雷,'.' 表示不是地雷。題目第一列的兩個整數分別表示地雷矩陣的大小,亦即列數與行數。
17 36
*..*.**..........*.....*.........*..
.......*.....*.*......*.....*...**..
...**...............*...*.....*.....
........*...........*......**.*.....
.......*.....**..........*.....*..*.
...**............*...*.*.*.....*....
*...*.....**.*.*..*........*...**...
*...............*....*.....*........
.................*..*...........*...
....*...............**...**...*.....
*..**..*..*...........*.*....*.*...*
..**......*..........*.*.**...**....
.*.....*.............*..*...*.....*.
...*..**...*.*.**.....*......**.*.*.
....*.*.....*............**.........
..*...........*.*..*.**..*....**.**.
.*.*..**..**.....*...*...........*..

作業的輸出是以C語言計算所有非地雷點的週遭地雷數。有地雷的地方仍然印著 '*' ,沒地雷的地方則印著其週遭的地雷數。上述例題的答案如下所示。
*11*2**2100011212*10012*100111013*20
1123433*10001*2*211112*32101*212**20
001**112210011211002*312*11334*32210
00122112*10012210002*201222**3*31111
0012211*21001**1111122213*32223*21*1
111**211112233422*211*2*3*31103*4211
*213*20001**2*2*33*12231213*202**100
*201110001222122*3222*10002*20133200
11011100000000012*12*42012321112*100
1113*211111100001112**222**112*32111
*23**21*12*20000000134*3*5421*5*201*
23**312222*2000000002*4*4**223**2122
1*43213*2122212221002*43*322*44423*2
112*23**201*3*2**10012*223222**2*3*2
0123*3*31012*334321123322**113443432
12*32333112322*2*22*3**12*3101**3**1
1*3*11**11**11122*213*31111001223*31

解答下載
回到作業目錄
回到首頁

2008年5月10日 星期六

C 程式設計作業八,結構使用

作業內容:
本作業將計算兩個圓的交點。

輸入的值為兩個圓的圓心及半徑,輸出為兩個圓的交點座標。你必須使用下列的結構
typedef struct
{
double x, y;
} Point;

typedef struct
{
Point center;
double r;
} Circle;

輸入值的順序為 x1 y1 r1 x2 y2 r2,前三個數字為第一個圓的圓心(x1, y1)與半徑(r1),後三個數字為第二個圓的圓心(x2, y2)與半徑(r2),輸出的值為兩個交點的座標,精確度為兩位小數點,如果無交點,則印出 No cross points.

輸入範例:
1 0 2 -1 0 2
1 0 1 -1 0 1
1 0 0.5 -1 0 1

輸出範例:
The cross points are (0.00,1.73) and (0.00,-1.73).
The cross points is (0.00,0.00).
No cross points.

作業提示:
一、使用Google搜尋「兩圓交點」,以尋找公式求解。
二、或下載兩圓交點公式說明
解答下載
回到作業目錄
回到首頁

2008年5月2日 星期五

C 程式設計作業七,指標之使用:解題

這題的重點是要熟悉指標的應用,但也只是簡單的指標。或許以後讀到C語言指標比實際自己用指標的機會還多,加上Java, VB, c#都不用指標,看到的機會也會變少,但是這種存放資料位址的觀念卻是每個程式語言都會用到的。
題目中有一個要注意的地方,就是在x1或y1為零的地方是沒有花的,所以計算起始點時要加入考量。xi,yi是for迴圈的起始點,如果可以整除10,則得到零,所以在 (test)?(true_result):(false_result) 的語法中,在x1不為零且可以整除時(相反則為X1為零或不可整除時),由x1/10起算花的數目。
以下是C語言函數calcRose的解答。
void calcRose(int x1, int y1, int x2, int y2, int *rnum, int *ynum)
{
int i, j, xi, yi;

xi = x1%10||x1==0?x1/10+1:x1/10;
yi = y1%10||y1==0?y1/10+1:y1/10;
for (i=xi;i<=x2/10;i++)
for (j=yi;j<=y2/10;j++)
if (i%2)
if (j%2)
(*ynum)++;
else
(*rnum)++;
else
if (j%2)
(*rnum)++;
else
(*ynum)++;
}

作業七題目
回到作業目錄
回到首頁

2008年4月14日 星期一

C 程式設計作業七,指標之使用

作業內容:
有一天,小甜甜和他的同學到鄉下的奶奶家去玩,奶奶家有一個很大很大的玫瑰花園,種著各式各樣的玫瑰花,其中有一片玫瑰園種植著紅黃相間的玫瑰,排列整齊,非常好看。

這些黃白相間的玫瑰花每10公分的距離種一棵,小甜甜心理在想,隨便一個方塊中,會有多少棵玫瑰呢?

這次的作業,你要計算的是玫瑰的數量,在一個已知位置的矩形中,算出紅黃玫瑰各有多少棵。以下圖為例:

矩形左下角座標為(35, 35),右上角座標為(95, 75),算出的紅玫瑰有12棵,黃玫瑰也有12棵。

本作業基本要求如下:

1. 完成函數void calcRose( int, int, int, int, int *, int *),輸入參數有六個,前四個整數為矩形座標,分別為x1, y1, x2, y2,而(x1, y1)為左下角,(x2, y2)為右上角座標。最後兩個參數為整數指摽,分別代表紅色與黃色玫瑰的數量。

2. 程式的輸入為四個整數,以空白格開,範圍在0~4000(含)之間,當四個值都是 0 時,則程式停止。

3. 程式的輸出為兩個整數,分別為紅色與黃色玫瑰的數量,各佔六格寬度。

4. 主程式呼叫calcRose函數,在求出玫瑰數量後,依格式輸出結果。

範例輸入:
35 35 95 75
30 20 50 30
1000 1005 1008 1055
0 0 0 0

範例輸出:
    12    12
3 3
3 2

作業提示:
使用 if (x1==0&&y1==0&&x2==0&&y2==0) break;來結束程式。

解答下載
回到作業目錄
回到首頁

2008年3月22日 星期六

C 程式設計作業六,陣列與字串之使用:解題

這題的C語言作業雖然是要做乘法,但是被乘數只有兩位數,所以,最簡單的作法是用加法來完成。也就是讀進來的第一個字串去累加,累加的次數由讀入的第二個數字決定。
完整的解答如下,我以add(prod, temp)函數呼叫來完成加法,使用addOn來處理進位。另外讀進來的數字字串最高位數是在str[0]的位置,所以轉成temp時要顛倒過來。digitNo是用來計算答案的位數,列印時前置的0才可以不用印出來。

#include <stdio.h>
#include <stdlib.h>

#define MSIZE 34

void multiply(int prod[], int multiplier[], int num);
void add(int prod[], int temp[]);
int main(void)
{
int prod[MSIZE], temp[MSIZE];
char str[MSIZE];
int i, num, digitNo;

while (1)
{
scanf("%s%d", str, &num);
if (!strcmp(str,"0") && num==0)
break;
for (i=0;i<MSIZE;i++)
{
prod[i]=0;
temp[i]=0;
}
for (i=strlen(str)-1;i>=0;i--)
temp[strlen(str)-i-1] = str[i]-48;
/*
for (i=strlen(str)-1;i>=0;i--)
printf("%d",temp[i]);
printf("\n");
*/
for (i=0;i<num;i++)
add(prod, temp);
digitNo = 0;
for (i=MSIZE-1;i>=0;i--)
if (prod[i]!=0)
{
digitNo = i;
break;
}
for (i=digitNo;i>=0;i--)
printf("%d",prod[i]);
printf("\n");
}


return 0;
}

void add(int prod[], int temp[])
{
int addOn=0, i;
for (i=0;i<MSIZE;i++)
{
prod[i] = prod[i]+temp[i] + addOn;
if (prod[i]>9)
{
prod[i] %= 10;
addOn = 1;
}
else
addOn = 0;
}
}

作業六題目
回到作業目錄
回到首頁

2008年2月28日 星期四

C 程式設計作業六,陣列與字串之使用

作業內容:
你會乘法嗎?當然會。每個人小學就開始學九九乘法表。

電腦也會乘法,你跟電腦比賽過嗎?當然電腦瞬間就可以算出來了。可是電腦在精確度方面是有限制的。int 變數型態通常是四個位元組,計有32個位元,所以能代表數字的範圍為−2,147,483,648 ~ +2,147,483,647。double 變數型態所能代表的範圍比 int 大,可高達10的16次方,即10,000,000,000,000,000。

這次的作業,你要處理的是兩個整數的相乘,第一個整數的範圍在0~ 10^32(10的32次方),第二個整數的範圍是0~99。

例如,999,999,999,999,999,999,999,999 x 11,得到的答案是 10,999,999,999,999,999,999,999,989

本作業基本要求如下:

1. 每次讀取兩個值,進行相乘運算後,印出乘積。

2. 當輸入的兩個值都是 0 時,則程式停止。

3. 必須使用陣列。

3. 必須製作函數,以呼叫進行運算。

範例輸入:
999999999999999999999999 11
0 99
12345678901234567890 0
12345678901234567890 11
98765432109876543210111222333 58
11111111111111111111111111111 99
999999999999999999999999999999 99
0 0


範例輸出:
10999999999999999999999989
0
0
135802467913580246790
5728395062372839506186450895314
1099999999999999999999999999989
98999999999999999999999999999901


作業提示:
使用scanf 讀取 %s%d,再將第一個讀取的字串,轉存成陣列,以計算乘積。

解答下載
回到作業目錄
回到首頁

2008年1月13日 星期日

C 程式設計期末作業,解題練習:解答


/* C Programming, final project */
#include <stdio.h>
#include <stdlib.h>

int getSortedNum(int, int);
int numInChain(int, int);
int chain[1000];
int main(void)
{
int num, size, idx;
int bigNum, smallNum;
while (1)
{
scanf("%d", &num);
if (num==0)
break;
printf("Original number was %d\n", num);
idx = 0;
do
{
bigNum = getSortedNum(num,1);
smallNum = getSortedNum(num,-1);
chain[idx] = bigNum - smallNum;
num = chain[idx];
printf("%d - %d = %d\n", bigNum, smallNum, chain[idx]);
idx++;
} while (!numInChain(chain[idx-1], idx-2));
printf("Chain length %d\n\n",idx);
}

return 0;
}

int getSortedNum(int num, int order)
{
int digit[10]={0};
int i, j, temp, size=0;

for (i=0; i<10; i++)
{
digit[i] = num%10;
num /= 10;
}
i = 9;
while (i>=0)
{
if (digit[i]!=0)
{
size = i+1;
break;
}
i--;
}
if (order == 1)
{
for (i=0;i<size-1;i++)
for (j=i; j<size; j++)
if (digit[i]<digit[j])
{
temp = digit[i];
digit[i] = digit[j];
digit[j] = temp;
}
}
else if (order == -1)
{
for (i=0;i<size-1;i++)
for (j=i; j<size; j++)
if (digit[i]>digit[j])
{
temp = digit[i];
digit[i] = digit[j];
digit[j] = temp;
}
}
temp = digit[0];
for (i=1;i<size;i++)
{
temp *= 10;
temp += digit[i];
}
return temp;
}

int numInChain(int newNum, int idx)
{
int i;
for (i=0;i<=idx;i++)
if (newNum == chain[i])
return 1;
return 0;
}

作業五題目
回到首頁

2007年12月28日 星期五

C 程式設計期末作業,解題練習

作業內容:
有一數字問題,可以透過其最大值與最小值的差值,進行一連串的計算,

其計算過程如下:

一、先將該數依照數字由大排到小,形成新的數字。例如該數為14095,排列完變成95410。

二、再將該數依照數字由小排到大,形成另一個新的數字。例如該數為14095,排列完變成1459。

三、將大的數減去小的數字,所得的差值,取代該數。

四、重複一至三的步驟,直到第三步的差值已經在前面出現過為止。

輸入的值最大為六位數,當輸入為 0 時,則停止。

作業要求:顯示原始輸入數字、顯示最大數與最小數相減結果、顯示數字鏈的長度。

執行範例:
94510
Original number was 94510
95410 - 1459 = 93951
99531 - 13599 = 85932
98532 - 23589 = 74943
97443 - 34479 = 62964
96642 - 24669 = 71973
97731 - 13779 = 83952
98532 - 23589 = 74943
Chain length 7

3412
Original number was 3412
4321 - 1234 = 3087
8730 - 378 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
Chain length 4

777
Original number was 777
777 - 777 = 0
0 - 0 = 0
Chain length 2

0

期末作業解答
回到作業目錄
回到首頁

C 程式設計作業五,解題練習:解答


/* C Programming, Project 5 */
#include <stdio.h>
#include <stdlib.h>

int getZoneNo(int);
int compZoneMin(int, int, int, int);
int main(void)
{
char phoneNo[20];
double zonePrice1=5.4, zonePrice2=3.6;
int startHr, startMin, endHr, endMin;
int zone1, zone2, zoneMin1, zoneMin2;
while (scanf("%s %d %d %d %d",phoneNo, &startHr, &startMin, &endHr, &endMin)==5)
{
printf("%s %6d %6d %6d %6d\n",phoneNo, startHr, startMin, endHr, endMin);
zone1 = getZoneNo(startHr);
zone2 = getZoneNo(endHr);
if (zone1==zone2)
{
if (zone1==1)
{
zoneMin1 = compZoneMin(startHr, startMin, endHr, endMin);
zoneMin2 = 0;
}
else
{
zoneMin1 = 0;
if (startHr<endHr)
zoneMin2 = compZoneMin(startHr, startMin, endHr, endMin);
else
zoneMin2 = compZoneMin(startHr, startMin, endHr+24, endMin);
}
}
else
{
if (zone1==1)
{
if (endHr<8)
endHr += 24;
zoneMin1 = compZoneMin(startHr, startMin, 18, 0);
zoneMin2 = compZoneMin(18, 0, endHr, endMin);
}
else
{
endHr += 24;
if (startHr<8)
startHr += 24;
zoneMin1 = compZoneMin(startHr, startMin, 32, 0);
zoneMin2 = compZoneMin(32, 0, endHr, endMin);
}
}
printf("--------------------------->%6d%6d%10.0lf\n", zoneMin1, zoneMin2, zoneMin1*zonePrice1+zoneMin2*zonePrice2);
}

system("pause");
return 0;
}

int getZoneNo(int hr)
{
if (hr>=8 && hr<18)
return 1;
else
return 2;
}
int compZoneMin(int startHr, int startMin, int endHr, int endMin)
{
printf("%6d %6d %6d %6d\n",startHr, startMin, endHr, endMin);
if (startMin<endMin)
return (endHr-startHr)*60+endMin-startMin;
else
return (endHr-startHr-1)*60+endMin-startMin+60;
}

作業五題目
回到首頁

2007年12月18日 星期二

C 程式設計作業五,解題練習

作業內容:
有一家電話公司的電話費率按照時間區段來計算手機費率,計費方式採分鐘計費,費率表如下:
時段8am~6pm6pm~8am
費率5.43.6


例如:電話時間從 5:58pm 到 6:04pm 則電話費為5.4*2 + 3.6*4 = 25.2元。通話費小於一分鐘不計費,通話時間不會超過24小時。

作業基本要求如下:

輸入電話通話紀錄,每行首項為撥打電話號碼,格式為xxxx-xxxxxxxx (例如:0912-345678),接著為開始時數、開始分數、結束時數、結束分數,各資料以一個空格分隔。一行只有 # 表示輸入結束。

輸出為撥出號碼、各時段時間(以分計算)、價格(四捨五入)。

輸入範例:
0912-345678 17 58 18 04
0912-345678 23 55 00 20
0911-223344 07 50 12 30
#


輸出範例:
0912-345678     2     4      25
0912-345678 0 25 90
0911-223344 270 10 1494


作業五解答
回到作業目錄
回到首頁

C 程式設計作業四,迴圈與函數應用:解題


/* C Programming, Project 4 */
#include <stdio.h>
#include <stdlib.h>

int whoWin(int, int);
int main(void)
{
int position = 0;
int computerPlay, myPlay;
int result;

srand(time(NULL));
while (position>4 && position<-4)
{
printf("%*c\n", (position+4)*5-1, 'f');
printf("| -3 | -2 | -1 | 0 | 1 | 2 | 3 |\n\n");
do
{
printf("Enter your play: scissors(1), paper(2), or rock(3) ");
scanf("%d", &myPlay);
} while (myPlay!=1 && myPlay!=2 && myPlay!=3 );
computerPlay = rand()%3 + 1;
if (computerPlay==1)
printf("Computer play: scissors\n");
else if (computerPlay==2)
printf("Computer play: paper\n");
else if (computerPlay==3)
printf("Computer play: rock\n");

result = whoWin(computerPlay, myPlay);
position += result;
}

system("pause");
return 0;
}

int whoWin(int comp, int me)
{
int winner;
if (comp==me)
{
printf("--------------- MATCH ---------------\n");
winner = 0;
}
else if (comp-me==1 || comp-me==-2)
{
printf("------------ PLAYER WIN -------------\n");
winner=1;
}
else
{
printf("----------- COMPUTER WIN ------------\n");
winner=-1;
}
return winner;
}

作業四題目
回到首頁

2007年12月4日 星期二

C 程式設計作業四,迴圈與函數應用

作業內容:
這次的作業是製作一個簡單的剪刀、石頭、布遊戲。
這個遊戲有點像拔河遊戲,只是拔河的雙方並不以力氣作為前進、後退的依據,而是用猜拳來決定前進後退。
在遊戲的開始,有一個旗子位於中央位置,如下圖所示。


玩家是你和電腦,以剪刀、石頭、布來決定旗子向左或向右,所以任何一方累積贏的次數超過四次,遊戲就結束。
範例畫面如下:

此作業基本要求如下:

1. 顯示旗子所在的位置,可能的位置有七個。
2. 以隨機方式,產生電腦出拳的結果。
3. 完成函數 int whoWin(int, int),這個函數傳入玩家與電腦出拳的值,輸出為輸贏的結果,有三種結果:輸、贏、平手。
4. 任一方累積贏的次數超過四次,遊戲結束。

作業四解答
回到作業目錄
回到首頁

C 程式設計作業三,迴圈與隨機數應用:解題


/* C Programming Project 3 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(void)
{
int dice = 0, currentPos = 0, roundNo = 0;
int money = 10000;
int lucky;
char finger[3] = "↖";

srand(time(NULL));
while (roundNo < 3)
{
printf("\n春分 機會 夏至\n\n");
printf("命運 %s 川堂\n\n", finger);
printf("冬至 大門 秋分\n");
printf("================\n");
printf("現存現金 %d 元\n\n", money);
printf("按任意鍵來丟骰子...");
getch();
system("cls");
dice = rand()%6 + 1;
printf("骰子為 %d\n\n", dice);
currentPos += dice;
if (currentPos/8 == 1)
roundNo++;
currentPos %= 8;
switch(currentPos)
{
case 0:
printf("由此過得2000元\n");
money += 2000;
break;
case 1:
printf("機會:");
lucky = rand()%4 + 1;
switch (lucky)
{
case 1:
dice = rand()%3 + 1;
printf("前進 %d 格\n", dice);
currentPos += dice;
break;
case 2:
dice = rand()%3 + 1;
printf("後退 %d 格\n", dice);
currentPos -= dice;
break;
case 3:
printf("損失 500 元\n");
money -= 500;
break;
case 4:
printf("獲利 600 元\n");
money += 600;
break;
}
break;
case 2:
printf("損失 1000元\n");
money -= 1000;
break;
case 3:
printf("獲利 1500元\n");
money += 1500;
break;
case 4:
printf("前進 二格\n");
currentPos += 2;
break;
case 5:
printf("後退 二格\n");
currentPos -= 2;
break;
case 6:
printf("損失 800元\n");
money -= 800;
break;
case 7:
printf("命運:");
lucky = rand()%4 + 1;
switch (lucky)
{
case 1:
dice = rand()%3 + 1;
printf("前進 %d 格\n", dice);
currentPos += dice;
break;
case 2:
dice = rand()%3 + 1;
printf("後退 %d 格\n", dice);
currentPos -= dice;
break;
case 3:
printf("損失 600 元\n");
money -= 600;
break;
case 4:
printf("獲利 500 元\n");
money += 500;
break;
}
break;
default:
break;
}

switch(currentPos)
{
case 0:
strcpy(finger, "↖");
break;
case 1:
strcpy(finger, "↑");
break;
case 2:
strcpy(finger, "↗");
break;
case 3:
strcpy(finger, "→");
break;
case 4:
strcpy(finger, "↘");
break;
case 5:
strcpy(finger, "↓");
break;
case 6:
strcpy(finger, "↙");
break;
case 7:
strcpy(finger, "←");
break;
default:
break;
}
}
system("pause");
return 0;
}

作業三題目
回到首頁

2007年11月20日 星期二

C 程式設計作業三 ,迴圈與隨機數應用

作業內容:
你玩過「大富翁」遊戲嗎?沒錯,這個作業就是要設計一個類似的紙上遊戲,只不過是個簡單單人版的紙上遊戲。

這個遊戲基本上有八個或以上的格子,你的棋子由某個角落出發,然後,你用骰子來決定走幾步,再根據走到的地方來決定發生什麼事情,例如有的格子會獲得固定的獎勵,有的會受到固定的懲罰,有的是機會、命運,有的是前進幾格,有的是退後幾格。

下圖是一個類似的紙上遊戲圖:

起點由春分點開始,按順時針行走,所以遊戲的基本執行畫面,會像下面範例。



光是顯示現在的位置是不夠的,因為這樣一點都沒有遊戲的樣子,像樣的遊戲至少會有些變化。這個作業,基本要求的變化如下:

1. 遊戲有一個適當的故事地點,例如:每個點的名稱是街道名、縣市名、鄉鎮名、國名、科幻遊戲地點、線上遊戲地點、或自己編輯的虛幻地名等,都可以。

2. 遊戲必須能呈現現在位置,利用適當的指標來表示玩家的現在位置。例如,上面是用箭頭來表示現在位置。你可以用相同,或別的方式來表示玩家的位置。

3. 除了機會、命運外,你必須在每一個格子設定獲利、損失,前進、或後退四種規則中的一種。在這四種規則中,獲利多少、損失多少,前進多少格、後退多少格,每種至少要有一個格子屬於該規則。

4. 有一個機會和一個命運,這時你必須以隨機的方式,選出你上述四種規則中的一種,每一個機會或命運,必須有四種以上的變化作為選擇。

5. 遊戲有一個固定的起點,在第三次經過該起點以後(繞三圈),遊戲結束。此時你必須計算玩家的獲利或損失的情形,並列印出來。

作業三解答
回到作業目錄
回到首頁

2007年11月12日 星期一

C 程式設計作業二,選擇邏輯與迴圈應用:解題


這題主要在能解決前兩天的訂貨能被納進來計算,且能更新。所以我們使用兩個分別處理兩天的訂貨量,如下:
arrivedNum = firstOrder;
firstOrder = secondOrder;

如此第一天的的訂貨為到貨量,第二天的訂貨變成第一天的訂貨,而第二天的訂貨則是今天計算後再決定
。

/* C Programming, Project 2 */
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int orderNum, stockNum, arrivedNum, soldNum;
int orderMax = 500;
int orderMin = 300;
int firstOrder = 0;
int secondOrder = 0;
int i;

stockNum = 450;
for (i=1; i<=10; i++) {
printf("Enter the sold amount: ");
scanf("%d", &soldNum);
arrivedNum = firstOrder;
firstOrder = secondOrder;
stockNum = stockNum + arrivedNum - soldNum;
if (stockNum < 300)
secondOrder = 500 - stockNum;
else
secondOrder = 0;
printf("Order = %3d, Arrived = %3d, Stock = %3d\n", secondOrder, arrivedNum, stockNum);
}

system("pause");
return 0;
}

作業二題目
回到首頁

2007年11月8日 星期四

C 程式設計作業二,選擇邏輯與迴圈應用

作業內容:
貴公司販售單一進口商品,每週四為存貨計算時間,並紀錄每週的銷售量與到貨量,每次清點完存貨,若發現存貨低於最低安全存量(訂購點)時,則會交給採購部門下訂單進行訂購工作,如有訂購需求,在週五會發出訂單,每次訂購量以到達標準存量來計算,通常在10天後(下下個星期一),訂貨準時到達。

資訊長(CIO)在週末時告訴你一件不幸的事,他說,清點存貨的員工,電腦中毒,有關十周之內的存貨與訂貨資料都消失了,只剩銷售量的資訊, 所以要求你設計一個程式可用銷售量來計算存貨與訂貨的資料。

基本資訊如下(前四項為固定不變,第五項為假設資訊):
一、最低安全存量為 300 件。
二、標準存量為 500 件。
三、十周之前的存貨量為 450 件。
四、前十二、十一周的訂貨量均為 0。
五、假設前十周的銷售量為 160, 90, 115, 170, 150, 140, 165, 155, 185, 175。

在你經過紙上計算,你所獲得的計算結果如下:



前週數121110987654321
銷售量  16090115170150140165155185175
訂貨量002103002050003254803400
到貨量  00210300205000325480
存貨量 45029020029542548034017520160465


紅色部分是經過上述所提供的基本資訊所得到的計算結果。

請完成你的作業,並能輸入不同的十周銷售量進行類似的計算,輸出類似紅色部分的相關資料。

測試方式:
測試題:銷售量 180, 135, 120, 155, 110, 160, 155, 90, 125, 145。

作業二解答
回到作業目錄
回到首頁

2007年11月4日 星期日

C 程式設計作業一(B),選擇邏輯與輸出、輸入應用:解題


/* C Programming, Project 1B */
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int price, shampoo_L, shampoo_S, conditioner_L, conditioner_S;
int free_S, isOdd;

printf("Enter large shampoo number:");
scanf("%d", &shampoo_L);
printf("Enter large conditioner number:");
scanf("%d", &conditioner_L);
printf("Enter small shampoo number:");
scanf("%d", &shampoo_S);
printf("Enter small conditioner number:");
scanf("%d", &conditioner_S);

free_S = (shampoo_L+conditioner_L)/2;
isOdd = (shampoo_L+conditioner_L)%2;
if (isOdd==1)
{
if (shampoo_L>0)
{
if (conditioner_S > free_S)
{
conditioner_S -= free_S;
free_S = 0;
} else {
free_S -= conditioner_S;
conditioner_S = 0;
}
if (free_S > 0)
{
if (shampoo_S > free_S)
shampoo_S -= free_S;
else
shampoo_S = 0;
}
} else if (conditioner_L>0) {
if (shampoo_S > free_S)
{
shampoo_S -= free_S;
free_S = 0;
} else {
free_S -= shampoo_S;
shampoo_S = 0;
}
if (free_S > 0)
{
if (conditioner_S > free_S)
conditioner_S -= free_S;
else
conditioner_S = 0;
}
}
} else {
if (shampoo_S>0) {
if (shampoo_S > free_S)
{
shampoo_S -= free_S;
free_S = 0;
} else {
free_S -= shampoo_S;
shampoo_S = 0;
}
if (free_S > 0)
{
if (conditioner_S > free_S)
conditioner_S -= free_S;
else
conditioner_S = 0;
}
} else if (conditioner_S>0) {
if (conditioner_S > free_S)
{
conditioner_S -= free_S;
free_S = 0;
} else {
free_S -= conditioner_S;
conditioner_S = 0;
}
if (free_S > 0)
{
if (shampoo_S > free_S)
shampoo_S -= free_S;
else
shampoo_S = 0;
}
}
}
if (isOdd==1 && ((shampoo_L>0 && shampoo_S>0) || (conditioner_L>0 && conditioner_S>0)))
price = (shampoo_L+conditioner_L)*280+(shampoo_S+conditioner_S-1)*150+150*0.6;
else
price = (shampoo_L+conditioner_L)*280+(shampoo_S+conditioner_S)*150;

if (shampoo_L+conditioner_L >=6)
price *= 0.95;

printf("Price checkout:\n\n");
printf("Large shampoo x %d\n", shampoo_L);
printf("Large conditioner x %d\n", conditioner_L);
printf("Small shampoo x %d\n", shampoo_S);
printf("Small conditioner x %d\n", conditioner_S);
printf("\nPrice = %d\n", price);

system("pause");
return 0;
}

請參考下方流程圖,此圖僅顯示針對偶數大瓶裝處理的過程。

作業一(B)題目
回到首頁

2007年10月29日 星期一

C 程式設計作業一(B),選擇邏輯與輸出、輸入應用

作業內容:
貴公司接受顧客委託,修改軟體,該顧客為百貨零售業者,預計進行一項促銷活動,商品是「愛情洗髮護髮系列」。
愛情洗髮精與潤髮乳有分500ml與250ml兩種大小瓶裝,大瓶愛情洗髮精與潤髮乳原售價都是 280 元,小瓶都是150元。
促銷專案如下:

1. 凡購買大瓶愛情洗髮精一瓶,得以六折優待購買小瓶愛情洗髮精一瓶。
2. 凡購買大瓶愛情潤髮乳一瓶,得以六折優待購買小瓶愛情潤髮乳一瓶。
3. 購買大瓶愛情洗髮精或潤髮乳任兩瓶,可免費附贈小瓶愛情洗髮精或潤髮乳一瓶(任選其一)。
4. 購買大瓶愛情洗髮精或潤髮乳任六瓶,總價可再打九五折。
(大瓶裝以免費折扣使用為優先,不得重複與小瓶再進行搭售折扣)

你的任務就是要完成這個商品的價格計算程式,你必須提供適當的文字說明介面,以利使用者輸入,方便結帳。

測試方式:
假設 A1:一瓶大瓶愛情洗髮精,一瓶小瓶愛情洗髮精,370元。
假設 A2:一瓶大瓶愛情潤髮乳,一瓶小瓶愛情洗髮精,430元。
假設 A3:二瓶大瓶愛情潤髮乳,一瓶小瓶愛情洗髮精,560元。
假設 A4:一瓶大瓶愛情潤髮乳,二瓶大瓶愛情洗髮精,二瓶小瓶愛情洗髮精,930元。(難)
假設 A5:二瓶大瓶愛情潤髮乳,二瓶大瓶愛情洗髮精,三瓶小瓶愛情潤髮乳,1270元。
假設 A6:二瓶大瓶愛情潤髮乳,四瓶大瓶愛情洗髮精,二瓶小瓶愛情洗髮精、二瓶小瓶愛情潤髮乳,1738元。
解答下載
回到作業目錄
回到首頁

2007年10月23日 星期二

C 程式設計作業九,使用結構、結構陣列與檔案處理:解題


/* C Programming, Project 9 */

#include <stdio.h>
#include <stdlib.h>
#define MAX 20

struct record {
char name[20];
char sex;
double score;
};

int queryDB(char [], struct record *);
void insertDB(struct record *);
void listAllName(void);
char *getLine(FILE *);

int main(void)
{
struct record *stdPtr;
char qname[20];
char ch;
int idx, ifFound;

do {
printf("Command(h for help):");
ch = getche();
switch (ch) {
case 'h':
printf("\nUse\n");
printf("h to show this help;\n");
printf("f to find a record by name;\n");
printf("l to list all names in the file;\n");
printf("i to insert a new record;\n");
printf("d to delete a specified record;\n");
printf("m to modify a specified record;\n");
printf("q to quit.\n");
break;
case 'f':
printf("\nFind the name:");
gets(qname);
ifFound = queryDB(qname, stdPtr);
if (!ifFound) {
printf("\nName %s not found!\n", qname);
} else {
printf("\nName: %s\n", stdPtr->name);
printf("Sex: %c\n", stdPtr->sex);
printf("Score: %f\n", stdPtr->score);
}
break;
case 'l':
listAllName();
break;
case 'i':
break;
case 'd':
break;
case 'm':
break;
case 0xd:
break;
case 'q':
printf("\nQuit now.\n");
break;
default:
printf("\nNot a valid command\n");
}
fflush(stdin);
printf("\n");
} while (ch!='q');

system("pause");
return 0;

}


int queryDB(char qname[], struct record *stdPtr) {
struct record *studtemp;
char *str, aname[20];
char asex;
double ascore;
FILE *fptr;
int i, rno, found=0;
fptr = fopen("db.txt","r");
if (fptr==NULL)
return 0;
else {
fscanf(fptr, "%d\n", &rno);
for (i=0;i<rno;i++) {
str = getLine(fptr);
strcpy(aname, str);
str = getLine(fptr);
asex = str[0];
str = getLine(fptr);
ascore = atof(str);
if (!strcmp(aname,qname)) {
strcpy(stdPtr->name, aname);
stdPtr->sex = asex;
stdPtr->score = ascore;
found = 1;
}
}
fclose(fptr);
if (found)
return 1;
else
return 0;
}
}
void insertDB(struct record *stdPtr) {
}

void listAllName(void) {
FILE *fptr;
int i, rno=0;
char *str;

fptr = fopen("db.txt","r");
if (fptr==NULL)
printf("Failed to open file\n");
else {
fscanf(fptr, "%d\n", &rno);
printf("\n");
for (i=0;i<rno;i++) {
str = getLine(fptr);
puts(str);
str = getLine(fptr);
str = getLine(fptr);
}
}
fclose(fptr);
}
char *getLine(FILE *fptr) {
static char str[80];
int i=0;

while ((str[i]=getc(fptr)) != '\n')
i++;
str[i] = '\0';
return str;
}

作業九題目
回到首頁