Re: [問題] C++新手之學校作業 程式碼露露登 有辦ꨠ…

看板C_and_CPP作者 (非天夜翔)時間15年前 (2010/04/13 13:55), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
※ 引述《koilook (高級黑心貨)》之銘言: : 這學期才有的C++課程 : 老師出了一個作業題目 : -------題目------------------ : 三種水果: 每一種水果有一個條碼編號. 利用條碼編號來分辨價錢. : 由使用者輸入條碼編號及重量, 將所有水果的總價計算出來. : 水果條碼(Barcode): 水果單價 : AppleBR = 234567 100 BananaBR = 563453 35 PapayaBR = 734523 45 : 每樣水果買超過 10 公斤 打8折. 老主顧一律打95折. 這個我是這樣解釋的,老主顧再打95折, 也就是說如果老主顧買超過10Kg應該是乘以0.8再乘以0.95 : 利用 printf("\nInput BarCode: "); scanf("%d", &BarCode); // 取得條碼 題目有說需要用到 Barcode ,但是妳的 code 裡面並無做到這點。 : printf("\nInput Fruit Weight: "); scanf("%lf", &Weight); // 取得重量 : 三種水果都要買. 題目要三種都買到,所以需要能夠判斷是否都有買到。 因為不知道什麼時候才能買完三種,所以我用do-while。 : -------------------------------------- 版大高手推文中提到了 struct + array 這個可以是所短你的 code 的一種方法, 小弟不才借用高手的提示和你的題目來練練手, #define _Weight 0 #define _New 1 #define _Old 2 sturct Fruit { int barcode, pricePerKg; Fruit(int br,int price) { barcode = br; pricePerKg = price; } } Apple(234567,100), Banana(573453,35),Papaya(734523,45); float commission[] = {0.80,1.00,0.95}; int main() { int customLv; float totalPrice = 0; int bought* = {0,0,0}; struct Fruits* = {Apple,Banana,Papaya}; // showText // getCustomLv customLv = (customLv == _Old ? _Old : _New); int br,id,n; float price,weight; do { // showText // getBr,getWeight; if(weight <= 0.0) continue; for(id = 0 ,n = 0; id < 3 ; ++id) { // 檢查哪樣水果的條碼符合輸入的 if(Fruits[i].barcode == inputBr) { price = weight * Fruits[i].pricePerKg; price *= commission[customLv] * (weight>10.0?commission[_Weight]:1); totalPrice += price; bought[id] = 1; } n += bought[id]; } } while(n<3); // showResult return 0; } 不過,我覺得我這樣的code, 檢查條碼的地方還是很糟糕, 還有就是檢查是否所有水果都買了的部份。 還請板上的大大多多指教。謝謝。 m(_@_)m : 以下程式碼有辦法縮短嗎@@? : 寄個站內信 感恩阿里阿豆 : ------以下是新手我打的程式碼有點長-------- : #include "stdafx.h" : #define _New 1 : #define _Old 2 : #define _New_apple 1 : #define _New_banana 2 : #define _New_papaya 3 : #define _Old_apple 4 : #define _Old_banana 5 : #define _Old_papaya 6 : int _tmain(int argc, _TCHAR* argv[]) : { : int Fruit,Name; : double Price ; : double Weight =0 ; : double TotalPrice =0 ; : int Number =0,Count=0; : printf("你想要買幾種水果? "); : scanf("%d", &Number); : Count = 0; : while(Count < Number) : { : printf(" Please Input Name(New=1,Old=2) :"); : scanf("%d", &Name); : if(Name == 1) : { : printf(" Please Input Fruit(apple=1,banana=2,papaya=3) :"); : scanf("%d", &Fruit); : } : else : printf(" Please Input Fruit(apple=4,banana=5,papaya=6) :"); : scanf("%d", &Fruit); : printf(" Please Input Weight:"); : scanf("%lf", &Weight); : if(Weight > 10.0) : { : switch(Fruit) : { : case _New_apple: : Price = 80 * Weight; : break; : case _New_banana: : Price = 28 * Weight; : break; : case _New_papaya : : Price = 36 * Weight; : break; : case _Old_apple: : Price = 80 * Weight; : break; : case _Old_banana: : Price = 28 * Weight; : break; : case _Old_papaya : : Price = 36 * Weight; : break; : } : } : else : switch(Name) : { : case _New: : switch(Fruit) : { : case _New_apple: : { : Price = 100 * Weight; : } : break; : case _New_banana: : Price = 35 * Weight ; : break; : case _New_papaya : : Price = 45 * Weight; : break; : } : case _Old: : switch(Fruit) : { : case _Old_apple: : { : Price = 95 * Weight; : } : break; : case _Old_banana: : Price = 33.25 * Weight; : break; : case _Old_papaya : : Price = 42.75 * Weight ; : break; : } : break; : } : TotalPrice += Price ; : Count ++; : } : return 0; : } -- 渴望飛翔在自由中, 期望逃離這拘束的現實, 一切都讓他隨著而去, 獨自躲在黑暗空氣中, 舔舐被狠狠撕裂的傷口。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.74.9.2 ※ 編輯: csihcs 來自: 211.74.9.2 (04/13 21:57)
文章代碼(AID): #1Bn7VDYF (C_and_CPP)