Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 插入时根据变量对数组排序_C_Arrays - Fatal编程技术网

C 插入时根据变量对数组排序

C 插入时根据变量对数组排序,c,arrays,C,Arrays,我一直在为图书结构的图书馆数据库编写一个小程序。在每个索引ibsn long变量中使用0分配和初始化数组,并提示菜单允许选择。添加一本书后,它将被放置在数组中的第一个可用位置(ISBN编号为!=0的第一个位置)。 我的问题是如何将一本书添加到数组中,使其按isbn编号排序,然后首先检查数组中是否有空格? 示例:在数组中输入ISBN 1234的图书;第二本书添加了ISBN 123,放在第一个索引处,将前一本书移动到下一个索引 void addbook(book_t* lib, int size)

我一直在为图书结构的图书馆数据库编写一个小程序。在每个索引ibsn long变量中使用0分配和初始化数组,并提示菜单允许选择。添加一本书后,它将被放置在数组中的第一个可用位置(ISBN编号为!=0的第一个位置)。
我的问题是如何将一本书添加到数组中,使其按isbn编号排序,然后首先检查数组中是否有空格?
示例:在数组中输入ISBN 1234的图书;第二本书添加了ISBN 123,放在第一个索引处,将前一本书移动到下一个索引

void addbook(book_t* lib, int size)
{
    int i;

    //// Loop through the array until you find an empty element
    for (i = 0; i < size; i++) {
        /// Found an empty element
        if ((lib + i)->isbn == 0) {
            //// Ask for and store the book details
            printf("Please enter the book name\n");
            scanf("%s", (lib + i)->name);
            printf("Please enter the book author\n");
            scanf("%s", (lib + i)->author);
            printf("Please enter the book number\n");
            scanf("%ld", &(lib + i)->isbn);
            printf("Please enter the book price\n");
            scanf("%lf", &(lib + i)->price);

            printf("We will now ask you for the release date of the book by day then month then year");

            printf("\nPlease enter release date for this book (dd):"); //user able to change default 
            scanf("%d", &(lib + i)->releaseDate.date);

            printf("\nPlease enter release month for this book (mm):"); //user able to change default 
            scanf("%d", &(lib + i)->releaseDate.month);

            printf("\nPlease enter release year for this book (yyyy):"); //user able to change default 
            scanf("%d", &(lib + i)->releaseDate.year);

            printf("\nDate entered is (%d / %d / %d)", (lib + i)->releaseDate.date, (lib + i)->releaseDate.month, (lib + i)->releaseDate.year); //user able to change default 
                                                                                                                                       /// Set i to size so that the loop finished
            i = size;
        }

        //// If no empty element found and at the last element of the array 
        ///  then library is not found
        else if (i == size - 1)
        {
            printf("The array is full\n");
        }
    }    
}
void addbook(book\t*lib,int size)
{
int i;
////循环遍历数组,直到找到一个空元素
对于(i=0;iisbn==0){
////询问并存储书籍的详细信息
printf(“请输入图书名称\n”);
scanf(“%s”,(lib+i)->名称);
printf(“请输入图书作者\n”);
scanf(“%s”,(lib+i)->作者);
printf(“请输入书号\n”);
scanf(“%ld”&(lib+i)->isbn);
printf(“请输入图书价格”);
scanf(“%lf”、&(lib+i)->价格;
printf(“我们现在将要求您按天、月、年列出本书的发行日期”);
printf(“\n请输入本书的发行日期(dd):”;//用户可以更改默认值
scanf(“%d”和(lib+i)->releaseDate.date);
printf(“\n请输入本书的发行月份(mm):”;//用户可以更改默认值
scanf(“%d”和(lib+i)->releaseDate.month);
printf(“\n请输入本书的发行年份(yyyy):”;//用户可以更改默认值
scanf(“%d”和(lib+i)->releaseDate.year);
printf(“\n输入的数据是(%d/%d/%d)”,(lib+i)->releaseDate.date,(lib+i)->releaseDate.month,(lib+i)->releaseDate.year);//用户可以更改默认值
///将i设置为“大小”,以便循环完成
i=尺寸;
}
////如果未找到空元素,则在数组的最后一个元素处
///那么就找不到库了
else if(i==大小-1)
{
printf(“数组已满\n”);
}
}    
}
我在删除一本书时使用了类似的方法,如果找到了搜索的书,它将移动到数组中的最后一个空间,并将其变量设置为0

void deleteBook(book_t *lib, int size) {
    int i, j, location = 0;
    long searchISBN;
    int found = 0;

    printf("Enter ISBN to search\n");
    scanf("%ld", &searchISBN);

    for (i = 0; i < size; i++) {

        if ((lib + i)->isbn == searchISBN) {
            found++;
            printf("Book Name %s\n", (lib + i)->name);
            printf("Book Author %s\n", (lib + i)->author);
            printf("Book ISBN %ld\n", (lib + i)->isbn);
            printf("Book Price %lf\n", (lib + i)->price);
            location = i;
            i = size;
        } else
        if (i == size - 1) {
            location++;

        }//add to location and try again
    }
    if (found == 1) {
        for (j = location; j < size; j++) {
            strcpy(lib->name, (lib + j)->name);
            strcpy(lib->author, (lib + j)->author);
            (lib)->isbn = (lib + j)->isbn;
            (lib)->price = (lib + j)->price; //user able to change default 
            (lib)->releaseDate.date = (lib + j)->releaseDate.date;
            (lib)->releaseDate.month = (lib + j)->releaseDate.month;
            (lib)->releaseDate.year = (lib + j)->releaseDate.year;

        }//end swapping of elements

        strcpy(lib->name, "0");
        strcpy(lib->author, "0");
        (lib)->isbn = 0;
        (lib)->price = 0;
        (lib)->releaseDate.date = 0;
        (lib)->releaseDate.month = 0;
        (lib)->releaseDate.year = 0;
    } else {
        printf("not found");
    }    
}//end deleteBook method
void deleteBook(book_t*lib,int size){
int i,j,位置=0;
长搜索ISBN;
int=0;
printf(“输入ISBN进行搜索\n”);
scanf(“%ld”,&searchISBN);
对于(i=0;iisbn==searchISBN){
发现++;
printf(“书名%s\n”,(lib+i)->书名);
printf(“图书作者%s\n”,(lib+i)->作者);
printf(“图书ISBN%ld\n”,(lib+i)->ISBN);
printf(“图书价格%lf\n”,(lib+i)->价格);
位置=i;
i=尺寸;
}否则
如果(i==大小-1){
位置++;
}//添加到位置,然后重试
}
如果(找到==1){
用于(j=位置;jname,(lib+j)->name);
strcpy(lib->author,(lib+j)->author);
(lib)->isbn=(lib+j)->isbn;
(lib)->price=(lib+j)->price;//用户可以更改默认值
(lib)->releaseDate.date=(lib+j)->releaseDate.date;
(lib)->releaseDate.month=(lib+j)->releaseDate.month;
(lib)->releaseDate.year=(lib+j)->releaseDate.year;
}//元素的结束交换
strcpy(lib->name,“0”);
strcpy(lib->author,“0”);
(lib)->isbn=0;
(lib)->价格=0;
(lib)->releaseDate.date=0;
(lib)->releaseDate.month=0;
(lib)->releaseDate.year=0;
}否则{
printf(“未找到”);
}    
}//结束删除本方法

您的函数
deleteBook
无法正常工作:它会将找到的条目之后的所有条目的字段复制到数组中的第一个条目上。您只需使用
memmove()
将其余条目作为块复制即可。使用
memmove()
而不是
memcpy()
,因为源和目标内存区域重叠

以下是更正的版本:

#include <stdio.h>
#include <string.h>

void deleteBook(book_t *lib, int size) {
    long searchISBN;
    int found = 0;

    printf("Enter ISBN to search\n");
    if (scanf("%ld", &searchISBN) != 1)
        return;

    for (int i = 0; i < size;) {
        if (lib[i].isbn == searchISBN) {
            book_t book = { 0 };  // empty book record
            found++;
            // display book data before erasing it
            printf("Book Name %s\n", lib[i].name);
            printf("Book Author %s\n", lib[i].author);
            printf("Book ISBN %ld\n", lib[i].isbn);
            printf("Book Price %lf\n", lib[i].price);
            // copy the remainder elements one position to the left
            memmove(lib + i, lib + i + 1, sizeof(*lib) * (size - i - 1))
            // reinitialize the last entry
            memcpy(lib + size - 1, &book, sizeof(*lib));
            // rescan the current entry in case of duplicates
            // hence do not increment i
        } else {
            i++;  // test the next entry
        }
    }
    if (found == 0) {
        printf("not found");
    }    
}
#包括
#包括
void deleteBook(book_t*lib,int size){
长搜索ISBN;
int=0;
printf(“输入ISBN进行搜索\n”);
如果(scanf(“%ld”,&searchISBN)!=1)
返回;
对于(int i=0;i
您可以使用类似的技术在表中的适当位置插入新条目:

void addbook(book_t *lib, int size) {
    book_t book = { 0 };  // empty book record
    int i, used;

    // Loop through the array and pack empty elements
    for (i = used = 0; i < size; i++) {
        if (lib[i].isbn != 0) {
            if (used < i) {
                memcpy(lib + used, lib + i, sizeof(*lib));
            }
            used++;
        }
    }
    // reset unused elements:
    for (i = used; i < size; i++) {
        if (lib[i].isbn != 0) {
            memcpy(lib + i, &book, sizeof(*lib));
        }
    }

    // check if the array is full:
    if (used >= size) {
        printf("The array is full\n");
        return;
    }

    // Ask for and store the book details to local book structure
    printf("Please enter the book name\n");
    if (scanf("%s", book.name) != 1)
        return;
    printf("Please enter the book author\n");
    if (scanf("%s", book.author) != 1)
        return;
    printf("Please enter the book number\n");
    if (scanf("%ld", &book.isbn) != 1)
        return;
    printf("Please enter the book price\n");
    if (scanf("%lf", &book.price) != 1)
        return;

    printf("We will now ask you for the release date of the book by day then month then year");

    printf("\nPlease enter release date for this book (dd):");
    if (scanf("%d", &book.releaseDate.date) != 1)
        return;

    printf("\nPlease enter release month for this book (mm):");
    if (scanf("%d", &book.releaseDate.month) != 1)
        return;

    printf("\nPlease enter release year for this book (yyyy):");
    if (scanf("%d", &book.releaseDate.year) != 1)
        return;

    printf("\nDate entered is (%d / %d / %d)",
           book.releaseDate.date, book.releaseDate.month, book.releaseDate.year);

    //// Loop through the array until you find location to insert book
    for (i = 0; i < used; i++) {
        if (lib[i].isbn > book.isbn)
            break;
    }
    // book should be inserted at position i

    // shift book with larger ISBNs
    memmove(lib + i + 1, lib + i, sizeof(*lib) * (used - i));
    used++;
    // copy book into the array
    memcpy(lib + i, &book, sizeof(*lib));
 }
void addbook(book\t*lib,int size){
book\u t book={0};//空的book记录
使用int i;
//在数组中循环,然后执行p