2014-09-24 60 views
0

我是新來的,所以我不知道我是否發佈了這個正確的。我做了教練告訴我們爲這個項目做的所有事情,但最後一個讓我很難過,因爲我們從來沒有在課堂上談論過分類。它說:「修改print()函數,使其顯示按零件號升序排列的零件。」我試圖翻閱這本書和互聯網,但我只是讓自己變得更加困惑。誰能幫我?這裏是我的代碼:C - 通過零件號碼以升序顯示零件

#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; 
} 
+0

請問您的分配要求你做出自己的排序程序?如果沒有,你可能想看看'qsort'這是一個c庫函數。 – 2014-09-24 22:34:46

+0

@MichaelPetch教練沒有指定他想要的。我們甚至從來沒有討論過分類。這就是爲什麼我感到困惑,爲什麼它的任務。 – InSeriousNeedOfAspirin 2014-09-24 22:37:39

+0

@InSeriousNeedOfAspirin然後,給您的教授的電子郵件是最好的選擇。 – 2014-09-24 22:38:40

回答

1

如果你的教授,您可以使用函數從標準libary,檢查出C library function to do sort,它提供了一個答案,告訴您如何使用qsort()。如果你在linux上,你也可以通過執行man qsort來獲得關於函數(和其他標準庫函數)的文檔。如果您不在Linux上,請查看各種在線手冊頁。

如果不是,你的教授可能希望你做自己的研究。通常氣泡排序算法因爲其簡單性而被教給初學者。 rosettacode提供冒泡排序應該是什麼樣子的僞代碼:

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()

qsort(inv, np, sizeof(part), compare); 

在您的排序算法(你應該自己寫):

if (item.number at index) > (item.number at (index + 1))