-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSOFTWARE.CPP
538 lines (528 loc) · 10.9 KB
/
SOFTWARE.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
#include<math.h>
#include<ctype.h>
#include<graphics.h>
#include<time.h>
#include"img.h"
//img.h required for getimg()
float prec = 20; //Precision
float scale=50; //Not greater than 100
void pause(){ // system("pause") for graphics
cout<<"Press any key to continue...";
getch();
}
void clr(){ //Clearing screen in Graphics
setbkcolor(0);
cleardevice();
gotoxy(1,1);
}
int height(){ //Text mode screen size
int i=1;
int x = wherex(), y = wherey(); //Save current cursor position
while(wherey()!=(i-1)){
gotoxy(1,++i);
}
gotoxy(x,y); //Return back to original position
return (i-1);
}
int width(){ //Text mode screen size
int i=1;
int x = wherex(), y = wherey(); //Save current cursor position
gotoxy(1,1);
while(wherex()!=(i-1)){
gotoxy(++i,1);
}
gotoxy(x,y); //Return back to original position
return (i-1);
}
int maxx;
int maxy;
//For graph
void Line(float slope, float intercept){
float x,y;
for (x=-maxx/2;x<maxx;x++){
y=slope*x+intercept;
putpixel(maxx/2+x,maxy/2-y,9);
}
}
void Line(int x1,int y1, int x2, int y2)
{
line(maxx/2+x1,maxy/2-y1,maxx/2+x2,maxy/2-y2);
}
void Axes(){
line(maxx/2,0,maxx/2,maxy);
line(0,maxy/2,maxx,maxy/2);
}
void mark(){
int x,y,color;
color=WHITE;
setcolor(8);
for (x=scale;x/scale<=maxx/2;x+=scale){
Line(x,maxy/2,x,-maxy/2);
Line(-x,maxy/2,-x,-maxy/2);
}
for (y=scale;y/scale<=maxy/2;y+=scale){
Line(maxx/2,y,-maxx/2,y);
Line(maxx/2,-y,-maxx/2,-y);
}
setcolor(color);
for (x=scale;x/scale<=maxx/2;x+=scale){
Line(x,2,x,-2);
Line(-x,2,-x,-2);
}
for (y=scale;y/scale<=maxy/2;y+=scale){
Line(1,y,-1,y);
Line(1,-y,-1,-y);
}
}
int size = 0; //Size of the stack;
int top = -1;
fstream fl("FL.TXT",ios::out|ios::in|ios::trunc);
char *infix, *postfix, *Stack;
double *eval;
//Calculation
/*--------Function prototypes-------*/
void getinput();
int precedence(char ch); //Function to get precedence of operator
char Pop(); //Function to pop an element
char Topelement(); //Function to return top element without popping
void Push(char ch); //Function to push an element in the stack
int braces(char *s); //Function to match number of braces
void intopost();
int posteval(double x);
void sendfeedback();
int showfeedback();
/*----End of Function Prototypes----*/
/*--------Function Definitions-------*/
void getinput(){
char ch = 0;
size=0;
while(ch!='\r'){
fl.seekp(size, ios::beg);
ch = getch();
if(ch==' ')
continue;
else if(ch=='\b'){
if(size)
size--;
cout<<"\b \b";
}
else{
fl<<ch;
cout<<ch;
size++;
}
}
cout<<'\n';
infix = new char[2 * size];
int i = 0;
fl.seekg(0, ios::beg);
while(i < size-1){
fl>>ch;
infix[i++] = ch;
}
infix[i] = '\0';
postfix = new char [2 * size];
Stack = new char [2 * size];
}
int precedence(char ch){ //Function to get precedence of operator and/or the math functions
switch(ch){
case 'm': return 12; //fabs()
case 'f': return 11; //floor()
case 'n': return 10; //ln()
case 'l': return 9; //log()
case 't': return 8; //tan()
case 's': return 7; //sin()
case 'c': return 6; //cos()
case '^': return 5;
case '/': return 4;
case '*': return 3;
case '-': return 2;
case '+': return 1;
default : return 0;
}
}
char Pop(){ //Function to pop an element
char ret;
if(top!=-1){
ret = Stack[top--];
return ret;
}
else
return '#';
}
char Topelement(){ //Function to return top element without popping
char ch;
if(top!=-1)
ch = Stack[top];
else
ch = '#';
return ch;
}
void Push(char ch){ //Function to push an element in the stack
if (top!=size-1)
Stack[++top] = ch;
}
int braces(char *s){ //Function to match number of braces
int leftbr,rightbr;
leftbr = rightbr = 0;
for (int i = 0; s[i]; i++){
if (s[i]=='(')
leftbr++;
else if(s[i]==')')
rightbr++;
}
if(leftbr==rightbr)
return 0;
else if(leftbr<rightbr)
return 1;
else return -1;
}
void intopost(){
char ele,elem,st[2];
int prep,pre,popped,j=0,chk=1;
strcpy(postfix, " ");
while(chk!=0){
getinput();
chk = braces(infix);
if(chk!=0){
cout<<"Unbalanced no. of braces.\nExtra ";
cout<<(chk==1?"right braces":"left braces")<<'\n';
}
}
for (int i=0;infix[i]!='\0';i++){
//cout<<i; <--For debugging
if (precedence(infix[i])==0 && infix[i]!='(' && infix[i]!=')')
postfix[j++]=infix[i];
else if(infix[i]=='('){
elem = infix[i];
Push(elem);
}
else if(infix[i]==')'){
while((popped = Pop())!='(')
postfix[j++] = popped;
}
else {
elem = infix[i];
pre = precedence(elem); //Precedence of operator from infix
ele = Topelement();
prep = precedence(ele); //Precedence of operator from top of stack
if(pre > prep)
Push(elem);
else{
while(prep >= pre){
if(ele=='#')
break;
popped = Pop();
ele = Topelement();
postfix[j++] = popped;
postfix[j++] = ' ';
prep = precedence(ele);
}
Push(elem);
}
}
if (!(precedence(infix[i+1])==0 && infix[i+1]!='(' && infix[i+1]!=')'))
postfix[j++] = ' ';
}
while((popped = Pop()) != '#'){
postfix[j++] = ' ';
postfix[j++] = popped;
}
postfix[j] = '\0';
}
int posteval(double x){
eval = new double(strlen(postfix));
double num=0,result=0;
int v=0,dec=0,top=-1,i=0;
char ch;
int success=1;
while(postfix[i]!='\0' && i<strlen(postfix)){
v=dec=0;
num=0;
ch = postfix[i];
if (ch==' '){
i++;
continue;
}
if (isdigit(ch)){
while(ch!=' ' && postfix[i]!=0){
if(v)
dec++;
if(ch=='.')
v=1;
else
num = (ch-'0') + num*10;
ch = postfix[++i];
}
num/=pow(10,dec);
eval[++top] = num;
i++;
}
else if (ch=='x'||ch=='X'){
eval[++top] = x;
i++;
}
else if (ch=='e'||ch=='E'){
eval[++top] = M_E;
i++;
}
else if (ch=='p'||ch=='P'){
eval[++top] = M_PI;
i++;
}
else {
result = 0;
switch(ch){
case 'm':
result = fabs(eval[top]);
top++;
break;
case 'f':
result = floor(eval[top]);
top++;
break;
case 'n':
if(eval[top]>0){ //Error checking
result = log(eval[top]);
}
else
success = 0;
top++;
break;
case 'l':
if(eval[top]>0){
result = log10(eval[top]);
}
else
success = 0;
top++;
break;
case 't':
for (int i=0;(2*i+1)*M_PI_2<=scale*maxx;i++){
if (fabs(eval[top])+(0.1/scale)>=(2*i+1)*M_PI_2&&fabs(eval[top])-(0.1/scale)<=(2*i+1)*M_PI_2)
success = 0;
else if (scale*fabs(tan(eval[top]))>=10e3)
success = 0;
else
result = tan(eval[top]);
}
top++;
break;
case 's':
result = sin(eval[top]);
top++;
break;
case 'c':
result = cos(eval[top]);
top++;
break;
case '^':
if (eval[top-1]==0){
if (eval[top]<=0)
success = 0;
}
else if (eval[top-1]<0 && ceil(eval[top])!=eval[top])
success = 0;
else
result = pow(eval[top-1],eval[top]);
break;
case '/':
if (eval[top] == 0)
success = 0;
else
result = eval[top-1]/eval[top];
if (scale*fabs(result)>=10e3)
success = 0;
break;
case '*':
result = eval[top-1]*eval[top];
break;
case '+':
result = eval[top-1]+eval[top];
break;
case '-':
result = eval[top-1]-eval[top];
break;
}
if (scale*fabs(result)>=10e3)
success = 0;
eval[--top] = result;
++i;
}
}
return success;
}
class Feed{
char name[30];
char email[30];
char feed[500]; //User's message
time_t t;
public:
Feed(){
t = time(0);
}
void getd(){
cout<<"\n Enter Name : ";
gets(name);
cout<<" Enter Email : ";
gets(email);
cout<<" Enter Message (max 500 chars) :\n";
gets(feed);
}
void putd(){
int wt=width();
cout<<"\n Name : "<<name;
gotoxy(wt-30,wherey());
cout<<"At : ";
cout<<ctime(&t);
cout<<" Email : "<<email;
cout<<"\n Message : "<<feed;
}
};
int showfeedback(){
clr();
Feed post;
int i=1;
int n=4; //Max entries per page
fstream rev("FEED.DAT",ios::in|ios::binary);
if (!rev)
return 0;
clr();
while(!rev.eof()){
rev.read((char*)&post,sizeof(post));
if(rev.eof()){
if(i%n!=1){
cout<<"\n\n";
pause();
}
break;
}
post.putd();
cout<<"\n\n";
if(i%n==0){
pause();
clr();
}
i++;
}
rev.close();
return 1;
}
void sendfeedback(){
Feed post;
post.getd();
post.putd();
getch();
fstream rev("FEED.DAT",ios::out|ios::app|ios::binary);
rev.write((char*)&post,sizeof(post));
rev.close();
}
/*----End of Function Definitions----*/
void main(){
clrscr();
int choice=1;
int ht = height();
while(choice){
getimg();
cout<<"\t\t\t\t Menu\n";
cout<<" 1. Plot a function.\n";
cout<<" 2. Help and Documentation.\n";
cout<<" 3. View Submitted feedback.\n";
cout<<" 4. Send feedback/Request a feature.\n";
cout<<" 0. Exit\n\n";
cout<<" Enter your choice : ";
cin>>choice;
switch(choice){
case 1:
cout<<" Enter the function below :\n> ";
intopost();
cout<<" Enter the scale for the graph (between 5 and 100) : ";
cin>>scale;
if (scale>100 || scale <5)
scale = 50;//Default
cout<<" Enter the precision (1 to 50) :";
cout<<"\n [Warning : tan() will be slow with high precision, unless the CPU is fast]\n> ";
cin>>prec;
if(prec<1||prec>50)
prec = 20; //Default
pause();
int gd=DETECT,gm;
initgraph(&gd, &gm, "c:/tc/bgi ");
maxx = getmaxx();
maxy = getmaxy();
double x, y;
mark();
Axes();
for (x=(-maxx/2)/scale;x<maxx;x+=(1.0/((scale)*prec))){
if(!posteval(x))
continue;
y=scale*(eval[0]);
delete eval;
if (x*scale>=maxx/2)
break;
x=x*scale;
if (y>(maxy)*scale||y<(-maxy)*scale)
continue;
else
putpixel(maxx/2+x,maxy/2-y,LIGHTGREEN);
x=x/scale;
// delay(0);
}
getch();
break; //End of choice 1
case 2:
clr();
fstream doc("doc.txt", ios::in);
if(!doc){
cout<<" Documentation unavailable!";
getch();
}
else{
char ch;
while(!fl.eof()){
ch = doc.get();
if(doc.eof()){
cout<<"\n";
pause();
break;
}
if(ch=='\n' && wherey()==(ht-2)){
cout<<ch;
pause();
clr();
}
else{
if(wherey()==(ht-2)){
cout<<'\n';
pause();
clr();
}
cout<<ch;
}
}
}
doc.close();
break;
case 3:
clr();
if(!showfeedback()){
cout<<" No feedback history available!";
getch();
}
break;
case 4:
sendfeedback();
getch();
break;
}
closegraph();
restorecrtmode();
}
closegraph();
restorecrtmode();
fl.close();
}