Advertisement
Nkzlxs

advancedCompare Function

Apr 7th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.92 KB | None | 0 0
  1. int advancedCompare(string q, int attributeNo, int pos)
  2. {
  3.     //Check if the element in that pos is a valid data
  4.     if (STR1[pos].length() == 0)
  5.     {
  6.         return -1;
  7.     }
  8.  
  9.     string a = "";
  10.     string b = "";
  11.     int w = 0;
  12.  
  13.     char o_DOB[3] = {'=', '=', '='};
  14.     int i_DOB[3] = {0, 0, 0};
  15.     int e_DOB[3] = {0, 0, 0};
  16.     int temp_store_2 = 0;
  17.  
  18.     if (attributeNo >= 5 && !(isdigit(q[0])) && attributeNo != 6)
  19.     {
  20.         a = q.substr(0, 1);
  21.         b = q.substr(1, 30);
  22.         w = stoi(b);
  23.     }
  24.     else if (attributeNo >= 5 && isdigit(q[0]) && attributeNo != 6)
  25.     {
  26.         w = stoi(q);
  27.     }
  28.     else if (attributeNo == 6)
  29.     {
  30.         string temp_STR1 = "";
  31.         string temp_STR2 = "";
  32.         int met_slash = 0;
  33.  
  34.         if (isdigit(q[0]))
  35.         {
  36.             b = q.substr(0, 2);
  37.             i_DOB[0] = stoi(b);
  38.         }
  39.         else
  40.         {
  41.             o_DOB[0] = q[0];
  42.             b = q.substr(1, 2);
  43.             i_DOB[0] = stoi(b);
  44.         }
  45.  
  46.         int encounter_times = 0;
  47.         for (int u = 0; u < q.length(); u++)
  48.         {
  49.             met_slash = 0;
  50.             if (q[u] == '/')
  51.             {
  52.                 //if encounter a space then set met_space to 1
  53.                 met_slash = 1;
  54.                 encounter_times++;
  55.             }
  56.             if (met_slash == 1)
  57.             {
  58.                 if (isdigit(q[u + 1]) && encounter_times == 1)
  59.                 {
  60.                     b = q.substr(u + 1, 2);
  61.                     i_DOB[1] = stoi(b);
  62.                 }
  63.                 else if (!(isdigit(q[u + 1])) && encounter_times == 1)
  64.                 {
  65.                     o_DOB[1] = q[u + 1];
  66.                     b = q.substr(u + 2, 2);
  67.                     i_DOB[1] = stoi(b);
  68.                 }
  69.                 else if (isdigit(q[u + 1]) && encounter_times == 2)
  70.                 {
  71.                     b = q.substr(u + 1, 4);
  72.                     i_DOB[2] = stoi(b);
  73.                 }
  74.                 else if (!(isdigit(q[u + 1])) && encounter_times == 2)
  75.                 {
  76.                     o_DOB[2] = q[u + 1];
  77.                     b = q.substr(u + 2, 4);
  78.                     i_DOB[2] = stoi(b);
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.     int compareResult = 0;
  85.  
  86.     switch (attributeNo)
  87.     {
  88.     //name
  89.     case 0:
  90.         compareResult = STR1[pos].find(q);
  91.         break;
  92.     //country
  93.     case 1:
  94.         compareResult = STR2[pos].find(q);
  95.         break;
  96.     //designation
  97.     case 2:
  98.         compareResult = STR3[pos].find(q);
  99.         break;
  100.     //gender
  101.     case 3:
  102.         if (STR4[pos].compare("female") == 0)
  103.         {
  104.             if (q.compare("female") == 0)
  105.             {
  106.                 // cout << "is female" << endl;
  107.  
  108.                 compareResult = pos;
  109.             }
  110.             else
  111.             {
  112.                 compareResult = -1;
  113.             }
  114.         }
  115.         else if (STR4[pos].compare("male") == 0)
  116.         {
  117.             if (q.compare("male") == 0)
  118.             {
  119.                 // cout << "is male" << endl;
  120.                 compareResult = pos;
  121.             }
  122.             else
  123.             {
  124.                 compareResult = -1;
  125.             }
  126.         }
  127.         break;
  128.     //level of education
  129.     case 4:
  130.         compareResult = STR5[pos].find(q);
  131.         break;
  132.     //ID
  133.     case 5:
  134.  
  135.         if (a.find('>') == 0)
  136.         {
  137.             if (w < col1[pos])
  138.             {
  139.                 compareResult = pos;
  140.             }
  141.             else
  142.             {
  143.                 compareResult = -1;
  144.             }
  145.         }
  146.         else if (a.find('<') == 0)
  147.         {
  148.             if (w > col1[pos])
  149.             {
  150.                 compareResult = pos;
  151.             }
  152.             else
  153.             {
  154.                 compareResult = -1;
  155.             }
  156.         }
  157.         else if (w == col1[pos])
  158.         {
  159.             compareResult = pos;
  160.         }
  161.         else
  162.         {
  163.             compareResult = -1;
  164.         }
  165.         break;
  166.  
  167.     //DOB
  168.     case 6:
  169.  
  170.         temp_store_2 = col2[pos];
  171.         for (int d = 1000000; d <= 31000000; d += 1000000)
  172.         {
  173.             if (temp_store_2 >= d && temp_store_2 < (d + 1000000))
  174.             {
  175.                 e_DOB[0] = (d / 1000000);
  176.                 temp_store_2 -= (d);
  177.             }
  178.         }
  179.         for (int m = 10000; m <= 120000; m += 10000)
  180.         {
  181.             if (temp_store_2 >= m && temp_store_2 < (m + 10000))
  182.             {
  183.                 e_DOB[1] = (m / 10000);
  184.                 temp_store_2 -= (m);
  185.             }
  186.         }
  187.         for (int y = 0; y < 10000; y++)
  188.         {
  189.             if (temp_store_2 == y)
  190.             {
  191.                 e_DOB[2] = y;
  192.             }
  193.         }
  194.         // cout << "I_DOB: " << o_DOB[0] << i_DOB[0] << "/" << o_DOB[1] << i_DOB[1] << "/" << o_DOB[2] << i_DOB[2] << endl;
  195.         // cout << "E_DOB: " << e_DOB[0] << '/' << e_DOB[1] << '/' << e_DOB[2] << endl;
  196.         if (i_DOB[0] >= 0 && i_DOB[1] >= 0 && i_DOB[2] >= 0)
  197.         {
  198.             char flags[3] = {'n', 'n', 'n'};
  199.             for (int n = 0; n < 3; n++)
  200.             {
  201.                 switch (o_DOB[n])
  202.                 {
  203.                 case '>':
  204.                     if (e_DOB[n] > i_DOB[n])
  205.                     {
  206.                         flags[n] = 'y';
  207.                     }
  208.                     else
  209.                     {
  210.                         flags[n] = 'n';
  211.                     }
  212.                     break;
  213.                 case '<':
  214.                     if (e_DOB[n] < i_DOB[n])
  215.                     {
  216.                         flags[n] = 'y';
  217.                     }
  218.                     else
  219.                     {
  220.                         flags[n] = 'n';
  221.                     }
  222.                     break;
  223.                 case '=':
  224.                     if (e_DOB[n] == i_DOB[n])
  225.                     {
  226.                         flags[n] = 'y';
  227.                     }
  228.                     else
  229.                     {
  230.                         flags[n] = 'n';
  231.                     }
  232.                     break;
  233.                 }
  234.             }
  235.             if (flags[0] == 'y' && flags[1] == 'y' && flags[2] == 'y')
  236.             {
  237.                 compareResult = pos;
  238.             }
  239.             else
  240.             {
  241.                 compareResult = -1;
  242.             }
  243.         }
  244.         else
  245.         {
  246.             compareResult = -1;
  247.         }
  248.         break;
  249.     //Height
  250.     case 7:
  251.  
  252.         if (a.find('>') == 0)
  253.         {
  254.             if (w < col3[pos])
  255.             {
  256.                 compareResult = pos;
  257.             }
  258.             else
  259.             {
  260.                 compareResult = -1;
  261.             }
  262.         }
  263.         else if (a.find('<') == 0)
  264.         {
  265.             if (w > col3[pos])
  266.             {
  267.                 compareResult = pos;
  268.             }
  269.             else
  270.             {
  271.                 compareResult = -1;
  272.             }
  273.         }
  274.         else if (w == col3[pos])
  275.         {
  276.             compareResult = pos;
  277.         }
  278.         else
  279.         {
  280.             compareResult = -1;
  281.         }
  282.         break;
  283.     //Weight
  284.     case 8:
  285.  
  286.         if (a.find('>') == 0)
  287.         {
  288.             if (w < col4[pos])
  289.             {
  290.                 compareResult = pos;
  291.             }
  292.             else
  293.             {
  294.                 compareResult = -1;
  295.             }
  296.         }
  297.         else if (a.find('<') == 0)
  298.         {
  299.             if (w > col4[pos])
  300.             {
  301.                 compareResult = pos;
  302.             }
  303.             else
  304.             {
  305.                 compareResult = -1;
  306.             }
  307.         }
  308.         else if (w == col4[pos])
  309.         {
  310.             compareResult = pos;
  311.         }
  312.         else
  313.         {
  314.             compareResult = -1;
  315.         }
  316.         break;
  317.     //Years of working
  318.     case 9:
  319.  
  320.         if (a.find('>') == 0)
  321.         {
  322.             if (w < col5[pos])
  323.             {
  324.                 compareResult = pos;
  325.             }
  326.             else
  327.             {
  328.                 compareResult = -1;
  329.             }
  330.         }
  331.         else if (a.find('<') == 0)
  332.         {
  333.             if (w > col5[pos])
  334.             {
  335.                 compareResult = pos;
  336.             }
  337.             else
  338.             {
  339.                 compareResult = -1;
  340.             }
  341.         }
  342.         else if (w == col5[pos])
  343.         {
  344.             compareResult = pos;
  345.         }
  346.         else
  347.         {
  348.             compareResult = -1;
  349.         }
  350.         break;
  351.     //Basic Salary
  352.     case 10:
  353.  
  354.         if (a.find('>') == 0)
  355.         {
  356.             if (w < col6[pos])
  357.             {
  358.                 compareResult = pos;
  359.             }
  360.             else
  361.             {
  362.                 compareResult = -1;
  363.             }
  364.         }
  365.         else if (a.find('<') == 0)
  366.         {
  367.             if (w > col6[pos])
  368.             {
  369.                 compareResult = pos;
  370.             }
  371.             else
  372.             {
  373.                 compareResult = -1;
  374.             }
  375.         }
  376.         else if (w == col6[pos])
  377.         {
  378.             compareResult = pos;
  379.         }
  380.         else
  381.         {
  382.             compareResult = -1;
  383.         }
  384.         break;
  385.     //Allowances
  386.     case 11:
  387.         if (a.find('>') == 0)
  388.         {
  389.             if (w < col7[pos])
  390.             {
  391.                 compareResult = pos;
  392.             }
  393.             else
  394.             {
  395.                 compareResult = -1;
  396.             }
  397.         }
  398.         else if (a.find('<') == 0)
  399.         {
  400.             if (w > col7[pos])
  401.             {
  402.                 compareResult = pos;
  403.             }
  404.             else
  405.             {
  406.                 compareResult = -1;
  407.             }
  408.         }
  409.         else if (w == col7[pos])
  410.         {
  411.             compareResult = pos;
  412.         }
  413.         else
  414.         {
  415.             compareResult = -1;
  416.         }
  417.         break;
  418.     }
  419.     //cout << "Debug: Compare Result in Advanced Compare String: " << attributeNo << endl;
  420.     if (compareResult == string::npos)
  421.     {
  422.         return -1;
  423.     }
  424.     else
  425.     {
  426.         return pos;
  427.     }
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement