如果该值存在于C中的数组函数中,则添加值的数量

时间:2017-07-24 06:09:34

标签: c arrays function pointers

我在编写用于支付图书的代码方面遇到了问题。 它必须添加数量,如果ISBN号相同,但它不会增加数量,它会产生另一个具有相同ISBN的值......我无法弄清楚如何在现有值中添加数量。我认为问题出在searchinventory函数或addbook本身。

#include <stdio.h>
#define MAX_BOOKS 10
#define MAX_TITLE_SIZE 20

struct Book {
    int _isbn; //International Standard Book Number
    float _price; //Book Price
    int _year; // Publication Year
    char _title[MAX_TITLE_SIZE]; //Book Title
    int _qty; //Book Quantity
};
//////////////////////////////////
void flushKeyboard(void);
void displayInventory(const struct Book book[],const int size);
void addBook(struct Book book[],int *size);
int searchInventory(const struct Book book[],const int isbn,const int size);
void checkPrice(const struct Book book[],const int size);
///////////////////////////////////
void flushKeyboard(void)
{
    char enter;
    do{
        scanf("%c",&enter);


    }while(enter != '\n');
}
void displayInventory(const struct Book book[],const int size){
    int i = 0;
    printf("\n\nInventory\n");
    printf("===================================================\n");
    printf("ISBN      Title               Year Price  Quantity\n");
    printf("---------+-------------------+----+-------+--------\n");

    for(i=0;i<size;i++){
            printf("%-10.0d%-20s%-5d$%-8.2f%-8d\n",book[i]._isbn,book[i]._title,book[i]._year,book[i]._price,book[i]._qty);
    }
    printf("===================================================\n\n");
}
//////////////////////////////////////////////
void addBook(struct Book book[],int *size){
    int result = -1;
    int _isbn;
    int _qty;

    if(*size == MAX_BOOKS){
        printf("the inventory is full");

    }

    else{
        printf("ISBN:");

        scanf("%d",&_isbn);
        result = searchInventory(&book[*size],_isbn,*size);

        printf("_isbn = %d\n",_isbn);
        printf("Quantity:");

        scanf("%d",&_qty);

        if(result != -1){
            book[result]._isbn = _isbn;
            book[result]._qty = book[result]._qty + _qty;
            printf("The book exists in the repository, quantity is updated.\n");

            printf("book[result]._qty is %d\n",book[result]._qty);
            printf("book[*size]._qty is %d\n",book[*size]._qty);
            }
        if(result == -1){
            book[*size]._isbn = _isbn;
            book[*size]._qty = _qty;
            printf("Title:");

            scanf(" %20[^\n]",book[*size]._title);
            flushKeyboard();
            printf("Year:");
            scanf("%d",&book[*size]._year);
            printf("Price:");
            scanf("%f",&book[*size]._price);
            printf("The book is successfully added to the inventory.\n\n");

        }
        }
}
//////////////////////////////////////////////////////////////////////////////
int searchInventory(const struct Book book[],const int isbn,const int size){
    int i = 0;
    int result = -1;
    for(i=0;i<size;i++){
        if(isbn == (book[i]._isbn)){

            result = i;
            return result;
            break;
        }
    }

    return result;
}
/////////////////////////////////////////////////////////////////////////////
void checkPrice(const struct Book book[],const int size){
    int isbn;
    int i = 0;

    printf("Please input the ISBN number of the book\n\n");
    scanf("%d",&isbn);

    i = searchInventory(&book[size],isbn,size);

    printf("Book %d costs %.2d\n\n",isbn,book[size]._price);
}
int main(void){

    struct Book book[MAX_BOOKS];
    int size=0;
    int option;
    printf("Welcome to the Book Store\n=========================\n");
    do{

        printf("Please select from the following options:\n");
        printf("1) Display the inventory.\n");
        printf("2) Add a book to the inventory.\n");
        printf("3) Check price.\n");
        printf("0) Exit.\n");

        printf("\nSelect: ");
        scanf("%d",&option);
        switch(option){
            case 1:
                if(size == 0){
                    printf("The inventory is empty!\n");
                    printf("===================================================\n\n");

                }
                else{
                        displayInventory(book,size);
                }

                break;
            case 2:
            //SIZE++;
            addBook(book,&size);
            size++;
                break;
            case 3:
                printf("Please select input the ISBN number of the book: \n\n");
                //checkPrice();
                break;
            case 0:
                printf("Goodbye!\n");
                break;
            default: //wrong input
                printf("Invalid input, try again:\n");
                break;
        }
    }while(option!=0);


    return 0;
}

1 个答案:

答案 0 :(得分:0)

如果以下代码可以帮助您,请尝试。我试图修复错误,以便添加新书可以计算书店中该特定书籍的数量。

测试运行程序:

Welcome to the Book Store
=========================
Please select from the following options:
1) Display the inventory.
2) Add a book to the inventory.
3) Check price.
0) Exit.

Select: 1
The inventory is empty!
===================================================

Please select from the following options:
1) Display the inventory.
2) Add a book to the inventory.
3) Check price.
0) Exit.

Select: 2
ISBN:12345678
_isbn = 12345678
Quantity:2
Title:FOO
Year:1991
Price:19
The book is successfully added to the inventory.

Please select from the following options:
1) Display the inventory.
2) Add a book to the inventory.
3) Check price.
0) Exit.

Select: 1


Inventory
===================================================
ISBN      Title               Year Price  Quantity
---------+-------------------+----+-------+--------
12345678  FOO                 1991 $19.00   2       
===================================================

Please select from the following options:
1) Display the inventory.
2) Add a book to the inventory.
3) Check price.
0) Exit.

Select: 2
ISBN:87654321
_isbn = 87654321
Quantity:1
Title:BAZ
Year:1987
Price:4
The book is successfully added to the inventory.

Please select from the following options:
1) Display the inventory.
2) Add a book to the inventory.
3) Check price.
0) Exit.

Select: 1


Inventory
===================================================
ISBN      Title               Year Price  Quantity
---------+-------------------+----+-------+--------
12345678  FOO                 1991 $19.00   2       
87654321  BAZ                 1987 $4.00    1       
===================================================

Please select from the following options:
1) Display the inventory.
2) Add a book to the inventory.
3) Check price.
0) Exit.

Select: 2
ISBN:12345678
_isbn = 12345678
Quantity:3
The book exists in the repository, quantity is updated.
book[result]._qty is 5
Please select from the following options:
1) Display the inventory.
2) Add a book to the inventory.
3) Check price.
0) Exit.

Select: 1


Inventory
===================================================
ISBN      Title               Year Price  Quantity
---------+-------------------+----+-------+--------
12345678  FOO                 1991 $19.00   5       
87654321  BAZ                 1987 $4.00    1       
===================================================

Please select from the following options:
1) Display the inventory.
2) Add a book to the inventory.
3) Check price.
0) Exit.

Select: 

代码

#include <stdio.h>
#include <stdint.h>

#define MAX_BOOKS 10
#define MAX_TITLE_SIZE 20

struct Book {
    int64_t _isbn; //International Standard Book Number
    float _price; //Book Price
    int _year; // Publication Year
    char _title[MAX_TITLE_SIZE]; //Book Title
    int _qty; //Book Quantity
};
//////////////////////////////////
//////////////////////////////////
void flushKeyboard(void);
void displayInventory(const struct Book book[],const int size);
int addBook(struct Book book[],int *size);
int searchInventory(const struct Book book[],const int isbn,const int size);
void checkPrice(const struct Book book[],const int size);
///////////////////////////////////
void flushKeyboard(void)
{
    char enter;
    do{
        scanf("%c",&enter);


    }while(enter != '\n');
}
void displayInventory(const struct Book book[],const int size){
    int i = 0;
    printf("\n\nInventory\n");
    printf("===================================================\n");
    printf("ISBN      Title               Year Price  Quantity\n");
    printf("---------+-------------------+----+-------+--------\n");

    for(i=0;i<size;i++){
        printf("%-10.0d%-20s%-5d$%-8.2f%-8d\n",book[i]._isbn,book[i]._title,book[i]._year,book[i]._price,book[i]._qty);
    }
    printf("===================================================\n\n");
}
//////////////////////////////////////////////
int addBook(struct Book book[],int *size){
    int result = -1;
    int _isbn;
    int _qty;

    if(*size == MAX_BOOKS){
        printf("the inventory is full");

    }

    else{
        printf("ISBN:");

        scanf("%d",&_isbn);
        result = searchInventory(book,_isbn,*size);
        printf("_isbn = %d\n",_isbn);
        printf("Quantity:");

        scanf("%d",&_qty);

        if(result != -1){
            book[result]._isbn = _isbn;
            book[result]._qty = book[result]._qty + _qty;
            printf("The book exists in the repository, quantity is updated.\n");

            printf("book[result]._qty is %d\n",book[result]._qty);
            //printf("book[*size]._qty is %d\n",book[*size]._qty);
            return 0;
        }
        else {
            book[*size]._isbn = _isbn;
            book[*size]._qty = _qty;
            printf("Title:");

            scanf(" %19[^\n]",book[*size]._title);
            flushKeyboard();
            printf("Year:");
            scanf("%d",&book[*size]._year);
            printf("Price:");
            scanf("%f",&book[*size]._price);
            printf("The book is successfully added to the inventory.\n\n");
            return 1;

        }
    }
}
//////////////////////////////////////////////////////////////////////////////
int searchInventory(const struct Book book[],const int isbn,const int size){
    int i = 0;
    int result = -1;
    for(i=0;i<size;i++){
        if(isbn == (book[i]._isbn)){

            result = i;
            return result;
            break;
        }
    }

    return result;
}
/////////////////////////////////////////////////////////////////////////////
void checkPrice(const struct Book book[],const int size){
    int isbn;
    int i = 0;

    printf("Please input the ISBN number of the book\n\n");
    scanf("%d",&isbn);

    i = searchInventory(&book[size],isbn,size);

    printf("Book %d costs %.2f\n\n",isbn,book[i]._price);
}
int main(void){

    struct Book book[MAX_BOOKS];
    int size=0;
    int option;
    printf("Welcome to the Book Store\n=========================\n");
    do{

        printf("Please select from the following options:\n");
        printf("1) Display the inventory.\n");
        printf("2) Add a book to the inventory.\n");
        printf("3) Check price.\n");
        printf("0) Exit.\n");

        printf("\nSelect: ");
        scanf("%d",&option);
        switch(option){
            case 1:
                if(size == 0){
                    printf("The inventory is empty!\n");
                    printf("===================================================\n\n");

                }
                else{
                    displayInventory(book,size);
                }

                break;
            case 2:
                //SIZE++;
                size = size + addBook(book,&size);
                //size++;
                break;
            case 3:
                printf("Please select input the ISBN number of the book: \n\n");
                //checkPrice();
                break;
            case 0:
                printf("Goodbye!\n");
                break;
            default: //wrong input
                printf("Invalid input, try again:\n");
                break;
        }
    }while(option!=0);


    return 0;
}