當前位置:首頁 » 編程語言 » c語言附加題計算器怎麼做
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言附加題計算器怎麼做

發布時間: 2022-06-20 19:57:22

⑴ 怎麼用C語言編寫一個計算器程序

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <algorithm>

// 堆棧的數組實現,數組的大小固定。
template<class T>
class stack
{
private:
T *s; // 數組的首地址(棧底)
size_t N; // 指向棧頂第一個空閑塊
const size_t size; // 堆棧的大小,固定不變

public:
stack(size_t n) : size(n)
{
s = new T[n]; // 可能拋出異常
N = 0; // 設置棧頂指針
}

~stack()
{
delete [] s;
}

bool empty() const
{
return N == 0;
}

bool full() const
{
return N == size;
}

void push(const T &object)
{
if (full())
{
throw "error: stack is full !";
}

s[N++] = object;
}

T pop()
{
if (empty())
{
throw "error: stack is empty !";
}

return s[--N];
}

T peek() const
{
if (empty())
{
throw "error: stack is empty !";
}

return s[N-1];
}

friend std::ostream& operator<<(std::ostream& os, const stack<T> &stk)
{
for (size_t i = 0; i < stk.N; i++)
{
std::cout << stk.s[i] << " ";
}

return os;
}
};

// 堆棧的鏈表實現
template<class T>
class STACK
{
private:
typedef struct node
{
T item;
node *next;
node(T x, node *t = NULL) : item(x), next(t) {}
}*link;

link head; // 指向棧頂第一個有效對象

public:
STACK(size_t n)
{
head = NULL;
}

~STACK() // 也可以用pop的方法刪除,但效率低
{
link t = head;

while (t != NULL)
{
link d = t;
t = t->next;
delete d;
}
}

bool empty() const
{
return head == NULL;
}

bool full() const
{
return false;
}

void push(const T &object)
{
head = new node(object, head);
}

T pop()
{
if (empty())
{
throw "error: stack is empty !";
}

T v = head->item;
link t = head->next;
delete head;
head = t;
return v;
}

T peek() const
{
if (empty())
{
throw "error: stack is empty !";
}

return head->item;
}

friend std::ostream& operator<<(std::ostream& os, const STACK<T> &stk)
{
for (link t = stk.head; t != NULL; t = t->next)
{
std::cout << t->item << " ";
}

return os;
}
};

// 中綴表達式轉化為後綴表達式,僅支持加減乘除運算、操作數為1位十進制非負整數的表達式。
char* infix2postfix(const char *infix, char *postfix)
{
const size_t N = strlen(infix);

if (N == 0 || postfix == NULL)
{
return postfix;
}

stack<char> opcode(N); // 堆棧存放的是操作符

for (size_t i = 0; i < N; i++)
{
switch (infix[i])
{
case '(': // 直接忽略左括弧
break;

case ')': // 彈出操作符
*postfix++ = opcode.pop();
*postfix++ = ' ';
break;

case '+':
case '-':
case '*':
case '/':
opcode.push(infix[i]); // 壓入操作符
break;

default:
if (isdigit(infix[i])) // 如果是數字,直接輸出
{
*postfix++ = infix[i];
*postfix++ = ' ';
}
}
}

return postfix;
}

// 後綴表達式轉化為中綴表達式,僅支持加減乘除運算、操作數為1位十進制非負整數的表達式。
char* postfix2infix(const char *postfix, char *infix)
{
const size_t N = strlen(postfix);

if (N == 0 || infix == NULL)
{
return infix;
}

*infix = '\0'; // 初始化輸出字元串為空串
std::vector<std::string> v;

// 初始化,將所有有效字元放入容器
for (size_t i = 0; i < N; i++)
{
if (isdigit(postfix[i]) || postfix[i] == '+'
|| postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/')
{
v.push_back(std::string(1, postfix[i]));
}
}

// 處理每一個操作符
for (std::vector<std::string>::iterator b = v.begin(); b < v.end(); b++)
{
if (*b == "+" || *b == "-" || *b == "*" || *b == "/")
{
(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
std::cout << "------------------------------------------------" << std::endl;

std::string opcode = *(b);
std::string oprand1 = *(b - 2);
std::string oprand2 = *(b - 1);
b = v.erase(b - 2, b + 1); // 刪除原來的三個表達式,用一個新的表達式替換
b = v.insert(b, std::string("(") + oprand1 + opcode + oprand2 + std::string(")"));
}
}

for (std::vector<std::string>::iterator b = v.begin(); b < v.end(); b++)
{
strcat(infix, (*b).c_str());
}

return infix;
}

// 計算後綴表達式的值,僅支持加減乘除運算、操作數為非負整數的表達式。
int postfix_eval(const char * postfix)
{
const size_t N = strlen(postfix);

if (N == 0)
{
return 0;
}

STACK<int> operand(N); // 堆棧存放的是操作數

for (size_t i = 0 ; i < N; i++)
{
switch (postfix[i])
{
int op1, op2;

case '+':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 + op2);
break;

case '-':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 - op2);
break;

case '*':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 * op2);
break;

case '/':
op1 = operand.pop();
op2 = operand.pop();
operand.push(op1 / op2);
break;

default:

if (isdigit(postfix[i])) // 執行類似atoi()的功能
{
operand.push(0);

while (isdigit(postfix[i]))
{
operand.push(10 * operand.pop() + postfix[i++] - '0');
}

i--;
}
}

std::cout << operand << std::endl; // 輸出堆棧的內容
}

return operand.pop();
}

// 本程序演示了如何後綴表達式和中綴表達式的相互轉換,並利用堆棧計算後綴表達式。
// 轉換方向:org_infix --> postfix --> infix
int main(int argc, const char *argv[])
{
// const char *org_infix = "(5*(((9+8)*(4*6))+7))"; // section 4.3
const char *org_infix = "(5*((9*8)+(7*(4+6))))"; // exercise 4.12
std::cout << "原始中綴表達式:" << org_infix << std::endl;

char *const postfix = new char[strlen(org_infix) + 1];
infix2postfix(org_infix, postfix);
std::cout << "後綴表達式:" << postfix << std::endl;

char *const infix = new char[strlen(postfix) + 1];
postfix2infix(postfix, infix);
std::cout << "中綴表達式:" << infix << std::endl;

std::cout << "計算結果是:" << postfix_eval(postfix) << std::endl;
std::cout << "計算結果是:" << postfix_eval("5 9*8 7 4 6+*2 1 3 * + * + *") << std::endl; // exercise 4.13

delete []infix;
delete []postfix;
return 0;
}

⑵ 怎樣用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器

用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器的方法:

1、打開visual C++ 6.0-文件-新建-文件-C++ Source File;

⑶ 用C語言做一個計算器,能實現加減乘除混合運算

用C語言編寫一個簡單的可以進行加減乘除運算混合運算的計算器的方法:

1、打開visual C++ 6.0-文件-新建-文件-C++ Source File;

⑷ 用C語言怎麼寫個計算器

一、用戶界面是用圖形窗口還是命令行窗口。
如果是命令行窗口它的數據輸入輸出比較簡單。
如果是圖形窗口則要涉及,圖形窗口相同的操作了。這個部分如果你不會,那你需要專門學習一樣。

二、支持哪些計算功能。
除了加減乘除外,是否還支持其他高級的計算功能?
加減乘除的計算精度。
32位系統中,如果計算結果為不大於32位二進制的數。(64位系統則為不大於64位數)你可以直接使用C語言的相應的加減乘除表達式完成。
如果是支持超大數的運算,那就需要採取特殊手段了。

比如32位系統中,計算的數超過32位。比如兩個128位數相加。
需要將128位拆分成4個32位。將每個32位作為整體。在依照數學的多位數加進行計算。
A1 B1 C1 D1
A2 B2 C2 D2
--------------

D1與D2相加(需要檢測是否有進位,也就是計算結構是否有溢出)
C1與C2相加,同樣要檢測進位,並且要加上D1與D2結果的進位。
B1與B2相加,同樣要檢測進位,並且要加上C1與C2結果的進位。
B1與B2相加,同上類推。

在實際程序時,可以將用戶界面與加減乘除程序分離。即用戶界面的代碼要與計算程序的代碼分在不同的函數中。
又用戶界面代碼調用計算函數。計算函數將結果返回給用戶界面代碼。

以命令行界面為例,
用戶界面代碼,只是等待用戶輸入,將相應用戶信息轉換成合適的格式,
然後調用相應計算函數。
計算函數做完計算以後,返回相應數字。
用戶界面代碼,再將返回的數字轉換成適當的格式,顯示在窗口上。

⑸ 如何使用C語言做一個簡單的計算器

#include<stdio.h>

main()

{

floata[100];

inti,j;

charb[100];

while(1)

{

for(i=0;i<=99;i++)

{

scanf("%f%c",&a[i],&b[i]);

if(b[i]=='=')break;

}

for(j=0;j<=i;j++)

{

switch(b[j])

{

case'+':a[j+1]=a[j]+a[j+1];break;

case'-':a[j+1]=a[j]-a[j+1];break;

case'*':a[j+1]=a[j]*a[j+1];break;

case'/':a[j+1]=a[j]/a[j+1];break;

case'=':printf("%f ",a[j]);

}

}

}

getch();

}

說明:輸入格式為"10.2+1.8/3=",記住

最後一定要輸入"=",再敲回車鍵,在

TC中運行要加"getch();"以顯示結果。

回復:我用的是VC++6.0,調試和運行都

無異常,是不是你最後忘記加等號了,

還是輸入數字之後加了空格,為了輸入

的方便,我沒有設計加空格,直接輸入

就可以了,比如輸入「3+4-5/2=」,輸

出「1.000000",如還有問題可加我。

⑹ C語言怎麼做計算器

樓主你好

你寫的代碼比較繁瑣
我寫了一個較簡潔的
(應你的要求 只能用if else語句)
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int choice;//選擇
double a,result;

while(1)
{
result=0.0;
printf("(1.加法 2.減法 3.乘法 4.除法 5.退出)\n輸入你的選擇:");
scanf("%d",&choice);

if(1 == choice)
{
printf("輸入一個實數a:");
while(1 == scanf("%lf",&a))
result+=a;
}

else if(2 == choice)
{
int flag=1;
printf("輸入一個實數a:");
while(1 == scanf("%lf",&a))
{
if(flag == 1)
result+=a;
else
result-=a;
flag=0;
}
}

else if(3 == choice)
{
result=1.0;
printf("輸入一個實數a:");
while(1 == scanf("%lf",&a))
result*=a;
}

else if(4 == choice)
{
int flag=1;
result=1.0;

printf("輸入一個實數a:");
while(1 == scanf("%lf",&a))
{
if(flag == 1)
result/=a;
else
result*=a;
}
}

else if(5 == choice)
{
printf("最終結果:%.2f\n",result);
break;
}

else
printf("輸入錯誤!\n");

printf("最終結果:%.2f\n",result);
system("pause");
system("cls");
getchar();
}
return 0;
}

希望能幫助你哈

⑺ C語言中怎麼做一個能做加減乘的計算器

首先,你的scanf要寫成這樣:scanf("%d",&X);
參考這個程序:
#include<stdio.h>
voidmain(){floatx,y,z;charc;
scanf("%f%c%f",&x,&c,&y);
switch(c){
case'+':z=x+y;break;
case'-':z=x-y;break;
case'*':z=x*y;break;
case'/':z=(y==0)?(0):(x/y);break;
default:z=0;break;
}
printf("%f%c%f=%f ",x,c,y,z);
}

⑻ C語言製作簡單計算器

#include <stdio.h>

int main()
{
int num1,num2;

char sym;

printf("請輸入兩個整數:");

scanf("%d%c%d",num1,sym,num2);

switch(sym) /*只適用於整數的加減乘除 如果有小數可能會造成精度損失*/

{
case '+':printf("\n結果是%d",num1+num2);break;
case '-':printf("\n結果是%d",num1-num2);break;

case '*':printf("\n結果是%d",num1*num2);break;

case '/':printf("\n結果是%d",num1/num2);break;

default:printf("\n前檢查你的輸入是否正確 例: 1+2[enter]");break;

}

return 0;

}

⑼ C語言的簡單計算器怎麼做

#include<stdio.h>
intmain(void){
longa,b;
longmax;
charc;
printf("請輸入a,b的數值. ");
scanf("%ld%c%ld",&a,&c,&b);
switch(c){
case'+':
max=a+b;
break;
case'-':
max=a-b;
break;
case'*':
max=a*b;
break;
case'/':
max=(float)a/(float)b;
break;
default:
printf("keyerror! ");
return0;
}
printf("結果為%ld ",max);
printf("----beta0.1.0 ");
return0;
}

⑽ 用C語言做一個計算器

#include <dos.h> /*DOS介面函數*/

#include <math.h> /*數學函數的定義*/

#include <conio.h> /*屏幕操作函數*/

#include <stdio.h> /*I/O函數*/

#include <stdlib.h> /*庫函數*/

#include <stdarg.h> /*變數長度參數表*/

#include <graphics.h> /*圖形函數*/

#include <string.h> /*字元串函數*/

#include <ctype.h> /*字元操作函數*/

#define UP 0x48 /*游標上移鍵*/

#define DOWN 0x50 /*游標下移鍵*/

#define LEFT 0x4b /*游標左移鍵*/

#define RIGHT 0x4d /*游標右移鍵*/

#define ENTER 0x0d /*回車鍵*/

void *rar; /*全局變數,保存游標圖象*/

struct palettetype palette; /*使用調色板信息*/

int GraphDriver; /* 圖形設備驅動*/

int GraphMode; /* 圖形模式值*/

int ErrorCode; /* 錯誤代碼*/

int MaxColors; /* 可用顏色的最大數值*/

int MaxX, MaxY; /* 屏幕的最大解析度*/

double AspectRatio; /* 屏幕的像素比*/

void drawboder(void); /*畫邊框函數*/

void initialize(void); /*初始化函數*/

void computer(void); /*計算器計算函數*/

void changetextstyle(int font, int direction, int charsize); /*改變文本樣式函數*/

void mwindow(char *header); /*窗口函數*/

int specialkey(void) ; /*獲取特殊鍵函數*/

int arrow(); /*設置箭頭游標函數*/

int main()

{

initialize();/* 設置系統進入圖形模式 */

computer(); /*運行計算器 */

closegraph();/*系統關閉圖形模式返迴文本模式*/

return(0); /*結束程序*/

}

/* 設置系統進入圖形模式 */

void initialize(void)

{

int xasp, yasp; /* 用於讀x和y方向縱橫比*/

GraphDriver = DETECT; /* 自動檢測顯示器*/

initgraph( &GraphDriver, &GraphMode, "" );

/*初始化圖形系統*/

ErrorCode = graphresult(); /*讀初始化結果*/

if( ErrorCode != grOk ) /*如果初始化時出現錯誤*/

{

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;/* 計算縱橫比值*/

}

/*計算器函數*/

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; /*操作數和計算結果變數*/

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 );

/*畫一個二維矩形條顯示運算數和結果*/

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鍵結束程序,否則執行下面的循環*/

{

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=='.') /*判斷是否是數字或小數點*/

{

if(flag==-1) /*如果標志為-1,表明為負數*/

{

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); /*將第一個操作數轉換為浮點數*/

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; /*設置負數標志*/

else

{

num1=atof(str2); /*將第二個操作數轉換為浮點數*/

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); /*將第二個操作數轉換為浮點數*/

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); /*將第二個操作數轉換為浮點數*/

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); /*將第二個操作數轉換為浮點數*/

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); /*將第二個操作數轉換為浮點數*/

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); /*將第二個操作數轉換為浮點數*/

switch(act) /*根據運算符號計算*/

{

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); /*覆蓋結果區*/

sprintf(temp,"%f",result); /*將結果保存到temp中*/

outtextxy(5*width,height,temp); /*顯示結果*/

}

if(c=='c')

{

num1=0; /*將兩個操作數復位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); /*覆蓋結果區*/

outtextxy(5*width,height,"0."); /*顯示字元串*/

}

if(c=='Q')exit(0); /*如果選擇了q回車,結束計算程序*/

}

putimage(x,y,rar,XOR_PUT); /*在退出之前消去游標箭頭*/

return; /*返回*/

}

/*窗口函數*/

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所指的結構中*/

rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*畫矩形邊框*/

}

/*設計滑鼠圖形函數*/

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); /*分配內存區域*/

getimage(4,4,16,16,rar); /*存放游標箭頭圖象*/

putimage(4,4,rar,XOR_PUT); /*消去游標箭頭圖象*/

return 0;

}

/*按鍵函數*/

int specialkey(void)

{

int key;

while(bioskey(1)==0); /*等待鍵盤輸入*/

key=bioskey(0); /*鍵盤輸入*/

key=key&0xff? key&0xff:key>>8; /*只取特殊鍵的掃描值,其餘為0*/

return(key); /*返回鍵值*/

}