-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.c
More file actions
91 lines (85 loc) · 1.59 KB
/
node.c
File metadata and controls
91 lines (85 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
node *newNode(void);
char *INTEGER = "Integer";
char *REAL = "Real";
char *STRING = "String";
char *FUNCTION = "Function";
node *newIntegerNode(char *name, int length, int v)
{
node *p = newNode();
p->type = INTEGER;
p->ival = v;
int i = 0;
while(i < length)
{
p->chld[i] = ' ';
p->var_name[i] = name[i++];
}
p->var_name[i] = '\0';
p->next = NULL;
p->display = &print;
return p;
}
node *newRealNode(char *name, int length, double v)
{
node *p = newNode();
p->type = REAL;
p->rval = v;
int i = 0;
while(i < length)
{
p->chld[i] = ' ';
(p->var_name)[i] = name[i++];
}
p->display = &print;
p->next = NULL;
return p;
}
node *newFunctionNode(char *name, int length)
{
node *p = newNode();
p->type = FUNCTION;
p->next = NULL;
int i = 0;
while(i < length)
{
p->chld[i] = ' ';
(p->var_name)[i] = name[i++];
}
p->display = &print;
return p;
}
node *newStringNode(char *name, int length , char *v)
{
node *p = newNode();
p->type = STRING;
p->sval = v;
int i = 0;
while(i < length)
(p->var_name)[i] = name[i++];
p->display = &print;
p->next = NULL;
return p;
}
node *
newNode()
{
node *n = (node *) malloc(sizeof(node));
if (n == 0) { fprintf(stderr,"out of memory"); exit(-1); }
return n;
}
int print(struct nodeobject* node1)
{
if(node1 -> type == INTEGER)
printf("%d",node1 -> ival);
else if(node1 -> type == REAL)
printf("%f",node1 -> rval);
else if(node1 -> type == STRING)
printf("%s",node1 -> sval);
else if(node1 -> type != FUNCTION)
printf("NODE INCORRECT\n");
return 0;
}