2012-10-25 16 views
0

我是新的C,非常有興趣知道如何接近擁有超過3個或4個功能中的任何問題,我總是看所需的輸出和操縱在我的代碼調用函數其他功能並獲得所需的輸出。 以下是我通過他的Id首先&然後用戶名查找學生記錄的邏輯。 根據我的教授的這段代碼有一個過度的邏輯,缺乏很多方面,如果有人可以幫助我解決C語言或任何其他語言中的任何問題,那麼對我來說,作爲初學者會有很大的幫助,是的我首先寫了僞代碼。如何處理和優化代碼用C

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

typedef struct{ 

int id;         //Assuming student id to be unique 
int age; 
char *userName;       //Assuming student userName to be unique 
char *dept; 

}student;        // Alias "student" created for struct 

student* createstruct();    // All function prototype declared 
student* createArray(); 
void addstruct(student* s2); 
void searchChar(student* s2,int num); 
void searchInt(student* s2,int num); 

student* createstruct()       // function createStruct() to malloc data of struct student. 
{ 
    student *s; 
    s = (student*)malloc(sizeof(student)); 
    s->userName = (char*)malloc(sizeof(char)*32); 
    s->dept = (char*)malloc(sizeof(char)*32); 
    printf("please enter id "); 
    scanf("%d",&s->id); 
    printf("please enter age "); 
    scanf("%d",&s->age); 
    printf("please enter userName "); 
    scanf("%31s",s->userName); 
    printf("please enter department "); 
    scanf("%31s",s->dept); 
    printf("\n"); 

    return s; 
} 

student* createArray() 
{ 
    student *arr;         //declaration of arr poiter, type struct student 
    arr = (student*)malloc(sizeof(student)*10);  // memory allocated for a size of 10 
    return arr; 
} 

void addstruct(student *s2)      // function for adding data to the structures in array 
{ 
    int i,num; 
    student* s1; 
    printf("please enter the number of records to add:"); 
    scanf("%d",&num); 
    printf("\n"); 

    if(num>0 && num<11) 
    { 
     for(i=0;i<num;i++)     // if user want to enter 5 records loop will only run 5 times 
     { 
     s1 = createstruct(); 
     s2[i].id = s1->id;     // traversing each element of array and filling in struct data 
     s2[i].age = s1->age; 
     s2[i].userName = s1->userName; 
     s2[i].dept= s1->dept; 
     } 
    } 
    else if(num>10)       // if user enters more than 10 
    { 
     for(i=0;i<10;i++)      // loop will still run only 10 times 
     { 
     s1 = createstruct(); 
     s2[i].id = s1->id; 
     s2[i].age = s1->age; 
     s2[i].userName = s1->userName; 
     s2[i].dept = s1->dept; 
     } 

     printf("Array is full");   // Array is full after taking 10 records 
     printf("\n"); 
    } 

searchInt(s2,num);      // Calling searchInt() function to search for an integer in records 
searchChar(s2,num);      // Calling searchChar() function to search for a string in records 
free(s1); 
free(s2); 
} 

void searchChar(student* s2,int num)   // function for searching a string in records of structure 
{ 
    char *c; 
    int i; 
    c = (char*)malloc(sizeof(char)*32); 
    printf("please enter userName to search "); 
    scanf("%31s",c); 
    printf("\n"); 

    for (i=0;i<num;i++)        //num is the number of struct records entered by user 
    { 
     if ((strcmp(s2[i].userName,c)==0))   //using strcmp for comparing strings 
     { 
     printf("struct variables are %d, %d, %s, %s\n", s2[i].id,s2[i].age,s2[i].userName,s2[i].dept); 
     break; 
     } 
     else if(i == num-1) 
     { 
      printf("nothing in userName matches: <%s>\n",c); 
      break; 
     } 
    } 
} 

void searchInt(student* s2,int num)     //searchs for an integer and prints the entire structure 
{ 
    int i,z; 
    printf("please enter id to search "); 
    scanf("%d",&z); 
    printf("\n"); 

    for (i=0;i<num;i++) 
    { 
     if (s2[i].id == z) 
     { 
      printf("struct variables are %d, %d, %s, %s\n\n", s2[i].id,s2[i].age,s2[i].userName,s2[i].dept); 
      break; 
     } 
     else if(i == num-1) 
     { 
      printf("nothing in id matches: <%d>\n\n",z); 
      break; 
     } 
    } 
} 

int main(void) 
{ 
    student *s2; 
    s2 = createArray(); 
    addstruct(s2); 
    return 0; 
} 
+2

「幀代碼」是什麼意思? – Mikhail

+0

糾正之後,我剛纔只是說,方法以及如何與任何問題繼續 – Vbp

+1

查看這些問題的答案:http://stackoverflow.com/questions/4842817/how-do-i-learn-to-write-efficient-和維護-C代碼 http://stackoverflow.com/questions/1711/what-is-the-single-most-influential-book-every-programmer-should-read – Max

回答

0

我不打算進入優化,因爲如果你想更好的理論性能,你可能會使用不同的數據結構,如有序陣列/列表,樹,哈希表或某些類型的索引去。在這種情況下,沒有什麼關係,因爲你有一個處理少量數據的簡單程序。

但我要告訴你關於「過度邏輯」你的教授提到的,把你的searchInt功能爲例:

for (i=0;i<num;i++) 
{ 
    if (s2[i].id == z) 
    { 
     printf("struct variables are %d, %d, %s, %s\n\n", s2[i].id,s2[i].age,s2[i].userName,s2[i].dept); 
     break; 
    } 
    else if(i == num-1) 
    { 
     printf("nothing in id matches: <%d>\n\n",z); 
     break; 
    } 
} 

這裏的事情是,圍繞循環每次你測試看看你是否在循環中的最後一個元素。但循環已經做到了。所以你做了兩次,更糟糕的是,你正在做一個減法(可能會或可能不會被編譯器優化到一個寄存器中)。

你通常會做的是這樣的:

int i; 
student *s = NULL; 

for(i = 0; i < num; i++) 
{ 
    if(s2[i].id == z) { 
     s = &s2[i]; 
     break; 
    } 
} 

if(s != NULL) { 
    printf("struct variables are %d, %d, %s, %s\n\n", 
      s->id, s->age, s->userName, s->dept); 
} else { 
    printf("nothing in id matches: <%d>\n\n",z); 
} 

見你只需要擁有明知環找到了一些方法。在測試它是否找到某些東西之前,您等待循環結束。

在這種情況下,我用一個指針來指示成功,因爲我可以再使用指針來訪問相關記錄,而不必指數回陣列和雜亂的代碼。你不會總是使用指針。

有時候你設置一個標誌,有時你存儲數組索引,有時你只是從函數返回(如果循環漏網你知道它沒有發現任何東西)。

編程的目的是使明智的選擇爲你解決問題。只有在需要時才進行優化,不要使問題過度複雜化,並始終嘗試編寫易於閱讀/理解的代碼。