从并行阵列打印收据

时间:2014-10-17 23:33:01

标签: c arrays parallel-processing

我的程序是一个购物车程序,它接收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 . . .

注意:最后一个数组不是收据,它只是一天结束的库存。上面的数组是我需要帮助制作的收据。

1 个答案:

答案 0 :(得分:0)

当我用{};替换 所有 出现的{0};时。它马上跑了。

示例

float quant[6] = {};   

float quant[6] = {0};  

这是我得到的一个示例输出(你告诉我它缺少什么)

enter image description here

编辑 以发表评论:

在代码的这个区域添加if(upcR[] != 0)分支:

        for (int i = 0; i < 6; i++){
            if(upcR[i] != 0)//add this condition test
            {
                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]);
            }
        }

并且,您分配descR []的方式不正确。您不能使用=将一个字符串数组设置为等于另一个。 在代码中进行以下更改:

           // descR[element][25] = desc[element][25];
            strcpy(descR[element], desc[element]);  

通过这些更改,输出现在如下所示:

enter image description here