[ create a new paste ] login | about

Link: http://codepad.org/WSTx2aQF    [ raw code | output | fork ]

C++, pasted on Jan 18:
#include <string>
#include <iostream>

using namespace std;

bool charIsInLiteralString(const string& sourceStr, const char& searchCh, size_t pos);

int main() {

  string source_str = "<SCRIPT LANGUAGE=\"JavaScript1.2\">\n\n"
                      "<!-- Begin\n"
                      "  var dToday=new Date();\n"
                      "  dToday.setHours(0)\n"
                      "  dToday.setMinutes(0)\n"
                      "  dToday.setSeconds(0)\n"
                      "  dToday.setMilliseconds(0)\n"
                      "  document.writeln(\"<font size=3> <CENTER>\");\n"
                      "  document.write(FormatDateH(GregToHeb(dToday)))\n"
                      "  document.writeln(\"</CENTER></font>\");\n\n"
                      "  function MonSinceFirstMolad(nYearH) {\n"
                      "    var nMonSinceFirstMolad\n"
                      "    nYearH --\n\n"
                      "    nMonSinceFirstMolad = Math.floor(nYearH / 19) * 235\n"
                      "    nYearH = nYearH % 19\n"
                      "    nMonSinceFirstMolad += 12 * nYearH\n"
                      "    if (nYearH >= 17) {\n"
                      "      nMonSinceFirstMolad += 6\n"
                      "    } else if  (nYearH >= 14) {\n"
                      "      nMonSinceFirstMolad += 5\n"
                      "    } else if  (nYearH >= 11) {\n"
                      "      nMonSinceFirstMolad += 4\n"
                      "    } else if  (nYearH >= 8) {\n"
                      "      nMonSinceFirstMolad += 3\n"
                      "    } else if  (nYearH >= 6) {\n"
                      "      nMonSinceFirstMolad += 2\n"
                      "    } else if  (nYearH >= 3) {\n"
                      "      nMonSinceFirstMolad += 1\n"
                      "    }\n"
                      "    return nMonSinceFirstMolad\n"
                      "  }\n\n"
                      "  function IsLeapYear(nYearH) {\n"
                      "     var nYearInCycle\n\n"
                      "    nYearInCycle = nYearH % 19\n"
                      "    return ( nYearInCycle ==  3 ||\n"
                      "             nYearInCycle ==  6 ||\n"
                      "             nYearInCycle ==  8 ||\n"
                      "             nYearInCycle == 11 ||\n"
                      "             nYearInCycle == 14 ||\n"
                      "             nYearInCycle == 17 ||\n"
                      "             nYearInCycle == 0)\n"
                      "  }\n\n"
                      "  function Tishrei1(nYearH) {\n"
                      "    var nMonthsSinceFirstMolad\n"
                      "    var nChalakim\n"
                      "    var nHours\n"
                      "    var nDays\n"
                      "    var nDayOfWeek\n"
                      "    var dTishrei1\n\n"
                      "    nMonthsSinceFirstMolad = MonSinceFirstMolad(nYearH)\n"
                      "    nChalakim = 793 * nMonthsSinceFirstMolad\n"
                      "    nChalakim += 204\n"
                      "    nHours = Math.floor(nChalakim / 1080)\n"
                      "    nChalakim = nChalakim % 1080\n\n"
                      "    nHours += nMonthsSinceFirstMolad * 12\n"
                      "    nHours += 5\n\n"
                      "    nDays = Math.floor(nHours / 24)\n"
                      "    nHours = nHours % 24\n\n"
                      "    nDays += 29 * nMonthsSinceFirstMolad\n"
                      "    nDays += 2\n\n"
                      "    nDayOfWeek = nDays % 7\n\n"
                      "    if (!IsLeapYear(nYearH) &&\n"
                      "        nDayOfWeek == 3 &&\n"
                      "        (nHours * 1080) + nChalakim >= (9 * 1080) + 204) {\n"
                      "      nDayOfWeek = 5\n"
                      "      nDays += 2\n"
                      "    }\n"
                      "    else if ( IsLeapYear(nYearH - 1) &&\n"
                      "              nDayOfWeek == 2 &&\n"
                      "              (nHours * 1080) + nChalakim >= (15 * 1080) + 589 ) {\n"
                      "      nDayOfWeek = 3\n"
                      "      nDays += 1\n"
                      "    }\n"
                      "    else {\n"
                      "      if (nHours >= 18) {\n"
                      "        nDayOfWeek += 1\n"
                      "        nDayOfWeek = nDayOfWeek % 7\n"
                      "        nDays += 1\n"
                      "      }\n"
                      "      if (nDayOfWeek == 1 ||\n"
                      "          nDayOfWeek == 4 ||\n"
                      "          nDayOfWeek == 6) {\n"
                      "        nDayOfWeek += 1\n"
                      "        nDayOfWeek = nDayOfWeek % 7\n"
                      "        nDays += 1\n"
                      "      }\n"
                      "    }\n\n"
                      "    nDays -= 2067025\n"
                      "    dTishrei1 = new Date(1900, 0, 1)\n"
                      "    dTishrei1.setDate(dTishrei1.getDate() + nDays)\n\n"
                      "    return dTishrei1\n"
                      "  }\n\n"
                      "  function LengthOfYear(nYearH) {\n"
                      "    var dThisTishrei1\n"
                      "    var dNextTishrei1\n"
                      "    var diff\n\n"
                      "dThisTishrei1 = Tishrei1(nYearH)\n"
                      "dNextTishrei1 = Tishrei1(nYearH + 1)\n"
                      "diff = (dNextTishrei1 - dThisTishrei1) / ( 1000 * 60 * 60 * 24)\n"
                      "return Math.round(diff)\n"
                      "}\n\n"
                      "  function GregToHeb(dGreg) {\n"
                      "    var nYearH\n"
                      "    var nMonthH\n"
                      "    var nDateH\n"
                      "    var nOneMolad\n"
                      "    var nAvrgYear\n"
                      "    var nDays\n"
                      "    var dTishrei1\n"
                      "    var nLengthOfYear\n"
                      "    var bLeap\n"
                      "    var bHaser\n"
                      "    var bShalem\n"
                      "    var nMonthLen\n"
                      "    var bWhile\n"
                      "    var d1900 = new Date(1900, 0, 1)\n\n"
                      "    nOneMolad = 29 + (12 / 24) + (793 / (1080 * 24))\n"
                      "    nAvrgYear = nOneMolad * (235 / 19)\n"
                      "    nDays = Math.round((dGreg - d1900) / (24 * 60 * 60 * 1000))\n"
                      "    nDays += 2067025\n"
                      "    nYearH = Math.floor(nDays / nAvrgYear) + 1\n"
                      "    dTishrei1 = Tishrei1(nYearH)\n\n"
                      "    if (SameDate(dTishrei1, dGreg)) {\n"
                      "      nMonthH = 1\n"
                      "      nDateH = 1\n"
                      "    }\n"
                      "    else  {\n"
                      "      if (dTishrei1 < dGreg) {\n"
                      "        while (Tishrei1(nYearH + 1) <= dGreg) {\n"
                      "          nYearH += 1\n"
                      "        }\n"
                      "      }\n"
                      "      else {\n"
                      "        nYearH -= 1\n"
                      "        while (Tishrei1(nYearH) > dGreg) {\n"
                      "          nYearH -= 1\n"
                      "        }\n"
                      "      }\n\n"
                      "      nDays = (dGreg - Tishrei1(nYearH)) / (24 * 60 * 60 * 1000)\n"
                      "      nDays = Math.round(nDays)\n"
                      "      nLengthOfYear = LengthOfYear(nYearH)\n"
                      "      bHaser = nLengthOfYear == 353 || nLengthOfYear == 383\n"
                      "      bShalem = nLengthOfYear == 355 || nLengthOfYear == 385\n"
                      "      bLeap = IsLeapYear(nYearH)\n\n"
                      "      nMonthH = 1\n"
                      "      do {\n\n"
                      "        switch (nMonthH) {\n"
                      "          case 1:\n"
                      "          case 5:\n"
                      "          case 6:\n"
                      "          case 8:\n"
                      "          case 10:\n"
                      "          case 12:\n"
                      "            nMonthLen = 30\n"
                      "            break\n"
                      "          case 4:\n"
                      "          case 7:\n"
                      "          case 9:\n"
                      "          case 11:\n"
                      "          case 13:\n"
                      "            nMonthLen = 29\n"
                      "            break\n"
                      "          case 6:\n"
                      "            nMonthLen = 30\n"
                      "            break\n"
                      "          case 2:\n"
                      "            nMonthLen = (bShalem \? 30 : 29)\n"
                      "            break\n"
                      "          case 3:\n"
                      "            nMonthLen = (bHaser \? 29: 30)\n"
                      "            break\n"
                      "         }\n\n"
                      "         if (nDays >= nMonthLen) {\n"
                      "           bWhile = true\n"
                      "           if (bLeap || nMonthH != 5) {\n"
                      "             nMonthH ++\n"
                      "           }\n"
                      "             else {\n"
                      "               nMonthH += 2\n"
                      "             }\n"
                      "             nDays -= nMonthLen\n"
                      "         }\n"
                      "         else {\n"
                      "           bWhile = false\n"
                      "         }\n"
                      "       } while (bWhile)\n"
                      "       nDateH = nDays + 1\n"
                      "    }\n"
                      "    return nMonthH + \"/\" + nDateH + \"/\" + nYearH\n"
                      "  }\n\n"
                      "  function SameDate(d1, d2) {\n"
                      "    return (d1.getFullYear() == d2.getFullYear() &&\n" 
                      "            d1.getMonth() == d2.getMonth() &&\n" 
                      "            d1.getDate() == d2.getDate())\n\n"
                      "  }\n\n" 
                      "  function FormatDateH(cDate) {\n"
                      "    var aDate = new Array()\n"
                      "    var cFormatDate\n\n"
                      "    aDate = cDate.split(\"/\")\n"
                      "    switch (Number(aDate[0])) {\n"
                      "      case 1:\n"
                      "        cFormatDate = \"Tishrei\"\n"
                      "        break\n"
                      "      case 2:\n"
                      "        cFormatDate = \"Cheshvan\"\n"
                      "        break\n"
                      "      case 3:\n"
                      "        cFormatDate = \"Kislev\"\n"
                      "        break\n"
                      "      case 4:\n"
                      "        cFormatDate = \"Teves\"\n"
                      "        break\n"
                      "      case 5:\n"
                      "        cFormatDate = \"Shevat\"\n"
                      "        break\n"
                      "      case 6:\n"
                      "        cFormatDate = \"Adar A\"\n"
                      "        break\n"
                      "      case 7:\n"
                      "        cFormatDate = (IsLeapYear(Number(aDate[2])) \? \"Adar B\" : \"Adar\")\n"
                      "        break\n"
                      "      case 8:\n"
                      "        cFormatDate = \"Nisan\"\n"
                      "        break\n"
                      "      case 9:\n"
                      "        cFormatDate = \"Iyar\"\n"
                      "        break\n"
                      "      case 10:\n"
                      "        cFormatDate = \"Sivan\"\n"
                      "        break\n"
                      "      case 11:\n"
                      "        cFormatDate = \"Tamuz\"\n"
                      "        break\n"
                      "      case 12:\n"
                      "        cFormatDate = \"Av\"\n"
                      "        break\n"
                      "      case 13:\n"
                      "        cFormatDate = \"Elul\"\n"
                      "        break\n"
                      "    }\n"
                      "    cFormatDate += \" \" + aDate[1] + \", \" + aDate[2]\n"
                      "    return cFormatDate\n"
                      "  }\n\n"
                      "// End -->\n"
                      "</SCRIPT>";
  for (size_t i = 0; i < source_str.size(); i++) {
       if ((source_str.at(i) == '>') && (!charIsInLiteralString(source_str, source_str.at(i), i)))
           cout<< "It is true that source_str.at(" << i << ") is a '>' character.\n"
                  "It is false that that character is in a literal string.\n" <<endl;
       else if ((source_str.at(i) == '>') && (charIsInLiteralString(source_str, source_str.at(i), i)))
                cout<< "It is true that source_str.at(" << i << ") is a '>' character)\n"
                       "It is true that that character is in a literal string.\n" <<endl;
  }

  return 0;

}

bool charIsInLiteralString(const string& sourceStr, const char& searchCh, size_t pos) {

    bool found_doublequotes_start_char = false;
    bool found_doublequotes_end_char = false;
    bool found_searchCh = false;
    if (pos == 0) {
        cout<< "charIsInLiteralString is about to return false..." <<endl;
        //cin.get();
        return false; //since its not possible for the search char to be the first char of a string and still be inside a literal string
    }

    for (size_t i = 0; i < sourceStr.size(); i++) {
        if (sourceStr.at(i) == '\"') {
            if (found_doublequotes_start_char == false)
               found_doublequotes_start_char = true;

            else {
               if (found_doublequotes_end_char == false) {
                   found_doublequotes_end_char = true; //set it to true
                   if ((found_searchCh == true) && (found_doublequotes_start_char == true)) {
                      cout<< "charIsInLiteralString is about to return true..." <<endl;
                      //cin.get();
                      return true;
                   }
           
                   else if ((found_searchCh == false) && (i > pos) && (found_doublequotes_end_char == true)) {
                      cout<< "charIsInLiteralString is about to return false..." <<endl;
                      //cin.get();
                      return false;
                   }
               }
               else if (found_doublequotes_end_char == true) { 
                   if ((found_searchCh == true) && (found_doublequotes_start_char == true)) {
                      cout<< "charIsInLiteralString is about to return true..." <<endl;
                      //cin.get();
                      return true;
                   }
                   
                   else {
                     found_doublequotes_start_char = false; //reset it to false
                     found_doublequotes_end_char = false; //reset it to false
                   }
               }
            }
        }

        else if (sourceStr.at(i) == searchCh) {
            if (i > pos) {
                cout<< "charIsInLiteralString is about to return false..." <<endl;
                //cin.get();
                return false; //since the char at the pos passed for the search char wasn't the same char as the search ch
            }
            else if (i == pos) {
                if (found_searchCh == false)
                    found_searchCh = true; //set it to true

                if (found_doublequotes_start_char == true && found_doublequotes_end_char == true) {
                    cout<< "charIsInLiteralString is about to return false..." <<endl;
                    //cin.get();
                    return false; //since searchCh should be before the double-quotes end char
                }
            }

        }
    }
    if (found_searchCh == false || found_doublequotes_start_char == false || found_doublequotes_end_char == false) {
        cout<< "charIsInLiteralString is about to return false..." <<endl;
        //cin.get();
        return false; //since one of the 3 searched for chars wasn't found
    }
    else
        return true;

    return false;

}


Output:
charIsInLiteralString is about to return false...
It is true that source_str.at(32) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(198) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return true...
charIsInLiteralString is about to return true...
It is true that source_str.at(207) is a '>' character)
It is true that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(289) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(296) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(520) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(583) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(646) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(709) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(771) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(833) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(1891) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(2073) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(2170) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(3651) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return true...
charIsInLiteralString is about to return true...
It is true that source_str.at(4599) is a '>' character)
It is true that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(6203) is a '>' character.
It is false that that character is in a literal string.

charIsInLiteralString is about to return false...
It is true that source_str.at(6213) is a '>' character.
It is false that that character is in a literal string.



Create a new paste based on this one


Comments: