gpt4 book ai didi

c - 用 C 验证条形码

转载 作者:行者123 更新时间:2023-11-30 17:04:23 25 4
gpt4 key购买 nike

我是一名 C 初学者,正在开发一个验证条形码的程序。到目前为止,我有一个可以读取 12 位条形码的代码。但是,我在将 12 位输入附加到函数 odd_value 和 Even_value 时遇到问题。我需要这些值来验证我的条形码。

#include <stdio.h>
#define ARRAY_SIZE 12

int print_values(int* x) {
printf("\nSTEP 1: Sum of odds times 3 is \n" );
printf("STEP 2: Sum of the even digits is \n");
printf("STEP 3: Total sum is \n" );
printf("STEP 4: Calculated check digit is \n" );
printf("STEP 5: Check digit and last digit \n" );
return 0;
}

int odd_value() {
int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3;
return sum_odd;
}

int even_value() {
int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9] + x[11]);
return sum_even;
}

int fill_array(){
int i;
printf("Enter a barcode to check. Separate digits with a space >\n");
for(i=0; i<ARRAY_SIZE; i++){
if(scanf("%d:", &x[i]) != 1) return 0;
}
return 1;
}




int main(){

int bar_code[ARRAY_SIZE];
int i, last_digit, check_digit, odd, even;

if(fill_array(bar_code)) {
printf("\nYou entered the code: ");
for(i=0; i<ARRAY_SIZE; i++) printf("%d ", bar_code[i]);
} else {
puts("failed to read");
}

odd = odd_value();
even = even_value();
last_digit = odd + even;
if(last_digit == 0) {
check_digit = 0;
} else {
check_digit = 10 - last_digit;
}

print_values(bar_code);

if(check_digit == bar_code[ARRAY_SIZE]) {
printf("Barcode is VALID\n");
} else {
printf("Barcode is INVALID\n");
}

return 0;
}

编辑:我更新了我的代码一点,但现在我有点困惑。我仍在努力,但我遇到了一些主要错误。

第一个错误:函数“print_values”第 5 行:“x”未声明(在此函数中首次使用)(每个未声明的标识符仅报告一次)

第二个错误:函数“main”第 61 行:“const”之前的预期表达式

#include <stdio.h>
#define ARRAY_SIZE 12

void print_values(int odd_value(const int* x), int even_value(const int* x), int last_digit) {
printf("\nSTEP 1: Sum of odds times 3 is %d\n", odd_value(&x) );
printf("STEP 2: Sum of the even digits is %d\n", even_value(&x));
printf("STEP 3: Total sum is %d\n", last_digit);
printf("STEP 4: Calculated check digit is \n" );
printf("STEP 5: Check digit and last digit \n" );
}

int odd_value(const int* x) {
int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3;
return sum_odd;
}

int even_value(const int* x) {
int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9] + x[11]);
return sum_even;
}

int fill_array(int* x){
int i;
printf("Enter a barcode to check. Separate digits with a space >\n");

for(i=0; i<ARRAY_SIZE; i++){
if(scanf("%d:", &x[i]) != 1) return 0;
}
return 1;
}

int main(){

int bar_code[ARRAY_SIZE];
int i, last_digit, check_digit, odd, even;

if(fill_array(bar_code)) {
printf("\nYou entered the code: ");
for(i=0; i<ARRAY_SIZE; i++) printf("%d ", bar_code[i]);
} else {
puts("failed to read");
}

odd = odd_value(bar_code);
even = even_value(bar_code);

last_digit = odd + even;

if(last_digit == 0) {
check_digit = 0;
} else {
check_digit = 10 - last_digit;
}

if(check_digit == bar_code[11]) {
printf("Barcode is VALID\n");
} else {
printf("Barcode is INVALID\n");
}

print_values(odd_value(const int* x), even_value(const int* x), last_digit);

return 0;
}

编辑:

我相信我还有关于我的代码的最后一个问题。我需要我的最后一个数字等于值总和中的最后一个数字。在我的主函数中,我有last_digit = sum,但需要将其操作为值和中的last_digit。例如,如果 sum = 72,last_digit 将为 2。

#include <stdio.h>
#define ARRAY_SIZE 12

void print_values(int odd, int even, int sum, int last_digit, int check_digit) {
printf("\nSTEP 1: Sum of odds times 3 is %d\n", odd );
printf("STEP 2: Sum of the even digits is %d\n", even);
printf("STEP 3: Total sum is %d\n", sum);
printf("STEP 4: Calculated check digit is %d\n", check_digit);
if(check_digit == last_digit) {
printf("STEP 5: Check digit and last digit match\n" );
} else {
printf("STEP 5: Check digit and last digit do not match\n");
}
}

int odd_value(const int* x) {
int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3;
return sum_odd;
}

int even_value(const int* x) {
int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9]);
return sum_even;
}

int fill_array(int* x){
int i;
printf("Enter a barcode to check. Separate digits with a space >\n");

for(i=0; i<ARRAY_SIZE; i++){
if(scanf("%d:", &x[i]) != 1) return 0;
}
return 1;
}

int main(){

int bar_code[ARRAY_SIZE];
int i, last_digit, check_digit, odd, even;

if(fill_array(bar_code)) {
printf("\nYou entered the code: ");
for(i=0; i<ARRAY_SIZE; i++) printf("%d ", bar_code[i]);
} else {
puts("failed to read");
}

odd = odd_value(bar_code);
even = even_value(bar_code);

int sum = odd + even;
last_digit = sum;

if(last_digit == 0) {
check_digit = 0;
} else {
check_digit = 10 - last_digit;
}

print_values(odd, even, sum, last_digit, check_digit);

if(check_digit == last_digit) {
printf("Barcode is VALID\n");
} else {
printf("Barcode is INVALID\n");
}

return 0;
}

最佳答案

创建一个参数来传递数组(指向第一个元素的指针)并使用它。

int odd_value(const int *x) {
int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3;
return sum_odd;
}

int even_value(const int *x) {
int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9] + x[11]);
return sum_even;
}

调用它们应该像这样:

odd = odd_value(bar_code);
even = even_value(bar_code);

请注意,您对 fill_array() 的定义也是错误的,它的第一行应该是 int fill_array(int *x){

更新:对于新代码:

  • print_values() 中定义 x,否则无法在函数中使用它。
  • 您调用 print_values() 的参数无效。只需写入函数名称(标识符)即可传递函数指针。

我还更正了 print_values() 中的 odd_value()even_value() 参数,使它们适合添加的 x .

改进的代码:

#include <stdio.h>
#define ARRAY_SIZE 12

void print_values(const int *x, int odd_value(const int* x), int even_value(const int* x), int last_digit) {
printf("\nSTEP 1: Sum of odds times 3 is %d\n", odd_value(x));
printf("STEP 2: Sum of the even digits is %d\n", even_value(x));
printf("STEP 3: Total sum is %d\n", last_digit);
printf("STEP 4: Calculated check digit is \n" );
printf("STEP 5: Check digit and last digit \n" );
}

int odd_value(const int* x) {
int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3;
return sum_odd;
}

int even_value(const int* x) {
int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9] + x[11]);
return sum_even;
}

int fill_array(int* x){
int i;
printf("Enter a barcode to check. Separate digits with a space >\n");

for(i=0; i<ARRAY_SIZE; i++){
if(scanf("%d:", &x[i]) != 1) return 0;
}
return 1;
}

int main(void){

int bar_code[ARRAY_SIZE];
int i, last_digit, check_digit, odd, even;

if(fill_array(bar_code)) {
printf("\nYou entered the code: ");
for(i=0; i<ARRAY_SIZE; i++) printf("%d ", bar_code[i]);
} else {
puts("failed to read");
}

odd = odd_value(bar_code);
even = even_value(bar_code);

last_digit = odd + even;

if(last_digit == 0) {
check_digit = 0;
} else {
check_digit = 10 - last_digit;
}

if(check_digit == bar_code[ARRAY_SIZE - 1]) {
printf("Barcode is VALID\n");
} else {
printf("Barcode is INVALID\n");
}

print_values(bar_code, odd_value, even_value, last_digit);

return 0;
}

关于c - 用 C 验证条形码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35835464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com