include stdio define PLUS -1 define MINUS -2 define UMN -3 struct tree

  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
168
169
170
171
172
173
174
175
176
177
178
#include <stdio.h>
#define PLUS -1
#define MINUS -2
#define UMN -3
struct tree
{
int elem;
tree *left, *right;
};
struct data{data *next; tree *elem; int i;};
struct list{data *begin, *end;};
//Функция создания очереди
void creatoch(list *och)
{
och->begin=NULL;
och->end=NULL;
}
//Добавить элемент в очередь
void intooch(list *och, tree *elem,int i)
{
data *p;
p=new data;
p->next=NULL;
p->elem=elem;
p->i=i;
if(och->begin==NULL&&och->end==NULL)
och->begin=p;
else
och->end->next=p;
och->end=p;
}
//Взять элемент из очереди
int outoch(list *och, tree **elem, int *i)
{
data *p;
if(och->begin!=NULL)
{
*elem=och->begin->elem;
*i=och->begin->i;
p=och->begin;
if(och->begin==och->end)
och->begin=och->end=NULL;
else
och->begin=och->begin->next;
delete p;
return 1;
} else return 0;
}
//Проверить пуста ли очередь
int nulloch(list *och)
{
if(och->begin!=NULL&&och->end!=NULL) return 0; else return 1;
}
int n=0,k=0;
int in(FILE *f, tree **D) //Формирование дерева формулы
{
char ch,t;
fscanf(f,"%c",&ch);
while(!feof(f) && ch != '\n')
{
printf("%c",ch);
if(ch <= '9' && ch >= '0')
{
int x = ch - '0'; //численное значение символа
fscanf(f, "%c", &ch);
while(ch >= '0' && ch <= '9')
{
x = 10*x + ch - '0';
fscanf(f, "%c", &ch);
}
ungetc(ch,f);
(*D)->elem = x;
(*D)->left = (*D)->right = NULL;
return 0;
}
else if(ch == '+')
{
(*D)->elem = PLUS;
(*D)->right = new tree;
(*D)->right->left = (*D)->right->right = NULL;
in(f,&((*D)->right));
}
else if(ch == '-')
{
(*D)->elem = MINUS;
(*D)->right = new tree;
(*D)->right->left = (*D)->right->right = NULL;
in(f,&((*D)->right));
}
else if(ch == '*')
{
(*D)->elem = UMN;
(*D)->right = new tree;
(*D)->right->left = (*D)->right->right = NULL;
in(f,&((*D)->right));
}
else if(ch == '(')
{
k++;
(*D)->left = new tree;
(*D)->left->left = (*D)->left->right = NULL;
in(f,&((*D)->left));
}
else if(ch == ')'){k--; return 0;}
fscanf(f,"%c",&ch);
}
//k++;
return 0;
}
int calc(tree **D) //Высчитывание формулы
{
tree* d = (*D);
if(d->elem >= 0) return d->elem;
else
{
int l = calc(&(d->left));
int r = calc(&(d->right));
switch(d->elem)
{
case PLUS: return l+r; break;
case MINUS: return l-r; break;
case UMN: return l*r; break;
}
}
return 0;
}
void print_tree(tree *d)
{
list Q;
int i=2,j=2,omg;
creatoch(&Q);
if(d!=NULL)
{
intooch(&Q,d,1);
do
{
outoch(&Q,&d,&i);
switch(d->elem)
{
case PLUS: printf("+ "); break;
case MINUS: printf("- "); break;
case UMN: printf("* "); break;
default : printf("%d ",d->elem);
}
if(i!=j) printf("\n");
i++;
if(d->left!=NULL) intooch(&Q,d->left,i);
if(d->right!=NULL) intooch(&Q,d->right,i);
j=i;
}
while(!nulloch(&Q));
}
}
int main()
{ char stroka[15];
int n=1;
tree *D = new tree;
D->left = D->right = NULL;
FILE *f = fopen("input.txt","r");
//fscanf(f,"%s", &stroka);
//printf("%s\n",stroka);
in(f,&D);
int res = calc(&D);
printf("=%d\n",res);
print_tree(D);
}
© 2008 lorien