0% found this document useful (0 votes)
3 views4 pages

Treasure Bowl Queue Management System

The document contains a C program that simulates a treasure bowl managing supply and consumption of items over a period of 30 days. It defines a structure for the treasure bowl, functions for initializing, adding, removing, and displaying items, as well as handling supply and consumption rates based on the bowl's state. The program randomly adjusts supply and consumption rates each day and provides feedback on the bowl's status throughout the simulation.

Uploaded by

f23562468
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Treasure Bowl Queue Management System

The document contains a C program that simulates a treasure bowl managing supply and consumption of items over a period of 30 days. It defines a structure for the treasure bowl, functions for initializing, adding, removing, and displaying items, as well as handling supply and consumption rates based on the bowl's state. The program randomly adjusts supply and consumption rates each day and provides feedback on the bowl's status throughout the simulation.

Uploaded by

f23562468
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define Treasure_Bowl_SIZE 30
#define nDayS 30

typedef struct
{
int nItems[ Treasure_Bowl_SIZE ];
int nFront;
int nRear;
int nCount;
int nMode;
int nState;
} Treasure_Bowl;

void InitTreasure_Bowl( Treasure_Bowl *t )


{
t->nFront = 0;
t->nRear = 0;
t->nCount = 0;
t->nMode = 0;// 0: 空倉, 1: 正常, 2: 滿倉
t->nState = 0;// 0: 進貨前, 1: 進貨後
}

int AddItem( Treasure_Bowl *t, int Item )


{
if( t->nCount == Treasure_Bowl_SIZE) { // 聚寶盆滿
return 0;
}
t->nItems[ t->nRear ] = Item; //加入物品
t->nRear = ( t->nRear + 1 ) % Treasure_Bowl_SIZE;
t->nCount++;
return 1;
}

int RemoveItem( Treasure_Bowl *t )


{
if( t->nCount == 0 ) { // 聚寶盆空
return 0;
}
t->nFront = ( t->nFront + 1 ) % Treasure_Bowl_SIZE;
t->nCount--;
return 1;
}

void ShowTreasure_Bowl( Treasure_Bowl *t )


{
for( int i = 0; i < Treasure_Bowl_SIZE; i++ ) {
if(( t->nFront <= t->nRear && i >= t->nFront && i < t->nRear ) ||
( t->nFront > t->nRear && (i >= t->nFront || i < t->nRear ))) {
printf( "X" );
}
else {
printf( "-" );
}
}
printf( "] (%d/%d)\n", t->nCount, Treasure_Bowl_SIZE );
}

void DisplayTreasure_Bowl( Treasure_Bowl *t )


{
if( t->nState == 0 ) {
printf( "進出貨前聚寶盆內飼料狀態: [" );
}
else {
printf( "進出貨後聚寶盆內飼料狀態: [" );
}
}

void DisplayTreasure_BowlnState( Treasure_Bowl *t )


{
if( t->nCount == 0 ) {
printf( "聚寶盆內飼料空了!增加進貨速度 (依據市場供應需求,聚寶盆隨機進貨 1~10,出貨 1~5)\n" );
t->nMode = 0;
}
else if( t->nCount >= 25 ) {
printf( "聚寶盆內飼料快滿了!減少進貨速度 (聚寶盆隨機進貨 1~5,出貨 1~5)\n" );
t->nMode = 2;
}
else {
printf( "聚寶盆內飼料數量正常,維持當前進出貨速度 (聚寶盆隨機進貨 1~8,出貨 1~5)\n" );
t->nMode = 1;
}
}

void HandleSupply( Treasure_Bowl *t, int *nSupplyRate, int *nConsumeRate )


{
int nActualSupply = 0;

for( int i = 0; i < *nSupplyRate; i++ ) {


AddItem( t, 1 ); // 每次嘗試進貨 1 個飼料,這裡的 t 代表址。
nActualSupply++;
// 如果進貨後聚寶盆內飼料滿了,則停止
if( t->nCount == Treasure_Bowl_SIZE ) {
printf( ">> 聚寶盆內飼料已滿!實際進貨 %d 件。\n", nActualSupply );
sleep( 1 ); // 暫停一秒
return;
}
}
printf( "✅ 成功進貨 %d 件。\n", nActualSupply );
}

void HandleConsume( Treasure_Bowl *t, int *nSupplyRate, int *nConsumeRate )


{
int nActualConsume;
// 判斷實際出貨數量
if( *nConsumeRate > t->nCount ) {
nActualConsume = t->nCount; // 只能出庫存量
printf( "⚠️ 出貨量 (%d) 超過庫存,只能出貨 %d 件。\n", *nConsumeRate, nActualConsume );
}
else {
nActualConsume = *nConsumeRate; // 正常出貨
}
// 執行出貨
for( int i = 0; i < nActualConsume; i++ ) {
RemoveItem( t );
}
printf( "✅ 成功出貨 %d 件。\n", nActualConsume );
}

int main()
{
Treasure_Bowl Treasure_Bowl;
InitTreasure_Bowl( &Treasure_Bowl );
srand( time( NULL ) );

int nSupplyRate;
int nConsumeRate;
int nDay = 0;

while (nDay < nDayS) {


printf( "\n================ 第 %d 天 ================\n", nDay + 1 );
DisplayTreasure_Bowl( &Treasure_Bowl );
ShowTreasure_Bowl( &Treasure_Bowl );
DisplayTreasure_BowlnState( &Treasure_Bowl );

if( Treasure_Bowl.nMode == 0 ) { // 聚寶盆空,增加進貨


nSupplyRate = rand() % 10 + 1;
nConsumeRate = rand() % 5 + 1;
if( nSupplyRate < nConsumeRate ) {
nConsumeRate = rand() % nSupplyRate + 1;
}
}
else if( Treasure_Bowl.nMode == 1 ) { // 正常模式
nSupplyRate = rand() % 8 + 1;
nConsumeRate = rand() % 5 + 1;
}
else { // 聚寶盆接近滿,減少進貨
nSupplyRate = rand() % 5 + 1;
nConsumeRate = rand() % 5 + 1;
}

printf( "進貨速度: %d 件/秒, 出貨速度: %d 件/秒\n", nSupplyRate, nConsumeRate );

printf( "倉鼠正在管理飼料...\n" );
Treasure_Bowl.nState = 1;

// 處理進貨
HandleSupply( &Treasure_Bowl, &nSupplyRate, &nConsumeRate );

// 處理出貨
HandleConsume( &Treasure_Bowl, &nSupplyRate, &nConsumeRate );

// 顯示當天結束時的聚寶盆狀態
DisplayTreasure_Bowl( &Treasure_Bowl );
ShowTreasure_Bowl( &Treasure_Bowl );
Treasure_Bowl.nState = 0;

sleep( 1 );
nDay ++ ;
}

printf( "\n============ 30 天模擬結束 ============\n" );


return 0;
}

You might also like