Advertisement
apl-mhd

Sadia's Assignment

Feb 4th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.21 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<ctype.h>
  5. #include<stdbool.h>
  6. //#define MAX 10
  7.  
  8. using namespace std;
  9.  
  10. struct SymbolInfo
  11. {
  12.     char name[50];
  13.     char type[50];
  14.     struct SymbolInfo *next;
  15. };
  16. typedef struct SymbolInfo node;
  17. node *head[100], *temp, *prev;
  18. int MAX;
  19.  
  20. //HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
  21.  
  22.  
  23. int getHashKey(char *name)
  24. {
  25.     int index, ascii = 0;
  26.  
  27.     for(int i=0; i < strlen(name); i++)
  28.     {
  29.         ascii = ascii + name[i];
  30.     }
  31.     index = ascii % MAX;
  32.  
  33.     return index;
  34. }
  35.  
  36. void Insert_Menu(char *n, char *t)
  37. {
  38.     //system("cls");
  39.     int index =  getHashKey(n);
  40.     temp = new node();
  41.     strcpy(temp->name,n);
  42.     strcpy(temp->type,t);
  43.     temp->next = head[index]->next;
  44.     head[index]->next=temp;
  45.  
  46.     //system("cls");
  47.     //SetConsoleTextAttribute(h,FOREGROUND_INTENSITY);
  48.     printf("\nInformation Added!\n");
  49. }
  50.  
  51. void Delete_Menu(char *n, char *t)
  52. {
  53.     //system("cls");
  54.     //SetConsoleTextAttribute(h,FOREGROUND_RED);
  55.  
  56.     int flag=0;
  57.     int index =  getHashKey(n);
  58.  
  59.     prev = head[index];
  60.     temp = head[index]->next;
  61.  
  62.     while(temp!=NULL)
  63.     {
  64.         if(strcmp(temp->name,n)==0&&strcmp(temp->type,t)==0)
  65.         {
  66.             prev->next=temp->next;
  67.             free(temp);
  68.             break;
  69.         }
  70.         temp=temp->next;
  71.         prev=prev->next;
  72.     }
  73.     if(temp==NULL)
  74.     {
  75.         //SetConsoleTextAttribute(h,FOREGROUND_RED);
  76.         printf("Not found");
  77.         flag=1;
  78.     }
  79.     if(flag==0){
  80.         //system("cls");
  81.         //SetConsoleTextAttribute(h,FOREGROUND_RED);
  82.         printf("\nInformation Deleted!\n");
  83.     }
  84.  
  85. }
  86.  
  87. void Show_Menu()
  88. {
  89.     //system("cls");
  90.     int i;
  91.     printf("______list_____\n");
  92.     for(i=0; i<MAX; i++)
  93.     {
  94.         //SetConsoleTextAttribute(h,FOREGROUND_BLUE);
  95.         printf("%d ",i);
  96.         temp=head[i]->next;
  97.         while(temp!=NULL)
  98.         {
  99.             //SetConsoleTextAttribute(h,FOREGROUND_INTENSITY);
  100.             printf("%s,%s ",temp->name,temp->type);
  101.             temp=temp->next;
  102.         }
  103.         printf("\n");
  104.     }
  105. }
  106.  
  107. void Update_Menu(char *n, char *t, char *udType)
  108. {
  109.     //system("cls");
  110.  
  111.     int index = getHashKey(n);
  112.  
  113.     temp=head[index]->next;
  114.  
  115.     while(temp!=NULL)
  116.     {
  117.         if(strcmp(temp->name,n)==0&&strcmp(temp->type,t)==0)
  118.         {
  119.             strcpy(temp->type,udType);
  120.             break;
  121.         }
  122.         temp=temp->next;
  123.     }
  124.     //system("cls");
  125.     //SetConsoleTextAttribute(h,FOREGROUND_INTENSITY);
  126.     printf("\nInformation Updated!\n");
  127.  
  128. }
  129.  
  130. bool Search_Menu(char *n, char *t)
  131. {
  132.     //system("cls");
  133.  
  134.     int index = getHashKey(n);
  135.  
  136.     temp=head[index]->next;
  137.  
  138.     while(temp!=NULL)
  139.     {
  140.         if(strcmp(temp->name,n)==0&&strcmp(temp->type,t)==0)
  141.         {
  142.             return true;
  143.             break;
  144.         }
  145.         temp=temp->next;
  146.     }
  147.     //system("cls");
  148.     //SetConsoleTextAttribute(h,FOREGROUND_INTENSITY);
  149.     printf("\nAfter searching!\n");
  150.     return false;
  151. }
  152. int main()
  153. {
  154.     char n[100], t[100], updType[100];
  155.  
  156.     int ch='0';
  157.     printf("####%d",ch);
  158.  
  159.     bool TF;
  160.     int i;
  161.     char choice;
  162.  
  163.     printf("Enter your maximum size: ");
  164.     scanf("%d",&MAX);
  165.  
  166.     for(i=0; i<MAX; i++)
  167.     {
  168.         head[i] = new node();
  169.         head[i]->next = NULL;
  170.     }
  171.  
  172.     while(true)
  173.     {
  174.         //SetConsoleTextAttribute(h,FOREGROUND_GREEN);
  175.         fflush(stdout);
  176.         printf("\nWelcome To Our First Assignment\n");
  177.  
  178.         printf("-- -----------------------------\n");
  179.         printf("1 - Insert Option\n");
  180.         printf("2 - Delete Option\n");
  181.         printf("3 - Show Option\n");
  182.         printf("4 - Update Option\n");
  183.         printf("5 - Search Option\n");
  184.         printf("-- -----------------------------\n");
  185.         printf("Q - Quit Option\n\n");
  186.  
  187.         //SetConsoleTextAttribute(h,FOREGROUND_BLUE);
  188.         printf("\t Enter your choice: ");
  189.         fflush(stdin);
  190.         choice=toupper(getchar());
  191.         printf("\n");
  192.         //SetConsoleTextAttribute(h,FOREGROUND_GREEN);
  193.         switch(choice)
  194.         {
  195.             case '1':
  196.                 printf("-->Enter your name: ");
  197.                 scanf("%s",&n);
  198.                 printf("-->Enter your classType: ");
  199.                 scanf("%s",&t);
  200.  
  201.                 Insert_Menu(n,t);
  202.                 printf("\n");
  203.                 break;
  204.  
  205.             case '2':
  206.                 printf("-->Enter the  name which you want to delete: ");
  207.                 scanf("%s",&n);
  208.  
  209.                 printf("-->Enter the  classType which you want to delete: ");
  210.                 scanf("%s",&t);
  211.  
  212.                 Delete_Menu(n,t);
  213.                 printf("\n");
  214.                 break;
  215.  
  216.             case '3':
  217.                 Show_Menu();
  218.                 printf("\n");
  219.                 break;
  220.  
  221.             case '4':
  222.                 printf("-->Enter your old name: ");
  223.                 scanf("%s",&n);
  224.  
  225.                 printf("-->Enter your old classType: ");
  226.                 scanf("%s",&t);
  227.  
  228.                 printf("Enter your Updated classType: ");
  229.                 scanf("%s",&updType);
  230.  
  231.                 Update_Menu(n,t,updType);
  232.                 printf("\n");
  233.                 break;
  234.             case '5':
  235.                 printf("-->Enter your searching name: ");
  236.                 scanf("%s",&n);
  237.  
  238.                 printf("-->Enter your searching classType: ");
  239.                 scanf("%s",&t);
  240.  
  241.                 TF = Search_Menu(n,t);
  242.                 printf("And it was ->");
  243.                 if(TF==true)
  244.                 {
  245.                     //SetConsoleTextAttribute(h,FOREGROUND_INTENSITY);
  246.                     printf("\tFound") ;
  247.                     printf("\n");
  248.                 }
  249.                 else
  250.                 {
  251.                     //SetConsoleTextAttribute(h,FOREGROUND_RED);
  252.                     printf("\tNot found");
  253.                     printf("\n");
  254.                 }
  255.                 printf("\n");
  256.                 break;
  257.             case 'Q':
  258.                 exit(0);
  259.                 printf("\n");
  260.                 break;
  261.             default:
  262.                 printf("Wrong Choice\n");
  263.                 printf("\n");
  264.                 break;
  265.         }
  266.  
  267.     }while(1);
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement