2013-11-09 62 views
0

我想將我的大程序拆分爲多個文件... 也許只有一個結構, 然後函數讀取一個csv文件,並提取單詞,並把它們放入一個數組.. 然後其他功能使用bubblesort對數組進行排序, ,最後是我把所有東西放在一起的主函數... 這個東西就是我在main函數的開頭聲明瞭必要的變量,當我想要分離函數時,變量我在主函數中聲明,並且我用於這些函數不再被發現...我是否必須再次將它們作爲參數傳遞給函數? OR還有其他更好的方法嗎?我可以聲明外部或靜態變量嗎?C++如何將代碼拆分爲多個文件?

其次,我不知道什麼時候創建一個.h文件或另一個.cpp文件,如果另一個.cpp文件,它不允許有另一個主要功能,那麼我該怎麼做?宣佈公開課?

這裏是我的代碼,我要分開:

#include <cstdlib> 
#include <stdio.h> 
#include <stdlib.h> 
#include <iostream> 
#include <string.h> 
//#include "studentStruct.h" 

using namespace std;  


struct Stud{ 
     long matrnr; 
     char vorname[30]; 
     char name[30]; 
     char datum[30]; 
     float note; 
    }; 


void bubbleSort(Stud mystud[], int studentCounter);    


int main(int argc, char **argv) 
{ 

    const int MAX = 30; 
    Stud stud; 
    Stud mystud[30]; // <<-- Array of "Stud" type 
    //memset((void*)mystud,0,sizeof(mystud) * sizeof(Stud)); 
    int wordCounter(0); 
    int i(0); //thats the charCounter or index 
    int studentCounter(0); 
    char wort[MAX]; 



    FILE * pFile; 
    int cnr(0);  

    pFile=fopen("studentendaten.txt","r"); 
    if (pFile==nullptr) 
    { 
     perror ("Fehler beim öffnen der Datei"); 
    } 

    else 
    {  
    while (cnr != EOF) 
    {  
     (cnr=fgetc(pFile)) ;  

     if ((char)cnr == '\n') { 
      mystud[studentCounter] = stud; 
      studentCounter++;      
      continue;   
     } 

     if ((char)cnr == ';') { 


      wort[i] = '\0'; 

      switch (wordCounter % 5) { 

       case 0:    
       stud.matrnr = atol(wort); 
       break; 

       case 1: 
       strcpy(stud.name, wort); 
       break; 

       case 2: 
       strcpy(stud.vorname, wort); 
       break; 

       case 3: 
       strcpy(stud.datum,wort); 
       break; 

       case 4: 
       stud.note = atof(wort); 
       break; 
      }   

      wordCounter++;   
      i = 0; 
      continue; 
     } 

     wort[i] = (char)cnr; 
     i++;     

    } 

    mystud[studentCounter] = stud; 
    fclose (pFile); 
} 


    bubbleSort(mystud , studentCounter); 

    for (int i(0) ; i <= studentCounter; i++) { 
    //cout <<mystud[i].matrnr << " | " << mystud[i].name << " | " << mystud[i].vorname <<" | " 
    //<< mystud[i].datum <<" | " << mystud[i].note << endl; 
    printf("%ld %15s %15s %15s %6.1f\n",mystud[i].matrnr,mystud[i].name,mystud[i].vorname,mystud[i].datum,mystud[i].note); 
    } 

    return 0; 
} 

void bubbleSort(Stud mystud[], int studentCounter) { 
    Stud tmp; 
    for (int i = 0 ; i<= studentCounter; ++i) { 
     for (int j=0; j<= studentCounter-1; ++j) { 
      if (mystud[j].note > mystud[j+1].note) 
      { 
       /** 
       tmp = mystud[j+1]; 
       mystud[j+1] = mystud[j]; 
       mystud[j] = tmp; 
       **/ 

        tmp.matrnr = mystud[j+1].matrnr; 
        strcpy(tmp.vorname,mystud[j+1].vorname); 
        strcpy(tmp.name,mystud[j+1].name); 
        strcpy(tmp.datum , mystud[j+1].datum); 
        tmp.note = mystud[j+1].note; 

        mystud[j+1].matrnr = mystud[j].matrnr; 
        strcpy(mystud[j+1].vorname ,mystud[j].vorname);     
        strcpy(mystud[j+1].name , mystud[j].name); 
        strcpy(mystud[j+1].datum ,mystud[j].datum); 
        mystud[j+1].note = mystud[j].note; 

        mystud[j].matrnr = tmp.matrnr; 
        strcpy(mystud[j].vorname , tmp.vorname); 
        strcpy(mystud[j].name , tmp.name); 
        strcpy(mystud[j].datum , tmp.datum); 
        mystud[j].note = tmp.note; 


       }   
      } 
     }  
    } 

回答

0
  • 一個頭文件說 - 我們可以做到這一點。
  • .cpp文件說 - 這是我們如何做 - 即。將其編譯爲一個對象。

.cpp文件使用頭文件知道什麼是可用的。

編譯.cpp文件。生成目標文件。將它們連接在一起,這將會把鬆散的目標結合起來。 Bang - 有一個可執行文件。

2

做的最好的(標準)的事情可能會是:

  1. 寫一個類梭哈,不是結構(類的私有成員將namevorname等)
  2. 化妝一個頭文件,在這裏你可以用所有函數的原型編寫該類的定義
  3. 在單獨的.cpp文件中定義這些函數
  4. 最後,你只需要爲實現(main )。

當然,您需要將頭文件包含在.cpp文件中。

相關問題