2011-04-09 25 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传递的变量是真正的变量。原型中使用的名称只是一个被替换的占位符。这些单位需要被编译,链接器会将其整理出来。

也许从一个简单的东西开始,一个或两个函数在一个单独的单位,直到你得到它的窍门?