計算器的編程代碼
⑴ 簡單計算器圖形編程代碼是什麼
//java版本的,你早晚能用到
package bag;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;
  
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import java.awt.Dimension;
public class SCul extends JFrame {
	private static final long serialVersionUID = 1L;
	public JTextField jTextField1;
	private static String s1,s2,s,s3,s4;
	private static double result;
	public Component createComponents(){
	   final JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20;
	  
		GridLayout gl1=new GridLayout(4,5);
		JPanel panel1=new JPanel();
		panel1.setLayout(gl1);
		b1=new JButton("1");  b1.setText("1");b1.setForeground(Color.blue);
		b2=new JButton("2");  b2.setText("2");b2.setForeground(Color.blue);
		b3=new JButton("3");  b3.setText("3");b3.setForeground(Color.blue);
		
		b6=new JButton("4");  b6.setText("4");b6.setForeground(Color.blue);
		b7=new JButton("5");  b7.setText("5");b7.setForeground(Color.blue);
		b8=new JButton("6");  b8.setText("6");b8.setForeground(Color.blue);
		
		b11=new JButton("7");  b11.setText("7");b11.setForeground(Color.blue);
		b12=new JButton("8");  b12.setText("8");b12.setForeground(Color.blue);
		b13=new JButton("9");  b13.setText("9");b13.setForeground(Color.blue);
		b16=new JButton("0");  b16.setText("0");b16.setForeground(Color.blue);
		
		b4=new JButton("+");  b4.setText("+");b4.setForeground(Color.red);
	    b5=new JButton("C");  b5.setText("C");b5.setForeground(Color.red);
		
		b9=new JButton("-");  b9.setText("-");b9.setForeground(Color.red);
		b10=new JButton("退格");  b10.setText("退格");b10.setForeground(Color.red);
		
		b14=new JButton("*");  b14.setText("*");b14.setForeground(Color.red);
		b15=new JButton("1/x");  b15.setText("1/x");b15.setForeground(Color.blue);
		
		b17=new JButton("+/-");  b17.setText("+/-");b17.setForeground(Color.blue);
		b18=new JButton(".");  b18.setText(".");b4.setForeground(Color.blue);
		b19=new JButton("/");  b19.setText("/");b19.setForeground(Color.red);
		b20=new JButton("=");  b20.setText("=");b20.setForeground(Color.red);
		//設置大小和顏色
		b1.setSize(100, 100);
		b2.setSize(100, 100);
		b3.setSize(100, 100);
		b4.setSize(100, 100);
		b5.setSize(100, 100);
		b6.setSize(100, 100);
		b7.setSize(100, 100);
		b8.setSize(100, 100);
		b9.setSize(100, 100);
		b10.setSize(100, 100);
		b11.setSize(100, 100);
		b12.setSize(100, 100);
		b13.setSize(100, 100);
		b14.setSize(100, 100);
		b15.setSize(100, 100);
		b16.setSize(100, 100);
		b17.setSize(100, 100);
		b18.setSize(100, 100);
		b19.setSize(100, 100);
		b20.setSize(100, 100);
		
		panel1.add(b1);
		panel1.add(b2);
		panel1.add(b3);
		panel1.add(b4);
		panel1.add(b5);
		panel1.add(b6);
		panel1.add(b7);
		panel1.add(b8);
		panel1.add(b9);
		panel1.add(b10);
		panel1.add(b11);
		panel1.add(b12);
		panel1.add(b13);
		panel1.add(b14);
		panel1.add(b15);
		panel1.add(b16);
		panel1.add(b17);
		panel1.add(b18);
		panel1.add(b19);
		panel1.add(b20);
		panel1.setVisible(true);
		b1.addActionListener(new ActionListener(){//按1
		   public void actionPerformed(ActionEvent e){
			 jTextField1.setText(jTextField1.getText().concat(b1.getText().toString()));
			 s=jTextField1.getText().toString();
		   }	
		});
		b2.addActionListener(new ActionListener(){//按2
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b2.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b3.addActionListener(new ActionListener(){//按3
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b3.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b6.addActionListener(new ActionListener(){// 按4
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b6.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b7.addActionListener(new ActionListener(){//按5
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b7.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b8.addActionListener(new ActionListener(){//按6
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b8.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b11.addActionListener(new ActionListener(){//按7
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b11.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b12.addActionListener(new ActionListener(){//按8
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b12.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b13.addActionListener(new ActionListener(){//按9
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b13.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b16.addActionListener(new ActionListener(){//按0
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b16.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b4.addActionListener(new ActionListener(){//按+
			   public void actionPerformed(ActionEvent e){
				   if(s=="")
				   {
					   s="0";
					   jTextField1.setText(s);
				   }
				   else{
				     s1=s;//將第一操作數保存至s1
				     s3="+";
				     jTextField1.setText(jTextField1.getText().concat(b4.getText().toString()));
				     s=jTextField1.getText().toString();
				   }
				  }	
			});
		b9.addActionListener(new ActionListener(){//按-
			   public void actionPerformed(ActionEvent e){
				   if(s=="")
				   {
					   s="0";
					   jTextField1.setText(s);
				   }
				   else{
				     s1=s;//將第一操作數保存至s1
				     s3="-";
				     jTextField1.setText(jTextField1.getText().concat(b9.getText().toString()));
				     s=jTextField1.getText().toString();
				   }
				  }	
			});
		b14.addActionListener(new ActionListener(){//按*
			   public void actionPerformed(ActionEvent e){
				   if(s=="")
				   {
					   s="0";
					   jTextField1.setText(s);
				   }
				   else{
				     s1=s;//將第一操作數保存至s1
				     s3="*";
				     jTextField1.setText(jTextField1.getText().concat(b14.getText().toString()));
				     s=jTextField1.getText().toString();
				   }
				  }	
			});
		b19.addActionListener(new ActionListener(){//按/
			   public void actionPerformed(ActionEvent e){
				   if(s=="")
				   {
					   s="0";
					   jTextField1.setText(s);
				   }
				   else{
				     s1=s;//將第一操作數保存至s1
				     s3="/";
				     jTextField1.setText(jTextField1.getText().concat(b19.getText().toString()));
				     s=jTextField1.getText().toString();
				   }
				  }	
			});
		b5.addActionListener(new ActionListener(){//按C清零
			   public void actionPerformed(ActionEvent e){
				     s="0";
				     jTextField1.setText(s);
				   
				  }	
			});
		b10.addActionListener(new ActionListener(){//按退格
			   public void actionPerformed(ActionEvent e){
				   
				     int l=s.length();
				     if(l!=0)
				        s=s.substring(0, l-1);//刪除一個字元
				     else
				     {
				    	 s="0";
				    	 s4="+";
				     }
				     jTextField1.setText(s);//將刪除後的字元串顯示到單行編輯框中 
				
				  }	
			});
		b15.addActionListener(new ActionListener(){
			   public void actionPerformed(ActionEvent e){//求倒數
				   if(s=="" || s=="0")
				   {
					   s="0";
					   jTextField1.setText("0");
				   }
				   else{
				     result=1/Double.parseDouble(s);
				     s=String.valueOf(result);
				     jTextField1.setText(s4+s);
				    
				   }
				  }	
			});
		
		b17.addActionListener(new ActionListener(){//按+/-
			   public void actionPerformed(ActionEvent e){
				   if(s=="")
				   {
					 jTextField1.setText("0");  
				   }else{
				       
				    	   if(s4=="+")
				    	   {  s4="-";
				    	      jTextField1.setText(s4+s);
				    	   }
				    	   else{
				    		   s4="+";
				    	        jTextField1.setText(s4+s);
				    	}
					    
				   }
}	
			});
		b18.addActionListener(new ActionListener(){//按.        
			   public void actionPerformed(ActionEvent e){
				 jTextField1.setText(jTextField1.getText().concat(b18.getText().toString()));
				 s=jTextField1.getText().toString();
			   }	
			});
		b20.addActionListener(new ActionListener(){//按=求結果
			   public void actionPerformed(ActionEvent e){//求結果
				   if(s=="")
				   {
					   s="0";
					   s4="+";
					   s3="";
					   jTextField1.setText(s);
				   }
				   else if(s!=""){
					
					 int m1=s.indexOf(s3);
					 s1=s.substring(0, m1);
					 s2=s.substring(m1+1, s.length());
					 if(s4=="+"){
					    if(s3=="+")
				           result=Double.parseDouble(s1)+Double.parseDouble(s2);
					    if(s3=="-")
					        result=Double.parseDouble(s1)-Double.parseDouble(s2);
					    if(s3=="*")
					       result=Double.parseDouble(s1)*Double.parseDouble(s2);
					    if(s3=="/" && s2!="0" && s2!="")
					       result=Double.parseDouble(s1)/Double.parseDouble(s2);
					    if(s3=="/" && (s2=="0" || s2=="")) 
					    	result=00.00;
				        s=String.valueOf(result);
				     }
					 else if(s4=="-"){
						    if(s3=="+"){
					           result=Double.parseDouble(s2)-Double.parseDouble(s1);
					           s=String.valueOf(result);
					           }
						    if(s3=="-"){
						        result=Double.parseDouble(s1)+Double.parseDouble(s2);
						        s="-"+String.valueOf(result);
						        }
						    if(s3=="*"){
						       result=Double.parseDouble(s1)*Double.parseDouble(s2);
						       s="-"+String.valueOf(result);
						    }
						    if(s3=="/" && (s2!="0" || s2!="")){
						       result=Double.parseDouble(s1)/Double.parseDouble(s2); 
						       s="-"+String.valueOf(result);
						     }
						    if(s3=="/" && s2=="0" && s2=="")
						    {
						    	s="00.00";
						    }
					 }
				    jTextField1.setText(s);
				    
				   }
				  }	
			});
		
        return panel1;
	}
	public static void main(String[] args) {
	
		SCul cul1=new SCul();
		JFrame frame1=new JFrame("計算器");
		frame1.setBackground(Color.lightGray);
		frame1.add(cul1.jTextField1,BorderLayout.NORTH);
		frame1.setResizable(false);
		Component contents=cul1.createComponents();
		frame1.getContentPane().add(contents, BorderLayout.SOUTH);
		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame1.pack();
		frame1.setVisible(true);
		
	}
	/**
	 * This is the default constructor
	 */
	public SCul() {
		super();
		jTextField1=new JTextField(); 
		s="";
		s1="";
		s2="";
		s3="";
		s4="+";
		jTextField1.setText(s);
		jTextField1.setHorizontalAlignment(JTextField.RIGHT);
		
	}
} 
一個簡單的計算器如圖所示,使用「計算器」可以完成任意的的標准運算,「計算器」可用於基本的算術運算,比如加減運算等。
⑵ c語言計算器程序源代碼
棧操作~
輸入的弄成字元串。
如:a+(b+c*(d+e))
先檢索「(」入棧,b入棧,+入棧,c入棧,*入棧,後面的不是數字那麼「(」入棧,+,入棧,e入棧,
「)」入棧,遇到「)」,出棧操作,直到遇到第一個「(」。
得到字元:d+e,通過演算法計算出值D,然後把D入棧。那麼棧中的字元為:a+(b+c*D
向後判斷一步看是不是乘或除,不是繼續向後入棧,是就出棧。
得到c*D,通過演算法計算值為C,入棧,那麼棧中的字元為:a+(b+C,向後判斷,無乘或除運算,繼續入棧:「)」,遇到「)」,出棧操作,直到遇到第一個「(」,得到字元串:b+c,演算法計算值為B,入棧
棧中字元串為:a+B 字元串結束,出棧操作計算出a+B,值。
如果字元串的式子很長的話,就反復的入棧出棧計算。
思路是這樣的......
關於COS,SIN類的關鍵字,也是上面的思路,
(入棧,直到遇到)出棧,先計算出括弧里的,然後後退一步判斷前面的是乘除或者關鍵字,
如:sin(0.1+0.2)
操作得出sin0.3然後判斷前面的是不是關鍵字,是那句計算sin0.3。 
但是遇到關鍵字的時候不是一個一個字元在一起的,你可以入棧的時候判斷是不是字母,是字母,那麼入棧直到不是字母為止,出棧判斷關鍵字也是一樣......
不明白的繼續問~
⑶ 求一道C語言中關於計算器的程序代碼
這個已經有計算功能了,但記事本還沒有,給你急用。因為C沒有專門的字元串類型:有事再聯系405719864// C語言計算器.cpp : Defines the entry point for the console application.
//
/*注意!本程序用C編寫,在VC++6.0中運行通過,如果要有記事本功能,可能要用C++寫,因為C沒有專門的字元串類型*/
#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>char c,operate;//c用於保存計算符號,operate表示當前輸入
double n,m;//記錄輸入的數字
bool check_c,check_n,check_m;//各個輸入的開關,當輸入發生改變時,可以有根據地操作double count(double n,char ch,double m)//根據不同的計算符號計算結果
{
 if(ch == '+') return n+m;
 if(ch == '-') return n-m;
 if(ch == '*') return n*m;
 if(ch == '/')
 {
  if(m != 0) return n/m;
  else printf("0不能作除數!\n");
 }
 return n;
}int main()
{
 n=m=0;
 check_c=check_n=check_m=false;
 printf("0\n"); printf("若要結束,請輸入任意字母!\n");
 while(scanf("%c",&operate) != EOF)//表示輸入以EOF結束
 {
  if(operate == 0x0d || operate==' ' || operate=='\n') continue;//表示排除無效字元
  if(operate == '#')
  {
   n=m=0;
   check_c=check_n=check_m=false;
   printf("0\n");
  }
  else
  {
   if(isdigit(operate))
   {
    if(!check_n) { n=n*10+operate-'0'; printf("%.0lf\n",n); }
    else { m=m*10+operate-'0'; check_m=true; check_c=true; printf("%.0lf\n",m); }
   }
   else
   {
    if(operate=='+' || operate=='-' || operate=='*' || operate=='/' || operate=='=')//出現運算符號
    {
     if(operate != '=')//非「=」,則第一個數字輸入結束,並記錄當前計算符號
     {
      check_n=true;
      if(!check_c) c=operate;
     }
     if(check_n && check_m)//兩個數字都有輸入,就進行計算
     {
      n=count(n,c,m);
      printf("%.0lf\n",n);
      if(operate != '=')//出現連續等號,連續計算,非等號,則記錄並從新輸入第二個數字
      {
       c=operate;
       check_c=false;
       m=0;
       check_m=false;
      }
     }
     else printf("%.0lf\n",n);
    }
    else
    {
     printf("輸入數據無效!\n");//表示以任意字母字元結束
     break;
    }
   }
  }
 }
 return 0;
}
⑷ 簡單計算器的編寫
這是個支持表達式的計算器,代碼很短。
可以輸入類似(3+3*2)*5+6這樣的表達式,自己看吧。
不懂其中的原理來找我,或者自助一下,學點編譯原理的知識,一定要懂EBNF,EBNF不知道是什麼東西?那就google一下吧。
/*
simple integer arithmetic calculator according to the EBNF
<exp> -> <term>{<addop><term>}
<addop>->+|-
<term>-><factor>{<mulop><factor>}
<mulop> -> *
<factor> -> ( <exp> )| Number
Input a line of text from stdin 
Outputs "Error" or the result.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char token;/*global token variable*/
/*function prototypes for recursive calls*/
int exp(void);
int term(void);
int factor(void);
void error(void)
{
 fprintf(stderr,"Error\n");
 exit(1);
}
void match(char expectedToken)
{
 if(token==expectedToken)token=getchar();
 else error();
}
main()
{
 int result;
 token = getchar();/*load token with first character for lookahead*/
 result = exp();
 if(token=='\n')/*check for end of line */
  printf("Result = %d\n",result);
 else error();/*extraneous cahrs on line*/
 return 0;
}
int exp(void)
{
 int temp = term();
 while((token=='+')||(token=='-'))
  switch(token)
  {
  case '+':
   match('+');
   temp+=term();
   break;
  case '-':
   match('-');
   temp-=term();
      break;
  }
 return temp;
}
int term(void)
{
 int temp = factor();
 while (token=='*')
 {
  match('*');
  temp*=factor();
 }
 return temp;
}
int factor(void)
{
 int temp;
 if(token=='('){
  match('(');
  temp = exp();
  match(')');
 }
 else if(isdigit(token)){
  ungetc(token,stdin);
  scanf("%d",&temp);
  token = getchar();
 }
 else error();
 return temp;
}
⑸ 求C++編寫的科學計算器源代碼
給你個地址,自己去下載。
★★[源碼庫]811款導彈-制導-彈道-慣性導航-GPS-雷達等模擬源代碼★★
http://bbs.81tech.com/read.php?tid=209564
其中有兩款計算機源代碼:
1. 科學計算器程序--VC++源代碼.rar (30 K)
2. 非常實用的一款計算器-VC++源代碼.rar (138 K)
⑹ 求C#計算器編程代碼
前邊你應該有兩個變數吧 比如_nu1,_nu2
把得到的值重新賦給_num1
_num2記錄重新輸入的數字
_num1 = getnum(_num1, _num2);
this.textBox1.Text = _num1;
_num2 = "";
⑺ 急求VB計算器的編程代碼
控制項:3個標簽(text1~3) 從左到右2個標簽(label1~2) 從左到右5個按鈕(command1~5) 從左到右  代碼:Private Sub Command1_Click()
Label1.Caption = "+"
End SubPrivate Sub Command2_Click()
Label1.Caption = "-"
End SubPrivate Sub Command3_Click()
Label1.Caption = "*"
End SubPrivate Sub Command4_Click()
Label1.Caption = "/"
End SubPrivate Sub Command5_Click()
Select Case Label1.Caption
Case "+"
Text3.Text = Val(Text1.Text) + Val(Text2.Text)
Case "-"
Text3.Text = Val(Text1.Text) - Val(Text2.Text)
Case "*"
Text3.Text = Val(Text1.Text) * Val(Text2.Text)
Case "/"
Text3.Text = Val(Text1.Text) / Val(Text2.Text)
End Select
End SubPrivate Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub
希望對你能有所幫助。
⑻ 簡單的計算器設計的程序代碼
#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); /*返回鍵值*/
}
⑼ 用C語言編寫計算器程序
http://www.softhouse.com.cn/html/200509/2005091911241600011153.html這個頁面可能對你有幫助。
