comparison c/regexParser/main.cc @ 54:f540de861cd6

add Node struct
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Mon, 08 Jun 2015 15:47:52 +0900
parents 82fbc8478f7b
children 883e3473a9f5
comparison
equal deleted inserted replaced
53:82fbc8478f7b 54:f540de861cd6
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <string.h> 3 #include <string.h>
4 4
5 int bitBlock = sizeof(unsigned long) * 8; 5 typedef struct node {
6 struct node *left;
7 struct node *right;
8 int type;
9 int value;
10 } Node, *NodePtr;
6 11
7 typedef struct bitInfo {
8 int arrayNum;
9 unsigned long *bitContainer;
10 }BitInfo,*BitInfoPtr;
11 12
12 void bitSet(BitInfoPtr bi, int bitSetPosition) { 13 NodePtr newNode(int type, int value, NodePtr left, NodePtr right) {
13 14 NodePtr d = (NodePtr)malloc(sizeof(Node));
14 unsigned long tmp = 1; 15 d->type = type;
15 int arrayPosition = 0; 16 d->value = value;
16 17 d->left = left;
17 arrayPosition = bitSetPosition / bitBlock; 18 d->right = right;
18 bitSetPosition = bitSetPosition % bitBlock; 19 return d;
19
20 tmp = tmp << (bitBlock - 1 - bitSetPosition);
21 bi->bitContainer[arrayPosition] = bi->bitContainer[arrayPosition] | tmp;
22 }
23
24 void bitPrint(BitInfoPtr bi) {
25 for (int i = 0; i < bi->arrayNum ; i++) {
26 for (int j = bitBlock - 1; j >= 0; j--) {
27 printf( "%lu", ( bi->bitContainer[i] >> j ) & 1 );
28 }
29 printf(" ");
30 }
31 puts("");
32 } 20 }
33 21
34 int main(int argc, char **argv) { 22 int main(int argc, char **argv) {
35 23
36 BitInfoPtr bi = (BitInfoPtr)malloc(sizeof(BitInfo)); 24 for (int i = 1; i < argc ; i++) {
37 int bitSetPosition = 0;
38 25
39 for (int i = 1; i < argc ; i++) {
40 if (strcmp(argv[i],"-n") == 0) {
41 bitSetPosition = atoi(argv[i+1]);
42 }
43 } 26 }
44 27
45 bi->arrayNum = (bitSetPosition + 1) / bitBlock;
46 if (((bitSetPosition + 1) % bitBlock) != 0) bi->arrayNum++;
47
48 printf("Array Num : %d\n",bi->arrayNum);
49
50 unsigned long bitContainer[bi->arrayNum];
51 for (int i = 0; i < bi->arrayNum; i++) {
52 bitContainer[i] = 0;
53 }
54
55 bi->bitContainer = bitContainer;
56 bitSet(bi,bitSetPosition);
57 bitPrint(bi);
58 return 0; 28 return 0;
59 } 29 }