c-我如何根据我的课程的具体选择做出选择



在我的程序中,我计划为用户筛选选项,以获得库存的特定数据。例如,在程序开始时,会有一个菜单,用户可以选择添加或删除数据,例如,如果他们说插入,我会让他们选择一家汽车公司(如丰田或本田),一旦他们选择了该公司,我会给他们选择他们可以添加到库存中的所有丰田车型。这会发生,这样我就可以缩小范围,对我得到的信息进行选定的操作。

问题是,我不知道如何筛选出特定公司的模型。我创建了包含不同模型的不同数组,但我不知道如何显示该公司模型的用户选项。

这是我的密码。。

#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("nnn1.Toyota n2.Honda n3.Hyundai n4.Nissan n5.Mitsubishi n6.Volksvagon n7.Acura n8.Ford n9.Dodge n10.GMCn");
    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("nnEnter the car model: ");
    scanf("%s", carmodel);
    printf("nnEnter the car year: ");
    scanf("%s", caryear);
    printf("nnEnter the car type: ");
    scanf("%s", cartype);
    printf("nnEnter 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("nnLinked 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  Quantityn");
        while(ptr != NULL){
            printf("n%d.t%s   %s   %s   %s   %dn", 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("nnn1. Insert n2. Delete n3. Display n4. Exitn");
        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("nnInvalid choice. Please try again. n");
        }
    } while(1);

    return 0;
}

OP:"不知道如何显示模型的用户选项。"

您需要从所选的汽车公司动态形成问题。建议不要使用固定的列表大小(去掉10),并在指定的列表末尾附加一个NULL。通过检查扫描结果和参数范围改进错误处理。

const char **CompanyModels[] = { toyotaModels, hondaModels, 0 /* List is 2 long */};
int ModelPrompt(const char **CompanyModels) {
  printf("nnn");
  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("nnn1.Toyota n2.Honda n3.Hyundai n4.Nissan n5.Mitsubishi n6.Volksvagon n7.Acura n8.Ford n9.Dodge n10.GMCn");
  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.  

相关内容

  • 没有找到相关文章

最新更新