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)題目
回到首頁

沒有留言: