2013-08-07 48 views
0

在我的程序中,我計劃篩選用戶的選項以獲取庫存的特定數據。 例如,在程序開始時,會出現菜單讓用戶選擇添加或刪除數據,然後例如如果他們說插入,然後我會給他們選擇一家汽車公司(如豐田或本田)的選項和一旦他們選擇了那家公司,我會給他們選擇他們可以在庫存中添加的所有豐田車型。這將會發生,所以我可以縮小範圍並對我提供的信息進行選定的操作。如何根據我的計劃的具體選擇給出選擇?

事情是我不知道如何過濾掉某個特定公司的模型。我已經創建了包含不同模型的不同數組,但我不知道如何顯示該公司模型的用戶選項。

這裏是我的代碼..

#include <stdio.h> 
#include <stdlib.h> 

#define MAX_WORD_LENGTH 20 

#define MAX_SIZE 100 
typedef struct cardata{ 

    char carname[MAX_WORD_LENGTH]; 
    char carmodel[MAX_WORD_LENGTH]; 
    char caryear[MAX_WORD_LENGTH]; 
    char cartype[MAX_WORD_LENGTH]; 
    int quantity; 

}CarData; 


struct node{ 

    CarData data; 
    struct node *next; 
    struct node *prev; 
}*start=NULL; 

const char *companyList[10] = {"Toyota", "Honda","Hyundai","Nissan","Mitsubishi","VoksWagon","Acura","Ford","Dodge","GMC"}; 
const char *companyModels[10] = {toyotaModels,hondamodels,hyundaimodels,nissanmodels,mitsubishimodels,vokswagonmodels,acuramodels,fordmodels,dodgemodels,gmcmodels}; 
const char *toyotaModels[10]={"Corolla","Camery"} 
const char *hondaModels[10]={"Civic","Accord"}; 


void insert_first(){ 
    struct node *ptr; 

    char carname[MAX_WORD_LENGTH]; 
    char carmodel[MAX_WORD_LENGTH]; 
    char caryear[MAX_WORD_LENGTH]; 
    char cartype[MAX_WORD_LENGTH]; 
    int carQuantity; 
    int ch; 

    printf("\n\n\n1.Toyota \n2.Honda \n3.Hyundai \n4.Nissan \n5.Mitsubishi \n6.Volksvagon \n7.Acura \n8.Ford \n9.Dodge \n10.GMC\n"); 
    printf("\nPress a number to select corresponding car(i.e. 1 for toyota, 2 for honda): "); 
    scanf("%d", &ch); 
    strcpy(carname,companyList[ch-1]); 
    printf("\n\nEnter the car model: "); 
    scanf("%s", carmodel); 
    printf("\n\nEnter the car year: "); 
    scanf("%s", caryear); 
    printf("\n\nEnter the car type: "); 
    scanf("%s", cartype); 
    printf("\n\nEnter the quantity of models: "); 
    scanf("%d", &carQuantity); 


    if(start==NULL){ 

     start=(struct node *)malloc(sizeof(struct node)); 
     strcpy(start->data.carname,carname); 
     strcpy(start->data.carmodel,carmodel); 
     strcpy(start->data.caryear,caryear); 
     strcpy(start->data.cartype,cartype); 
     start->data.quantity=carQuantity; 
     start->prev=NULL; 
     start->next=NULL; 

    }else{ 

     ptr=start; 
     start=(struct node *)malloc(sizeof(struct node)); 
     strcpy(start->data.carname,carname); 
     strcpy(start->data.carmodel,carmodel); 
     strcpy(start->data.caryear,caryear); 
     strcpy(start->data.cartype,cartype); 
     start->data.quantity=carQuantity; 
     start->next=ptr; 

    } 

} 

void delete_first(){ 

    struct node *ptr; 
    char carname[MAX_WORD_LENGTH]; 
    char carmodel[MAX_WORD_LENGTH]; 
    char caryear[MAX_WORD_LENGTH]; 
    char cartype[MAX_WORD_LENGTH]; 
    char modelNumber[MAX_WORD_LENGTH]; 
    int carQuantity; 

    if(start==NULL){ 
     printf("\n\nLinked list is empty.\n"); 
    }else{ 
     ptr=start; 
     printf("\nThe car for which the entry is removed is %s \n",ptr->data.carname); 
     strcpy(start->data.carname,carname); 
     strcpy(start->data.carmodel,carmodel); 
     strcpy(start->data.caryear,caryear); 
     strcpy(start->data.cartype,cartype); 
     start->data.quantity=carQuantity; 
     start=start->next; 
     free(ptr); 
    } 
} 

void display() 
{ 
    struct node *ptr=start; 
    int i=1; 

    if(ptr == NULL){ 
     printf("\nLinklist is empty.\n"); 
    }else{ 
     printf("\nSr. No Make  Model Year Type Quantity\n"); 
     while(ptr != NULL){ 
      printf("\n%d.\t%s %s %s %s %d\n", i,ptr->data.carname,ptr->data.carmodel,ptr->data.caryear,ptr->data.cartype,ptr->data.quantity); 
      ptr = ptr->next; 
      i++; 
     } 

    } 
} 

int main(void) 
{ 
    int ch; 
    do 
    { 
     printf("\n\n\n1. Insert \n2. Delete \n3. Display \n4. Exit\n"); 
     printf("\nEnter your choice: "); 
     scanf("%d", &ch); 

     switch(ch) 
     { 

      case 1: 
       insert_first(); 
       break; 
      case 2: 
       delete_first(); 
       break; 
      case 3: 
       display(); 
       break; 
      case 4: 
       exit(0);            
      default: 
       printf("\n\nInvalid choice. Please try again. \n"); 

     } 

    } while(1); 


    return 0; 
} 

回答

0

OP: 「不知道如何顯示的機型用戶的選擇。」

您需要從選定的汽車公司動態形成問題。建議不要使用固定列表大小(放下10)並將NULL附加到列表的末尾。通過檢查scanf結果和參數範圍來改進錯誤處理。

const char **CompanyModels[] = { toyotaModels, hondaModels, 0 /* List is 2 long */}; 

int ModelPrompt(const char **CompanyModels) { 
    printf("\n\n\n"); 
    int i; 
    for (i=0; CompanyModels[i], i++) { 
    printf("%d.%s \n", d+1, CompanyModels[i]); 
    } 
    return i; 
} 

void insert_first(){ 
    ... 
    // recommend replacing next 2 lines with a CompanyPrompt(companyList); 
    printf("\n\n\n1.Toyota \n2.Honda \n3.Hyundai \n4.Nissan \n5.Mitsubishi \n6.Volksvagon \n7.Acura \n8.Ford \n9.Dodge \n10.GMC\n"); 
    printf("\nPress a number to select corresponding car(i.e. 1 for toyota, 2 for honda): "); 
    scanf("%d", &ch); 
    strcpy(carname,companyList[ch-1]); 

    int MaxModelIndex = ModelPrompt(CompanyModels[ch]); 
    int carmodelIndex = -1; 
    if ((1 != scanf("%d", carmodelIndex)) || (carmodelIndex < 0) || (carmodelIndex > MaxModelIndex)) { 
    ; // handle error 
    } 
    strcpy(carmodel, CompanyModels[ch][carmodelIndex]); 

    // continue with similar code for cartype. 
相關問題