Advertisement
Guest User

min

a guest
Dec 5th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.11 KB | None | 0 0
  1. #include <stdio.h>;
  2. #include <string.h>;
  3. #include <stdlib.h>;
  4. #include <ctype.h>;
  5. #define AND 0;
  6. #define OR 1;
  7. #define XOR 2;
  8. #define NOT 3;
  9. typedef int bool;
  10. #define True 1;
  11. #define False 0;
  12. struct Logic_Element {
  13.     int ID;
  14.     int *input_ids;
  15.     int *input_types;
  16.     int kind;
  17.     int nr_inputs;
  18.     bool output;   
  19. };
  20.  
  21. struct Sub_System{
  22. int nr_elements;
  23. int nr_inputs;
  24. struct  Logic_Element *elements_pointer;   
  25. };
  26.  
  27.  
  28. bool test(int nr_inputs,int kind)
  29. {
  30.     bool return_value;
  31.     switch(kind)
  32.     {
  33.         case 0:
  34.         {
  35.            
  36.             return_value=True;
  37.     break;         
  38.             }      
  39.        
  40.        
  41.         }
  42.         return return_value;
  43.    
  44.    
  45. }
  46.  
  47. void show_help(int code)
  48. {
  49. switch (code)
  50. {
  51. case 0:
  52. {
  53.     printf("supported gate types:\n AND (return True only if all inputs are True)\n OR (returns False only if all inputs are False)\n   NOT(returns True if input is False, and returns False if input is True)\n   XOR (returns True only is exactly 1 input is True)\n");
  54. break; 
  55.     }  
  56.    
  57.    
  58.     }  
  59.    
  60.     }
  61.    
  62. void check_element_type_input(char* str,unsigned *i,struct Logic_Element * element_pointer)
  63. {
  64.     if(!strcmp(str,"help"))
  65.     {
  66.         show_help(0);
  67.         *i=*i-1;
  68.         return;
  69.         }  
  70.         if(!strcmp(str,"AND"))
  71.         {
  72.                 (*element_pointer).kind=AND;       
  73.                 return;
  74.             }
  75.                     if(!strcmp(str,"OR"))
  76.         {
  77.                 (*element_pointer).kind=OR;    
  78.                 return;
  79.             }
  80.             if(!strcmp(str,"NOT"))
  81.         {
  82.                 (*element_pointer).kind=NOT;       
  83.                 return;
  84.             }
  85.             if(!strcmp(str,"XOR"))
  86.         {
  87.                 (*element_pointer).kind=XOR;       
  88.                 return;
  89.             }
  90.             printf("Invalid input");
  91.             *i=*i-1;
  92.     }
  93.    
  94. struct Sub_System* build_ssystem()
  95. {
  96.     unsigned nr;
  97.     struct Sub_System *ssystem=(struct Sub_System*)malloc(sizeof(struct Sub_System));
  98.     char str [10];
  99.     printf("enter number of inputs needed for sub system: ");
  100.         scanf("%d",&(*ssystem).nr_inputs);
  101.     printf("enter number of elements needed for sub system: ");
  102.         scanf("%d",&(*ssystem).nr_elements);
  103.    
  104.     (*ssystem).elements_pointer=(struct Logic_Element*)malloc(sizeof(struct Logic_Element)*nr);
  105.     for(int i=0;i<(*ssystem).nr_elements;i=i+1)
  106.     {
  107.         printf("enter kind of element, type 'help' for info\n");
  108.         scanf("%s",str);
  109.         check_element_type_input(str,&i,&(*ssystem).elements_pointer[i]);
  110.        
  111.     if((*ssystem).elements_pointer[i].kind != 3)
  112.     {
  113.         printf("enter number of inputs this element has\n");
  114.         scanf("%d",&(*ssystem).elements_pointer[i].nr_inputs);
  115.         (*ssystem).elements_pointer[i].input_ids=  (int*)malloc(sizeof(int)*(*ssystem).elements_pointer[i].nr_inputs);
  116.         (*ssystem).elements_pointer[i].input_types=(int*)malloc(sizeof(int)*(*ssystem).elements_pointer[i].nr_inputs);
  117.         printf("For each of the inputs, enter the ID of the element it is connected to. The ID equal the order in which the elements are added.\n to connect the gate input to a sub system input, type 'o' followed by the nr of the input.\n for example o1 would connect the gate input to sub system input 1.\n");
  118. for(int j=0;j<(*ssystem).elements_pointer[i].nr_inputs;j=j+1)
  119.     {
  120.                 printf("input %d: ",j);
  121.                 scanf("%s",str);               
  122.             //-----------------------------------------------------------------------PROBLEM---------------------------------- 
  123.                 //  (*ssystem).elements_pointer[i].input_ids[0]=66666;
  124.                 //  (*ssystem).elements_pointer[i].input_types[0]=66666;        executing those at the same time DOES work
  125.                
  126.                
  127.                     (*ssystem).elements_pointer[i].input_ids[0]=1;
  128.                     (*ssystem).elements_pointer[i].input_types[0]=str[0]; //executing those at the same time doesn't work. executing only one DOES work.
  129.             //-----------------------------------------------------------------------PROBLEM---------------------------------- 
  130.         }  
  131.         }              
  132.         }
  133.        
  134.    
  135.     for(int i=0;i<(*ssystem).nr_elements;i=i+1)
  136.         {
  137.         printf("element %d:\t kind: %d \t nr inputs: %d\n",i,(*ssystem).elements_pointer[i].kind,(*ssystem).elements_pointer[i].nr_inputs);        
  138.             for(int j=0;j<(*ssystem).elements_pointer[j].nr_inputs;j++)
  139.             {
  140.             printf("%d\t%d\t",(*ssystem).elements_pointer[j].input_types[j],(*ssystem).elements_pointer[j].input_types[j]);            
  141.                 }
  142.                 printf("\n");
  143.             }
  144.         return (ssystem);
  145.    
  146. }
  147.  
  148. int main()
  149. {
  150.     printf("Welkom \n");
  151.     struct Sub_System *testt=build_ssystem();
  152.     printf("%d",test(0,0));
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement