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;
}

作業二題目
回到首頁

沒有留言: