本帖最後由 陳品肇 於 2019-2-16 15:10 編輯
新增 "購物清單" 功能, 使結帳時能不只計算出價錢, 還能列出細目.
參考執行畫面如下:
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- system("cls"); //清空畫面
- cout<<"☆★☆智能玩具店☆★☆"<<endl<<endl;
- cout<<"[商品價目表]"<<endl;
-
- string a[]={"遙控汽車","飛機模型","足球 ","拼圖 ","玩具槍 ","可愛玩偶","籃球 "};
- int price[7]={450,550,325,200,660,
- 150,380 };
-
- int tmpQty[]={0,0,0,0,0,0,0}; //創一個數量暫存的陣列
- int p,q,sum=0; //p商品代碼 q商品數量
- for(int i=0;i<7;i++)
- {
- cout<<"("<<i+1<<")"<<a[i]<<"\t"<<price[i]<<"元"<<endl;
- }
- cout<<"(8)結帳"<<endl<<endl;
-
- re:
- cout<<"請輸入商品代碼: ";
- cin>> p;
-
- if(p>=1 && p<=7)
- {
- cout<<"數量: ";
- cin>> q;
- if(q>0) //數量>0
- {
- sum= sum+ price[p-1]*q; //計算總金額
- tmpQty[p-1] = tmpQty[p-1] + q; // 選擇的商品數量存起來
- goto re;
- }else //數量<0
- {
- cout<<"您輸入的數量有誤!!"<<endl;
- goto re;
- }
- }else if(p==8)
- {
- goto checkout;
- }else
- {
- cout<<"您輸入的代碼有錯!!"<<endl;
- goto re;
- }
-
- checkout:
- cout<<endl;
- cout<<"[購物清單]"<<endl;
- cout<<"------------------------------"<<endl;
- for(int i =0;i<7;i++) //印出陣列裡的內容
- {
- if( tmpQty[i] !=0) //剛才數量不為0 ,才印出來 != 不等於 == 等於
- {
- cout<<a[i]<<"\t\t"<<price[i]<<"元 * "<<tmpQty[i]<<"個"<<endl;
- }
- }
-
- cout<<"-------------------------------"<<endl;
-
- cout<<"總金額: "<<sum<<"元"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |