Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdalign.h>
  4. #define N 5
  5.  
  6. typedef struct Var_Msg{
  7.         char c;
  8.         int n;
  9.         char d;
  10.         }Var_Msg_t;
  11.  
  12.     typedef struct Fixed_Msg{
  13.             unsigned char mid;
  14.             char data[10];
  15.         }Fixed_Msg_t;
  16.        
  17.         typedef union Mesage{
  18.             Var_Msg_t *variable_length_message;
  19.             Fixed_Msg_t *fixed_length_message;
  20.         }Message_t;
  21.        
  22.        
  23. union Test{
  24.     char c;
  25.     unsigned n;
  26.     float f;
  27. } uniune;
  28.  
  29. int v[10];
  30.  
  31. unsigned char parityBit(unsigned char n){
  32. // B7 B6 B5 B4 B3 B2 B1 B0
  33. //  0  0  1  1  1  0  1  1 //OddParity
  34.     unsigned ones=0;
  35.     for(int i=1; i<=7; i++){
  36.         if ((n&1u)!=0){
  37.             ones++;
  38.         }  
  39.         n=n>>1;
  40.     }
  41.     return ones;
  42. }
  43.  
  44. unsigned char setParityBit(unsigned char n){
  45.     unsigned char parityBitValue=parityBit(n)%2;
  46.     n=n&(~(1u<<7u)); //pune fortat pe 0 bitul 7
  47.     n=n|(parityBitValue<<7u); //pune bitul 7 pe valoarea bitului de paritate
  48.     return n;
  49. }
  50.  
  51. /* sa se returneze valoarea maxima care se poate obtine
  52. * prin deplsasari cu recirculare la stanga a bitilor
  53. * unui argument de tip unsigned
  54. */
  55.  
  56. unsigned shLRot(unsigned n){
  57.     unsigned nBits=sizeof(n)*8;
  58.     unsigned msb=n&(1u<<(nBits-1)); //scoate msb
  59.     n<<=1; //n=n<<1;    //muta la stanga 1 poz
  60.     n=n|(msb>>(nBits-1)); // lsb(n)<-msb
  61.     //printf("0x%x\n", n);
  62.     return n;
  63. }
  64. unsigned maxShLRot(unsigned n){
  65.     // 01...00011001001
  66.     // 1...000110010010
  67.     // ...0001100100101
  68.     // ..0001100100101.
  69.     // .0001100100101..
  70.     // 0001100100101...
  71.     // 001100100101...0
  72.     unsigned max=n;
  73.     for (unsigned i=0; i<sizeof(n)*8; i++){
  74.         unsigned nou=shLRot(n);
  75.         printf ("%x %x\n", n, nou);
  76.         if (nou>max){
  77.             max=nou;
  78.         }
  79.         n=nou;
  80.     }
  81.     return max;
  82. }
  83.  
  84. int stergeElem(const int t[], int* n, int poz){
  85. //  /* ret 0 daca e ok si valoare negativa pt eroare */
  86. //  if (poz<0 || poz>n-1) return -1;
  87. //  for (int i=poz; i<(*n-1); i++){
  88. //      t[i]=t[i+1];
  89. //  }
  90. //  *n=*n-1;
  91. //  //(*n)--;
  92.    
  93. }
  94.  
  95. void stergePare(int t[], int* n){
  96.     for (int i=0; i<*n; i++){
  97.         if (t[i]%2==0){
  98.             stergeElem(t, n, i);
  99.             i--;
  100.         }
  101.     }
  102. }
  103.  
  104.  
  105. void printTab(int t[], int n){
  106.     printf("\n");
  107.     for (int i=0; i<n; i++){
  108.         printf("%d ", t[i]);
  109.     }
  110.     printf("\n");
  111. }
  112.  
  113. int eSimetric(int t[], int n){
  114.     //  printf("Marimea lui t2 este=%u\n", sizeof(t));
  115.     /* elem egal departate de capate sunt identice */
  116.     int ok=1;
  117.     for (int i=0; i<=n/2 && ok; i++){
  118.         if (t[i]!=t[n-1-i]){
  119.             ok=0;
  120.         }
  121.     }
  122.     return ok;
  123. }
  124.  
  125. void f(int w[10]){
  126.     printf("Sizeof de v in fct este %d\n", sizeof(v));
  127.     printf("Nr de eleme in fct sete %d\n", sizeof(v)/sizeof(v[0]));
  128.     printf("Sizeof de v in fct este %d\n", sizeof(w));
  129.     printf("Nr de eleme in fct sete %d\n", sizeof(w)/sizeof(w[0]));
  130. }
  131.  
  132. void handleCommand(char *cmd) {
  133.     /* led, 1, ON */
  134. //    char cuv[10]={0};
  135. //  //  memset(cuv,0,10);
  136. //    char *pv=strchr(cmd,',');
  137. //    strncpy(cuv, cmd, pv-cmd);
  138. //    cuv[pv-cmd]='\0';
  139.     char* cuv=strtok(cmd,", ;");
  140.     printf("Cuvant este %s\n", cuv);
  141.     if (strcmp(cuv, "led") == 0) {
  142.         printf("cautam care led\n");
  143.         char* ledId = strtok(NULL, ", ;");
  144.         uint8_t ledNr = atoi(ledId);
  145.         printf("Ledul numarul %d\n", ledNr);
  146.         if (ledNr <= 2 - 1) {
  147.             char *actiune = strtok(NULL, ", ;");
  148.             if (strcmp(actiune, "ON") == 0) {
  149. //              STM_EVAL_LEDOn(1);
  150.                 printf("Aprinde led %d\n",ledNr);
  151.             }
  152.             if (strcmp(actiune, "OFF") == 0) {
  153.                 printf("stinge led %d\n",ledNr);
  154.             }
  155.         }
  156.     }
  157. }
  158.  
  159. typedef union {
  160.     uint32_t word;
  161.     uint8_t bytes[4];
  162. } word_msg_t;
  163.  
  164. typedef struct DateTime{
  165.     unsigned y:6;
  166.     unsigned m:4;
  167.     unsigned d:5;
  168.     unsigned h:5;
  169.     unsigned mm:6;
  170.     unsigned ss:6;
  171. }DateTime_t;
  172.  
  173. char* expandeazaSir(char *s){
  174.     int n=1;
  175.     char *rez=malloc(n* sizeof(char));
  176.     if(rez==NULL){
  177.         printf("nu s-a reusit alocarea");
  178.         return NULL;
  179.     }
  180.     rez[n-1]='\0';
  181.     int i=0;
  182.     while(s[i]!=0 ){
  183.         if (isalpha(s[i])){
  184.             char c=s[i];
  185.         //  printf("caracterul este %c\n", c);
  186.             i++;
  187.             int nAp=0;
  188.             nAp=atoi(s+i);
  189.            
  190. //          while(isdigit(s[i])){
  191. //              nAp=nAp*10+(s[i]-'0');
  192. //              i++;
  193. //          }
  194. //          for (int j=1; j<=nAp; j++){
  195. //              printf("%c",c);
  196. //          }
  197.             for (int j=1; j<=nAp; j++){
  198.                 n++;
  199.                 rez=realloc(rez,100*n*sizeof(char));
  200.                 if (rez==NULL){
  201.                     printf("Nu s-a reusit realocarea");
  202.                     return NULL;
  203.                 }
  204.                 rez[n-2]=c;
  205.             }
  206.             rez[n-1]='\0';
  207.         }
  208.         i++;
  209.     }
  210.     //printf("Rez este %s\n", rez);
  211.    
  212.     return rez;
  213. }
  214.  
  215. int main(){
  216.    
  217.    
  218. //  char* s=expandeazaSir("g5i2g32e2l1a3");
  219. //  if(s!=NULL){
  220. //      printf("%s",s);
  221. //      free(s);
  222. //  }
  223.  
  224.     unsigned long long q=1;
  225.     char *aux=expandeazaSir("g5i2g32e2l1a3");
  226.     char *s=aux;
  227.     while(s!=NULL){
  228.         if (q%10000==0) printf("%d\n", q);
  229.         s=expandeazaSir("g5i2g32e2l1a3");
  230.         if (s!=NULL){
  231.             aux=s;
  232.         }
  233.         else{
  234.             printf("Ultimul nenull: %p\n", aux);
  235.         }
  236.         q++;
  237.     }
  238.     printf("Am reusit sa aloc de %d ori\n", q);
  239.     printf("Ultimul aux este %p\n", aux);
  240.     free(aux);
  241.    
  242.     printf("gata");
  243.    
  244.     printf("\n\n\n\n");
  245.    
  246.     unsigned x=1547710819;
  247.    
  248.     DateTime_t date=*(DateTime_t*)&x;
  249. //  date.d=17;
  250. //  date.m=1;
  251. //  date.y=2019-1970;
  252. //  date.h=10;
  253. //  date.mm=4;
  254. //  date.ss=0;
  255.     printf("%u-%u-%u, %u:%u:%u\n", date.y+1970, date.m, date.d, date.h, date.mm, date.ss);
  256.    
  257.     word_msg_t test;
  258.     //test.word=(0xAB)<<24|(0xCD<<16)|(0x12<<8)|(0x34);
  259.     test.word=0xABCDE1234;
  260. //  test.bytes[0]=0x34;
  261. //  test.bytes[1]=0x12;
  262. //  test.bytes[2]=0xCD;
  263. //  test.bytes[3]=0xAB;
  264.     printf("%x\n", test.bytes[0]);
  265.     printf("%x\n", test.word&0xFF);
  266.    
  267.     uniune.f=3.14;
  268.     printf("IEEE in hexa este 0x%x\n", uniune.n);
  269.     printf("Nr este %d\n", uniune.f);
  270.  
  271.    
  272.     printf("\n\n\n\n");
  273.    
  274.     printf("Marimea ste %d\n", sizeof(Var_Msg_t));
  275.     printf("Numarul de biti de aliniament este %d\n", _Alignof(Var_Msg_t));
  276.     //char s[10]="led,1,ON";
  277.     handleCommand(s);
  278.     while(1);
  279.     return 0;
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement