From c513d24ba7fe4ac0f6346b852ff292ae4dc55f8e Mon Sep 17 00:00:00 2001 From: Igor Dzuro Date: Thu, 2 Apr 2020 23:34:39 +0200 Subject: [PATCH] pumpit --- du4/program | Bin 17064 -> 17064 bytes du4/program.c | 183 +++++++++++++++----------------------------------- 2 files changed, 56 insertions(+), 127 deletions(-) diff --git a/du4/program b/du4/program index 9bec58c074aee98d49e7d01776b03061399f1944..a63b52e49e7260e1828c535543e51a72e02e6245 100755 GIT binary patch delta 639 zcmX|?YN#3tdrwX1OBZQHqZ|AVoBSbInwjWm-((2eZQgWQlJ9xvW)}xc*N4~S)fl%M~l}di!9bC z&z#6LFs6R}$9c(1vuE0D4JRy4uYA+?uB~>gEYoY5d3A-XT6VQ5F{$q0T}wMm;Va7+ zv|`x0bP)D4Ru(TVmPK>4QWi~TzL4xGVT6V)qydZ5m5?Q9>r za5~Dm<$+TXt$JCCABSdLXq3iuCZ^xwTx({S_=z}9oF)$bRp#7Itr>oC-lerj z>?h`kJ>v3j$|0)6RigZdbC}L} z#ba(Kyv6}iFJ2)%iT6o6Fhgoo*4#l8*cEffc>(M=;xEDmMgl$X2-5){tYRkc1HR$o j&LH^kb7vS@&>if7S!E>nv7R>chJE{Lzn3x1kDCy53lH$o?Cl{kCs$)fc~PzFUojQLmpSP(Sk@&}0x zMmJVbqxvIQe+1DBasS9hY*DNTHn1ot!NRdW+?dsf*zG(8AH4Iq=bU@K=iJMkNx@7C zUMM+(t%KCRCh0Eji&=dj`e872Aw--= zef+~%fhcjEc~e=~W=zZoTVi5#i^MFtTs^HT%wevbCnPx+vx*Z(`n7~qqPRCN_g;Mu zO(j|60wyv-IV+SC6%g;~a>*9CNkgT`5t3N90xFs7)s$K{|r?G;TUn!j!84&K6vZVJWtXpzLUIPen< z4m|&lv9Bv2=_+_=$h-t9s<&8R zle#R6J6&q55Bz0p^4#lTkItTyZ-ER?zO*XTV}nB{!a|4)4U)6R1rib?{Xufsm^cJ` z_`VU`hP{bmov~NQOUSRt7*ZCAY%o?sCN~)~;}N(TV_sw&c?y|F#*pujcaRIePzPB? zt{{*7W<0ZTG~F@P-ifzEf?9o=3$^XwU7V&>No*Lpj{gYlb=YB%M$kIxHMEE6eY74r zi&iClcKG#RlMDw>b6}%UZxKo~=o^5CG~?@n6*}vy!cY3F#}A#f)DwWc)Zrh1$5Pb) StrZKxfi6|^Jxb#NH~$}OT>pds diff --git a/du4/program.c b/du4/program.c index e349f85..861b6ee 100644 --- a/du4/program.c +++ b/du4/program.c @@ -9,15 +9,11 @@ #define SCAN_SECOND_NUMBER 2 #define SCAN_SECOND_OPERATOR 3 #define SCAN_RESULT 4 -// #define SCAN_END 5 char* scanValue(char *s, float *output); char* scanOperator(char *s, char *output); -// bool isDigit(char c); bool isOperator(char c); -// bool isFloatingPoint(char c); -// bool isWhitespace(char c); bool isNewline(char c); bool evaluate(float a, float b, float c, char op); @@ -25,84 +21,74 @@ void error(); int main(){ char buffer[2048]; - // char str[512]; + char *retVal; - float a; - float b; - float c; - char operator; - - char *retVal = fgets (buffer, sizeof(buffer), stdin); - - if (retVal == 0 || isNewline(buffer[0]) == true) { - puts("KONIEC"); + while ((retVal = fgets(buffer, sizeof(buffer), stdin)) != 0) { + float a; + float b; + float c; + char operator; - return 0; - } + if (retVal == 0 || isNewline(buffer[0]) == true) { + // puts("KONIEC"); - int l = strlen(buffer); - - if (isNewline(buffer[l - 1]) == false) { - error(); - - return 0; - } - - int scanStatus = SCAN_FIRST_NUMBER; - char *currentChar = buffer; - char *endChar = buffer + l - 1; - - // for (char *i = 0; i < l; i++) { - // // int forward = 0; - - // char currentChar = buffer[i]; - - // if (i == l - 1 && isNewline(currentChar) == false) { - - // } - // } - - while (currentChar < endChar) { - if (scanStatus == SCAN_FIRST_NUMBER) { - retVal = scanValue(currentChar, &a); - } else if (scanStatus == SCAN_FIRST_OPERATOR) { - retVal = scanOperator(currentChar, &operator); - } else if (scanStatus == SCAN_SECOND_NUMBER) { - retVal = scanValue(currentChar, &b); - } else if (scanStatus == SCAN_SECOND_OPERATOR) { - char eqOp; - - retVal = scanOperator(currentChar, &eqOp); - - if (eqOp != '=') { - retVal = 0; - } - } else if (scanStatus == SCAN_RESULT) { - retVal = scanValue(currentChar, &c); - } else { - error(); - - break; + return 0; } - if (retVal == 0) { + int l = strlen(buffer); + + if (isNewline(buffer[l - 1]) == false) { error(); - break; - } else if (scanStatus == SCAN_RESULT) { - bool validResult = evaluate(a, b, c, operator); + return 0; + } - if (validResult == true) { - puts("OK"); + int scanStatus = SCAN_FIRST_NUMBER; + char *currentChar = buffer; + char *endChar = buffer + l - 1; + + while (currentChar < endChar) { + if (scanStatus == SCAN_FIRST_NUMBER) { + retVal = scanValue(currentChar, &a); + } else if (scanStatus == SCAN_FIRST_OPERATOR) { + retVal = scanOperator(currentChar, &operator); + } else if (scanStatus == SCAN_SECOND_NUMBER) { + retVal = scanValue(currentChar, &b); + } else if (scanStatus == SCAN_SECOND_OPERATOR) { + char eqOp; + + retVal = scanOperator(currentChar, &eqOp); + + if (eqOp != '=') { + retVal = 0; + } + } else if (scanStatus == SCAN_RESULT) { + retVal = scanValue(currentChar, &c); } else { - puts("ZLE"); + error(); + + break; } - break; - } + if (retVal == 0) { + error(); - currentChar = retVal; - scanStatus++; + break; + } else if (scanStatus == SCAN_RESULT) { + bool validResult = evaluate(a, b, c, operator); + + if (validResult == true) { + puts("OK"); + } else { + puts("ZLE"); + } + + break; + } + + currentChar = retVal; + scanStatus++; + } } return 0; @@ -118,51 +104,6 @@ char* scanValue(char *s, float *output) { } return retVal; - - // int outPosition = 0; - // int l = strlen(s); - // bool hasPoint = false; - - // for (int i = 0; i < l - 1; i++) { - // char c = s[i]; - // char cNext = s[i+1]; - // bool isError = (outPosition == 0 && isDigit(c) == false) - // || (outPosition > 0 && ( - // (hasPoint == true && isFloatingPoint(c)) - // || (hasPoint == false && isFloatingPoint(c) == true && isDigit(cNext) == false) - // || (isDigit(c) == true && ( - // (hasPoint == true && isOperator(cNext) == false && isWhitespace(cNext) == false && isNewline(cNext) == false) - // || (hasPoint == false && isOperator(cNext) == false && isWhitespace(cNext) == false && isNewline(cNext) == false && isFloatingPoint(cNext) == false)) - // ) - // || (hasPoint == true && isDigit(c) == false) - // || (hasPoint == false && isFloatingPoint(c) == false && isDigit(c) == false) - // )); - - // if (isError == true) { - // return 0; - // } - - // if (isFloatingPoint(c)) { - // hasPoint = true; - // outPosition++; - - // continue; - // } - - // bool endsHere = isOperator(cNext) == true || isWhitespace(cNext) == true || isNewline(cNext) == true; - - // if (endsHere == true) { - // break; - // } - - // outPosition++; - // } - - // if (outPosition > 0) { - // *output = strtof(s, s + outPosition - 1); - // } - - // return outPosition; } char* scanOperator(char *s, char *output) { @@ -183,22 +124,10 @@ char* scanOperator(char *s, char *output) { return start + 1; } -// bool isDigit(char c) { -// return c >= '0' && c <= '9'; -// } - bool isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '='; } -// bool isFloatingPoint(char c) { -// return c == '.' || c == ','; -// } - -// bool isWhitespace(char c) { -// return c == ' ' || c == '\t'; -// } - bool isNewline(char c) { return c == '\n'; }