C-按零件号升序显示零件

C-按零件号升序显示零件,c,function,sorting,inventory,C,Function,Sorting,Inventory,我是新来的,所以我不知道我是否贴对了。我做了老师让我们为这个项目做的所有事情,但最后一个让我难堪,因为我们从来没有在课堂上谈论过分类。它说,“修改print()函数,使其显示按零件号升序排序的零件。”我试着翻阅这本书和上网,但我只是让自己变得更加困惑。有人能帮我吗?这是我的密码: #include <stdio.h> #include <string.h> #include <ctype.h> #define NAME_LEN 25 #define MAX_P

我是新来的,所以我不知道我是否贴对了。我做了老师让我们为这个项目做的所有事情,但最后一个让我难堪,因为我们从来没有在课堂上谈论过分类。它说,“修改print()函数,使其显示按零件号升序排序的零件。”我试着翻阅这本书和上网,但我只是让自己变得更加困惑。有人能帮我吗?这是我的密码:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define NAME_LEN 25
#define MAX_PARTS 100

struct part {
int number;
char name[NAME_LEN + 1];
int on_hand;
float price;
};

int find_part(int number, const struct part inv[], int np);
void insert(struct part inv[], int *np);
void search(const struct part inv[], int np);
void update(struct part inv[], int np);
void print(const struct part inv[], int np);
int read_line(char [], int);

/**************************************************************
 * main:    Prompts the user to enter an operation code,      *
 *          then calls a function to perform the requested    *
 *          action. Repeats until the user enters the         *
 *          command 'q'. Prints an error message if the user  *
 *          enters an illegal code.                           *
 **************************************************************/
int main(void)
{   
char code;
struct part inventory[MAX_PARTS];
int num_parts = 0;

for (;;)
{
    printf("Enter operation code: ");
    scanf(" %c", &code);
    while (getchar() != '\n')    /* skips to end of line */
    {
        ;
    }

    switch (code)
    {
        case 'i':
            insert(inventory, &num_parts);
            break;

        case 's': 
            search(inventory, num_parts);
            break;

        case 'u':
            update(inventory, num_parts);
            break;

        case 'p': 
            print(inventory, num_parts);
            break;

        case 'q':
            return 0;

        default:
            printf("Illegal code\n");
            break;
    }

    printf("\n");
}
}

/************************************************************
 * find_part:   Looks up a part number in the inv array.    *
 *              Returns the array index if the part number  *
 *              is found; otherwise, returns -1.            *
 ************************************************************/
int find_part(int number, const struct part inv[], int np)
{
int i;

for (i = 0; i < np; i++)
{
    if (inv[i].number == number)
    {
        return i;
    }
}

return -1;
}

/****************************************************************
 * insert: Prompts the user for information about a new         *
 *               part and then inserts the part into the inv    *
 *               array. Prints an error message and returns     *
 *               prematurely if the part already exists or the  *
 *               array is full.                                 *
 ****************************************************************/
void insert(struct part inv[], int *np)
{
int part_number;

if (*np == MAX_PARTS)
{
    printf("Database is full; can't add more parts.\n");
    return;
}

printf("Enter part number: ");
scanf("%d", &part_number);

if (find_part(part_number, inv, *np) >= 0)
{
    printf("Part already exists.\n");
    return;
}

inv[*np].number = part_number;
printf("Enter part name: ");
read_line(inv[*np].name, NAME_LEN);
printf("Enter quantity on hand: ");
scanf("%d", &inv[*np].on_hand);
printf("Enter the price of the item: ");
scanf("%f", &inv[*np].price);
(*np)++;
}

/************************************************************
 * search:  Prompts the user to enter a part number, then   *
 *          looks up the part in the inv array. If the      *
 *          part exists, prints the name and quantity on    *
 *          hand; if not, prints an error message.          *
 ************************************************************/
void search(const struct part inv[], int np)
{
int i, number;


printf("Enter part number: ");
scanf("%d", &number);

i = find_part(number, inv, np);
if (i >= 0)
{
    printf("Part name: %s\n", inv[i].name);
    printf("Quantity on hand: %d\n", inv[i].on_hand);
    printf("Item Price: %.2f\n", inv[i].price);
}
else
{
    printf("Part not found.\n");
}
}

/*************************************************************
 * update:  Prompts the user to enter a part number.         *
 *          Prints an error message if the part can't be     *
 *          found in the inv array; otherwise, prompts the   *
 *          user to enter change in quantity on hand and     *
 *          updates the array.                               *
 *************************************************************/
void update(struct part inv[], int np)
{
int i, number, change, userChoice, changePartNum;
float changePrice;
char *changeName[] = {""};

printf("Enter part number: ");
scanf("%d", &number);

i = find_part(number, inv, np);
if (i >= 0)
{   
    printf("Enter your selection to edit this particular part:\n"
            "\t\t Type 1 to change the Part Number\n"
            "\t\t Type 2 to change the Part Name\n"
            "\t\t Type 3 to change the Price\n"
            "\t\t Type 4 to change the Quantity on Hand\n"
            "\t\t Type 5 to exit without making changes\n\n"
            "\t\t Enter your choice here: ");
    scanf("%d", &userChoice);
    switch ( userChoice )
        {
            //printf("Would you like to change the Part Number? \nType 1 for yes or 2 for no.");
            //scanf("%d", &userChoice);
            case 1:
                    printf("Enter new part number: ");
                    scanf("%d", &changePartNum);
                    inv[i].number = changePartNum;
                    printf("Change part num: %d\n", changePartNum);
                    printf("inv[i].number: %d\n", inv[i].number);
                    break;

            //printf("Would you like to change the Part Name? \nType 1 for yes or 2 for no.");
            //scanf("%d", &userChoice);
            case 2:
                    printf("Enter new name of part: ");
                    scanf("%s", changeName);
                    printf("Change part name: %s\n", changeName);
                    //strcpy (*changeName, inv[i].name[NAME_LEN + 1]);
                    //printf("&inv[i].name[NAME_LEN + 1]: %d\n", &inv[i].name[NAME_LEN + 1]);
                    break;


            //printf("Would you like to change the price? \nType 1 for yes or 2 for no.");
            //scanf("%d", &userChoice);
            case 3:
                    printf("Enter change in item price: ");
                    scanf("%f", &changePrice);
                    inv[i].price = changePrice;
                    break;

            //printf("Would you like to change the quantity on hand? \nType 1 for yes or 2 for no.");
            //scanf("%d", &userChoice);
            case 4:
                    printf("Enter change in quantity on hand: ");
                    scanf("%d", &change);
                    inv[i].on_hand = change;
                    break;
            case 5:
                printf("Exiting the editor.");
                break;
            default:
                printf("Your choice is not on the list.");
                break;
        }   
} 
else
{
    printf("Part not found.\n");
}
}

/************************************************************
 * print:   Prints a listing of all parts in the inv array, *
 *          showing the part number, part name, and         *
 *          quantity on hand. Parts are printed in the      *
 *          order in which they were entered into the       *
 *          array.                                          *                                                           *
 ************************************************************/
void print(const struct part inv[], int np)
{
int i;

printf("Part Number  Part Name       "
             "Quantity on Hand    "
             "  Price\n");
for (i = 0; i < np; i++)
{
    printf("%7d\t\t    %-5s%31d\t%.2f\n", inv[i].number,
                 inv[i].name, inv[i].on_hand, inv[i].price);
}
}

/*************************************************************
 * read_line:   Skips leading white-space characters, then   *
 *              reads the remainder of the input line and    *
 *              stores it in str.   Truncates the line if its *
 *              length exceeds n.   Returns the number of    *
 *              characters stored.                           *
 *************************************************************/
int read_line(char str[], int n)
{
int ch = 0;
int i = 0;

while (isspace (ch = getchar()))
{
    ;
}

while (ch != '\n' && ch != EOF)
{
    if (i < n)
    {
        str[i++] = ch;
    }

    ch = getchar();
}

str[i] = '\0';

return i;
}
#包括
#包括
#包括
#定义名称\u LEN 25
#定义最大零件数100
结构件{
整数;
字符名[name_LEN+1];
手边是int;
浮动价格;
};
int find_part(int number,const struct part inv[],int np);
无效插入(结构零件库存[],整数*np);
无效搜索(const struct part inv[],int np);
无效更新(结构零件库存[],内部np);
无效打印(const struct part inv[],int np);
int读取行(字符[],int);
/**************************************************************
*main:提示用户输入操作码*
*然后调用函数来执行请求的*
*行动。重复,直到用户输入*
*命令'q'。如果用户*
*输入非法代码*
**************************************************************/
内部主(空)
{   
字符码;
结构零件库存[最大零件];
int num_parts=0;
对于(;;)
{
printf(“输入操作代码:”);
scanf(“%c”、&code);
而(getchar()!='\n')/*则跳到行尾*/
{
;
}
开关(代码)
{
案例“i”:
插入(库存和零件数量);
打破
案例s:
搜索(库存、零件数量);
打破
案例“u”:
更新(库存、零件数量);
打破
案例“p”:
打印(库存、零件数量);
打破
案例‘q’:
返回0;
违约:
printf(“非法代码”);
打破
}
printf(“\n”);
}
}
/************************************************************
*查找零件:在inv数组中查找零件号*
*如果零件号不正确,则返回数组索引*
*被发现;否则,返回-1*
************************************************************/
int find_零件(int编号,常量结构零件库存[],int np)
{
int i;
对于(i=0;i=0)
{
printf(“部件已存在。\n”);
返回;
}
库存[*np]。编号=零件号;
printf(“输入零件名称:”);
读取行(inv[*np]。名称,名称;
printf(“输入现有数量:”);
scanf(“%d”和&inv[*np]。在手);
printf(“输入项目价格:”);
scanf(“%f”、&inv[*np]。价格);
(*np)++;
}
/************************************************************
*搜索:提示用户输入零件号,然后*
*在inv数组中查找零件。如果*
*零件存在,打印零件上的名称和数量*
*手;如果不是,则打印错误消息*
************************************************************/
无效搜索(常量结构零件库存[],整数np)
{
int i,数字;
printf(“输入零件号:”);
scanf(“%d”和编号);
i=查找零件(编号、库存、np);
如果(i>=0)
{
printf(“零件名称:%s\n”,库存[i]。名称);
printf(“现存量:%d\n”,库存[i]。现存量);
printf(“项目价格:%.2f\n”,存货[i]。价格);
}
其他的
{
printf(“找不到零件。\n”);
}
}
/*************************************************************
*更新:提示用户输入零件号*
*如果无法删除零件,则打印错误消息*
*在inv数组中找到;否则,将提示*
*用户输入现有数量的变化,以及*
*更新阵列*
*************************************************************/
无效更新(结构零件库存[],内部np)
{
int i,number,change,userChoice,changePartNum;
浮动价格;
char*changeName[]={”“};
printf(“输入零件号:”);
scanf(“%d”和编号);
i=查找零件(编号、库存、np);
如果(i>=0)
{   
printf(“输入您的选择以编辑此特定部分:\n”
“\t\t键入1以更改零件号\n”
“\t\t键入2以更改部件名称\n”
“\t\t键入3以更改价格\n”
“\t\t键入4以更改现有数量\n”
“\t\t键入5退出而不进行更改\n\n”
“\t\t在此处输入您的选择:”;
scanf(“%d”、&userChoice);
开关(用户选择)
{
//printf(“是否更改零件号?\n键入1表示是,键入2表示否”);
//scanf(“%d”、&userChoice);
案例1:
printf(“输入新零件号:”);
scanf(“%d”、&changePartNum);
存货[i].number=changePartNum;
printf(“更改部件号:%d\n”,changePartNum);
printf(“库存[i]。编号:%d\n”,库存[i]。编号);
打破
//printf(“你想要什么?”
repeat
    hasChanged := false
    decrement itemCount
    repeat with index from 1 to itemCount
        if (item at index) > (item at (index + 1))
            swap (item at index) with (item at (index + 1))
            hasChanged := true
until hasChanged = false
int compare(struct part a, struct part b)
{
    return (a.number < b.number);
}
qsort(inv, np, sizeof(part), compare);
if (item.number at index) > (item.number at (index + 1))