ماشین حساب
جمعه 29 شهریور 1392 7:50 AM
ماشین حساب گرافیکی که جمع و ضرب و تفریق اعداد تا 128 رقم رو میتونه خیلی سریع حساب کنه ( ماشین حساب ویندوز تا 32 رقم اجازه میده ) .
کد این برنامه رو به صورت خیلی ساده و با استفاده از آرایه ها نوشتم. از تابع ضرب اون برای حساب کردن فاکتوریل اعداد بزرگ نیز میشه استفاده کرد. و برای قسمت گرافیکی آن به ناچار از Turbo C++ 3.0 استفاده کردم.
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <stdlib.h>
#include <dos.h>
#define ESC 0x1b
#define EXIT 120
void draw(void);
void getArray( int[],char& );
void printArray( int[] );
void reverseArray( int[] );
void plus(int[], int[], int[]);
void minus(int[], int[], int[]);
void multiple(int[], int[], int[] );
void initgraph();
void animate( char );
void wellcome(void);
void error(void);
int main(void)
{
char func='n';
int value1[128] ,
value2[128] ,
result[128] = {0};
initgraph();
wellcome();
draw();
getArray( value1, func );
reverseArray( value1 );
getArray( value2, func );
reverseArray( value2 );
switch( func )
{
case '+':
plus(value1, value2, result);
break;
case '-':
minus(value1, value2, result);
break;
case '*':
multiple(value1, value2, result);
break;
default: cout << "Oh sorry! You made a mistake.";
}
printArray ( result );
if(getch() == ESC )
{
cleardevice();
main();
}
closegraph();
return 0;
}
/*=================Draw===========*/
void draw (void)
{
int x = getmaxx();
int y = getmaxy();
settextstyle(2,HORIZ_DIR,4);
setfillstyle(SOLID_FILL, LIGHTBLUE);
bar(x / 2 - 210, 50, x / 2 + 210, 270);
setfillstyle(SOLID_FILL, WHITE);
bar(x / 2 - 200, 60, x / 2 + 200, 155);
setcolor(BLACK);
line(125,120,x / 2 + 190, 120);
line(125,125,x / 2 + 190, 125);
setfillstyle(SOLID_FILL, BLUE);
setcolor(BLUE);
int Y = 165;
for ( int i = 0; i < 3; i++)
{
int X = 220;
for ( int j = 0; j < 4; j++)
{
bar3d(X,Y,X + 25,Y + 25,3,1);
X += 35;
if ( j == 2 )
X += 15;
}
Y += 35;
}
bar3d(375,165,410,260,3,1);
setcolor(WHITE);
outtextxy(230,172,"1");
outtextxy(265,172,"2");
outtextxy(300,172,"3");
outtextxy(350,172,"+");
outtextxy(230,207,"4");
outtextxy(265,207,"5");
outtextxy(300,207,"6");
outtextxy(350,207,"-");
outtextxy(390,207,"=");
outtextxy(230,242,"7");
outtextxy(265,242,"8");
outtextxy(300,242,"9");
outtextxy(350,242,"x");
}
/*======= initgraph ===============*/
void initgraph()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "C:\\tc\\bgi\\");
}
/*=====================getArray===============*/
void getArray( int value1[], char& func )
{
int xpos = 115, ypos = 65;
if( func != 'n' ) ypos += 3 * textheight("1");
for ( int i = 0; i<128; i++ )
value1[i] = -1;
int digit = 0;
char c;
char* cc;
while(digit < 128)
{
setcolor(BLUE);
c = getch();
animate(c);
if ( c == '+' )
{
func = '+';
outtextxy(125,65 + 2 * textheight("1"),"+");
return;
}
else if ( c == '-' )
{
func = '-';
outtextxy(125,65 + 2 * textheight("1"),"-");
return;
}
else if ( c == '*' )
{
func = '*';
outtextxy(125,65 + 2 * textheight("1"),"x");
return;
}
else if ( (int)c == 13 )
{
return;
}
else if ( c == ESC )
{
cleardevice();
main();
}
else if ( c == EXIT )
{
exit(0);
}
else
value1[digit++] = c - 48;
cc[0] = c;
cc[1] = '\0';
if( digit == 65 )
{
xpos = 115;
ypos += textheight("1");
}
xpos += textwidth("1");
outtextxy(xpos,ypos,cc);
}
return;
}
/*===================printArray=======================*/
void printArray( int value[])
{
int digit = 0, xpos = 115, ypos = 130;
char* cc;
cc[1] = '\0';
char c;
for ( digit = 0; digit < 128 && value[digit] == 0; digit++)
{
}
if ( digit == 127 )
outtextxy(115 + textwidth("1"),130,"0");
for(int count = 0 ; digit < 128; digit++,count++)
{
c = value[digit] + 48;
cc[0] = c;
xpos += textwidth("1");
if ( count == 65 )
{
ypos += textheight("1");
xpos = 121;
}
outtextxy(xpos,ypos,cc);
}
return;
}
/*===================plus=============================*/
void plus(int value1[], int value2[], int result[])
{
for ( int i = 127; i >= 0 && value1[i] >= 0; i-- )
result[i] += value1[i];
for (i = 127; i >= 0 && value2[i] >= 0; i-- )
result[i] += value2[i];
for ( i = 127; i >= 0; i-- )
{
if ( (result[1] / 10) + result[0] > 9 )
error();
if ( result[i] > 9 )
{
result[i-1] += result[i] / 10;
result[i] %= 10;
}
}
}
/*====================reverseArray===============*/
void reverseArray( int a[] )
{
int size = 127, temp;
for ( int i = 0; i < 128; i++ )
{
if ( *(a + i) == -1 )
{
size = i - 1;
break;
}
else
continue;
}
for ( i = 0; i <= size; i++ )
{
temp = *(a + size - i);
*(a + size - i) = *(a + 127 - i);
*(a + 127 - i ) = temp;
}
return;
}
/*========================minus=======================*/
void minus(int value1[], int value2[], int result[])
{
int big;
for ( int i = 0; i < 128; i++ )
{
if ( value2[i] > value1[i] )
{
big = 2;
break;
}
if ( value1[i] > value2[i] )
{
big = 1;
break;
}
}
if ( big == 1 )
{
for ( i = 127; i >= 0 && value1[i] >= 0; i-- )
result[i] += value1[i];
for ( i = 127; i >= 0 && value2[i] >= 0; i-- )
result[i] -= value2[i];
}
if ( big == 2 )
{
for ( i = 127; i >= 0 && value2[i] >= 0; i-- )
result[i] += value2[i];
for ( i = 127; i >= 0 && value1[i] >= 0; i-- )
result[i] -= value1[i];
outtextxy(115,130,"-");
}
for ( i = 127; i >= 0; i-- )
{
if ( (result[1] / 10) + result[0] > 9 )
error();
if ( result[i] < 0 )
{
result[i] += 10;
result[i-1]--;
}
}
return;
}
/*===========================multiple=========================*/
void multiple( int value1[], int value2[], int result[] )
{
int size1 = 127, size2 = 127;
for ( int i = 127; i >= 0; i-- )
{
if ( *(value1 + i) == -1 )
{
size1 = 127 - i;
break;
}
else
continue;
}
for (i = 127; i >= 0; i-- )
{
if ( *(value2 + i) == -1 )
{
size2 = 127 - i;
break;
}
else
continue;
}
if( size1 + size2 > 129 )
error();
/* cal */
for (int j = 0; j < size2; j++)
{
for( i = 0; i < size1; i++)
*(result + 127 - i - j) += *(value1 + 127 - i ) * *(value2 + 127 - j);
}
for ( i = 127; i >= 0; i-- )
{
if ( (result[1] / 10) + result[0] > 9 )
error();
if ( result[i] > 9 )
{
result[i-1] += result[i] / 10;
result[i] %= 10;
}
}
}
/*=========== animate============*/
void animate( char ch )
{
const int x[12] = {220,255,290,220,255,290,220,255,290,340,340,340};
const int y[12] = {165,165,165,200,200,200,235,235,235,165,200,235};
char* cc;
int k;
cc[0] = ch;
cc[1] = '\0';
if ( ch == '*' ) cc[0] = 'x';
switch(ch)
{
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
k = int(ch) - 49;
break;
case '+' :
k = 9;
break;
case '-' :
k = 10;
break;
case '*' :
k = 11;
break;
default : return;
}
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar(x[k], y[k] - 5, x[k] + 30, y[k] + 25);
setfillstyle(SOLID_FILL,BLUE);
bar(x[k] + 3, y[k] - 2, x[k] + 28, y[k] + 23);
delay(50);
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar(x[k], y[k] - 5, x[k] + 30, y[k] + 25);
setfillstyle(SOLID_FILL,BLUE);
setcolor(BLUE);
bar3d(x[k], y[k], x[k] + 25, y[k] + 25,3,1);
setcolor(WHITE);
outtextxy(x[k] + 10, y[k] + 7,cc);
setcolor(BLUE);
}
/*======= well come ==========*/
void wellcome ( void )
{
cout<<"\n\n\n";
cout<<"\t\tWELCOME TO THE 128 DIGITS CACLULATER "<<endl;
cout<<"\n\n\n\n";
cout<<"FEW POINTS TO USE THIS CALCULATER. "<<endl<<endl;
cout<<"\n1. FIRST YOU NEED TO ENTER AN INTEGERS, THE OPERATOR THEN SECOND INTEGER. "<<endl;
cout<<"\n2. DO NOT ENTER CHARECTER INSTEAD OF INTEGER AS IT MAY CAUSE THE PROGRAM TO STOP RESPONDING."<<endl;
cout<<"\n3. TO CLEAR THE CALCULATOR AND USE AGAIN PRESS ESC"<<endl;
cout<<"\n4. TO EXIT THE PROGRAM PRESS X "<<endl<<endl;
cout<<"\n\n\n\n\n\t\t\twrite by: hossein azarpevand ";
sleep(7);
cleardevice();
}
/*========Error===================*/
void error( void )
{
setcolor(RED);
outtextxy(125,130,"Error.Result is out of range");
getch();
exit(1);
}
Seyyed.Reza.Hashemian@Gmail.Com
دوستانی که سوالی دارند یا مایل به تماس هستند می توانند از اطلاعات بالا استفاده نمایند.
اگر هم تایپکی زدید و احتیاج به پاسخگویی سریع داشتید اطلاع دهید