2011-04-09 24 views
0

我必須在一個程序中寫入不同的CPP文件我寫作的樂趣和我的主文件中我從我的IsValid cpp文件調用算法。我的IsValid算法還需要從我的主文件,電子郵件中獲取一個變量,然後通過我寫的循環運行它。從獨立的CPP文件調用函數/變量

我想知道如何做到這一點,因爲如果我聽說包括cpp文件是不好的編碼習慣,並且如果我這樣做,反正會導致包含循環。有沒有人對如何解決這個問題有任何建議。

下面的代碼:

#include <iostream> 
#include <fstream> 
#include "Validation.h" 

using namespace std; 

#define MAX_SIZE 260 
#define NOT_FOUND -1 


void main() 
{ 
    //High level alg 

    // O) Open input file and output file 
    ifstream input("Email.txt"); 
    ofstream output("Result.txt"); 

    // Oa) While not at the end of input 
    while(!input.eof()) 
    { 
     char email[MAX_SIZE]; 
     Validation A(email); 
     // Ob)Read email input 
     input.getline(email, MAX_SIZE); 

     // Validat email 
     if (A.Valid(email)) 

     // Write results to output file 
     output<< "1" << endl; 
     else 
     output << "0" << endl; 
    } 

    system("pause"); 
} 

第二個文件:

#include <iostream> 
#include <string> 
#include "Validation.h" 

using namespace std; 

#define NOT_FOUND -1 

bool IsValid(char* email) 
{ 
    int Length, atIndex, LocalLength, DomainLength, aCheck = 0; 
    char* Invalid = "()[]\\;:,<>"; 

    // Searches for the NULL terminator at the end of the char array and returns Length of the arry 
      Validation a(email); 
    Length = a.GetLength; 

    if(Length <= 256) 
    { 
     // Finds the @ char and returns an int variable labeling its position 
     Validation b(email, Length); 

     atIndex = b.Find; 

     aCheck++; 

     if(atIndex != NOT_FOUND) 
     { 
      Validation c(email, atIndex); 

      LocalLength = c.CheckLengthLocal; 
      DomainLength = c.CheckLengthDomain; 

      aCheck++; 

      if(LocalLength <= 64 && DomainLength <= 253) 
      { 
       aCheck++; 
       Validation d(email, Invalid, atIndex); 

       if(d.ValidCharsL == true && d.ValidCharsD == true) 
       { 
        aCheck++; 
        Validation e(email, atIndex, Length); 

        if(c.CheckRuleLocal== true && e.CheckRuleDomain == true) 
        { 
         aCheck++; 

         return true; 
        } 
       } 
      } 
     } 
    } 
    if(aCheck != 5) 
     return false; 
} 

第三檔:

#define MAX_SIZE 260 
#define NOT_FOUND -1 

#include <iostream> 

#pragma once 

using namespace std; 

struct Validation 
{ 
    //Returns the length of text by scanning for null-term 
int GetLength(char* text) 
{ 
    int i; 
    for(i = 0; text[i] != '\0'; i++) 
    { 

    } 
    return i; 
} 

Validation::Validation(char* email) 
{ 
    char* emailC = email; 

} 

Validation::Validation(char* email, int Length) 
{ 
    char* emailC = email; 
    int Size = Length; 
} 

Validation::Validation(char* email, int atIndex, int Length) 
{ 
    char* emailC = email; 
    int Size = Length; 
    int IndA = atIndex; 
} 

Validation::Validation(char* email, char* Invalid, int atIndex) 
{ 
    char* emailC = email; 
    char* InNum = Invalid; 
    int IndA = atIndex; 
} 

bool Valid(char * email) 
{ 
    char* emailT = email; 
} 

bool CheckRuleLocal(char* text, int Achar) 
{ 
    int invalid = 0; 
    if(text[0] == '.'|| text[Achar - 1] == '.') 
     invalid++; 
    if(text[0] == '-'|| text[Achar - 1] == '-') 
     invalid++; 
    for(int i = 0; i < Achar && invalid == 0; i++) 
    { 
     if(text[i] == '.' && text[i + 1] == '.') 
      invalid++; 
     if(text[i] == '-' && text[i + 1] == '-') 
      invalid++; 
    } 

    if(invalid > 0) 
     return false; 
    else 
     return true; 
} 

bool CheckRuleDomain(char* text, int Achar, int length) 
{ 
    int invalid = 0; 
    if(text[Achar + 1] == '.'|| text[length - 1] == '.') 
     invalid++; 
    if(text[Achar + 1] == '-'|| text[length - 1] == '-') 
     invalid++; 
    for(int i = Achar + 1; i < MAX_SIZE && invalid == 0; i++) 
    { 
     if(text[i] == '.' && text[i + 1] == '.') 
      invalid++; 
     if(text[i] == '-' && text[i + 1] == '-') 
      invalid++; 
    } 

    if(invalid > 0) 
     return false; 
    else 
     return true; 
} 


bool ValidCharsL(char* text, char* invalidCharacters, int Achar) 
{ 
    int invalid = 0; 

    for(int i = 0; i < Achar && invalid == 0; i++) 
    { 
     for(int t = 0; t < GetLength(invalidCharacters); t++) 
     { 
      if(text[i] == invalidCharacters[t]) 
       invalid++; 
     } 
    } 
    if(invalid > 0) 
     return false; 
    else 
     return true; 
} 

bool ValidCharsD(char* text, char* invalidCharacters, int Achar) 
{ 
    int invalid = 0; 

    for(int i = Achar + 1; text[i] != '\0'; i++) 
    { 
     for(int t = 0; t < GetLength(invalidCharacters); t++) 
     { 
      if(text[i] == invalidCharacters[t]) 
       invalid++; 
     } 
    } 
    if(invalid > 0) 
     return false; 
    else 
     return true; 
} 

//Finds the position of @ and returns an int that will store value if not found returns -1 
int Find(char* text, int arraySize) 
{ 
    int AIndex = 0; 
    for(int i = 0; i <= arraySize; i++) 
    { 
     if(text[i] == '@') 
      AIndex = i; 
    } 
     if(AIndex > 0) 
      return AIndex; 
     else 
      return NOT_FOUND; 

} 


int CheckLengthLocal(char* text, int Achar) 
{ 
    int count = 0; 
    for(int i = 0; i != Achar; i++) 
    { 
     count ++; 
    } 
    return count; 
} 

int CheckLengthDomain(char* text, int Achar) 
{ 
    int count = 0; 
    for(int i = Achar + 1; text[i] != '\0'; i++) 
    { 
     count ++; 
    } 
    return count; 
} 
}; 
+1

Godddddddddddd .. so long post ... – Nawaz 2011-04-09 17:16:42

+0

...等格式不好。順便說一下,歡迎來到StackOverflow。您可能需要仔細閱讀常見問題解答,以瞭解如何發佈好問題,以便人們可以直接瞭解問題並提供及時答案(儘管它沒有具體說明您在此處展示的「代碼牆」問題):http: //stackoverflow.com/faq – Jollymorphic 2011-04-09 17:24:39

+0

我試圖格式化你的代碼,所以它更具可讀性,但我懷疑你會得到很多反應。我認爲你需要創建一個* small *的例子來展示你想要完成的事情。 – 2011-04-09 17:26:34

回答

0

一般不贊成在與全局變量,以配合不同的部門和職能部門一起。但是,如果你有一種方法是在頭文件中使用extern聲明,並且在你的cpp文件中聲明瞭實際的變量。

0

爲您的cpp文件創建頭文件,並將要公開的函數原型放入其中的其他單元。您可以將實際的實現保留在cpp文件中。將頭部包含在需要頭文件編譯的cpp單元中。

我沒有瀏覽所有的代碼,但它看起來並不像你有單獨的變量,你認爲。你從main傳遞的變量是真正的變量。原型中使用的名稱只是一個被替換的佔位符。這些單位需要被編譯,鏈接器會將其整理出來。

也許從一個簡單的東西開始,一個或兩個函數在一個單獨的單位,直到你得到它的竅門?