編程計算器代碼
⑴ 簡單計算器圖形編程代碼是什麼
//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語言程序設計簡易計算器
1、首先,打開Vs 2010,如圖。
2、找到左上角的新建並點擊,給文件為簡單計算器,單擊確定。
3、點擊下一步,注意勾選空項目,點擊下一步,點擊完成。
4、點擊左側的源文件,右擊選擇「添加—>項目」,選擇C++文件,命名為簡單計算器,因為是C程序,注意後綴名要加上.c,點擊確定完成文件新建工作。
5、輸入以下代碼,好了,一個簡單的計算器便做好了
⑶ c語言編寫計算器程序
C語言編寫計算器
我們可以用printf和scanf函數輸出結果和獲取用戶的輸入。需要<stdio.h>頭文件。scanf函數在讀取數據的時候不需要再一行上輸入每個數據,只要數據和數據之間留出空白就可以了。先聲明兩個變數number1和number2,operation變數用來存儲運算符。用scanf函數獲取這兩個數字和運算符。分別用%lf %c %lf
⑷ 怎麼用JAVA編程編寫一個計算器
打開IED:打開自己java編程的軟體,採用的是eclipse軟體。
建立java工程。
編寫類。
⑸ C 語言 : 編程實現計算器功能
代碼如下: #include<stdio.h> #include<stdlib.h> int Add(); int Sub(); int Mul(); int Div(); int Sur(); int main() { int i; for(;i;) { printf("1.加法\n"); printf("2.減法\n"); printf("3.乘法\n"); printf("4.除法\n"); printf("5.取余\n"); printf("0.退出\n"); printf("請選擇你希望的操作(0~5):"); scanf("%d",&i); switch(i) { case 1: Add(); break; case 2: Sub(); break; case 3: Mul(); break; case 4: Div(); break; case 5: Sur(); break; case 0: system("CLS"); break; default: printf("對不起,你輸入了非法數字,請重新輸入!\n"); system("CLS"); } } return 0; } int Add() { int x,y,sum,i=1; while (i) { printf("請輸入兩個加數,逗號隔開。\n"); scanf("%d,%d",&x,&y); sum=x+y; printf("%d+%d=%d\n",x,y,sum); printf("是否繼續?是【1】,否【0】\n"); scanf("%d",&i); } system("CLS"); return 0; } int Sub() { int x,y,dif,i=1; while (i) { printf("請輸入兩個減數,逗號隔開。\n"); scanf("%d,%d",&x,&y); dif=x-y; printf("%d-%d=%d\n",x,y,dif); printf("是否繼續?是【1】,否【0】\n"); scanf("%d",&i); } system("CLS"); return 0; } int Mul() { int x,y,pro,i=1; while (i) { printf("請輸入兩個乘數,逗號隔開。\n"); scanf("%d,%d",&x,&y); pro=x*y; printf("%d*%d=%d\n",x,y,pro); printf("是否繼續?是【1】,否【0】\n"); scanf("%d",&i); } system("CLS"); return 0; } int Div() { int x,y,i=1; double quo; while (i) { printf("請輸入一個被除數和一個除數,逗號隔開。\n"); scanf("%d,%d",&x,&y); if(y!=0) { quo=1.0*x/y; printf("%d/%d=%lf\n",x,y,quo); printf("是否繼續?是【1】,否【0】\n"); scanf("%d",&i); } else { printf("除數不能為零,請重輸入!\n"); i=0; } } system("CLS"); return 0; } int Sur() { int x,y,i=1,su; while (i) { printf("請輸入一個被除數和一個除數,逗號隔開。\n"); scanf("%d,%d",&x,&y); if(y!=0) { su=1*x%y; printf("%d%d=%lf\n",x,y,su); printf("是否繼續?是【1】,否【0】\n"); scanf("%d",&i); } else { printf("除數不能為零,請重輸入!\n"); i=0; } } system("CLS"); return 0; }
⑹ 用VB編寫一個計算器程序的代碼
1、創建控制項組的方法
首先創建一個命令按鈕,調整其大小(覺得合適就行),名稱為Command1,Caption 屬性為數字 0 ;然後進行「復制」和「粘貼」,當選擇「粘貼」時,出現對話框提示已有一個同名控制項,詢問是否創建控制項組,選擇「是」後,即創建了一個名為「Command」的控制項組。
這時,第一個按鈕的Index屬性值默認為「0」,第二個的Index屬性值自動設為「1」,並且大小與第一個按鈕相同,只需修改其 Caption 屬性為數字「1」並將其拖至合適位置即可。此後繼續使用「粘貼」的方法建立其他控制項組中其餘按鈕,共20個按鈕,每建立一個,就將它拖到合適處,並修改相應的Caption屬性值。
2、各控制項組其屬性設置如下:
二、編寫代碼
Dim s1 As Single, s2 As Single, ysf As String
『定義兩個單精度數變數用與存放參與運算的數,一個字元型存放運算符
Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text & Command1(Index).Caption 』將command1的單擊事件與文本框顯示的內容連接
End Sub
Private Sub Command2_Click()
Text1.Text = Text1.Text + 「。」
If (InStr(Text1.Text, 「。」) = 1) Then 『第一位不能為小數
Text1.Text = 「」
End If
If InStr(Text1.Text, 「。」) 《 Len(Text1.Text) Then 』防止出現兩個小數點
Text1.Text = Left
(Text1.Text, Len(Text1.Text) - 1)
End If
End Sub
Private Sub
Command3_Click()
s2 = Val(Text1.Text) 『開始加減乘除運算
Select Case ysf Case 「+」
Text1.Text = s1 + s2
Case 「-」
Text1.Text = s1 - s2
Case 「*」
Text1.Text = s1 * s2
Case 「/」
If s2 = 0 Then
MsgBox 「分母不能為零!」
Text1.Text = 「」
Else
Text1.Text = s1 / s2 End If End Select
Text1 = IIf(Left(Text1.Text, 1) = 「。」, 0 & Text1.Text, Text1.Text) 『
這個很關鍵,如果沒有這個的話,得出小於1的小數前面沒有0
End Sub
Private Sub Command4_Click()
If Text1.Text = 「」 Then 』文本為空就結束
Exit Sub
End If
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) 『文本退一格
End Sub
Private Sub Command5_Click()
Text1.Text = 「」 』清除當前框內文本
End Sub
Private Sub Command6_Click(Index As Integer)
s1 = Val(Text1.Text) 『將s1隱藏起來 ysf = Command6(Index).Caption
Text1.Text = 「」
End Sub
Private Sub Command7_Click()
If Left(Text1.Text, 1) 《》 「-」 Then 』判斷作為負數
Text1.Text = 「-」 & Text1.Text
Else
Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
End If
End Sub
Private Sub Command8_Click()
Text1.Text = Text1.Text * Text1.Text 『平方
End Sub
拓展資料
Visual Basic(VB)是由微軟公司開發的包含環境的事件驅動編程語言。它源自於BASIC編程語言。VB擁有圖形用戶界面(GUI)和快速應用程序開發(RAD)系統,可以輕易的使用DAO、RDO、ADO連接資料庫,或者輕松的創建ActiveX控制項。程序員可以輕松地使用VB提供的組件快速創建一個應用程序。
參考鏈Visual Basic——網路接
⑺ c語言設計一個簡單的計算器程序
#include<stdio.h>//計算器
voidmenu()//自定義的菜單界面
{
printf("--------------------\n");
printf("請輸入你的選擇\n");
printf("1.+\n");
printf("2.-\n");
printf("3.*\n");
printf("4./\n");
printf("--------------------\n");
}
intmain()
{
inti=0;
intj=0;
intnum=0;//計算結果存放在nun
intselect=0;//選擇的選項存放在select
do//do-while先執行再判斷循環條件,即可實現重復計算功能
{
menu();//列印出菜單界面
scanf("%d",&select);//輸入你的選項
printf("請輸入計算值:");
scanf("%d%d",&i,&j);//輸入要計算的數值
switch(select)
{
case1:
printf("%d+%d=%d\n",i,j,num=i+j);//實現加法功能
break;
case2:
printf("%d-%d=%d\n",i,j,num=i-j);//實現減法功能
break;
case3:
printf("%d*%d=%d\n",i,j,num=i*j);//實現乘法功能
break;
case4:
printf("%d-%d=%d\n",i,j,num=i/j);//實現除法功能
break;
default:
printf("輸入有誤重新選擇");
break;
}
}while(select);
return0;
}
運行結果:
(7)編程計算器代碼擴展閱讀:
return表示把程序流程從被調函數轉向主調函數並把表達式的值帶回主調函數,實現函數值的返回,返回時可附帶一個返回值,由return後面的參數指定。
return通常是必要的,因為函數調用的時候計算結果通常是通過返回值帶出的。如果函數執行不需要返回計算結果,也經常需要返回一個狀態碼來表示函數執行的順利與否(-1和0就是最常用的狀態碼),主調函數可以通過返回值判斷被調函數的執行情況。
⑻ 簡單的計算器設計的程序代碼
#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); /*返回鍵值*/
}
⑼ 用vb編寫計算器的代碼程序
在窗體上放置以下控制項:
1、lblResult
Label控制項
設置lblResult.Caption
=
0
2、btnNum(0)~btnNum(9)
按鈕控制項數組,下標對應齊數字(如:btnNum(0).Caption=0,依次類推)
3、添加btnAdd(加法)、btnSub(減法)、btnMul(乘法)、btnDiv(除法)、btnEnter(等於號)、btnReset(清除鍵)六個按鈕。
在窗體代碼中輸入以下內容:
Dim
byteOperation
As
Byte
'操作符
Dim
byteTmp
As
Byte
'臨時操作符
Dim
intNum
As
Double
'輸入的數字
Sub
Calc()
Select
Case
byteOperation
Case
1
'Add
lblResult.Caption
=
CDbl(lblResult.Caption)
+
intNum
Case
2
'Sub
lblResult.Caption
=
intNum
-
CDbl(lblResult.Caption)
Case
3
'Mul
lblResult.Caption
=
CDbl(lblResult.Caption)
*
intNum
Case
4
'Div
lblResult.Caption
=
intNum
/
CDbl(lblResult.Caption)
Case
Else
End
Select
End
Sub
Private
Sub
btnNum_Click(Index
As
Integer)
If
byteTmp
<>
0
Then
intNum
=
lblResult.Caption
lblResult.Caption
=
0
byteTmp
=
0
End
If
If
lblResult.Caption
=
0
Then
lblResult.Caption
=
Index
Else
If
Len(lblResult.Caption)
<
9
Then
lblResult.Caption
=
lblResult.Caption
&
Index
End
If
End
If
End
Sub
Private
Sub
btnAdd_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
1
byteTmp
=
1
End
Sub
Private
Sub
btnReset_Click()
byteTmp
=
0
byteOperation
=
0
intNum
=
0
lblResult.Caption
=
0
End
Sub
Private
Sub
btnSub_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
2
byteTmp
=
2
End
Sub
Private
Sub
btnMul_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
3
byteTmp
=
3
End
Sub
Private
Sub
btnDiv_Click()
If
byteOperation
<>
0
Then
Call
Calc
End
If
byteOperation
=
4
byteTmp
=
4
End
Sub
Private
Sub
btnEnter_Click()
Call
Calc
byteTmp
=
0
End
Sub
⑽ 編程設計一個簡單的計算器程序
方法一:
#include <stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
struct complex multiply(struct complex x, struct complex y);
struct complex{
int real;
int imag;
};
int main()
{
struct complex a,b,s;
scanf("%d%d%d%d",&a.real,&a.imag,&b.real,&b.imag);
s=multiply(a,b);
printf("(%d+%di)*(%d+%di)=%d+%di ",a.real,a.imag,b.real,b.imag,s.real,s.imag);
return 0;
}
struct complex multiply(struct complex x, struct complex y)
{
struct complex m;
m.real=x.real*y.real-x.imag*y.imag;
m.imag=x.imag*y.real+x.real*y.imag;
return m;
}
方法二:
#include<stdio.h>
int main()
{
int a,b,c,d,e,f;
scanf("%d %d %d %d",&a,&b,&c,&d);
e = a * c - b * d;
f = a * d + b * c;
printf("(%d+%di)*(%d+%di)=%d+%di ",a,b,c,d,e,f);
}