如何将二维数组乘以c

时间:2015-11-09 22:33:56

标签: c arrays

此代码用于取六位数字,并查看最后一位数字是否等于第一位数字+(第二位数字* 3)+第三位数字+(第四位数字* 3)+第五位数字。然后它将该答案修改为10,以便为您提供最后一个数字以及原始的六位数字号是否有效以及它是否与实际的最后一位数字不同。此代码在乘法位不起作用,因为当您输入数字365121这是一个正确的数字时,您得到的第一个数组插槽等于整数进入第一个数组插槽,其他数字似乎是随机生成的。这可能是我的一个简单错误。

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int barcode_array[1][6], x = 0, y = 0, weighted_1 = 0, weighted_2 = 0, weighted_3 = 0, weighted_4 = 0, weighted_5 = 0, temp = 0, check_digit = 0, menu = 0, menu_2 = 0;
    while(x != 1)
    {
        fflush(stdin);
        printf("Do you want to input a barcode? \n1.Yes \n2.No \n: ");
        scanf("%d",&menu);
        y = 0;
        switch(menu)
        {
            case 1:
                while (y != 1)
                {
                    fflush(stdin);
                    printf("Please input a barcode 6 numbers long: ");
                    scanf("%d",&barcode_array[0]);
                    weighted_1 = barcode_array[0][0];
                    weighted_2 = barcode_array[0][1] * 3;
                    weighted_3 = barcode_array[0][2];
                    weighted_4 = barcode_array[0][3] * 3;
                    weighted_5 = barcode_array[0][4];
                    temp = weighted_1 + weighted_2 + weighted_3 + weighted_4 + weighted_5;
                    printf("%d , %d , %d , %d , %d , %d , %d \n",temp, barcode_array[y][0], barcode_array[y][1], barcode_array[y][2], barcode_array[y][3], barcode_array[y][4]);
                    check_digit = temp % 10;
                    if (barcode_array[y][5] == check_digit)
                    {
                        printf("This is a valid barcode \n");
                    }
                    else
                    {
                        printf("The barcode was not valid it should have ended in a %d \n",check_digit);
                    }
                    y = 1;
                }
                break;
            case 2:
                x = 1;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题是您没有使用scanf函数解析6位数输入。你使用scanf是错误的。

这是我的解决方案;

首先,我使用scanf取六位数字并将其存储在一个简单的int中 其次,我解析六位数字并将每个数字存储在数组中

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int barcode_input;
    int barcode_array[6], x = 0, y = 0, temp = 0, check_digit = 0, menu = 0;

    while(x != 1)
    {
        fflush(stdin);
        printf("Do you want to input a barcode? \n1.Yes \n2.No \n: ");
        scanf("%d",&menu);
        y = 0;
        switch(menu)
        {
            case 1:
                while (y != 1)
                {
                    fflush(stdin);
                    printf("Please input a barcode 6 numbers long: ");
                    scanf("%d",&barcode_input);
                    /* parse barcode_input and put each digit into array */
                    if( (barcode_input<=999999) && (barcode_input>99999) ) /* input must be a 6-digit number */
                    {
                        barcode_array[5] = barcode_input%10;
                        barcode_array[4] = ( (barcode_input - barcode_array[5]) / 10 ) %10;
                        barcode_array[3] = ( (barcode_input - barcode_array[5] - barcode_array[4]*10) /100) %10;
                        barcode_array[2] = ( (barcode_input - barcode_array[5] - barcode_array[4]*10 - barcode_array[3]*100) /1000) %10;
                        barcode_array[1] = ( (barcode_input - barcode_array[5] - barcode_array[4]*10 - barcode_array[3]*100 - barcode_array[2]*1000) / 10000) %10;
                        barcode_array[0] = ( (barcode_input - barcode_array[5] - barcode_array[4]*10 - barcode_array[3]*100 - barcode_array[2]*1000 - barcode_array[1]*10000) / 100000) %10;    
                        temp = barcode_array[0] + barcode_array[1]*3 + barcode_array[2] + barcode_array[3]*3 + barcode_array[4];
                        printf("%d , %d , %d , %d , %d , %d\n",barcode_array[0], barcode_array[1], barcode_array[2], barcode_array[3], barcode_array[4], barcode_array[5]);
                        check_digit = temp % 10;
                        if ( barcode_array[5] == check_digit )
                        {
                            printf("This is a valid barcode \n");
                        }
                        else
                        {
                            printf("The barcode was not valid it should have ended in a %d \n",check_digit);
                        }
                    }
                    else
                    {
                        printf("The input MUST be 6-digit");
                    }

                    y = 1;
                }
                break;
            case 2:
                x = 1;
        }
    }
}

这是输出,我刚用 gcc 编译它;

D:\SOF>gcc barcodecheck.c -Wall -Wextra -pedantic -std=c11 -o barcodecheck

D:\SOF>barcodecheck
Do you want to input a barcode?
1.Yes
2.No
: 1
Please input a barcode 6 numbers long: 1234567
The input MUST be 6-digitDo you want to input a barcode?
1.Yes
2.No
: 1
Please input a barcode 6 numbers long: 365121
3 , 6 , 5 , 1 , 2 , 1
This is a valid barcode
Do you want to input a barcode?
1.Yes
2.No
: 2

希望它有所帮助!