재미있는 내용으로 작성한 프로그램에서 다른 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;
}
};
Godddddddddddd .. 너무 오래 ... – Nawaz
... 너무 심하게 형식이 지정되었습니다. StackOverflow에 오신 것을 환영합니다. 좋은 질문을 올리는 방법을 알기 위해 자주 묻는 질문 (FAQ)을 살펴보고 사람들이 직접 이해하고 적절한 답을 제공 할 수 있도록하십시오 (여기에 제시된 "코드 벽"문제는 특별히 다루지는 않지만). http : //stackoverflow.com/faq – Jollymorphic
코드를보다 쉽게 읽을 수 있도록 코드 형식을 시도했지만, 어쨌든 많은 응답을 받을지는 의심 스럽습니다. 나는 당신이 달성하고자하는 것을 보여주기 위해 * 작은 예를 만들어야한다고 생각합니다. –