C使用数组值擦除数组

时间:2017-04-29 17:18:04

标签: c arrays

当我使用数组值分配另一个数组值时,我很困惑。原始数组删除使用的值

int main(int argc, char *argv[]) {

    char original[ORIGINAL_SIZE];
    int isbn[ISBN_SIZE];
    int index = 0;
    int code;
    int weight = 10;
    int weightedValue;
    int weightedSum = 0;

    printf("Enter an ISBN to validate: ");
    validateISBNArray(original);

    while(index < ORIGINAL_SIZE){
        if(original[index] != '-'){
            if(original[index] == 'x' || original[index] == 'X') isbn[index] = 10;
            else if(original[index] == 0) isbn[index] = 0;
            else isbn[index] = original[index]-48;
            code = isbn[index];
            //printf("%d", code);
            weightedValue= code*weight;
            weight--;
            weightedSum += weightedValue;
        }
        index++;
    }
    printf("%s",original);
    if(weightedSum%11==0) printf("The ISBN %s, is VALID", original);
    else printf("The ISBN %s, is NOT VALID", original);

    return 0;
}

validateISBNArray对原始数组

没有影响

这是不影响功能的代码

void validateISBNArray(char array[]){

    int index = 0;
    int countDigits = 0;
    int value = 0;  

    clearArray(array, ORIGINAL_SIZE);
    scanf("%s",array);      

    while(index < ORIGINAL_SIZE){
        //printf("%d %c %d\n", index, array[index], countDigits);
        if(((array[index]-48) >= 0 && (array[index]-48) <= 9) || (array[index] == 'x'|| array[index] == 'X')) {
            countDigits++;
            index++;
        }
        else if(array[index] == '-' || array[index] == 0) index++;
        else{
            printf("INVALID CHARACTER %d = %c. Please Enter Digits Or/And Hyphens Only: ", index, array[index]);
            index = 0;
            countDigits = 0;
            clearArray(array, ORIGINAL_SIZE);
            scanf("%s",array);
        }
        if(index == ORIGINAL_SIZE && countDigits != 10){
            printf("INVALID NUMBER OF DIGITS %d. Please Enter 10 Digits: ", countDigits);
            index = 0;
            countDigits = 0;
            clearArray(array, ORIGINAL_SIZE);
            scanf("%s",array);
        }       
    }
    //printf("%s", array);
}

1 个答案:

答案 0 :(得分:1)

好的,我修复了它,无需使用isbn数组

int main(int argc, char *argv[]) {

    char original[ORIGINAL_SIZE];
    int isbn[ISBN_SIZE];
    int index = 0;
    int code;
    int weight = 10;
    int weightedValue;
    int weightedSum = 0;

    printf("Enter an ISBN to validate: ");
    validateISBNArray(original);

    while(index < ORIGINAL_SIZE){
        if(original[index] != '-'){
            if(original[index] == 'x' || original[index] == 'X') code = 10;
            else if(original[index] == 0) code = 0;
            else code = original[index]-48;
            weightedValue = code*weight;
            weight--;
            weightedSum += weightedValue;
        }
        index++;
    }
    if(weightedSum%11==0) printf("The ISBN %s, is VALID", original);
    else printf("The ISBN %s, is NOT VALID", original);

    return 0;
}