Материал: Larin_Anton_AiSD_21_3

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам

///Variables

string iFileName;

string oFileName;

///Parce argsuments

if(parseArgs(argc,argv,iFileName,oFileName))exit(0);

ofstream oFile;

ifstream iFile;

///File management

if (printToFile){

oFile.open(oFileName,ios::out);

if(!oFile){

printStr("Cannot open file: "+oFileName+"\n");

exit(1);

}

setOFile(&oFile);

}

if (readFromFile) {

iFile.open(iFileName);

if (!iFile) {

printStr("Cannot open file: " + iFileName + "\n");

exit(1);

}

setIFile(&iFile);

}

while(repeat || awto) {///Main inpit-process-output-out cycle

///Input

string str;

printStr("\n");

if(!awto)printStr("Enter string:\n");

if(input(str)){

if(awto)return 0;

else {printStr("Input failt!");return 0;}

}

printStr("Input: \n" + str + "\n");

///Processing with inter-results output

printStr("Inter results:\n");

int i = 0;

char c = processStr(str, i);

printStr("Result:\n");

if (c == 0) {

printStr("Correct!\n");

} else if (c == 1) {

printStr(str.substr(0, i + 1) + " <<ERROR HERE!" + "\n");

string o = "";

o += str[i];

printStr("Unexpected '" + o + "'\n");

} else {

if (i == str.length()){

string o = "";

o += c;

printStr("Bracket '"+o+"' left unclosed\n");

}

else {

printStr(str.substr(0, i + 1) + " <<ERROR HERE!" + "\n");

string o = "";

o += bracketPair(c);

string q = "";

q += str[i];

printStr("Expected '" + o + "' while got '" + q + "'");

}

}

if(repeat){

printStr("\nRepeat? [1-Yes|0-No]\n");

cin.clear();

int t;

cin >> t;

repeat = t;

if (repeat)printStr("\n");

else printStr("Good bye!\n");

}

}

return (0);

}

int parseArgs(int argc, char** argv, string &iFileName, string &oFileName){

//Run through all arguments looking for matches. Wrong keys are ignored. Wrong values leads to an undefined behaviour.

int i=1;

while(i<argc){

if(!((string)argv[i]).compare("-h")){

help();

return 1;

} else if(!((string)argv[i]).compare("-i")){

readFromFile=true;

if(i!=argc-1 && argv[i+1][0]!='-'){

iFileName=(string)argv[i+1];

i++;

} else iFileName = DEFAULT_IFILE_NAME;

i++;

} else if(!((string)argv[i]).compare("-o")){

printToFile=true;

if(i!=argc-1 && argv[i+1][0]!='-'){

oFileName=(string)argv[i+1];

i++;

} else oFileName = DEFAULT_OFILE_NAME;

i++;

} else if(!((string)argv[i]).compare("-r")){

repeat=true;

i++;

}

else if(!((string)argv[i]).compare("-A")){

awto=true;

if(i!=argc-1 && argv[i+1][0]!='-'){

awtoLim=atoi(argv[i+1]);

i++;

} else awtoLim = 1;

i++;

} else i++;

}

/*

if(awto && (readFromFile||repeat)){

printStr("Warning! -A is incompatable with -r and -i. These are ignored!\n");

readFromFile=repeat=false;

}*/

/*

if((readFromFile||printToFile)&&repeat){

repeat=false;

printStr("Warning! -i and -o are incompatable with -r. -r is ignored!\n");

}

*/

return 0;

}

int isOpenBracket(char b){

return b=='('||b=='['||b=='{';

}

int isCloseBracket(char b){

return b==')'||b==']'||b=='}';

}

char bracketPair(char b){

switch (b){

case '(':{

return ')';

}

case ')':{

return '(';

}

case '[':{

return ']';

}

case ']':{

return '[';

}

case '{':{

return '}';

}

case '}':{

return '{';

}

}

}

char processStr(string str,int &i, int reclvl){

int len=str.length();///Needed meny times

Stack<char> s;///Stack to work with

while(1){///Main cycle within one recursion level

//int i = st;

while(i<len && !isOpenBracket(str[i]) && !isCloseBracket(str[i]))i++;///Skip all none-brackets

if(i==len){///If the string is done...

if(s.isEmpty())return 0;///...and stack is empty - oll is korrect!

else return s.ttop();///...and stack is not empty - it has on top unclosed bracket

}

if(isOpenBracket(str[i])){///As we met open bracket...

for (int j = 0; j < reclvl; ++j)printStr("\t");

string p="";p+=str[i];///ECHO OPEN BRACKET

printStr(p);

printStr("\n");

s.push(str[i]);

i++;

char c = processStr(str,i,reclvl+1);///

if(c!=1&&c!=0)return bracketPair(c);///...we dive into next lvl recursion

} else if(isCloseBracket(str[i])){///As we met close bracket...

if(!s.isEmpty()&&s.top()==bracketPair(str[i])){///... korrekt bracket...

for (int j = 0; j < reclvl; ++j)printStr("\t");

string p="";p+=str[i];///ECHO CLOSE BRACKET

printStr(p);

printStr("\n");

i++;

s.pop();///...open and close brackets are inter-annihilated

//if(s.isEmpty())return 0;

} else{///... wrong bracket or none oppened...

for (int j = 0; j < reclvl; ++j)printStr("\t");

//string p="";p+=str[i];

printStr("<-");///ECHO CLOSE BRACKET

printStr("\n");

if(s.isEmpty()){return 1;}///...none oppened so far - retur deff err code

else if(s.top()!=bracketPair(str[i])){///Met wrong close bracket

return s.ttop();///Return bracket we expected to be closed; str[i] is what we actually got. INNER RETURN

}

}

}

}

}

void printStr(string str){

cout<<str;

if(printToFile&&outFile&&(*outFile))(*outFile)<<str;

}

int input(string &inp){

#define INS ( readFromFile?(*inFile):cin )

if(INS.eof())return 1;

char *c=new char[BUF_SIZE];

if(!c)return 1;

INS.getline(c,BUF_SIZE-1,'\n');

//INS.get();

if(INS.fail()&&!INS.eof())

return 1;

inp=c;

return 0;

}

21

Источник: https://studfile.net/preview/15918772/