E
ekiMbo
Hey, I'm a newbie C programmer doing first semester kind of coding. I'm
attempting to read input from the keyboard of the form "Af RtS"
representing a boolean logic expression, here it represents ((NOT a AND f)
OR (NOT R and t and NOT s)).
This will (eventually) get simplified as much as possible and be used to
generate a PostScript file to draw a circuit diagram (logic gates etc).
I've only just started though and I'm wondering what the best data
structure to store characters is. A linked list will work with the simple
things I'm doing now but ideally I want to be able to use parenthesis to
allow more complicated statements to be made. At the moment I have a kind
of binary tree with each node having an AND and an OR pointer.
A -OR-> R
| |
and and
v v
f t
|
and
v
S
Traversing it by going down each branch sequentially... Not sure how I'm
going to allow parentheses though.
Just wondering if this is a decent way of doing it, any suggestions on
improvements or different ideas much appreciared. Also any comments on
general coding style etc. would be helpful.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char letter;
struct node *child;
struct node *sibling;
} node;
node *createFirstNode(char c)
{
node *firstNode = malloc(sizeof(node));
firstNode->letter = c;
firstNode->child = NULL;
firstNode->sibling = NULL;
return firstNode;
}
node *createNode(char c)
{
node *newNode = malloc(sizeof(node));
if(currentNode==NULL)
{
currentParent->sibling = newNode;
currentParent = newNode;
currentNode = newNode;
}
else if(currentNode==currentParent)
{
currentNode->child = newNode;
currentNode = newNode;
}
else
{
currentNode->sibling = newNode;
currentNode = newNode;
}
newNode->letter = c;
return newNode;
}
node *firstNode, currentNode, currentParent;
int main()
{
int i;
char c;
while(1)
{
c = getchar;
if ( c >= 97 && c <= 122 )
{
firstNode=createFirstNode(c);
break;
}
}
currentParent = firstNode;
currentNode = firstNode;
while(1)
{
c = getchar();
if (c == 10) /*if c is the enter key */
{
printf("Finished inputting the tree\n");
break;
}
if (c == 32) /*if c is the space key*/
currentNode=NULL;
if ( c >= 97 && c <= 122 )
{
createNode(c);
printf("fn:%p cp:%p cn:%p\n",firstNode,currentParent,currentNode);
}
}
return(0);
}
attempting to read input from the keyboard of the form "Af RtS"
representing a boolean logic expression, here it represents ((NOT a AND f)
OR (NOT R and t and NOT s)).
This will (eventually) get simplified as much as possible and be used to
generate a PostScript file to draw a circuit diagram (logic gates etc).
I've only just started though and I'm wondering what the best data
structure to store characters is. A linked list will work with the simple
things I'm doing now but ideally I want to be able to use parenthesis to
allow more complicated statements to be made. At the moment I have a kind
of binary tree with each node having an AND and an OR pointer.
A -OR-> R
| |
and and
v v
f t
|
and
v
S
Traversing it by going down each branch sequentially... Not sure how I'm
going to allow parentheses though.
Just wondering if this is a decent way of doing it, any suggestions on
improvements or different ideas much appreciared. Also any comments on
general coding style etc. would be helpful.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char letter;
struct node *child;
struct node *sibling;
} node;
node *createFirstNode(char c)
{
node *firstNode = malloc(sizeof(node));
firstNode->letter = c;
firstNode->child = NULL;
firstNode->sibling = NULL;
return firstNode;
}
node *createNode(char c)
{
node *newNode = malloc(sizeof(node));
if(currentNode==NULL)
{
currentParent->sibling = newNode;
currentParent = newNode;
currentNode = newNode;
}
else if(currentNode==currentParent)
{
currentNode->child = newNode;
currentNode = newNode;
}
else
{
currentNode->sibling = newNode;
currentNode = newNode;
}
newNode->letter = c;
return newNode;
}
node *firstNode, currentNode, currentParent;
int main()
{
int i;
char c;
while(1)
{
c = getchar;
if ( c >= 97 && c <= 122 )
{
firstNode=createFirstNode(c);
break;
}
}
currentParent = firstNode;
currentNode = firstNode;
while(1)
{
c = getchar();
if (c == 10) /*if c is the enter key */
{
printf("Finished inputting the tree\n");
break;
}
if (c == 32) /*if c is the space key*/
currentNode=NULL;
if ( c >= 97 && c <= 122 )
{
createNode(c);
printf("fn:%p cp:%p cn:%p\n",firstNode,currentParent,currentNode);
}
}
return(0);
}