Advertisement
apl-mhd

compiler assignment

Jul 21st, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. using  namespace std;
  5.  
  6.  
  7. int serial=1;
  8.  
  9. struct  table{
  10.     char varName[100];
  11.     char varType[100];
  12.     char varValue[100];
  13.  
  14.     bool dlt = false;
  15.  
  16. };
  17. struct  table st[100];
  18.  
  19. char varName[100];
  20. char varType[100];
  21. //char varValue[100];
  22.  
  23.  
  24. void  insert();
  25.  
  26. void  varNamsearch();
  27.  
  28. void  showall();
  29. void  showSpec();
  30. int findSerial();
  31.  
  32.  
  33. int main() {
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.     bool decision = true;
  43.  
  44.     while(decision){
  45.  
  46.         cout<<endl<<"1. Insert :\n";
  47.         cout<<"2. Delete :\n";
  48.         cout<<"3. Show specefic :\n";
  49.         cout<<"4. Show all :\n";
  50.         cout<<"5. Update :\n"<<endl;
  51.  
  52.  
  53.         string n;
  54.         cin>>n;
  55.  
  56.         if(n == "1" ){
  57.  
  58.  
  59.             insert();
  60.  
  61.  
  62.         }
  63.  
  64.         else if(n == "2"){
  65.  
  66.  
  67.  
  68.  
  69.  
  70.         }
  71.         else if(n == "3"){
  72.  
  73.  
  74.  
  75.             showSpec();
  76.  
  77.  
  78.  
  79.         }
  80.         else if(n == "4"){
  81.  
  82.  
  83.             showall();
  84.  
  85.  
  86.         }
  87.  
  88.         else{
  89.             cout<<"Invalid input, enter again \n";
  90.         }
  91.  
  92.     }
  93.  
  94.  
  95.  
  96.     return 0;
  97. }
  98.  
  99.  
  100. void  insert(){
  101.  
  102.  
  103.  
  104.  
  105.  
  106.     cin.ignore();
  107.  
  108.     string input;
  109.     getline(cin, input);
  110.  
  111.     int findEqual;
  112.     findEqual =  input.find('=');
  113.  
  114.     int splt=0;
  115.     int n=0;
  116.  
  117.  
  118.     if(findEqual == -1) {
  119.  
  120.         for (int i = 0; '\0' != input[i]; ++i) {
  121.             // cout<<"first"<<endl;
  122.  
  123.             if (splt == 0) {
  124.  
  125.                 if (input[i] == ' ') {
  126.  
  127.                     st[serial].varType[n] = '\0';
  128.                 } else {
  129.  
  130.                     st[serial].varType[n] = input[i];
  131.                     n++;
  132.                 }
  133.  
  134.             }
  135.             if (splt == 1) {
  136.  
  137.                 if (input[i] == ';') {
  138.  
  139.                     st[serial].varName[n] = '\0';
  140.                 } else {
  141.  
  142.                     st[serial].varName[n] = input[i];
  143.                     n++;
  144.                 }
  145.             }
  146.  
  147.             if (input[i] == ' ') {
  148.  
  149.                 splt++;
  150.                 n = 0;
  151.             }
  152.  
  153.         }
  154.  
  155.         st[serial].varValue[0] ='N';
  156.         st[serial].varValue[1] ='U';
  157.         st[serial].varValue[2] ='L';
  158.         st[serial].varValue[3] ='L';
  159.         st[serial].varValue[4] ='\0';
  160.  
  161.         serial++;
  162.  
  163.  
  164.     } else{ // for float amount=2.25
  165.  
  166.         //cout<<"second"<<endl;
  167.  
  168.         for (int i = 0; '\0' != input[i]; ++i) {
  169.  
  170.             if (splt == 0) {
  171.  
  172.                 if (input[i] == ' ') {
  173.  
  174.                     st[serial].varType[n] = '\0';
  175.                 } else {
  176.  
  177.                     st[serial].varType[n] = input[i];
  178.                     n++;
  179.                 }
  180.  
  181.             }
  182.             if (splt == 1) {
  183.  
  184.                 if (input[i] == '=') {
  185.  
  186.                     st[serial].varName[n] = '\0';
  187.                 } else {
  188.  
  189.                     st[serial].varName[n] = input[i];
  190.                     n++;
  191.                 }
  192.             }
  193.  
  194.             if(splt == 2){
  195.  
  196.                 if (input[i] == ';') {
  197.  
  198.                     st[serial].varValue[n] = '\0';
  199.                 } else {
  200.  
  201.                     st[serial].varValue[n] = input[i];
  202.                     n++;
  203.                 }
  204.  
  205.             }
  206.  
  207.             if (input[i] == '=' || input[i] == ' ') {
  208.  
  209.                 splt++;
  210.                 n = 0;
  211.             }
  212.  
  213.         }
  214.  
  215.  
  216.         serial++;
  217.     }
  218.  
  219.  
  220.  
  221. }
  222.  
  223.  
  224. void  varNamsearch(){
  225.  
  226.     int a;
  227.  
  228.  
  229.     string input;
  230.  
  231.  
  232.     getline(cin, input);
  233.  
  234.     int start = input.find(' ', 0)+1;
  235.  
  236.  
  237.     int i;
  238.  
  239.     int j=0;
  240.  
  241.  
  242.  
  243.     for ( i= start; i<input.length(); i++) {
  244.  
  245.  
  246.  
  247.             if(input[i]== '=' || input[i] == ';'){
  248.  
  249.                 break;
  250.             }
  251.             else{
  252.  
  253.                 varName[j] = input[i];
  254.                 j++;
  255.                 //cout<<input[i];
  256.             };
  257.  
  258.  
  259.  
  260.  
  261.     }
  262.  
  263.     varName[j] = '\0';
  264.  
  265.     cout<<varName;
  266.  
  267.  
  268.  
  269.  
  270. }
  271.  
  272. void showall(){
  273.  
  274.  
  275.  
  276.         for (int i = 1; i <serial; ++i) {
  277.  
  278.             cout<<i<<" "<<st[i].varName<<" "<<st[i].varType<<" "<<st[i].varValue<<endl;
  279.  
  280.  
  281.         }
  282.  
  283. }
  284.  
  285. void  showSpec(){
  286.  
  287.    int id = findSerial();
  288.  
  289.    if (id == -1){
  290.        cout<<" Name not found"<<endl;
  291.    } else {
  292.  
  293.        cout << id << " " << st[id].varName << " " << st[id].varType << " " << st[id].varValue << endl;
  294.    }
  295.  
  296.  
  297. }
  298.  
  299. int  findSerial(){
  300.  
  301.  
  302.     string input;
  303.  
  304.     cin>>input;
  305.  
  306.     char x[100];
  307.  
  308.     int j;
  309.  
  310.     for (j = 0; j <input.length(); ++j) {
  311.  
  312.         x[j] = input[j];
  313.  
  314.     }
  315.  
  316.     x[j] = '\0';
  317.  
  318.  
  319.  
  320.     for (int i = 1; i <serial ; ++i) {
  321.  
  322.  
  323.             if(strcmp(st[i].varName, x) == 0)
  324.  
  325.                 return i;
  326.  
  327.     }
  328.  
  329.     return  -1;
  330.    
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement