如何根据我的计划的具体选择做出选择?

时间:2013-08-07 02:27:37

标签: c arrays data-structures linked-list inventory-management

在我的计划中,我计划过滤用户的选择,以获取库存的特定数据。 例如,在程序开始时,将有菜单给用户添加或删除数据的选项,然后例如,如果他们说插入然后我将给他们选择汽车公司(如丰田或本田)和一次他们选择那家公司我会让他们选择他们可以在库存中添加的所有丰田车型。这样就可以了,所以我可以缩小范围,对我提供的信息做一个选定的操作。

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

这是我的代码..

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

1 个答案:

答案 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.