c - 从并行数组打印收据

标签 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 和数量,直到完成结帐。问题是我需要以某种方式将这些交易存储到一个数组中,以便我可以在结账后打印出收据。现在我有一个单独的数组用于每一列的收据,我正在将我 checkout 的每个元素设置到这个单独的数组中然后打印出来。这似乎效果不佳,因为 1) 我无法将 char 数组复制到 double 数组中,并且 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}; 替换 all 出现的 {}; 时。它马上就跑了。

示例:

float quant[6] = {};   

应该是:

float quant[6] = {0};  

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

enter image description here

EDITS 以解决评论:

在代码的这个区域添加 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

关于c - 从并行数组打印收据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434787/

相关文章:

gcc与cmake编译的编译区别——ldd命令

c# - 用于具有复杂后台计算的响应式 GUI 的多线程 C# - 异步/等待、TPL 或两者?

c# - 如何取消并发的繁重任务?

iphone - 将 Objective-C 对象传递到 CGFunctionRef

c - 不同大小的C指针中的x86 mov指令

c - 在 STM32L1 上删除我的闪存

javascript - 遍历包含图像的数组并删除带有错误 Javascript 的图像

c - 为什么我的所有 char 数组都不会被传递(在 C 中)?

javascript - 如何将 json 数组解析为数组而不是对象

c# - 带有延迟加载列表的并行 Foreach