我的程序是一個購物車程序,它接收UPC(物料ID),數量/重量並計算最終價格。該計劃必須打印出所有購買物品的收據。平行陣列打印收據
的產品列表:
UPC Description PST PPT CIL
4011 BANANAS 1 0.49 123.2
4383 MINNEOLAS 1 0.79 187.3
3144 TANGERINES 1 1.19 135.5
4028 STRAWBERRIES_PINT 0 0.99 104
4252 STRAWBERRIES_HALF_CASE 0 3.99 53
4249 STRAWBERRIES_FULL_CASE 0 7.49 67
UPC是項目碼 PST確定項是否被作爲單位或以重量出售 PPT是每單位/重量 價格CIL是庫存(不相干爲這個問題)
這些值存儲在並行數組中。
計劃:
#include<stdio.h>
#include<stdlib.h>
/*
Description: This program will simulate a checkout at a grocery store. User will input
a UPC (item id) and a wieght/unit. The program will use parallel arrays to store these
values and output a final price at the end.
*/
int main(){
int upc[6] = { 4011, 4383, 3144, 4028, 4252, 4249 };
char desc[6][25] = {
"BANANAS ",
"MINNEOLAS ",
"TANGERINES ",
"STRAWBERRIES_PINT ",
"STRAWBERRIES_HALF_CASE",
"STRAWBERRIES_FULL_CASE" };
int upcR[6] = {};//storage array for reciept
float pptR[6] = {};//storage array for reciept
char descR[6][25] = {};//storage array for reciept
int pst[6] = { 1, 1, 1, 0, 0, 0 };
float ppt[6] = { 0.49, 0.79, 1.19, 0.99, 3.99, 7.49 };
float stock[6] = { 123.2, 187.3, 135.5, 104, 53, 67 };//AKA CIL or Current Inventory Level
float quant[6] = {};
float price[6] = {};
int option;
do
{
printf("Welcome! Enter 1 to begin or 0 to exit.\n");
scanf_s("%d", &option);
if (option == 1){
float subtotal = 0;
float total = 0;
float quantity;
float discount1 = 0;
float discount2 = 0;
float tax = 0;
int input;
int element = -1; //element/row number
do
{
printf("Enter UPC item code or enter 0 to start a new purchase.\n");
scanf_s("%d", &input);
if (input == 0)
break;
for (int i = 0; i < 6; i++)//Searches for element in parallel array using UPC
{
if (upc[i] == input)
{
element = i;
break;
}
}
if (element == -1){ // checks to see if UPC is valid
printf("Invalid UPC. Please try again.\n");
continue;
}
if (pst[element] == 1)// checks if product is sold by units or weight
{
printf("Weight: ");
scanf_s("%f", &quantity);
quant[element] = quantity;//stores elements for reciept array
price[element] = quantity*ppt[element];//stores elements for reciept array
if (quantity > stock[element]){
printf("This item is not available in this quantity.\n");
continue;
}
}
else
{
printf("Units: ");
scanf_s("%f", &quantity);
if (quantity > stock[element]){
printf("This item is not available in this quantity.\n");
continue;
}
}
subtotal += quantity * ppt[element];
stock[element] -= quantity;
quant[element] = 10;//stores elements for reciept array
price[element] = quantity*ppt[element];//stores elements for reciept array
upcR[element] = upc[element];
pptR[element] = ppt[element];
descR[element][25] = desc[element][25];
} while (input != 0);
if (subtotal > 50) //5% discount for purchases over $50
{
discount1 = subtotal - (subtotal * 0.95);
}
int random = rand() % 10 + 1;
if (random == 1){//random 5% discount if random number generated is 1.
discount2 = subtotal - (subtotal * 0.95);
}
float discount = discount1 + discount2;//total 10% from >50 spent and/or random coupon
tax = subtotal*.0825;//tax
total = subtotal + tax - discount;
printf("\n\nUPC\tDescription\t\tPPT\tWeight/Units\tPrice\n");
for (int i = 0; i < 6; i++){
printf("%d \t", upcR[i]);
printf("%s \t", descR[i]);
printf("\t\t%.2f \t", ppt[i]);
printf("%.2f \t\t", quant[i]);
printf("%.2f \t\n", price[i]);
}
printf("\t\t\t\t\tSubtotal\t$%.2f\n", subtotal);
printf("\t\t\t\t\tDiscount\t$%.2f\n", discount);
printf("\t\t\t\t\tTax\t\t$%.2f\n", tax);
printf("\t\t\t\t\tTotal\t\t$%.2f\n", total);
}
else if (option == 0){
break;
}
else
printf("You have entered an invalid option.\n");
}
while (option != 0);
printf("\n\nUPC\tDescription\t\tPST\tPPT\tCIL\n");
for (int i = 0; i < 6; i++){
printf("%d \t", upc[i]);
printf("%s \t", desc[i]);
printf("%d \t", pst[i]);
printf("%.2f \t", ppt[i]);
printf("%.2f \t\n", stock[i]);
}
system("Pause");
return 0;
}
所以我在UPC進入和數量一遍又一遍,直到我做了檢查。問題是我需要以某種方式將這些交易存儲到數組中,以便在完成檢出後可以打印出收據。現在我所擁有的是每個列的收據單獨的數組,我將每個元素都設置到這個單獨的數組中,然後將其打印出來。這似乎不工作,因爲1)我不能將char數組複製到雙數組中,並且2)空數組都是0,我不想要。
有沒有更好的方法來做到這一點?如果有的話,我想建議如何將字符串從char數組複製到另一個數組,然後我會處理0。
下面是該方案的一個例子:
Welcome! Enter 1 to begin or 0 to exit.
1
Enter UPC item code or enter 0 to start a new purchase.
4011
Weight: 12.1
Enter UPC item code or enter 0 to start a new purchase.
4028
Units: 4
Enter UPC item code or enter 0 to start a new purchase.
4383
Weight: 8.3
Enter UPC item code or enter 0 to start a new purchase.
0
UPC Description PPT Weight/Units Price
4011 0.49 10.00 5.93
4383 M 0.79 10.00 6.56
0 T 1.19 0.00 0.00
4028 0.99 10.00 3.96
0 S 3.99 0.00 0.00
0 7.49 0.00 0.00
Subtotal $16.45
Discount $0.00
Tax $1.36
Total $17.80
Welcome! Enter 1 to begin or 0 to exit.
0
UPC Description PST PPT CIL
4011 BANANAS 1 0.49 111.10
4383 MINNEOLAS 1 0.79 179.00
3144 TANGERINES 1 1.19 135.50
4028 STRAWBERRIES_PINT 0 0.99 100.00
4252 STRAWBERRIES_HALF_CASE 0 3.99 53.00
4249 STRAWBERRIES_FULL_CASE 0 7.49 67.00
Press any key to continue . . .
注:最終的陣列是不是收據,這只是天庫存的結束。它上面的數組是我需要幫助製作的收據。
首先,如果你更換將建立所有的'{};'與'{0};'。當我這樣做的時候,它看起來工作順利。收據是列出小計,折扣等的部分嗎? – ryyker 2014-10-17 23:47:47
收款是小計正上方的表格。 – dsa22 2014-10-17 23:53:47
我的輸出(下面)似乎包含收據輸出,或者我錯誤地解釋了你所說的內容....只需使用'0'初始化陣列,並且應該設置。 – ryyker 2014-10-18 00:08:07