-
Notifications
You must be signed in to change notification settings - Fork 2
/
Po_parse_exptext.cpp
167 lines (153 loc) · 7.05 KB
/
Po_parse_exptext.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*******************************************************************/
/*** FILE : Po_parse_exptext.c ***/
/*** AUTHOR: Jeff Offutt ***/
/*** PROGRAMMER:Sekhar Muddana ***/
/*** PUBLIC ROUTINES: ***/
/*** struct polynomial *Parse_exptext() ***/
/*** PRIVATE ROUTINES: ***/
/*** void Parse_term() ***/
/*** void Create_LR() ***/
/*** MODULE DESCRIPTION: ***/
/*** This module contains routines to parse the expanded ***/
/*** string. ***/
/*** NOTE: ***/
/*** The typical expanded string looks like this: ***/
/*** -7x+10y+(x(yx)) ***/
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "Po_parse_exptext.h"
#include "Memory_routines.h"
static void Parse_term(struct term_node *Root, char String[], int *Strptr);
static void CreateLR(struct term_node *Root);
/*******************************************************************/
/* MODIFIES: */
/* Poly_str -- Polynomial string to be converted to structure */
/* polynomial. */
/* REQUIRES: None. */
/* RETURNS: */
/* Pointer to created polynomial. */
/* FUNCTION: */
/* Scan the input string to create the polynomial, in its */
/* internal representation. */
/* NOTE: */
/* The polynomial structure corresponding to Poly_str is */
/* created by calling Parse_term() which creates the tree for */
/* the term string. */
/*******************************************************************/
struct polynomial *Parse_exptext(char Poly_str[])
{
struct polynomial *poly; /* pointer that will be returned */
struct term_head *cur_head_ptr;
char *term_str; /* string passed to Parse_term(). */
int term_str_indx = 0; /* used to create string term_str */
int poly_str_indx = 0; /* position in the string being parsed. */
int parse_str_indx = 0; /* hold the position from where to parse. */
char sign = ' '; /* hold the sign of the scalar coef for term */
int i = 0,cur_coef = 0;
poly = Poly_alloc();
poly->degree = 0;
for (i=0;i<NUM_LETTERS;i++)
poly->deg_letter[i]= 0;
cur_head_ptr = Term_head_alloc();
poly->terms = cur_head_ptr;
cur_head_ptr->term = Term_node_alloc();
if (Poly_str[poly_str_indx] == '-') {
sign = '-';
poly_str_indx++;
}
if(!isdigit(Poly_str[poly_str_indx])) {
if (sign != '-')
cur_head_ptr->coef = 1;
else
cur_head_ptr->coef = -1;
}
else {
while (isdigit(Poly_str[poly_str_indx]))
cur_coef = cur_coef * 10 + (Poly_str[poly_str_indx++] - '0');
if (sign != '-')
cur_head_ptr->coef = cur_coef;
else
cur_head_ptr->coef = 0 - cur_coef;
sign = '+';
cur_coef = 0;
}
term_str = (char*) Mymalloc(strlen(Poly_str)+1);
while (poly_str_indx < (int)strlen(Poly_str)) {
if((Poly_str[poly_str_indx] != '+') && (Poly_str[poly_str_indx] != '-'))
term_str[term_str_indx++] = Poly_str[poly_str_indx++];
else {
sign = Poly_str[poly_str_indx++];
term_str[term_str_indx] = '\0';
term_str_indx = 0;
Parse_term(cur_head_ptr->term,term_str,&parse_str_indx);
parse_str_indx = 0;
cur_head_ptr->next = Term_head_alloc();
cur_head_ptr = cur_head_ptr->next;
cur_head_ptr->term = Term_node_alloc();
if(!isdigit(Poly_str[poly_str_indx])) {
if (sign == '+')
cur_head_ptr->coef = 1;
else
cur_head_ptr->coef = -1;
}
else {
while (isdigit(Poly_str[poly_str_indx]))
cur_coef = cur_coef * 10 + (Poly_str[poly_str_indx++] - '0');
if (sign == '+')
cur_head_ptr->coef = cur_coef;
else
cur_head_ptr->coef = 0 - cur_coef;
cur_coef = 0;
}
}
}
term_str[term_str_indx] = '\0';
Parse_term(cur_head_ptr->term,term_str,&parse_str_indx);
free(term_str);
return(poly);
}
/*******************************************************************/
/* MODIFIES: */
/* Root -- root of the tree for the term to be parsed. */
/* *Strptr -- Position where scanner is, in the input string. */
/* REQUIRES: */
/* String -- Term, for which to create a tree. */
/* RETURNS: None. */
/* FUNCTION: */
/* Build the tree with the given root, by scanning the String. */
/* NOTE: */
/* The tree is built recursively by bulding left and right sub */
/* trees. */
/*******************************************************************/
void Parse_term(struct term_node *Root, char String[], int *Strptr)
{
if (*Strptr < (int)strlen(String)) {
if (String[*Strptr] == '(') {
(*Strptr)++;
CreateLR(Root);
Parse_term(Root->left,String,Strptr);
Parse_term(Root->right,String,Strptr);
(*Strptr)++;
}
else {
Root->letter = String[*Strptr];
(*Strptr)++;
}
}
}
/*******************************************************************/
/* MODIFIES: */
/* Root -- root of the tree for the term to be parsed. */
/* REQUIRES: None. */
/* RETURNS: None. */
/* FUNCTION: */
/* Allocate space for the left child and the right child. */
/*******************************************************************/
void CreateLR(struct term_node *Root)
{
Root->left = Term_node_alloc();
Root->right = Term_node_alloc();
}