2011-04-09 5 views
0

재미있는 내용으로 작성한 프로그램에서 다른 CPP 파일을 사용해야하고, 내 주 파일에서 IsValid cpp 파일의 알고리즘을 호출해야합니다. 내 IsValid 알고리즘은 또한 내 메인 파일, 전자 메일에서 변수를 가져와야하고, 내가 작성한 루프를 통해이를 실행할 것이다.함수/변수를 별도의 CPP 파일에서 호출하기

나는 이것을 수행하는 방법에 대해 궁금해했다. 왜냐하면 cpp 파일을 포함시키는 것이 나쁜 코딩 방법이라고 들었고, 내가 포함했다면 어쨌든 include 루프를 일으킬 것입니다. 이 문제를 해결하는 방법에 대한 제안이있는 사람이 있습니까? 다음은

코드 :

#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 .. 너무 오래 ... – Nawaz

+0

... 너무 심하게 형식이 지정되었습니다. StackOverflow에 오신 것을 환영합니다. 좋은 질문을 올리는 방법을 알기 위해 자주 묻는 질문 (FAQ)을 살펴보고 사람들이 직접 이해하고 적절한 답을 제공 할 수 있도록하십시오 (여기에 제시된 "코드 벽"문제는 특별히 다루지는 않지만). http : //stackoverflow.com/faq – Jollymorphic

+0

코드를보다 쉽게 ​​읽을 수 있도록 코드 형식을 시도했지만, 어쨌든 많은 응답을 받을지는 의심 스럽습니다. 나는 당신이 달성하고자하는 것을 보여주기 위해 * 작은 예를 만들어야한다고 생각합니다. –

답변

0

. 그러나 한 가지 방법을 사용하는 경우에는 헤더 파일에 extern 선언을 사용하고 실제 변수는 cpp 파일 중 하나에 선언해야합니다.

0

cpp 파일의 헤더 파일을 만들고 노출하려는 기능 프로토 타입을 다른 장치에 넣으십시오. cpp 파일에 실제 구현을 남길 수 있습니다. 컴파일 할 헤더가 필요한 cpp 단위로 헤더를 포함하십시오.

나는 모든 코드를 살펴 보지 않았지만 생각한 변수가 따로있는 것처럼 보이지 않습니다. main에서 전달하는 변수는 실제 변수입니다. 프로토 타입에 사용 된 이름은 대체되는 자리 표시 자일뿐입니다. 단위를 컴파일해야하고 링커가이를 정렬해야합니다.

어쩌면 너는 그것의 걸림 새를 얻을 때까지, 더 간단한 무언가, 분리되는 단위에있는 하나 또는 2 개의 기능으로 시작 하는가?

관련 문제