📄 lecture.cpp
字号:
#include "Structure.h"
#include "Commun.h"
#include "Lecture.h"
CProgLog* ConstructProgLog(char* FileName)
{
CProgLog* pProgLogReturn = new CProgLog;
char sClause[MAX_LENGTH];
char *s_pClause, *tmp, *tmp2;
CRegle* pRegleTmp;
int parentheseOpen, nDictionnaryKey;
int placeTmpNot, placeTmpComma, tickPlace;
FILE* stream = fopen(FileName, "r");
if (!stream)
{
printf("File Not Found ! \n");
return NULL;
}
while (NULL != fgets(sClause, MAX_LENGTH, stream))
{
s_pClause = &sClause[0]; // We get a pointer to the first element
printf("\n\nReading New Line\n\n");
pRegleTmp = new CRegle;
// Get the head of the line - in format "(head :- atoms., degree)"
parentheseOpen = FindCaractere(s_pClause, "(");
s_pClause = &s_pClause[parentheseOpen+1];
tmp = CutAndRetrieved(":", s_pClause);
EraseCaractere(tmp, ' ');
nDictionnaryKey = pProgLogReturn->AddDictionnaryEntry(tmp);
pRegleTmp->SetHead(nDictionnaryKey);
printf("head : %s\n",tmp);
// Get the atom set in the line - in format "(head :- atoms., degree)"
tickPlace = FindCaractere(s_pClause, "-" );
s_pClause = &s_pClause[tickPlace+1];
tmp = CutAndRetrieved(".", s_pClause); // the next can be a degree or nothing ( ')' )
while (strlen(tmp) != 0)
{
char* newAtom = GetNextAtom(tmp); // Get the next atom of the rule
placeTmpNot = FindCaractere(newAtom, "not "); // Search about the atom's specificity
if (placeTmpNot != -1)
{
// Negative form found
tmp2 = &newAtom[placeTmpNot+strlen("not ")]; // We only want the atom's name
EraseCaractere(tmp2, ' '); // Erase all blank spaces (Formating)
// Add a reference of the atom into the dictionnary
nDictionnaryKey = pProgLogReturn->AddDictionnaryEntry(tmp2);
// If it's already existing, we have its reference in nDictionnaryKey
// Put the reference in the current rule's body minus
pRegleTmp->AddBodyMinusEntry(nDictionnaryKey);
printf("Current Element Put in m_vBodyMinus: %s\n",tmp2);
}
else
{
// Classic atom
EraseCaractere(newAtom, ' '); // Erase all blank spaces (Formating)
// Add a reference of the atom into the dictionnary
nDictionnaryKey = pProgLogReturn->AddDictionnaryEntry(newAtom);
// Put the reference in the current rule's body plus
pRegleTmp->AddBodyPlusEntry(nDictionnaryKey);
printf("Current Element Put in m_vBodyPlus: %s\n",newAtom);
}
}
pProgLogReturn->AddRegle(pRegleTmp);
// Get the degree for definite program
printf("%s", s_pClause);
}
printf("\n");
system("PAUSE");
fclose(stream);
return pProgLogReturn;
}
CProgLogPos* ConstructProgLogPos(char* FileName)
{
CProgLogPos* pProgLogReturn = new CProgLogPos;
char sClause[MAX_LENGTH];
char *s_pClause, *tmp, *tmp2;
CReglePos* pRegleTmp;
int parentheseOpen, nDictionnaryKey;
int placeTmpNot, placeTmpComma, tickPlace;
FILE* stream = fopen(FileName, "r");
if (!stream)
{
printf("File Not Found ! \n");
return NULL;
}
while (NULL != fgets(sClause, MAX_LENGTH, stream))
{
s_pClause = &sClause[0]; // We get a pointer to the first element
pRegleTmp = new CReglePos;
// Get the head of the line - in format "(head :- atoms., degree)"
parentheseOpen = FindCaractere(s_pClause, "(");
s_pClause = &s_pClause[parentheseOpen+1];
tmp = CutAndRetrieved(":", s_pClause);
if (strcmp(tmp, s_pClause) == 0) // There's only a head in the rule
{
tmp = CutAndRetrieved(".", s_pClause);
}
EraseCaractere(tmp, ' ');
nDictionnaryKey = pProgLogReturn->AddDictionnaryEntry(tmp);
pRegleTmp->SetHead(nDictionnaryKey);
// Get the atom set in the line - in format "(head :- atoms., degree)"
tickPlace = FindCaractere(s_pClause, "-" );
if (tickPlace != -1)
{
s_pClause = &s_pClause[tickPlace+1];
tmp = CutAndRetrieved(".", s_pClause); // Get the body of the rule
}
else
{
tmp = ""; // We don't want to enter in the next while instruction (it is not necessary because no body is available in the rule)
}
// Body's treatment
while (strlen(tmp) != 0)
{
char* newAtom = GetNextAtom(tmp); // Get the next atom of the rule
placeTmpNot = FindCaractere(newAtom, "not "); // Search about the atom's specificity
if (placeTmpNot != -1)
{
// Negative form found
tmp2 = &newAtom[placeTmpNot+strlen("not ")]; // We only want the atom's name
EraseCaractere(tmp2, ' '); // Erase all blank spaces (Formating)
// Add a reference of the atom into the dictionnary
nDictionnaryKey = pProgLogReturn->AddDictionnaryEntry(tmp2);
// If it's already existing, we have its reference in nDictionnaryKey
// Put the reference in the current rule's body minus
pRegleTmp->AddBodyMinusEntry(nDictionnaryKey);
}
else
{
// Classic atom
EraseCaractere(newAtom, ' '); // Erase all blank spaces (Formating)
// Add a reference of the atom into the dictionnary
nDictionnaryKey = pProgLogReturn->AddDictionnaryEntry(newAtom);
// Put the reference in the current rule's body plus
pRegleTmp->AddBodyPlusEntry(nDictionnaryKey);
}
}
// Get the degree for definite program
placeTmpComma = FindCaractere(s_pClause, ",");
if (placeTmpComma != -1)
{
s_pClause = &s_pClause[placeTmpComma+1];
strtok(s_pClause, ")");
EraseCaractere(s_pClause, ' ');
pRegleTmp->SetDegree(atoi(s_pClause));
}
else
{
pRegleTmp->SetDegree(0);
}
pProgLogReturn->AddRegle(pRegleTmp); // Add the new rule in the normal definite programme
}
fclose(stream);
return pProgLogReturn;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -