ماشین حساب مهندسی به زبان c++

Sharif_

مدیر بازنشسته
برانامه خیلی ساده هستش
بهتر بود خودتون مینوشتید
باز من برنامه رو ضمیمه کردم
 

پیوست ها

  • Calculator c++.zip
    382 بایت · بازدیدها: 0

پیرجو

مدیر ارشد
مدیر کل سایت
مدیر ارشد
In C

In C

کد:
#include <math.h>
 #include <dos.h>
 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 #include <stdlib.h>
 #define   ENTER 13
 #define   BKSP  8
 const int MAX=20;
 const int ESC=27;
 struct key{
      int x;
      int y;
      char p;
    };
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  struct key keys[]={
  {31,11,'('},{36,11,')'},{41,11,'^'},{46,11,16},
  {31,13,'7'},{36,13,'8'},{41,13,'9'},{46,13,'/'},
  {31,15,'4'},{36,15,'5'},{41,15,'6'},{46,15,'*'},
  {31,17,'1'},{36,17,'2'},{41,17,'3'},{46,17,'-'},
  {31,19,'0'},{36,19,'.'},{41,19,'='},{46,19,'+'},
      };
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 int pr[]={17,0,16,12,13,14,8,9,10,4,5,6};
 int getkey(void )
 {
 int key,low,high;
 key=getch();
 return (key) ?  key : getch()+256;
 }
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 void press(int i)
 {
 window(keys[i].x,keys[i].y,keys[i].x+3,keys[i].y);
 textbackground(WHITE);
 textcolor(1);
 clrscr();
 cprintf(" %c",keys[i].p);
 delay(50);
 window(keys[i].x,keys[i].y,keys[i].x+3,keys[i].y);
 textbackground(BLACK);
 textcolor(WHITE);
 clrscr();
 cprintf(" %c",keys[i].p);
 }
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 void display(char *s)
 {
 int len=strlen(s);
 window(31,8,49,9);
 textbackground(WHITE);
 textcolor(BLACK);
 clrscr();
 if (!len) len++;
 gotoxy(19-len-1,2);
 if (strlen(s)==0) cprintf("0.");
 else if (!strchr(s,'.'))   cprintf("%s.",s);
 else cprintf("%s ",s);
 }
/******************************************************************/

 void ftoa(char *s,double x)
 {
 sprintf(s,"%f",x);
 int i=strlen(s)-1;
 while(s[i]=='0') s[i--]=0;
 if (s[i]=='.') s[i--]=0;
 if (!x) strcpy(s,"");
 }
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 void calculater()
 {
 int ch,op=0;
 double x=0,y=0,k=0;
 char str[50]={0};
 char str2[50]={0};
 display(str);
/******************************************************************/
 do {
 ch=getkey();
 switch (ch){
 case '.':
 case '0':
 case '1':
 case '2':
 case '3':
 case '4':
 case '5':
 case '6':
 case '7':
 case '8':
 case '9':{
 char st[]={ch,0};

 press(pr[ch-'.']);

 if(strlen(str) < 14 ){

 if (ch!='.')
 strcat(str,st);
 else
 if (!strchr(str,'.'))
 strcat(str,st);
 display(str);
 }
 }
 break;
 case  '+':
 case  '-':
       (ch=='+') ? press(19) : press(15);
       if (!k){
      x=atof(str);
      strcpy(str2,str);
        }else
   switch(op){
     case 1:x+=atof(str);
     break;
     case 2:x-=atof(str);
     break;
     case 3:x*=atof(str);
     break;
     case 4:x/=atof(str);
     break;
        }
         ftoa(str,x);
        display(str);
    op = (ch=='+') ? 1 : 2;
  k=1;
       strcpy(str,"");
     break;
      case  '*':
      case  '/':
       (ch=='*') ? press(11) : press(7);
       if (!k){
      x=atof(str);
      strcpy(str2,str);
        }else
   switch(op){
     case 1:x+=atof(str);
     break;
     case 2:x-=atof(str);
     break;
     case 3:x*=atof(str);
     break;
     case 4:{double y;
      y=atof(str);
      if (y) x/=atof(str);
     }
     break;
         }
        ftoa(str,x);
        display(str);
        op = (ch=='*') ? 3 : 4;
        k=1;
        strcpy(str,"");
       break;
/************************************************************/
     case '(':{
   double x;
   press(0);
/*   x=atof(str);
   if(x>0) {
   x=sqrt(x);
   ftoa(str,x);
   display(str);
   }*/
   }
   break;
/************************************************************/
      case ')': {
   double x;
   press(1);
       // x=atof(str);
       // x*=-1;
       // ftoa(str,x);
       // display(str);
  }
   break;
/************************************************************/
      case '=':
      case ENTER:
  press(18);
  if (!op) x=atof(str);
   else if ( k )    y=atof(str);
        else y=atof(str2);

        switch(op){
   case 1: x+=y;
         break;
   case 2: x-=y;
        break;
   case 3: x*=y;
         break;
   case 4: x/=y;
         break;
        }
        ftoa(str,x);
        display(str);
        k=0;
       break;
/******************************************************************/
      case BKSP:{
   press(3);
   int i=strlen(str);
   if (i){
   str[i-1]=0;
   display(str);
         }
  }
        break;
/******************************************************************/
     case '^':
       press(2);
       op=0;
       x=0,y=0,k=0;
       strcpy(str,"");
       strcpy(str2,"");
       display(str);
     continue;
    }
   }while(ch!=ESC);
  }
/******************************************************************/
void menu4(void);
void meno1(void)
{ _setcursortype(_NOCURSOR);
  textbackground(1);
  clrscr();
  textbackground(0);
  window(7,6,19,10);
  clrscr();
  textbackground(19);
  textcolor(15);
  window(6,5,18,9);
  clrscr();
  gotoxy(1,2);cprintf(" Esc   :Exit  ");
  gotoxy(1,3);cprintf(" Enter :Result ");
  gotoxy(1,4);cprintf(" Bksp  :Undo   ");
  window(31,8,52,21);
  textbackground(0) ;
  clrscr();

  window(30,7,50,20);
  textbackground(19);
  clrscr();
  window(31,8,49,9);
  textbackground(7);
  textcolor(YELLOW);
  clrscr();
  _setcursortype(_NOCURSOR);
  for (int i=0 ; i<MAX ;i++){
  window(keys[i].x,keys[i].y,keys[i].x+3,keys[i].y);
  textbackground(0);
  textcolor(15);
  clrscr();
  cprintf(" %c",keys[i].p);
  }
  }
  void main()
  {
  int i;
  meno1();
  calculater();
  _setcursortype(_NORMALCURSOR);
  clrscr();
  }
 

deniz jafari

عضو جدید
[FONT=&quot]in ham mashin hesab be zabane c++
[FONT=&quot] #include <math.h>[/FONT]
[FONT=&quot] #include <string.h>[/FONT]

[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]int main [/FONT][FONT=&quot]()[/FONT][FONT=&quot]
[FONT=&quot] {[/FONT]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]//declare variable[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]char expr[/FONT][FONT=&quot];[/FONT][FONT=&quot]


[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]double number1[/FONT][FONT=&quot], [/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]number2[/FONT][FONT=&quot],[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]total[/FONT][FONT=&quot];[/FONT][FONT=&quot]

[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]//output to the user[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]cout [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]"This is a mini calculator.This calculator:\n"[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]<< [/FONT][FONT=&quot]"Add +\nSubtract -\nMultiply *\nDivide /\nPower ^\n"[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]<< [/FONT][FONT=&quot]"\nInput your calculation"[/FONT][FONT=&quot]<<[/FONT][FONT=&quot]endl[/FONT][FONT=&quot];[/FONT][FONT=&quot]

[FONT=&quot] while ([/FONT][/FONT]
[FONT=&quot]1[/FONT][FONT=&quot])[/FONT][FONT=&quot]
[FONT=&quot] {[/FONT]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]cin [/FONT][FONT=&quot]>>[/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]>> [/FONT][FONT=&quot]expr [/FONT][FONT=&quot]>> [/FONT][FONT=&quot]number2[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] switch ([/FONT][/FONT]
[FONT=&quot]expr[/FONT][FONT=&quot])[/FONT][FONT=&quot]
[FONT=&quot] {[/FONT]
[FONT=&quot] case [/FONT][/FONT]
[FONT=&quot]'+'[/FONT][FONT=&quot]:[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]total [/FONT][FONT=&quot]= [/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]+ [/FONT][FONT=&quot]number2[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]cout [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]" + " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number2 [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]" = " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]total [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]endl[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] break;[/FONT]

[FONT=&quot] case [/FONT][/FONT]
[FONT=&quot]'*'[/FONT][FONT=&quot]:[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]total [/FONT][FONT=&quot]= [/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]* [/FONT][FONT=&quot]number2[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]cout [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]" * " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number2 [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]" = " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]total [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]endl[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] break;[/FONT]

[FONT=&quot] case [/FONT][/FONT]
[FONT=&quot]'/'[/FONT][FONT=&quot]:[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]total [/FONT][FONT=&quot]= [/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]/ [/FONT][FONT=&quot]number2[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]cout [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]" / " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number2 [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]" = " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]total [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]endl[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] break;[/FONT]

[FONT=&quot] case [/FONT][/FONT]
[FONT=&quot]'-'[/FONT][FONT=&quot]:[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]total [/FONT][FONT=&quot]= [/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]- [/FONT][FONT=&quot]number2[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]cout [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]" - " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number2 [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]" = " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]total [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]endl[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] break;[/FONT]

[FONT=&quot] case [/FONT][/FONT]
[FONT=&quot]'^'[/FONT][FONT=&quot]:[/FONT][FONT=&quot]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]cout [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number1 [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]" ^ " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]number2 [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]" = " [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]pow [/FONT][FONT=&quot]([/FONT][FONT=&quot]number1[/FONT][FONT=&quot], [/FONT][FONT=&quot]number2[/FONT][FONT=&quot]) <<[/FONT][FONT=&quot]endl[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] break;[/FONT]

[FONT=&quot] default:[/FONT]
[FONT=&quot] [/FONT][/FONT]
[FONT=&quot]cout [/FONT][FONT=&quot]<< [/FONT][FONT=&quot]"This is an invalid input" [/FONT][FONT=&quot]<<[/FONT][FONT=&quot]endl[/FONT][FONT=&quot];[/FONT][FONT=&quot]

[FONT=&quot] }[/FONT]
[FONT=&quot] }[/FONT]

[FONT=&quot] return [/FONT][/FONT]
[FONT=&quot]0[/FONT][FONT=&quot];[/FONT][FONT=&quot]
[FONT=&quot] } [/FONT][/FONT]
[FONT=&quot][/FONT]
 

آرش تنها

عضو جدید
ماشین حساب مهندسی به زبان c++

سلام خیلی ضروریه تو رو خدا کسی سورس این رو نداره؟؟؟؟؟؟؟؟؟
 

pa1234

عضو جدید
1. #include <math.h>
2. #include <dos.h>
3. #include <stdio.h>
4. #include <conio.h>
5. #include <string.h>
6. #include <stdlib.h>
7. #define ENTER 13
8. #define BKSP 8
9. const int MAX=20;
10. const int ESC=27;
11. struct key{
12. int x;
13. int y;
14. char p;
15. };
16. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
17. struct key keys[]={
18. {31,11,'('},{36,11,')'},{41,11,'^'},{46,11,16},
19. {31,13,'7'},{36,13,'8'},{41,13,'9'},{46,13,'/'},
20. {31,15,'4'},{36,15,'5'},{41,15,'6'},{46,15,'*'},
21. {31,17,'1'},{36,17,'2'},{41,17,'3'},{46,17,'-'},
22. {31,19,'0'},{36,19,'.'},{41,19,'='},{46,19,'+'},
23. };
24. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
25. int pr[]={17,0,16,12,13,14,8,9,10,4,5,6};
26. int getkey(void )
27. {
28. int key,low,high;
29. key=getch();
30. return (key) ? key : getch()+256;
31. }
32. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
33. void press(int i)
34. {
35. window(keys.x,keys.y,keys.x+3,keys.y);
36. textbackground(WHITE);
37. textcolor(1);
38. clrscr();
39. cprintf(" %c",keys.p);
40. delay(50);
41. window(keys.x,keys.y,keys.x+3,keys.y);
42. textbackground(BLACK);
43. textcolor(WHITE);
44. clrscr();
45. cprintf(" %c",keys.p);
46. }
47. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
48. void display(char *s)
49. {
50. int len=strlen(s);
51. window(31,8,49,9);
52. textbackground(WHITE);
53. textcolor(BLACK);
54. clrscr();
55. if (!len) len++;
56. gotoxy(19-len-1,2);
57. if (strlen(s)==0) cprintf("0.");
58. else if (!strchr(s,'.')) cprintf("%s.",s);
59. else cprintf("%s ",s);
60. }
61. /******************************************************************/
62.
63. void ftoa(char *s,double x)
64. {
65. sprintf(s,"%f",x);
66. int i=strlen(s)-1;
67. while(s=='0') s[i--]=0;
68. if (s=='.') s[i--]=0;
69. if (!x) strcpy(s,"");
70. }
71. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
72. void calculater()
73. {
74. int ch,op=0;
75. double x=0,y=0,k=0;
76. char str[50]={0};
77. char str2[50]={0};
78. display(str);
79. /******************************************************************/
80. do {
81. ch=getkey();
82. switch (ch){
83. case '.':
84. case '0':
85. case '1':
86. case '2':
87. case '3':
88. case '4':
89. case '5':
90. case '6':
91. case '7':
92. case '8':
93. case '9':{
94. char st[]={ch,0};
95.
96. press(pr[ch-'.']);
97.
98. if(strlen(str) < 14 ){
99.
100. if (ch!='.')
101. strcat(str,st);
102. else
103. if (!strchr(str,'.'))
104. strcat(str,st);
105. display(str);
106. }
107. }
108. break;
109. case '+':
110. case '-':
111. (ch=='+') ? press(19) : press(15);
112. if (!k){
113. x=atof(str);
114. strcpy(str2,str);
115. }else
116. switch(op){
117. case 1:x+=atof(str);
118. break;
119. case 2:x-=atof(str);
120. break;
121. case 3:x*=atof(str);
122. break;
123. case 4:x/=atof(str);
124. break;
125. }
126. ftoa(str,x);
127. display(str);
128. op = (ch=='+') ? 1 : 2;
129. k=1;
130. strcpy(str,"");
131. break;
132. case '*':
133. case '/':
134. (ch=='*') ? press(11) : press(7);
135. if (!k){
136. x=atof(str);
137. strcpy(str2,str);
138. }else
139. switch(op){
140. case 1:x+=atof(str);
141. break;
142. case 2:x-=atof(str);
143. break;
144. case 3:x*=atof(str);
145. break;
146. case 4:{double y;
147. y=atof(str);
148. if (y) x/=atof(str);
149. }
150. break;
151. }
152. ftoa(str,x);
153. display(str);
154. op = (ch=='*') ? 3 : 4;
155. k=1;
156. strcpy(str,"");
157. break;
158. /************************************************************/
159. case '(':{
160. double x;
161. press(0);
162. /* x=atof(str);
163. if(x>0) {
164. x=sqrt(x);
165. ftoa(str,x);
166. display(str);
167. }*/
168. }
169. break;
170. /************************************************************/
171. case ')': {
172. double x;
173. press(1);
174. // x=atof(str);
175. // x*=-1;
176. // ftoa(str,x);
177. // display(str);
178. }
179. break;
180. /************************************************************/
181. case '=':
182. case ENTER:
183. press(18);
184. if (!op) x=atof(str);
185. else if ( k ) y=atof(str);
186. else y=atof(str2);
187.
188. switch(op){
189. case 1: x+=y;
190. break;
191. case 2: x-=y;
192. break;
193. case 3: x*=y;
194. break;
195. case 4: x/=y;
196. break;
197. }
198. ftoa(str,x);
199. display(str);
200. k=0;
201. break;
202. /******************************************************************/
203. case BKSP:{
204. press(3);
205. int i=strlen(str);
206. if (i){
207. str[i-1]=0;
208. display(str);
209. }
210. }
211. break;
212. /******************************************************************/
213. case '^':
214. press(2);
215. op=0;
216. x=0,y=0,k=0;
217. strcpy(str,"");
218. strcpy(str2,"");
219. display(str);
220. continue;
221. }
222. }while(ch!=ESC);
223. }
224. /******************************************************************/
225. void menu4(void);
226. void meno1(void)
227. { _setcursortype(_NOCURSOR);
228. textbackground(1);
229. clrscr();
230. textbackground(0);
231. window(7,6,19,10);
232. clrscr();
233. textbackground(19);
234. textcolor(15);
235. window(6,5,18,9);
236. clrscr();
237. gotoxy(1,2);cprintf(" Esc :Exit ");
238. gotoxy(1,3);cprintf(" Enter :Result ");
239. gotoxy(1,4);cprintf(" Bksp :Undo ");
240. window(31,8,52,21);
241. textbackground(0) ;
242. clrscr();
243.
244. window(30,7,50,20);
245. textbackground(19);
246. clrscr();
247. window(31,8,49,9);
248. textbackground(7);
249. textcolor(YELLOW);
250. clrscr();
251. _setcursortype(_NOCURSOR);
252. for (int i=0 ; i<MAX ;i++){
253. window(keys.x,keys.y,keys.x+3,keys.y);
254. textbackground(0);
255. textcolor(15);
256. clrscr();
257. cprintf(" %c",keys.p);
258. }
259. }
260. void main()
261. {
262. int i;
263. meno1();
264. calculater();
265. _setcursortype(_NORMALCURSOR);
266. clrscr();
267. }

 

Similar threads

بالا