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;
}
4

1 回答 1

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.  
于 2013-08-07T05:16:49.687 回答