943,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 378
  • C++ RSS
Jan 27th, 2010
0

URGENT: Please help test scientific calculator and report bugs

Expand Post »
I have to give this in around 10 hours and i still have lots to do.
Please report any bugs;

Its a scientific calculator, also supports sin, cos,tan and log
Take care to put the entire expression under brackets

For example, a valid input will be:-

(5+4*-3+sin(43+7)+cos(90/2))

I've attached the .exe as well.

Please try as many different combinations as you can.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. class expsolver
  7. {
  8. char inputstring[100],str[100],exp[100], prec[10],subexp[20];
  9. int pos[100],posl, no[5], neighbours[2], step,pass,neg, no_ops;
  10. double t1,t2,ta;
  11.  
  12. public:
  13. expsolver()
  14. {
  15. strcpy(prec,"^/*+-");
  16. step=0;
  17. pass=1;
  18. neg=0;
  19. no_ops=0;
  20. display();
  21. }
  22. void display();
  23. void manager();
  24. void master();
  25. void neighbourset(int);
  26. void pos_setter();
  27. void copier(char*,char*,int ,int);
  28. void convertno(char*,double &t);
  29. void operation(double,double,int);
  30. void convertalpha(double);
  31. void replacer(char*,char*,int,int);
  32. void endcopier(char*,char*,int);
  33. void negbugfix();
  34. };
  35.  
  36. void expsolver::display()
  37. {
  38.  
  39. char ch;
  40. do
  41. {
  42. clrscr();
  43. cout<<"Enter the expression to be evaluated\n";
  44. gets(inputstring);
  45. strcpy(str,inputstring);
  46. manager();
  47. cout<<"\n\nTry again? :";
  48. cin>>ch;
  49.  
  50. }
  51. while (ch!='n');
  52. }
  53.  
  54. void expsolver::manager()
  55. {
  56. int i,lcount=0,rcount=0,li=0,ri=0;
  57. for (i=0; i<strlen(str); i++)
  58. {
  59. if(str[i]=='(')
  60. lcount+=1;
  61. if(str[i]==')')
  62. rcount+=1;
  63. }
  64. for(int j=0;j<lcount;j++)
  65. {
  66. if (lcount==rcount)
  67. {
  68. for (i=0; i<strlen(str); i++)
  69. {
  70. if (str[i]=='(')
  71. {
  72. li=i;
  73. }
  74. if (str[i]==')')
  75. {
  76. ri=i;
  77. break;
  78. }
  79. }
  80. }
  81. copier(exp,str,li+1,ri-1);
  82. cout<<"\nPass "<<pass;
  83. master();
  84. replacer(str,exp,li,ri);
  85. cout<<"\nAfter the operation, result is "<<str<<endl;
  86. pass++;
  87. }
  88. cout<<"\nResult -"<<str;
  89. }
  90. void expsolver::master()
  91. {
  92. for (int i=0; i<4; i++)
  93. {
  94. no[i]=0;
  95. for (int j=1; j<(strlen(exp)); j++)
  96. if (exp[j]==prec[i])
  97. {
  98. no[i]+=1;
  99. no_ops++;
  100. }
  101. }
  102.  
  103. if(no_ops==0)
  104. {
  105. convertno(exp,ta);
  106. convertalpha(ta);
  107. replacer(exp,subexp,0,strlen(exp));
  108. }
  109.  
  110. for(int i=0; i<4; i++)
  111. {
  112. if(i==3)
  113. negbugfix();
  114. for (int j=0; j<no[i]; j++)
  115. {
  116. pos_setter();
  117.  
  118. if(pos[0]!=32767)
  119. for (int k=1; k<(strlen(exp)); k++)
  120. {
  121. if (exp[k]==prec[i])
  122. {
  123. neighbourset(k);
  124. char *a =new char [k-neighbours[0]];
  125. char *b =new char [neighbours[1]-k];
  126. copier (a,exp,neighbours[0],(k-1));
  127. copier (b,exp,(k+1),neighbours[1]);
  128. convertno (a,t1);
  129. convertno (b,t2);
  130. operation (t1,t2,i);
  131. convertalpha (ta);
  132. replacer (exp,subexp,neighbours[0],neighbours[1]);
  133. cout<<"\nStep "<<++step<<"- Do "<<prec[i]<<endl;
  134. puts(exp);
  135. break;
  136. }
  137. }
  138.  
  139. }
  140.  
  141. }
  142. }
  143. void expsolver::pos_setter()
  144. {
  145. unsigned int i=1,j=0;
  146. for (;exp[i]!='\0';i++)
  147. {
  148. if((exp[i]=='^')||(exp[i]=='/')||(exp[i]=='*')||(exp[i]=='+')||((exp[i]=='-')&&(neg)))
  149. {
  150. pos[j]=i;
  151. j++;
  152. }
  153. pos[j]=32767;
  154. posl=j;
  155. }
  156. }
  157. void expsolver::neighbourset (int k)
  158. {
  159. for (int i=0; pos[i]!=32767; i++)
  160. {
  161. if (k==pos[i])
  162. {
  163. neighbours[0]=pos[i-1]+1;
  164. neighbours[1]=pos[i+1]-1;
  165. if (i==0)
  166. neighbours[0]=0;
  167. if(i==posl-1)
  168. neighbours[1]=((strlen(exp))-1);
  169. }
  170. }
  171. }
  172. void expsolver::copier(char* a,char* b,int m,int n)
  173. {
  174. int i,j;
  175. for(i=m,j=0;i<=n ;i++,j++)
  176. {a[j]=b[i];
  177. }
  178. a[j]='\0';
  179. }
  180. void expsolver::endcopier(char* x,char* y,int start)
  181. {
  182. int i,j;
  183. for (i=start,j=0;x[i]!='\0' ;i++,j++ )
  184. y[j]=x[i];
  185. y[j]='\0';
  186. }
  187.  
  188. void expsolver::convertno(char* a,double &t)
  189. {
  190. char input[20], *endptr;
  191. double inputval;
  192.  
  193. if((strncmpi(a,"sin",3)==0)||(strncmpi(a,"cos",3)==0)||(strncmpi(a,"tan",3)==0)||(strncmpi(a,"log",3)==0))
  194. {
  195. if(strncmpi(a,"sin",3)==0)
  196. {
  197. endcopier(a,input,3);
  198. inputval=strtod(input,&endptr);
  199. t=sin(inputval*(3.1415926535/180));
  200. }
  201. if(strncmpi(a,"cos",3)==0)
  202. {
  203. endcopier(a,input,3);
  204. inputval=strtod(input,&endptr);
  205. t=cos(inputval*(3.1415926535/180));
  206. }
  207. if(strncmpi(a,"tan",3)==0)
  208. {
  209. endcopier(a,input,3);
  210. inputval=strtod(input,&endptr);
  211. t=tan(inputval*(3.1415926535/180));
  212. }
  213. if(strncmpi(a,"log",3)==0)
  214. {
  215. endcopier(a,input,3);
  216. inputval=strtod(input,&endptr);
  217. t=log(inputval);
  218. }
  219. }
  220. else
  221. t=strtod(a,&endptr);
  222. }
  223. void expsolver::operation(double x,double y,int i)
  224. {
  225. if (i==0)
  226. ta=pow(x,y);
  227. if (i==1)
  228. ta=x/y;
  229. if (i==2)
  230. ta=x*y;
  231. if (i==3)
  232. ta=x+y;
  233. if (i==4)
  234. ta=x-y;
  235. }
  236. void expsolver::replacer(char* a,char* b,int m,int n)
  237. {
  238. int j=0,k,i,l,size=(strlen(a)-n);
  239. char *sub=new char[size];
  240.  
  241. for(i=n+1; a[i]!='\0' ;i++)
  242. {
  243. sub[j]=a[i];
  244. j++;
  245. }
  246. sub[j]='\0';
  247.  
  248. for (i=m,j=0; b[j]!='\0' ; i++,j++,k=i)
  249. a[i]=b[j];
  250.  
  251. for (i=k,j=0; sub[j]!='\0'; i++,j++)
  252. a[i]=sub[j];
  253. a[i]='\0';
  254. }
  255. void expsolver::convertalpha(double x)
  256. {
  257. sprintf ( subexp, "%.2f", x );
  258. }
  259. void expsolver::negbugfix ()
  260. {
  261. for(int i=1; i<strlen(exp); i++)
  262. if(exp[i]=='-')
  263. {
  264. replacer(exp,"+",i,i-1);
  265. i++;
  266. puts(exp);
  267. no[3]++;
  268. }
  269. }
  270. void main()
  271. {
  272. expsolver obj;
  273. getch();
  274. }
Attached Files
File Type: zip ExpSolver.zip (68.7 KB, 4 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Chetan_ is offline Offline
7 posts
since Nov 2009
Jan 27th, 2010
0
Re: URGENT: Please help test scientific calculator and report bugs
Nothing urgent about it for any of us. You should have started earlier if you think 10 more hours isn't enough.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 27th, 2010
0
Re: URGENT: Please help test scientific calculator and report bugs
> You should have started earlier if you think 10 more hours isn't enough.
Well it's down to 5 hours now

As a C++ program, I would score this at 1/10 no matter how bug-free you thought it was.

It's basically a C program with a rudimentary class in it.

> gets(inputstring);
This earns you 0/10.
gets() is totally unsafe; there is no reason at all for being anywhere near this function.

All your allocated memory (new) gets leaked.
Team Colleague
Reputation Points: 5862
Solved Threads: 949
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 27th, 2010
0
Re: URGENT: Please help test scientific calculator and report bugs
Dude i'm a beginner

The rudimentary class is there because its still a WIP.
i'm neither an expert, nor do i claim to be.

anyways i have one more day now.
So please.
Quote ...
It's basically a C program with a rudimentary class in it.
OK, so what do i do about that;
Quote ...
gets() is totally unsafe; there is no reason at all for being anywhere near this function.

All your allocated memory (new) gets leaked.
thanks for that
they tell us nothing in class
Last edited by Chetan_; Jan 27th, 2010 at 7:45 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Chetan_ is offline Offline
7 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to make buttons navigate through tabs
Next Thread in C++ Forum Timeline: Recursively find area under curve





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC