2012-12-16 38 views
0
#include <stdio.h> 
#include <conio.h> 
#include <string.h> 
#include <stdlib.h> 

typedef struct { 
    char model[50], mark[50], color[50]; 
    int cylinderCap; 
} car; 

void read(car *cr, int *nr) { 
    printf("Insert mark: "); 
    (*nr)++; 
    fflush(stdin); 
    gets((cr + *nr)->mark); 
    printf("Insert model: "); 
    gets((cr + *nr)->model); 
    printf("Insert color: "); 
    gets((cr + *nr)->color); 
    printf("Insert the cylinder capacity: "); 
    scanf("%d", &((cr + *nr)->cylinderCap)); 
} 

void display(car *cr, int nr) { 
    printf("\n%-10s \t%-10s \t%-10s %d", (cr + nr)->mark, (cr + nr)->model, 
      (cr + nr)->color, cr[nr].cylinderCap); 
} 

void search_model(car *cr, int *nr, char mod[50]) { 
    int i; 
    for(i = 0; i <= (*nr); i++) 
     if(strcmp((cr + i)->model, mod) == 0) 
      display(cr, i); 
} 

void search_cc(car *cr, int *nr, int cc) { 
    int i; 
    for(i = 0; i <= (*nr); i++) 
     if((cr + i)->cylinderCap >= cc) 
      display(cr, i); 
} 


void clear(car *cr, int *nr, char mod[50]) { 
    int k = 0,i,j; 

    for(i = 0; i <= (*nr); i++) 
     if(strcmp((cr + i)->model, mod) == 0) 
     { 
      k++; 
      for(j = i; j <= (*nr - k); j++) 
       *(cr + j) = cr[j + 1]; 
     } 
    *nr = *nr - k; 
} 


void main() { 

    car cr[50]; 
    int opt, n = -1, i, cc = 1900; 
    char mod[50]; 

    do{ 
     system("CLS"); 
     printf("1.Add a car\n"); 
     printf("2.Display cars\n"); 
     printf("3.Search a car after its model\n"); 
     printf("4.Display all the cars with cc > 1900\n"); 
     printf("5.Remove a car after its model\n"); 
     printf("6.Exit\n"); 
     printf("7.Display the biggest cyclinder capacity of all the cars\n"); 
     printf("Insert option: "); 
     scanf("%d", &opt); 

     switch(opt) { 
      case 1: 
       read(&cr[0], &n); 
       break; 

      case 2: 
       printf("\n%-10s \t%-10s \t%-10s %s", "mark", "Model", "color", "Capacity"); 

       for(i = 0; i <= n; i++) 
        display(cr, i); 
       break; 

      case 3: 
       printf("Insert model: "); 
       scanf("%s", mod); 
       printf("\n%-10s \t%-10s \t%-10s %s", "mark", "Model", "color", "Capacity"); 
       search_model(&cr[0], &n, mod); 
       break; 

      case 4: 
       printf("\n%-10s \t%-10s \t%-10s %s", "mark", "Model", "color", "Capacity"); 
       search_cc(&cr[0], &n, cc); 
       break; 

      case 5: 
       printf("Insert the model you wish to delete: "); 
       scanf("%s", mod); 
       clear(&cr[0], &n, mod); 
       break; 

      case 6: 
       break; 

      case 7: 
       break; 

      default: printf("Error! Please try another option!\n"); 
       break; 
     } 
     getch(); 
    }while(opt != 6); 

    getch(); 
} 

這將顯示:顯示用C所有的汽車最大的汽缸容量 - 結構

1.Add a car 
2.Display cars 
3.Search a car after its model 
4.Display all the cars with cc > 1900 
5.Remove a car after its model 
6.Exit 
7.Display the biggest cylinder capacity of all the cars 
Insert option: 

我沒有任何想法如何做最後一個,我甚至不知道如何開始! 我想這樣的事情,但沒有成功:

void maximum(car *cr, int *nr, int max) { 
    int i; 
    for(i = 0; i <- (*nr); i++) 
     if(((cr + i)->car) >=max)) 
      max = (cr + i)->car; 
     display(max); 
} 

但它並沒有真正的工作,所以任何幫助,將不勝感激!

+0

'conio.h'不是標準標題。 'fflush(stdin)'是錯誤的。切勿使用'gets'。 'void main'是錯誤的。避免用戶輸入的'scanf'。 'scanf「%s」'是錯誤的。 「不起作用」不是問題描述。 – melpomene

+2

另外:'for(i = 0; i < - (* nr); i ++)'。也許你打算寫'for(i = 0; i <=(* nr); i ++)'? –

+0

對於(i = 0; i <=(* nr); i ++) @melpomene - 這就是他們在學校教我們的方式!這可能是錯誤的,但在這一點上,我只是一個初學者,現在我想專注於如何解決簡單的事情! – user1908349

回答

1

您的maximum函數沒有任何意義。你試圖使用一個對象類型作爲一個結構的項目,使用多餘的指針算術和其他已經在你的文章的評論中討論過的東西。

void maximum(car *cr, int nr) 
{ 
    int i, max = 0; 
    for(i = 1; i <= nr - 1; i++) 
    { 
     if(cr[i].cilinderCap >= cr[max].cilinderCap) 
     { 
      max = i; 
     } 
    } 
    display(cr, max); 
} 

注:嘗試複製到您的文件之前,瞭解我的代碼。

這是人們可以得到的最直觀的方法。注意我使用「氣缸容量」變量來實際檢查最大氣缸容量。

另請注意,如果您在索引0處啓動汽車,則應搜索n - 1,而不是nnr的引用參數也是多餘的,因爲您甚至不嘗試修改它。

所以,你只要致電:

maximum(cr, n); 

另一件事是,你應該在功能檢查錯誤,因爲在當前狀態下,如果在列表中沒有的元素它可能會崩潰。

代碼中存在一些其他「道德不正確的東西」,與問題無關,但我建議閱讀Google上關於結構列表的一些文章。

+0

謝謝你的回答和解釋。我認爲,與我在其他書中看到的相比,他們在學校教我們的東西實際上是無用的。我已經開始閱讀K. N. Kings關於C的書,它使我所瞭解的許多知識變得更容易。 但是由於我們只有一個學期的c,而且我們每週有一個課時爲2小時,所以同時學習這本書有點困難。我知道fflush(stdin)是錯的,當我問我的老師這件事時,她說她需要在網上看看它。 – user1908349

+0

我猜Google現在比老師做得更好。 – user1908349