?? jisuanqi.txt
字號:
#include <dos.h> _/*DOS接口函數(shù)*/
#include <math.h> _/*數(shù)學函數(shù)的定義*/
#include <conio.h> _/*屏幕操作函數(shù)*/
#include <stdio.h> _/*I/O函數(shù)*/
#include <stdlib.h> _/*庫函數(shù)*/
#include <stdarg.h> _/*變量長度參數(shù)表*/
#include <graphics.h> _/*圖形函數(shù)*/
#include <string.h> _/*字符串函數(shù)*/
#include <ctype.h> _/*字符操作函數(shù)*/
#define UP 0x48 _/*光標上移鍵*/
#define DOWN 0x50 _/*光標下移鍵*/
#define LEFT 0x4b _/*光標左移鍵*/
#define RIGHT 0x4d _/*光標右移鍵*/
#define ENTER 0x0d _/*回車鍵*/
void *rar; _/*全局變量,保存光標圖象*/
struct palettetype palette; _/*使用調(diào)色板信息*/
int GraphDriver; _/* 圖形設備驅(qū)動*/
int GraphMode; _/* 圖形模式值*/
int ErrorCode; _/* 錯誤代碼*/
int MaxColors; _/* 可用顏色的最大數(shù)值*/
int MaxX, MaxY; _/* 屏幕的最大分辨率*/
double AspectRatio; _/* 屏幕的像素比*/
void drawboder(void); _/*畫邊框函數(shù)*/
void initialize(void); _/*初始化函數(shù)*/
void computer(void); _/*計算器計算函數(shù)*/
void changetextstyle(int font, int direction, int charsize); _/*改變文本樣式函數(shù)*/
void mwindow(char *header); _/*窗口函數(shù)*/
int specialkey(void) ; _/*獲取特殊鍵函數(shù)*/
int arrow(); _/*設置箭頭光標函數(shù)*/
_/*主函數(shù)*/
int main()
{
initialize();_/* 設置系統(tǒng)進入圖形模式 */
computer(); _/*運行計算器 */
closegraph();_/*系統(tǒng)關(guān)閉圖形模式返回文本模式*/
return(0); _/*結(jié)束程序*/
}
_/* 設置系統(tǒng)進入圖形模式 */
void initialize(void)
{
int xasp, yasp; _/* 用于讀x和y方向縱橫比*/
GraphDriver = DETECT; _/* 自動檢測顯示器*/
initgraph( &GraphDriver, &GraphMode, "" );
_/*初始化圖形系統(tǒng)*/
ErrorCode = graphresult(); _/*讀初始化結(jié)果*/
if( ErrorCode != grOk ) _/*如果初始化時出現(xiàn)錯誤*/
{
printf("Graphics System Error: %s\n",
grapherrormsg( ErrorCode ) ); _/*顯示錯誤代碼*/
exit( 1 ); _/*退出*/
}
getpalette( &palette ); _/* 讀面板信息*/
MaxColors = getmaxcolor() + 1; _/* 讀取顏色的最大值*/
MaxX = getmaxx(); _/* 讀屏幕尺寸 */
MaxY = getmaxy(); _/* 讀屏幕尺寸 */
getaspectratio( &xasp, &yasp ); _/* 拷貝縱橫比到變量中*/
AspectRatio = (double)xasp/(double)yasp;_/* 計算縱橫比值*/
}
_/*計算器函數(shù)*/
void computer(void)
{
struct viewporttype vp; _/*定義視口類型變量*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; _/*操作數(shù)和計算結(jié)果變量*/
char cnum[5],str2[20]={""},c,temp[20]={""};
char str1[]="1230.456+-789*/Qc=^%";_/* 定義字符串在按鈕圖形上顯示的符號 */
mwindow( "Calculator" ); _/* 顯示主窗口 */
color = 7; _/*設置灰顏色值*/
getviewsettings( &vp ); _/* 讀取當前窗口的大小*/
width=(vp.right+1)/10; _/* 設置按鈕寬度 */
height=(vp.bottom-10)/10 ; _/*設置按鈕高度 */
x = width /2; _/*設置x的坐標值*/
y = height/2; _/*設置y的坐標值*/
setfillstyle(SOLID_FILL, color+3);
bar( x+width*2, y, x+7*width, y+height );
_/*畫一個二維矩形條顯示運算數(shù)和結(jié)果*/
setcolor( color+3 ); _/*設置淡綠顏色邊框線*/
rectangle( x+width*2, y, x+7*width, y+height );
_/*畫一個矩形邊框線*/
setcolor(RED); _/*設置顏色為紅色*/
outtextxy(x+3*width,y+height/2,"0."); _/*輸出字符串"0."*/
x =2*width-width/2; _/*設置x的坐標值*/
y =2*height+height/2; _/*設置y的坐標值*/
for( j=0 ; j<4 ; ++j ) _/*畫按鈕*/
{
for( i=0 ; i<5 ; ++i )
{
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x+width, y+height ); _/*畫一個矩形條*/
rectangle( x, y, x+width, y+height );
sprintf(str2,"%c",str1[j*5+i]);
_/*將字符保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =x+width+ (width / 2) ; _/*移動列坐標*/
}
y +=(height/2)*3; _/* 移動行坐標*/
x =2*width-width/2; _/*復位列坐標*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y); _/*移動光標到x,y位置*/
arrow(); _/*顯示光標*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); _/*設置str2為空串*/
while((v=specialkey())!=45) _/*當壓下Alt+x鍵結(jié)束程序,否則執(zhí)行下面的循環(huán)*/
{
while((v=specialkey())!=ENTER) _/*當壓下鍵不是回車時*/
{
putimage(x,y,rar,XOR_PUT); _/*顯示光標圖象*/
if(v==RIGHT) _/*右移箭頭時新位置計算*/
if(x>=x0+6*width)
_/*如果右移,移到尾,則移動到最左邊字符位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} _/*否則,右移到下一個字符位置*/
if(v==LEFT) _/*左移箭頭時新位置計算*/
if(x<=x0)
{
x=x0+6*width;
m=4;
} _/*如果移到頭,再左移,則移動到最右邊字符位置*/
else
{
x=x-width-width/2;
m--;
} _/*否則,左移到前一個字符位置*/
if(v==UP) _/*上移箭頭時新位置計算*/
if(y<=y0)
{
y=y0+4*height+height/2;
n=3;
} _/*如果移到頭,再上移,則移動到最下邊字符位置*/
else
{
y=y-height-height/2;
n--;
} _/*否則,移到上邊一個字符位置*/
if(v==DOWN) _/*下移箭頭時新位置計算*/
if(y>=7*height)
{
y=y0;
n=0;
} _/*如果移到尾,再下移,則移動到最上邊字符位置*/
else
{
y=y+height+height/2;
n++;
} _/*否則,移到下邊一個字符位置*/
putimage(x,y,rar,XOR_PUT); _/*在新的位置顯示光標箭頭*/
}
c=str1[n*5+m]; _/*將字符保存到變量c中*/
if(isdigit(c)||c=='.') _/*判斷是否是數(shù)字或小數(shù)點*/
{
if(flag==-1) _/*如果標志為-1,表明為負數(shù)*/
{
strcpy(str2,"-"); _/*將負號連接到字符串中*/
flag=1;
} _/*將標志值恢復為1*/
sprintf(temp,"%c",c); _/*將字符保存到字符串變量temp中*/
strcat(str2,temp); _/*將temp中的字符串連接到str2中*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,str2); _/*顯示字符串*/
}
if(c=='+')
{
num1=atof(str2); _/*將第一個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,""); _/*將str2清空*/
act=1; _/*做計算加法標志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); _/*顯示字符串*/
}
if(c=='-')
{
if(strcmp(str2,"")==0) _/*如果str2為空,說明是負號,而不是減號*/
flag=-1; _/*設置負數(shù)標志*/
else
{
num1=atof(str2); _/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,""); _/*將str2清空*/
act=2; _/*做計算減法標志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2); _/*畫矩形*/
outtextxy(5*width,height,"0."); _/*顯示字符串*/
}
}
if(c=='*')
{
num1=atof(str2); _/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,""); _/*將str2清空*/
act=3; _/*做計算乘法標志值*/
setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); _/*顯示字符串*/
}
if(c=='/')
{
num1=atof(str2); _/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,""); _/*將str2清空*/
act=4; _/*做計算除法標志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); _/*顯示字符串*/
}
if(c=='^')
{
num1=atof(str2); _/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,""); _/*將str2清空*/
act=5; _/*做計算乘方標志值*/
setfillstyle(SOLID_FILL,color+3); _/*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); _/*畫矩形*/
outtextxy(5*width,height,"0."); _/*顯示字符串*/
}
if(c=='%')
{
num1=atof(str2); _/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
strcpy(str2,""); _/*將str2清空*/
act=6; _/*做計算模運算乘方標志值*/
setfillstyle(SOLID_FILL,color+3); _/*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); _/*畫矩形*/
outtextxy(5*width,height,"0."); _/*顯示字符串*/
}
if(c=='=')
{
num2=atof(str2); _/*將第二個操作數(shù)轉(zhuǎn)換為浮點數(shù)*/
switch(act) _/*根據(jù)運算符號計算*/
{
case 1:result=num1+num2;break; _/*做加法*/
case 2:result=num1-num2;break; _/*做減法*/
case 3:result=num1*num2;break; _/*做乘法*/
case 4:result=num1/num2;break; _/*做除法*/
case 5:result=pow(num1,num2);break; _/*做x的y次方*/
case 6:result=fmod(num1,num2);break; _/*做模運算*/
}
setfillstyle(SOLID_FILL,color+3); _/*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); _/*覆蓋結(jié)果區(qū)*/
sprintf(temp,"%f",result); _/*將結(jié)果保存到temp中*/
outtextxy(5*width,height,temp); _/*顯示結(jié)果*/
}
if(c=='c')
{
num1=0; _/*將兩個操作數(shù)復位0,符號標志為1*/
num2=0;
flag=1;
strcpy(str2,""); _/*將str2清空*/
setfillstyle(SOLID_FILL,color+3); _/*設置用淡綠色實體填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); _/*覆蓋結(jié)果區(qū)*/
outtextxy(5*width,height,"0."); _/*顯示字符串*/
}
if(c=='Q')exit(0); _/*如果選擇了q回車,結(jié)束計算程序*/
}
putimage(x,y,rar,XOR_PUT); _/*在退出之前消去光標箭頭*/
return; _/*返回*/
}
_/*窗口函數(shù)*/
void mwindow( char *header )
{
int height;
cleardevice(); _/* 清除圖形屏幕 */
setcolor( MaxColors - 1 ); _/* 設置當前顏色為白色*/
setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); _/* 設置視口大小 */
height = textheight( "H" ); _/* 讀取基本文本大小 */
settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );_/*設置文本樣式*/
settextjustify( CENTER_TEXT, TOP_TEXT );_/*設置字符排列方式*/
outtextxy( MaxX/4, 2, header ); _/*輸出標題*/
setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); _/*設置視口大小*/
drawboder(); _/*畫邊框*/
}
void drawboder(void) _/*畫邊框*/
{
struct viewporttype vp; _/*定義視口類型變量*/
setcolor( MaxColors - 1 ); _/*設置當前顏色為白色 */
setlinestyle( SOLID_LINE, 0, NORM_WIDTH );_/*設置畫線方式*/
getviewsettings( &vp );_/*將當前視口信息裝入vp所指的結(jié)構(gòu)中*/
rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); _/*畫矩形邊框*/
}
_/*設計鼠標圖形函數(shù)*/
int arrow()
{
int size;
int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; _/*定義多邊形坐標*/
setfillstyle(SOLID_FILL,2); _/*設置填充模式*/
fillpoly(8,raw); _/*畫出一光標箭頭*/
size=imagesize(4,4,16,16); _/*測試圖象大小*/
rar=malloc(size); _/*分配內(nèi)存區(qū)域*/
getimage(4,4,16,16,rar); _/*存放光標箭頭圖象*/
putimage(4,4,rar,XOR_PUT); _/*消去光標箭頭圖象*/
return 0;
}
_/*按鍵函數(shù)*/
int specialkey(void)
{
int key;
while(bioskey(1)==0); _/*等待鍵盤輸入*/
key=bioskey(0); _/*鍵盤輸入*/
key=key&0xff? key&0xff:key>>8; _/*只取特殊鍵的掃描值,其余為0*/
return(key); _/*返回鍵值*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -