第一篇:java課程設計報告—計算器
1--計算器 Java實習報告
目錄
一、課程設計目的.................................................................................................................2
二、課程設計任務..................................................................................................................2
2.1、設計任務................................................................................................................2
2.2、課程設計要求:....................................................................................................2
2.3、需求分析................................................................................................................2
三、開發工具與平臺.............................................................................................................3
3.1、開發工具................................................................................................................3
3.2、開發平臺................................................................................................................3
四、設計思路..........................................................................................................................4
4.1、界面設計.................................................................................................................4
4.2.1、邏輯設計.............................................................................................................4
4.2.2、程序流程圖...........................................................................................................5
4.2.3、主要代碼展示及說明............................................................................................5
4.3、程序測試............................................................................................................10
五、實驗小結........................................................................................................................11
六、附錄(程序代碼)..........................................................................................................12
頁
第 1--計算器 Java實習報告
一、課程設計目的
1、熟練掌握java面向對象編程。
2、選擇合適的數據結構實現需求。
3、熟練使用各種控制結構。
4、GUI組件、事件處理技術。
二、課程設計任務
2.1、設計任務
設計一個簡易的計算器,可以進行四則運算:加、減、乘、除等(限于十進制下)
程序要求:
(1)應具有相應的界面,可參考Windows操作系統自帶的計算器界面。(2)操作符號定為:“+”,“-”,“*”,“/”,“+/-”等。(按國際慣例設計)(3)用戶通過點擊程序界面上按鈕,實現數字、運算符的輸入操作。(4)以上部分為必須完成的內容。選作部分:
(1)具有操作符號“1/x”,“sqrt”(開方),“.”(小數功能)等。
2.2、課程設計要求:
(1)應用自己所學課程知識完成對計算器的基本任務。
(2)查閱相關資料,學習和掌握項目中涉及的新知識,提高自學能力。
(3)通過應用java程序編寫計算器來提升自己對簡單的圖形界面有一定的掌握和了解。
2.3、需求分析
1.設計的計算器可以完成加法、減法、乘法、除法的簡單運算。2.實現一些簡單的擴展運算,如:正負號、倒數、退格、清零等功能。
頁
第 2--計算器 Java實習報告3.添加小數點功能,用以實現浮點型數據的計算。
4.使用布局管理器設計一個計算器的界面,使用事件監聽器處理數據的輸入,并完成相關的計算。
三、開發工具與平臺
3.1、開發工具
Microsoft Windows 7旗艦版
3.2、開發平臺
JDK1.6.0-02 和UE編譯器
頁
第 3--計算器 Java實習報告
四、設計思路
4.1、界面設計:(如圖3-1)
圖3-1
4.2.1、邏輯設計:
(1)根據所設計出來的界面,首先要設計其GUI界面,總體界面有一個文本框,20個按鈕,總體界面用BorderLayout布局,文本框放置在最NORTH,然后0到9以及+,-,*,/等按鈕放置到一個面板Panel中,完成界面設計。
(2)設計計算流程,首先點擊數字按鈕時,將按鈕數值添加到文本框當中,并將該數值保存到一個字符串中,再次點擊數字按鈕時,將之前保存的字符串與新的數值拼接起來,再添加到文本框當中,直到點擊運算符按鈕時,將文本框當中的字符串保存在一個字符串變量中,然后重置文本框內容,將運算符號顯示到文本框中,隨后輸入第二個計算數據時,用同樣的辦法保存數據,最后通過控制“=”運算符先將字符串數據轉化成雙精度類型,然后計算出結果并顯示到文本框當中。
(3)基本運算設計完成以后則開始考慮其他個別功能的實現,例如倒數、清零、退格等功能的實現,清零直接重置文本框內容,退格功能則采用substring函數截取字符串長度。
頁
第 4--計算器 Java實習報告
4.2.2、程序流程圖:
4.2.3、主要代碼展示及說明: 總體代碼的設計:
程序采用繼承windowadapter類,新建Jframe窗體,利用數組來定義JBotton按鈕,同時利用數組注冊監聽,采用4行5列網格布局,完成計算器界面的基本設置,在窗體的正常關閉方面,采用匿名類實現窗體的正常關閉。最后對按鈕進行計算分析,分別設定輸入數據的A類、運算符控制的Opertion類,退格功能的BackSpace類、計算結果的Result類等等,一步步實現計算器的基本功能!
(1)類A的設計(數據的輸入)
class A implements ActionListener { public void actionPerformed(ActionEvent e){
String a = Jtext.getText();
String s = e.getActionCommand();
if(a.equals(“0.”)||a.equals(“+”)||a.equals(“-”)||a.equals(“*”)||a.equals(“/”))
頁
第 5--計算器 Java實習報告
}
} Jtext.setText(s);else { if(flag2){
Jtext.setText(s);
flag2=false;} else
Jtext.setText(a+s);}
功能解釋:程序開始時,程序初始化文本框的內容為“0.”,點擊數字按鈕,則調用類A,首先用a來獲取當前文本框內容,s來獲取按鈕數值,然后進行判斷,若a的值為上述代碼的值則輸出s的值,再次點擊數字按鈕時,再次調用A類,此時a的值為上次輸入的s值,第一個if語句不滿足,執行下個if語句if(flag2),flag2初始值為false,該語句的功能是在執行了“=”號按鈕時,防止新的數字按鈕的值合并到到已經得出的結果上,例如:12+12=24,此時再點擊數字按鈕3時,則文本框內容被重置,輸出數值3,而不是243,如果if(flag2)不滿足,則將字符串a和s合并并輸出,得出第一個要計算的數據。
(2)類Opertion的設計:(運算符的控制)
class Opertion implements ActionListener { public void actionPerformed(ActionEvent e){
cal=e.getActionCommand();
if(flag1==true)
x=Jtext.getText();
Jtext.setText(cal);
flag1=false;}
頁
第 6--計算器 Java實習報告 }
功能解釋:當點擊運算符控制按鈕時,首先將運算符的數值賦值給cal(初值為空),緊接著進行判斷,flag1初值為ture,該類的作用為在點擊運算符按鈕時,將計算的第一個數據保存在x字符串變量當中,然后將文本框內容重置為點擊的運算符的數值,類的結尾將flag1賦值為false,防止再次點擊運算符按鈕時改變了x的值。
(附:此時文本框內容為運算符的值,輸入第二個計算數據時,點擊數字按鈕,則再次調用A類,此時滿足A類中第一個if語句,文本框內容被重置為數字按鈕的值,接下來與獲取第一個計算數據步驟一樣,直到點擊“=”號運算符為止!)
(3)類Result的設計:(計算并輸出結果)
class Result implements ActionListener //計算并顯示結果 { public void actionPerformed(ActionEvent e){
double num1;
num1=Double.parseDouble(x);
y=Jtext.getText();
double num2;
num2=Double.parseDouble(y);
double result=0;
if(num2!=0)
{
if(cal.equals(“+”))
result=num1+num2;
if(cal.equals(“-”))
result=num1-num2;
if(cal.equals(“*”))
result=num1*num2;
String s1=Double.toString(result);
Jtext.setText(s1);
}
if(cal.equals(“/”))
頁
第 7--計算器 Java實習報告
} {
if(num2==0)
Jtext.setText(“除數不能為0”);
else
{
result=num1/num2;
String s1=Double.toString(result);
Jtext.setText(s1);
} }
flag1=true;
flag2=true;} 功能解釋:首先定義兩個Double型num1,num2,將之前保存的第一個計算數據x強制轉換為Double型后賦值給num1,接著用字符串變量y來獲取當前文本框的內容,即第二個計算數據的值,同樣再將其強制轉換Double型后賦值給num2,然后進行運算符判斷,對cal的值進行比較,然后進行相應的計算,將計算的結果轉換成字符串后將其輸出到文本框中,在類的最后將flag1、flag2賦值為true,作用是將計算的結果當作第二次計算的數據進行再運算,即將結果重新賦值給x作為第一個計算數據!(附:在此類中還考慮了當除數為零的情況。)
(4)類BackSpace的設計:(功能類—退格)
class BackSpace implements ActionListener { public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
String s1 = Jtext.getText();
if(s.equals(“退格”))
s1=new String(s1.substring(0,s1.length()-1));
Jtext.setText(s1);} }
頁
第 8--計算器 Java實習報告
功能解釋:這是計算器附加功能的實現,這里只介紹退格功能,像正負號、求倒數、清零等功能相似,所以就不再一一介紹。首先獲取退格按鈕的命令值賦給s,然后獲取當前文本框的內容,即輸入的數據,將其賦給s1,接著進行判斷,利用substring函數將s1字符串截取為從第一個字符至倒數第二個字符為止的字符串并重新賦值給s1,再將其輸出到文本框,實現退格的功能。
第 9
頁 10--計算器 Java實習報告
4.3、程序測試
1.簡單的運算:(以加法為例:123+456)
分析:計算的結果為579.0,為雙精度型,計算的結果被設置在文本框的最右端,該計算器的一個特點是可直接在文本框中輸入數據以及進行更改。
2.倒數的運算:(以123為例)
分析:輸出的結果如圖所示,倒數功能實現,計算時,不僅是結果,輸入的數據同樣可以先實現倒數功能后再進行相應的計算,沒有影響!
3.退格的運算:(以123為例)
分析:輸出的結果如圖所示,本計算器退格鍵有一個特點是,就算是是計算后得出的結果也能實現退格,缺點是不能很好的處理小數點的問題,因為小數點也是字符串的一部分。
頁
第 10--計算器 Java實習報告
4.正負號的運算:(以123為例)
分析:輸出的結果如圖所示,正負號添加能夠很好的實現,但可以進行一些改進,比如在計算過程當中直接點擊負號運算符輸入負數進行計算!
5.總體分析:
該計算器基本運算沒有問題,清零、正負號、求倒數、退格功能都能很好的實現,總體能完成一個計算器的基本功能,但仍有許多地方需要改進,比如小數點的實現所存在的一些問題,雖然在基本的運算過程當中不會造成太大影響,但這依然不能認為是一個很好的計算器,同時,在另一方面,該計算器還沒能很好的實現連續計算的功能,必須每次按下等號按鈕計算出結果后才能用產生的結果接著進行下一次的計算,改進的方法是在運算符上同時注冊Result類,讓運算符同時擁有計算結果的功能。
五、實驗小結
本次課程設計到此算是告一段落了,經過這次的學習,我學到了很多東西,在此基礎上更加鞏固了自己對java的認識與了解。
在做本項目是時候,會遇到很多小問題,比如說,在整個運算過程中要如何確保輸入的計算數據哪個是第一個計算數據的,哪個是第二個計算
頁
第 11--計算器 Java實習報告數據的,同時也要區分運算符,因為該計算器程序應用的都是利用字符串來完成計算的,而且不能重復輸出運算符,更不能將運算符錯誤的存儲在了第一個計算數據的數值中,也得考慮到萬一不小心重復點擊了運算符按鈕會不會造成第一個計算數據的重新賦值等等問題,最后想到利用布爾類型來很好的控制運算符的應用!
此次課程設計讓我更了解熟悉了Java中的圖形用戶界面和它的編程方式。在完成課題的過程中也不斷充實了自己,學習到了很多以前沒有學習到的知識,收獲很大。最大的收獲就是對大學學習的總結和培養了解決困難的信心和能力,使我對所學知識能夠融會貫通,又不斷豐富了新知識。Java計算器設計使得我們對所學的專業課有了更為深刻的認識,使得知識得到了鞏固和提高。
在接下來的時間里,我覺得我要更加努力的往深一層次的方面看齊,了解更多有關java的知識,對java有更深一步的了解,我會一步一步的走下去!
六、附錄(程序代碼)import java.awt.*;import javax.swing.*;import java.awt.event.*;
public class TheCalculator extends WindowAdapter
//程序框架繼承自WindowAdapter類 { private JTextField Jtext=new JTextField(“0.”);private JFrame f=new JFrame(“計算器-趙磊”);private String x=“";private String y=”“;private String cal=”“;private boolean flag1=true;private boolean flag2=false;
public void init()//初始化
{
String[] buttonValue = new String[]{”1“,”2“,”3“,”+“,”C“,”4“,”5“,”6“,”-“,”退格
頁
第 12--計算器 Java實習報告“,”7“,”8“,”9“,”*“,”1/x“,”0“,”+/-“,”.“,”/“,”=“};
Container contain = f.getContentPane();
JPanel Jpan = new JPanel();
JButton[] Jb=new JButton[20];
contain.setLayout(new BorderLayout());//采用4行5列的網格布局
Jpan.setLayout(new GridLayout(4,5));
Jtext.setHorizontalAlignment(JTextField.RIGHT);
contain.add(Jtext,”North“);
contain.add(Jpan);
A num=new A();//數據
Result re=new Result();//結果
Opertion op=new Opertion();//運算符
Clear cl=new Clear();//清零
BackSpace back=new BackSpace();//退格
CountDown count_d=new CountDown();//倒數
Strains stra=new Strains();//相反數
for(int i = 0;i { Jb[i] = new JButton(buttonValue[i]); Jpan.add(Jb[i]); if(i==3 || i==8 || i==13 || i==18) Jb[i].addActionListener(op); if(i==0 || i==1 || i==2 || i==5 || i==6 || i==7|| i==10 || i==11 || i==12 || i==15 || i==17) Jb[i].addActionListener(num); if((i==3||i==4||i==8||i==9)||((i>12)&&(i<=19))&&i!=15) Jb[i].setForeground(new Color(255, 0, 0)); else Jb[i].setForeground(new Color(0, 0, 255));//控制按鈕字體顏色 } Jb[4].addActionListener(cl); Jb[9].addActionListener(back); Jb[14].addActionListener(count_d); Jb[16].addActionListener(stra); Jb[19].addActionListener(re); f.setSize(320,240); f.setVisible(true); f.addWindowListener(//采用匿名類實現窗口的正常關閉 new WindowAdapter() 頁 第 13--計算器 Java實習報告 { public void windowClosing(WindowEvent e) { System.exit(0); } });} class A implements ActionListener //輸入數據 { public void actionPerformed(ActionEvent e){ String a = Jtext.getText(); String s = e.getActionCommand(); if(a.equals(”0.“)||a.equals(”+“)||a.equals(”-“)||a.equals(”*“)||a.equals(”/“)) Jtext.setText(s); else { if(flag2) { Jtext.setText(s); flag2=false; } else Jtext.setText(a+s); } } } class Opertion implements ActionListener { public void actionPerformed(ActionEvent e){ cal=e.getActionCommand(); if(flag1==true) x=Jtext.getText(); Jtext.setText(cal); flag1=false;} } 頁 第 14--計算器 Java實習報告 class Clear implements ActionListener //清零功能 { public void actionPerformed(ActionEvent e){ Jtext.setText(”0.“);} } class CountDown implements ActionListener //求倒數類 { public void actionPerformed(ActionEvent e){ String s = e.getActionCommand(); String s1 = Jtext.getText(); if(s.equals(”1/x“)) s1 = new String(”“+1/Double.parseDouble(s1)); Jtext.setText(s1);} } class Strains implements ActionListener //求相反數類 { public void actionPerformed(ActionEvent e){ String s = e.getActionCommand(); String s1 = Jtext.getText(); if(s.equals(”+/-“)) s1=new String(”“+(0-Double.parseDouble(s1))); Jtext.setText(s1);} } class BackSpace implements ActionListener //退格功能 { public void actionPerformed(ActionEvent e){ String s = e.getActionCommand(); String s1 = Jtext.getText(); if(s.equals(”退格“)) s1=new String(s1.substring(0,s1.length()-1)); Jtext.setText(s1);} 頁 第 15--計算器 Java實習報告 } class Result implements ActionListener //計算并顯示結果 { public void actionPerformed(ActionEvent e){ double num1; num1=Double.parseDouble(x); y=Jtext.getText(); double num2; num2=Double.parseDouble(y); double result=0; if(num2!=0) { if(cal.equals(”+“)) result=num1+num2; if(cal.equals(”-“)) result=num1-num2; if(cal.equals(”*“)) result=num1*num2; String s1=Double.toString(result); Jtext.setText(s1); } if(cal.equals(”/“)) { if(num2==0) Jtext.setText(”除數不能為0"); else { result=num1/num2; String s1=Double.toString(result); Jtext.setText(s1); } } flag1=true; flag2=true;} } public static void main(String[] args)//main方法 { 頁 第 16--計算器 Java實習報告 } } TheCalculator count=new TheCalculator();count.init(); 頁 第 17 1、設計目的 運用MATLAB實現MATLAB的GUI程序設計。 2、題目分析 2.1課程設計的基本要求: A.熟悉和掌握MATLAB 程序設計方法。B.掌握MATLAB GUI程序設計。2.2課程設計的內容 要求利用MATLAB GUI設計實現一個圖形用戶界面的計算器程序,要求實現: A.具有友好的用戶圖形界面。實現十進制數的加、減、乘、除、乘方、取模等簡單計算。 B.科學計算函數,包括(反)正弦、(反)余弦、(反)正切、(反)余切、開方、指數等函數運行。 C.能夠保存上次歷史計算的答案,顯示答案存儲器中的內容。D.有清除鍵,能清除操作。 E.獨立存儲器功能,使之可以直接輸入存儲器,可與存儲器中的數值相加減。能夠清除獨立存儲器中的內容。2.3題目分析 本題目通過MATLAB的GUI程序設計較為簡單,在GUI設計中主要用到三種控件,顯示框用到文本編輯框(edit text),說明框用到靜態文本框(Static text),數字以及運算等按鈕用到命令按鈕(push button)。然后再通過各個按鈕的回調函數,實現簡單的計算功能。 3、總體設計 首先用MATLAB GUI功能,在繪制一個靜態文本框和一個文本編輯框,以及32個命令按鈕,調整好各控件大小、顏色,整體布局如圖所示: 然后通過雙擊個按鈕來改寫其屬性,在m文件中編寫其回調函數,最后在運行調試。 4、具體設計 4.1 各功能界面設計 GUI設計界面: 4.2 各功能模塊實現 算法設計: A.數字鍵設計:0—9以及小數點函數都一樣,只是參數不同: global jj textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','1');else textString =strcat(textString,'1');set(handles.text1,'String',textString)end jj=0;B.四則運算函數: textString = get(handles.text1,'String');textString =strcat(textString,'+');set(handles.text1,'String',textString)C.科學計算函數: textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.');else a = strread(textString, '%f');a=sin(a);set(handles.text1,'String',a)end 或 textString=handles.text1;textString=sin(str2num(get(handles.text1,'String'))*pi/180);set(handles.text1,'String',num2str(textString))D.退格鍵:通過取屏幕值,計算出其字符長度,然后取其前N-1項的值來實現退格: global jj textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','0.');else ss=char(textString);l=length(textString);textString=ss(1:l-1);set(handles.text1,'String',textString)end jj=0;E.清屏鍵函數: set(handles.text1,'String','0.');F.右鍵函數: gtext('大家好;我是智能機器人-my name is seven');close(gcf);4.3 各模塊實現結果 A.數字鍵: B.四則運算函數: C.科學計算函數: Sin45的計算結果= 經過計算,這些結果均與實際結果相吻合,計算器的功能實現的較為完好。5.2問題和解決方法: a.小數點可以連續輸入。解決方法是:用strfind函數查看文本框里有幾個小數點,如果已經有一個了,再按小數點就保持不變。b.按過運算符號后一個數不等于一個數,比如:輸入1,按等號,會出來一個3,經過長時間分析得知,這是由于在按運算符號時,系統記錄了文本框里的數但沒有清空,才會出現這種問題。解決方法是再申請一個不同于加減乘除的另一個符號,并將按過運算符后記錄的數值置0。 c.按對數函數鍵時,負數也能運算,通過加入if判斷語句來判斷輸入的值是否為負,若為負則輸出error.6、心得體會 通過本次的MATLAB課程設計,讓我對MATLAB尤其是其GUI設計的功能有了進一步的了解,認識到了它功能的強大。在MATLAB簡單計算器的設計中,了解了關于MATLAB圖形用戶界面的部分控件的使用方法;利用MATLAB的GUI提供的很多實用的控件,方便用于設計屬于自己的圖形界面。 7、附錄(源代碼) function varargout = jisuanqi(varargin)% JISUANQI M-file for jisuanqi.fig % JISUANQI, by itself, creates a new JISUANQI or raises the existing % singleton*.% % H = JISUANQI returns the handle to a new JISUANQI or the handle to % the existing singleton*.% % JISUANQI('CALLBACK',hObject,eventData,handles,...)calls the local % function named CALLBACK in JISUANQI.M with the given input arguments.% % JISUANQI('Property','Value',...)creates a new JISUANQI or raises the % existing singleton*.Starting from the left, property value pairs are % applied to the GUI before jisuanqi_OpeningFunction gets called.An % unrecognized property name or invalid value makes property application % stop.All inputs are passed to jisuanqi_OpeningFcn via varargin.% % *See GUI Options on GUIDE's Tools menu.Choose “GUI allows only one % instance to run(singleton)”.% % See also: GUIDE, GUIDATA, GUIHANDLES % Copyright 2002-2003 The MathWorks, Inc.% Edit the above text to modify the response to help jisuanqi % Last Modified by GUIDE v2.5 04-Dec-2012 17:06:43 % Begin initialization codeDO NOT EDIT %---Executes just before jisuanqi is made visible.function jisuanqi_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)% Get default command line output from handles structure varargout{1} = handles.output; function edit1_Callback(hObject, eventdata, handles)% hObject handle to edit1(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles emptyto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global jj textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','1');else textString =strcat(textString,'1');set(handles.text1,'String',textString)end jj=0; %---Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global jj textString = get(handles.text1,'String'); if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','3');else textString =strcat(textString,'3');set(handles.text1,'String',textString)end jj=0; %---Executes on button press in pushbutton13.function pushbutton13_Callback(hObject, eventdata, handles)% hObject handle to pushbutton13(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global jj textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','-');else textString = get(handles.text1,'String');textString =strcat(textString,'-');set(handles.text1,'String',textString)end jj=0; %---Executes on button press in pushbutton21.function pushbutton21_Callback(hObject, eventdata, handles)% hObject handle to pushbutton21(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)textString = get(handles.text1,'String');%strcmp(textString,'0.')textString=handles.text1; textString=sin(str2num(get(handles.text1,'String'))*pi/180);set(handles.text1,'String',num2str(textString))%a = strread(textString, '%f')%textString=get(handles.text1,'String')%textString=strcat(textString,'sin')%set(handles.text1,'String',textString)%---Executes on button press in pushbutton23.function pushbutton23_Callback(hObject, eventdata, handles)% hObject handle to pushbutton23(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global jj textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','4');else textString =strcat(textString,'4');set(handles.text1,'String',textString)end jj=0; %---Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)% hObject handle to pushbutton5(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global jj textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','6');else textString =strcat(textString,'6');set(handles.text1,'String',textString)end jj=0; %---Executes on button press in pushbutton15.function pushbutton15_Callback(hObject, eventdata, handles)% hObject handle to pushbutton15(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global jj textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','/');else textString = get(handles.text1,'String');textString =strcat(textString,'/');set(handles.text1,'String',textString)end jj=0; %---Executes on button press in pushbutton24.function pushbutton24_Callback(hObject, eventdata, handles)% hObject handle to pushbutton24(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)textString = get(handles.text1,'String');%strcmp(textString,'0.')textString=handles.text1; textString=cos(str2num(get(handles.text1,'String'))*pi/180);set(handles.text1,'String',num2str(textString))%---Executes on button press in pushbutton26.function pushbutton26_Callback(hObject, eventdata, handles)% hObject handle to pushbutton26(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global jj textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','7');else textString =strcat(textString,'7');set(handles.text1,'String',textString)end jj=0; %---Executes on button press in pushbutton8.function pushbutton8_Callback(hObject, eventdata, handles)% hObject handle to pushbutton8(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global jj textString = get(handles.text1,'String'); if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','9');else textString =strcat(textString,'9');set(handles.text1,'String',textString)end jj=0; %---Executes on button press in pushbutton17.function pushbutton17_Callback(hObject, eventdata, handles)% hObject handle to pushbutton17(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)set(handles.text1,'String',')');else textString =strcat(textString,')');set(handles.text1,'String',textString)end %---Executes on button press in pushbutton27.function pushbutton27_Callback(hObject, eventdata, handles)% hObject handle to pushbutton27(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)textString = get(handles.text1,'String');%strcmp(textString,'0.')textString=handles.text1; textString=tan(str2num(get(handles.text1,'String'))*pi/180);set(handles.text1,'String',num2str(textString))%---Executes on button press in pushbutton29.function pushbutton29_Callback(hObject, eventdata, handles)% hObject handle to pushbutton29(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA) global jj textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','0.');else textString =strcat(textString,'0');set(handles.text1,'String',textString)end jj=0; %---Executes on button press in pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles)% hObject handle to pushbutton11(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)set(handles.text1,'String','0.'); %---Executes on button press in pushbutton19.function pushbutton19_Callback(hObject, eventdata, handles)% hObject handle to pushbutton19(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)textString=get(handles.text1,'String')s=eval(textString) set(handles.text1,'String',s) %---Executes on button press in pushbutton30.function pushbutton30_Callback(hObject, eventdata, handles)% hObject handle to pushbutton30(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)textString = get(handles.text1,'String');%strcmp(textString,'0.')textString=handles.text1; textString=cot(str2num(get(handles.text1,'String'))*pi/180);set(handles.text1,'String',num2str(textString))%---Executes on button press in pushbutton32.function pushbutton32_Callback(hObject, eventdata, handles)% hObject handle to pushbutton32(see GCBO)% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)%open('1.bmp') gtext('大家好;我是智能機器人-my name is seven'); %------function J_Callback(hObject, eventdata, handles)% hObject handle to J(see GCBO) % eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)close(gcf); 8、參考書目: [1]《MATLAB語言及其在電子信息工程中的應用》 王洪元主編 清華大學出版社 [2] 《MATLAB中GUI的應用》 王洪元主編 清華大學出版社 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 機電信息工程學院 單片機系統課程設計報告 完成日期:2010年5月31日 系: 專 業: 班 級: 設計題目: 學生姓名: 指導教師: 多功能簡易計算器 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 目 錄 一、設計任務和性能指標......................................................................2 1.1設計任務..............................................................................................................................2 1.2性能指標..............................................................................................................................2 二.設計方案.............................................................................................2 三.系統硬件設計.....................................................................................3 3.1單片機最小系統......................................................................................3 3.2鍵盤接口電路.....................................................................................................................3 3.3數碼管顯示電路.................................................................................................................4 3.4錯誤報警電路.....................................................................................................................5 四、系統軟件設計..................................................................................6 4.1鍵盤掃描子程序設計..........................................................................................................6 4.2移位子程序及結果計算子程序設計................................................................................10 4.3顯示子程序設計...............................................................................................................12 4.4主程序設計.......................................................................................................................13 五、調試及性能分析............................................................................13 5.1調試步驟...........................................................................................................................13 5.2性能分析...........................................................................................................................14 六、心得體會........................................................................................14 參考文獻................................................................................................14 附錄1 系統硬件電路圖.......................................................................15 附錄2 程序清單.................................................................................16 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 一、設計任務和性能指標 1.1設計任務 利用單片機及外圍接口電路(鍵盤接口和顯示接口電路)設計制作一個計算器,用LED顯示計算數值及結果。 要求用Protel 畫出系統的電路原理圖(要求以最少組件,實現系統設計所要求的功能),印刷電路板(要求布局合理,線路清晰),繪出程序流程圖,并給出程序清單(要求思路清晰,盡量簡潔,主程序和子程序分開,使程序有較強的可讀性)。1.2性能指標 1.2.3.4.5.加法:四位加法,計算結果若超過四位則顯示計算錯誤 減法:四位減法,計算結果若小于零則顯示計算錯誤 乘法:個位數乘法 除法:整數除法 有清零功能,計算錯誤報警 二.設計方案 按照系統設計的功能的要求,初步確定設計系統由主控模塊、顯示模塊、錯誤報警模塊、鍵掃描接口電路共四個模塊組成,電路系統構成框圖如圖1.1所示。主控芯片使用51系列AT89C52單片機,采用高性能的靜態80C51設計,由先進工藝制造,并帶有非易失性Flash程序存儲器。它是一種高性能、低功耗的8位COMS微處理芯片,市場應用最多。 鍵盤電路采用4*4矩陣鍵盤電路。 顯示模塊采用4枚共陽極數碼管和74ls273鎖存芯片構成等器件構成。 錯誤報警電路采用5V蜂鳴器。 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 三.系統硬件設計 3.1單片機最小系統 單片機最小系統就是支持主芯片正常工作的最小電路部分,包括主控芯片、復位電路和晶振電路。 主控芯片選取STC89C52RC芯片,因其具有良好的性能及穩定性,價格便宜應用方便。 晶振選取11.0592MHz,晶振旁電容選取30pF。 采用按鍵復位電路,電阻分別選取100Ω和10K,電容選取10μF。以下為單片機最小系統硬件電路圖。 單片機最小系統硬件電路 3.2鍵盤接口電路 計算器所需按鍵有: 數字鍵:’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’,’0’ 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 功能鍵:’+’, ’-‘ , ’*’, ’/ ’ , ’ = ’, ’ C(清零)’ 共計16個按鍵,采用4*4矩陣鍵盤,鍵盤的行和列之間都有公共端相連,四行和四列的8個公共端分別接P1.0~P1.7,這樣掃描P1口就可以完成對矩陣鍵盤的掃描,通過對16個按鍵進行編碼,從而得到鍵盤的口地址,對比P1口德掃描結果和各按鍵的地址,我們就可以得到是哪個鍵按下,從而完成鍵盤的功能。 以下為鍵盤接口電路的硬件電路圖 鍵盤接口電路 3.3數碼管顯示電路 采用4位數碼管對計算數據和結果的顯示,這里選取共陽數碼管,利用NPN三極管對數碼管進行驅動,為了節省I/O資源,采取動態顯示的方法來顯示計算數據及結果。 利用74273鎖存器來實現數碼管的動態顯示,P0口輸出顯示值,P2.4為段選口,控制273鎖存器的時鐘引腳,從而得到對數碼管輸入數據的控制。 P2.0~P2.3用來作為位選端,控制哪幾位數碼管進行顯示。 以下為數碼顯示電路的硬件電路圖 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 數碼顯示電路硬件電路圖 3.4錯誤報警電路 錯誤報警電路就是在計算結果出現錯誤時或輸入數據出現錯誤時,發出聲音警報,提示使用者錯誤出現。 這里就采用5V蜂鳴器作為報警設備,利用PNP三極管對蜂鳴器進行驅動,有P2.5對其進行控制,這樣在出現錯誤的同時用P2.5輸出低,就可以使蜂鳴器工作,完成報警任務。 以下為報警電路硬件電路圖 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 報警電路硬件電路圖 系統整體硬件電路圖見附錄一 四、系統軟件設計 4.1鍵盤掃描子程序設計 要進行數據的計算就必須先進行數據的輸入,也就必須確定按鍵輸入的數值是什么,這就需要對鍵盤進行掃描,從而確定究竟是哪個鍵按下。 對于鍵盤的掃描,既可以用行掃描也可以用列掃描,這里采用行掃描的方法來完成對鍵盤的掃描。 行掃描就是逐行掃描鍵盤,看是哪一行有鍵按下,再通過返回的鍵碼來確定究竟是哪個按鍵按下。如對第一行掃描就令P1.0為低,P1口其余為高,這樣若第一行有鍵按下,則P1口的值就會由0xfe變為其他值,再由這個值來確定是哪個鍵按下。 以下為鍵盤掃描子程序的程序清單。 uchar keyscan(){ int i; P1=0xfe; temp=P1; temp=temp&0xf0;大連民族學院2007級電子信息工程專業單片機系統課程設計報告 while(temp!=0xf0){ delay(5); temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xee:{rdat++;num=1;left(rdat,num);} break; case 0xde:{rdat++;num=2;left(rdat,num);} break; case 0xbe:{rdat++;num=3;left(rdat,num);} break; case 0x7e:{rdat++;num=4;left(rdat,num);} break; } while(temp!=0xf0) { temp=P1; temp=temp&0xf0; } } } P1=0xfd;temp=P1;temp=temp&0xf0;while(temp!=0xf0){ delay(5); temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xed:{rdat++;num=5;left(rdat,num);} break; case 0xdd:{rdat++;num=6;left(rdat,num);} break;大連民族學院2007級電子信息工程專業單片機系統課程設計報告 } case 0xbd:{rdat++;num=7;left(rdat,num);} break; case 0x7d:{rdat++;num=8;left(rdat,num);} break;} while(temp!=0xf0){ temp=P1; temp=temp&0xf0;} } P1=0xfb;temp=P1;temp=temp&0xf0;while(temp!=0xf0){ delay(5); temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xeb:{rdat++;num=9;left(rdat,num);} break; case 0xdb:{rdat++;num=10;left(rdat,num);} break; case 0xbb:{equ();} break; case 0x7b:{rdat=0;add=0;subb=0;mul=0;div=0; result=0; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break; } while(temp!=0xf0) { temp=P1; temp=temp&0xf0; } 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 } } P1=0xf7;temp=P1;temp=temp&0xf0;while(temp!=0xf0){ delay(5); temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xe7:{rdat=0;add=1;subb=0;mul=0;div=0; for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } result=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break; case 0xd7:{rdat=0;add=0;subb=1;mul=0;div=0; for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } result=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break; case 0xb7:{rdat=0;add=0;subb=0;mul=1;div=0; for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } result=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break; case 0x77:{rdat=0;add=0;subb=0;mul=0;div=1;大連民族學院2007級電子信息工程專業單片機系統課程設計報告 } return num;} } while(temp!=0xf0){ temp=P1; temp=temp&0xf0;} } for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } result=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break;4.2移位子程序及結果計算子程序設計 輸入數據要存儲在一四位數組內,而我們鍵入的值是數據的高位,后鍵入的值是低位,這樣我們就需要在輸入低位數值時將高位數值從數組的低位移向數組的高位,這就是編寫移位子程序的目的。 對于結果計算子程序,包含加、減、乘、除四種運算。以加法運算為例,各種運算各有其標志位來代表計算類型,當加法標志位add=1是,就將輸入的兩個數據按照加法進行計算。 首先將數組內的數按照對應的位關系,將其轉化為一個十進制數,這樣我們就得到了加速和被加數這樣倆個十進制數,從而我們就可以簡單的將兩個數進行相加,結果就是我們所求的數值。但這個數值不能直接顯示到數碼管上,我們還要對其進行處理,使其變為對應進位的四個數存入數組內,以便顯示。既通過對結果數值分別除以1000、100、10和對10取余,得到我們想要的四個數,送顯示子程序顯示。其余減、乘、除的計算方法與加法的計算方法一樣,這里不再累述。 以下為移位子程序和結果計算子程序的程序清單。 void left(uchar rx,uchar date){ switch(rx) { case 1:dat[0]=date;break;大連民族學院2007級電子信息工程專業單片機系統課程設計報告 void equ(){ int i,j,k; long int s; if(add==1){for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } s=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; result=result+s;add=0;} if(subb==1){for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } s=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; result=result-s;subb=0;} if(mul==1){for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } s=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; result=result*s;mul=0; } if(div==1){for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } s=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; result=result/s;div=0; } If(result>9999){dat[0]=11;dat[3]=dat[2]=dat[1]=0;} if(result<=9999) { dat[0]=result%10; dat[1]=(result/10)%10; dat[2]=(result/100)%10; dat[3]=(result/1000)%10; } for(j=3;j>0;j--) { if(dat[j]>0) { case 2:dat[1]=dat[0],dat[0]=date;break; case 3:dat[2]=dat[1],dat[1]=dat[0],dat[0]=date;break; case 4:dat[3]=dat[2],dat[2]=dat[1],dat[1]=dat[0],dat[0]=date;break; } } 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 for(k=j-1;k>=0;k--) { if(dat[k]==0){dat[k]=10;} } } } if(dat[0]==0){dat[0]=10;} } 4.3顯示子程序設計 從始至終無論是輸入的計算數據,還是計算后的結果值。都存儲在同一數組dat[ ]中,這樣我們只要在顯示時一直調用dat[ ]中的值,就能正確的顯示數據。 以下為顯示子程序的程序清單。 void display(){ uchar aa; keyscan(); P2=0x07; aa=dat[0]; P0=table[aa]; P2=0x27; delay(3); P2=0x0b; aa=dat[1]; P0=table[aa]; P2=0x2b; delay(3); P2=0x0d; aa=dat[2]; P0=table[aa]; P2=0x2d; delay(3); P2=0x0e; aa=dat[3]; P0=table[aa]; P2=0x2e; delay(3); } 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 4.4主程序設計 主程序既把以上各子程序串連成一個整體,使整個程序循環運行。而在以上程序中也已經加入了個程序之間的連接點,首先進入程序后就立即進入顯示子程序,而顯示子程序內又調用鍵盤掃描子程序,若有鍵按下,則會跳轉到移位子程序和結果計算子程序進行相應的處理。通過計算或移位后,數組內的值發生改變,顯示的值也會同時發生改變。之后再進行鍵盤掃描,如此反復運行,就構成了程序的整體。 以下為主程序清單。 void main(){ num=0; while(1) { display(); } } 整體程序清單見附錄二。 五、調試及性能分析 5.1調試步驟 在焊接好器件后,先不要將芯片插在芯片座上,要先驗證先板上電源是否好用,有無短路等。接上USB電源,用萬用表測量個芯片座對應電源和地之間的電壓值,觀察電壓值是否正常。一切正常后方可將芯片插入芯片座,以繼續測試其他功能。 將芯片插上后,對各個模塊進行調試,按鍵是否工作正常,數碼管是否顯示正常等。編寫相關部分的測試程序對其進行測試。 各部分硬件檢測無誤后,下載程序進行整體調試,一切正常后,結束調試過程。 在具體調試時首先遇到的問題是程序無法下載進入單片機,通過將電路板接線與原理電路圖接線的對比發現,串口芯片與單片機連接的輸入,輸出接反,重新用銅線連接后,依然無法下載程序。后找到原因是由于下載串口與設計封裝不符,用相對應的下載線可以下載。 成功下載程序后,發現數碼管顯示不正確,查看后發現有先沒有連接,可能是制板時漏印,連接后顯示正常。大連民族學院2007級電子信息工程專業單片機系統課程設計報告 5.2性能分析 對于計算器的性能,主要的衡量指標就在于計算的精度,本次制作的計算器性能情況如下: 加法運算:四位加法運算,和值不超過9999,若超過上限,則顯示錯誤提示E,蜂鳴器報警提示。 減法運算:四位減法運算,若結果為負,對其取絕對值。 乘法運算:積不超過9999的乘法運算,若超出上限,顯示錯誤提示E,蜂鳴器報警提示。 除法運算:整數除法,既計算結果為整數,若除數為零,則顯示錯誤提示E,蜂鳴器報警提示。 通過對實際性能的分析,可以得到本次設計滿足設計的要求。 六、心得體會 通過本次課程設計我真正的自己完成了對給定要求系統的硬件設計、電路設計、電路板設計、軟件設計以及對成品的調試過程。從整個過程中學習到了很多方面的知識,了解到以往學習中自己知識在某方面的不足之處,是對以往學習科目的一種貫穿和承接,從而能更好的認識和學習,也對將來從事工作大有裨益。 從本次課設中我也看到了自身的很多不足之處,對知識的掌握不夠扎實,有一知半解的現象。有時做事不夠穩定,過于毛躁,不能平心靜氣的去分析所遇到的問題和錯誤。這在以后的工作和生活中是不可取的,通過對自身問題的認識與改正相信再遇到同樣問題時會更好的解決。以后的設計實驗也會更好的完成。 參考文獻 [1] 徐維祥、劉旭敏.單片微型機原理及應用.大連:大連理工大學出版社,1996 [2] 李光飛、樓然苗、胡佳文、謝象佐.單片機課程設計與實例指導.北京: 北京航空航天大學出版社,2004 [3] 余永權.89系列FLASH單片機原理及應用.北京:電子工業出版社,2002 [4] 李群芳,黃建.單片機微型計算機與接口技術.北京:電子工業出版社,2001 [5] 樓然苗、李光飛.51系列單片機設計實例.北京:北京航空航天大學出版社,2003 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 附錄1 系統硬件電路圖 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 附錄2 程序清單 #include for(y=110;y>0;y--);} void display();void main(){ num=0; while(1) { display(); } } void display(){ uchar aa; keyscan(); P2=0x07; aa=dat[0];大連民族學院2007級電子信息工程專業單片機系統課程設計報告 P0=table[aa]; P2=0x27; delay(3); P2=0x0b; aa=dat[1]; P0=table[aa]; P2=0x2b; delay(3); P2=0x0d; aa=dat[2]; P0=table[aa]; P2=0x2d; delay(3); P2=0x0e; aa=dat[3]; P0=table[aa]; P2=0x2e; delay(3); } uchar keyscan(){ int i; P1=0xfe; temp=P1; temp=temp&0xf0; while(temp!=0xf0) { delay(5); temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xee:{rdat++;num=1;left(rdat,num);} break; case 0xde:{rdat++;num=2;left(rdat,num);} break; case 0xbe:{rdat++;num=3;left(rdat,num);} break;大連民族學院2007級電子信息工程專業單片機系統課程設計報告 } case 0x7e:{rdat++;num=4;left(rdat,num);} break;} while(temp!=0xf0){ temp=P1; temp=temp&0xf0;} } P1=0xfd;temp=P1;temp=temp&0xf0;while(temp!=0xf0){ delay(5); temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xed:{rdat++;num=5;left(rdat,num);} break; case 0xdd:{rdat++;num=6;left(rdat,num);} break; case 0xbd:{rdat++;num=7;left(rdat,num);} break; case 0x7d:{rdat++;num=8;left(rdat,num);} break; } while(temp!=0xf0) { temp=P1; temp=temp&0xf0; } } } P1=0xfb;temp=P1;大連民族學院2007級電子信息工程專業單片機系統課程設計報告 temp=temp&0xf0;while(temp!=0xf0){ delay(5); temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp) { case 0xeb:{rdat++;num=9;left(rdat,num);} break; case 0xdb:{rdat++;num=10;left(rdat,num);} break; case 0xbb:{equ();} break; case 0x7b:{rdat=0;add=0;subb=0;mul=0;div=0; result=0; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break; } while(temp!=0xf0) { temp=P1; temp=temp&0xf0; } } } P1=0xf7;temp=P1;temp=temp&0xf0;while(temp!=0xf0){ delay(5); temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp)大連民族學院2007級電子信息工程專業單片機系統課程設計報告 { case 0xe7:{rdat=0;add=1;subb=0;mul=0;div=0; for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } result=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break; case 0xd7:{rdat=0;add=0;subb=1;mul=0;div=0; for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } result=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break; case 0xb7:{rdat=0;add=0;subb=0;mul=1;div=0; for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } result=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break; case 0x77:{rdat=0;add=0;subb=0;mul=0;div=1; for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } result=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; dat[0]=10;dat[1]=dat[2]=dat[3]=0; } break; } while(temp!=0xf0) { temp=P1; temp=temp&0xf0; } } 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 } return num;} void left(uchar rx,uchar date){ switch(rx) { case 1:dat[0]=date;break; case 2:dat[1]=dat[0],dat[0]=date;break; case 3:dat[2]=dat[1],dat[1]=dat[0],dat[0]=date;break; case 4:dat[3]=dat[2],dat[2]=dat[1],dat[1]=dat[0],dat[0]=date;break; } } void equ(){ int i,j,k;long int s; if(add==1){for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } s=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; result=result+s;add=0;} if(subb==1){for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } s=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; if(s>result){result=s-result;} else result=result-s;subb=0;} if(mul==1){for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } s=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; result=result*s;mul=0; } 大連民族學院2007級電子信息工程專業單片機系統課程設計報告 if(div==1){for(i=0;i<5;i++){ if(dat[i]==10){dat[i]=0;} } s=dat[0]+10*dat[1]+100*dat[2]+1000*dat[3]; if(s==0)result=10000; else result=result/s;div=0; } if(result>9999){dat[0]=11;dat[3]=dat[2]=dat[1]=0;} if(result<=9999){ dat[0]=result%10;dat[1]=(result/10)%10;dat[2]=(result/100)%10;dat[3]=(result/1000)%10;} for(j=3;j>0;j--) { if(dat[j]>0) { for(k=j-1;k>=0;k--) { if(dat[k]==0){dat[k]=10;} } } } if(dat[0]==0){dat[0]=10;} } 22 課 程 設 計 課程名稱 Java語言課程設計 題目名稱 人事管理系統的設計與實現 學生學院 應用數學學院 專業班級 學 號 學生姓名 指導教師 劉科峰 2014 年 10 月 29 日 一.設計內容 自學Swing圖形界面設計和數據庫程序設計。開發用于某單位的人事管理系統。該系統要求實現以下功能: 1.員工信息管理包括:員工信息一覽表、新員工的建立、修改 2.薪資福利管理:考勤與薪資的連動計算、薪資與福利的設置 3.考勤假期管理:考勤項目錄入, 實現考勤信息的查詢、統計、匯總 4.人事報表管理:按照員工性別,數目,薪酬自動生成相關報表, 可自定義統計分析條件并可對歷史報表進行管理。 設計亮點 1.null布局方式 Swing圖形界面設計中有很多布局方式,通過本次學習了解到了FlowLayout,BorderLayout,GridLayout以及null布局,就這四種布局方式而言,null布局的優點在于可以將組建的位置精準到坐標值,可以把組件放到任意想放置的位置,另外可以通過null去設置組件的大小,相較于其他布局方式來說是一個很大的優勢。但同時它的缺點在于不能根據Frame的大小變化而改變位置。 2.組件部分屬性的設計 Swing中有很多方法是用來實現組件屬性設置的,比如button.setVisible(false);按鈕通過調用這個方法實現不可見,這是我此次課程設計中的一個亮點,因為組件是沒有設置大小的方法的,只有依靠于相應的布局方式才能改變它的大小,通過設置多幾個按鈕設為不可見,將所要可見按鈕實現大小以及位置的控制是一種很好的方法。 二、設計方案 1主界面(只列出界面設計方面的代碼) JPanel panel=new JPanel();JTable table=null;JButton ibtn=new JButton(“員工信息”);JButton cbtn=new JButton(“考勤管理”);JButton mbtn=new JButton(“薪資管理”);JButton tbtn=new JButton(“人事報表”);ImageIcon rtou = new ImageIcon(“rentou.jpg”); JLabel imgLabel=new JLabel(rtou); //上面代碼定義了一個面板與四個標簽,并引入了一個圖片 panel.setLayout(new FlowLayout());//設置面板為流布局 panel.add(ibtn);panel.add(cbtn);panel.add(mbtn);panel.add(tbtn);panel.setBackground(Color.GRAY); //將按鈕加到面板中,設置面板為灰色 this.add(imgLabel,BorderLayout.CENTER);this.add(panel,BorderLayout.SOUTH);this.setBounds(300, 180, 600, 350);this.setVisible(true);this.getContentPane().setBackground(Color.GRAY);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*圖片標簽以邊框布局放置在中間,將面板也以邊框布局放置在下面,可見,灰色,點擊關閉程序*/ (2)查詢員工界面(只列出界面設計方面的代碼) Label idlabel = new JLabel(“ID”);JLabel namelabel = new JLabel(“姓名”);JLabel bumenlabel = new JLabel(“部門”);JLabel zhiweilabel = new JLabel(“職位”); JTextField idfiled = new JTextField(8);JTextField namefield = new JTextField(8);JComboBox bumenbox =new JComboBox();JComboBox zhiweubox = new JComboBox(); JButton btn=new JButton(“查詢”);JButton ibtn=new JButton(“添加”) JPanel spanel=new JPanel();JPanel vpanel=new JPanel(); /*定義和各個組件,4個標簽,兩個文本框,兩個復選框,兩個按鈕,兩個面板容器*/ spanel.setLayout(new FlowLayout()); vpanel.setLayout(new FlowLayout(FlowLayout.CENTER,45,25)); bumenbox.addItem(“"); bumenbox.addItem(”管理部“); bumenbox.addItem(”生產部“); bumenbox.addItem(”業務部“); zhiweubox.addItem(”“); zhiweubox.addItem(”部長“); zhiweubox.addItem(”副部長“); zhiweubox.addItem(”普工“); spanel.add(idlabel); spanel.add(idfiled); spanel.add(namelabel); spanel.add(namefield); spanel.add(bumenlabel); spanel.add(bumenbox); spanel.add(zhiweilabel); spanel.add(zhiweubox); spanel.add(btn); vpanel.add(ibtn); spanel.setBackground(Color.GRAY); vpanel.setBackground(Color.GRAY); //將各個組件放進對應面板,設置相關屬性 this.add(spanel,BorderLayout.NORTH); this.add(vpanel,BorderLayout.SOUTH); this.setBounds(300, 180, 600, 350); this.setVisible(true); this.getContentPane().setBackground(Color.GRAY); //將面板以邊框布局放入框架中,設置屬性(3)增加員工信息界面 JPanel panel=new JPanel(); JLabel timeLabel=new JLabel(”時間“);JLabel allnumLabel=new JLabel(”員工總數“); JLabel staffMoLabel=new JLabel(”考勤“);JLabel entryLabel=new JLabel(”全勤“); JLabel quitLabel=new JLabel(”缺勤“);JLabel mwLabel=new JLabel(”性別比例“); JLabel manLabel=new JLabel(”男“); JLabel womanLabel=new JLabel(”女“);JLabel proportionLabel=new JLabel(”男女比例“);JLabel distributionLabel=new JLabel(”人員部門分布“);JLabel mdpLabel=new JLabel(”管理部“); JLabel pdpLabel=new JLabel(”生產部“);JLabel bdpLabel=new JLabel(”業務部“);JLabel xmLabel=new JLabel(”項目“);JLabel xzLabel=new JLabel(”細則“);JLabel stLabel=new JLabel(”數據統計“); JTextField timeFiled = new JTextField(8); JTextField allnumFiled = new JTextField(8);JTextField entryFiled = new JTextField(8);JTextField quitFiled = new JTextField(8);JTextField manFiled = new JTextField(8);JTextField womanFiled = new JTextField(8);JTextField proportionFiled = new JTextField(8);JTextField mdpFiled = new JTextField(8);JTextField pdpFiled = new JTextField(8);JTextField bdpFiled = new JTextField(8);// 定義所需組件 public Selectrenshi(String tablename){ super(”人事管理“); panel.setLayout(null); panel.setBackground(Color.GRAY); panel.add(xmLabel); panel.add(xzLabel); panel.add(stLabel); panel.add(timeLabel); panel.add(timeFiled); panel.add(allnumLabel); panel.add(allnumFiled); panel.add(staffMoLabel); panel.add(entryLabel); panel.add(entryFiled); panel.add(quitLabel); panel.add(quitFiled); panel.add(mwLabel); panel.add(manLabel); panel.add(manFiled); panel.add(womanLabel); panel.add(womanFiled); panel.add(proportionLabel); panel.add(proportionFiled); panel.add(distributionLabel); panel.add(mdpLabel); panel.add(mdpFiled); panel.add(pdpLabel); panel.add(pdpFiled); panel.add(bdpLabel); panel.add(bdpFiled); //將組件加到相應面板中 xmLabel.setBounds(120,20,100,30); xzLabel.setBounds(240,20,100,30); stLabel.setBounds(360,20,100,30); timeLabel.setBounds(120,50,100,30); timeFiled.setBounds(360,50,100,30); allnumLabel.setBounds(120,100,100,30); allnumFiled.setBounds(360,100,100,30); staffMoLabel.setBounds(120,150,100,30); entryLabel.setBounds(240,150,100,30); entryFiled.setBounds(360,150,100,30); quitLabel.setBounds(240,200,100,30); quitFiled.setBounds(360,200,100,30); mwLabel.setBounds(120,250,100,30); manLabel.setBounds(240,250,100,30); manFiled.setBounds(360,250,100,30); womanLabel.setBounds(240,300,100,30); womanFiled.setBounds(360,300,100,30); proportionLabel.setBounds(240,350,100,30); proportionFiled.setBounds(360,350,100,30); distributionLabel.setBounds(120,400,100,30); mdpLabel.setBounds(240,400,100,30); mdpFiled.setBounds(360,400,100,30); pdpLabel.setBounds(240,450,100,30); pdpFiled.setBounds(360,450,100,30); bdpLabel.setBounds(240,500,100,30); bdpFiled.setBounds(360,500,100,30); //采用null布局,設置組建坐標值 this.add(panel,BorderLayout.CENTER); this.setBounds(300, 100, 600, 600); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.getContentPane().setBackground(Color.GRAY); //將面板放入框架,設置相應屬性(4)員工考勤界面 JLabel idlabel = new JLabel(”ID“);JComboBox idbox =new JComboBox();CheckTable model = null; JButton wbtn=new JButton(”修改“); JButton cbtn=new JButton(”查詢“);JButton mbtn=new JButton(”創建薪資表“);JButton rbtn=new JButton(”創建人事表“); JScrollPane scrollpane = null;JPanel spanel=new JPanel();JPanel vpanel=new JPanel();JTable table=null;spanel.add(idbox);panel.add(wbtn);spanel.add(cbtn);vpanel.add(mbtn);vpanel.add(rbtn);spanel.setBackground(Color.GRAY);vpanel.setBackground(Color.GRAY);this.add(spanel,BorderLayout.NORTH);this.add(vpanel,BorderLayout.SOUTH);this.setBounds(300, 180, 600, 350);this.setVisible(true);this.getContentPane().setBackground(Color.GRAY); (5)選擇考勤表界面 JLabel checklabel = new JLabel(”選擇考勤表“);JLabel newlabel = new JLabel(”創建考勤表“);JComboBox timebox =new JComboBox();JTextField newfield = new JTextField();JButton btn=new JButton(”確認“);JButton nbtn=new JButton(”創建");JPanel spanel=new JPanel();spanel.add(checklabel); spanel.add(timebox); spanel.add(btn); spanel.add(newlabel); spanel.add(newfield); spanel.add(nbtn); checklabel.setBounds(30,90,100,30); timebox.setBounds(180,90,100,30); btn.setBounds(360,90,100,30); newlabel.setBounds(30,200,100,30); newfield.setBounds(180,200,100,30); nbtn.setBounds(360,200,100,30); this.add(spanel); this.setBounds(300, 180, 600, 350); this.setVisible(true); 三、結果和數據處理 四、結論 1.Swing圖形界面 圖形用戶界面(GUI)是程序不可或缺的一部分,它便于用戶和程序的交互。在Swing界面中是以Frame為框架,采用合理的布局方式(FlowLayout,BorderLayout,GridLayout,null),將Panel加入框架之中,而實現界面的各個組件將調用各種方法設置自身屬性實行可視化界面以及加入到面板之中。再通過事件監聽器實現事件的監聽預處理機制。 2.數據庫 在這次課程設計中哦我主要負責Swing圖像界面,對于數據庫只做了最簡單的了解。程序中采用了最流行的關系型數據庫mysql,雪化了mysql的建立,刪除,與修改以及使用sql語言對數據庫進行一些簡單的操作,將按鈕等設置的監聽事件與數據庫相連接。 五、問題與討論 1.組件的方法調用 在這次課程設計中,用到了很多的組件,例如Button,Jlabel,TextField,ComBox,等。Swing圖形界面也確實提供了很多方法去設置相關組件的屬性,例如label.setVisible(false);設置標簽不可見等很多方法,只要查看API文檔幾乎就能實現很多功能,但是對于按鈕等最常見的組件來說設置大小如果不依靠相關布局方式很難設置,我在程序中之所以運用了很多的bull布局方式就是為了實現組件大小的設置,如果說能有一個方法直接設置組件大小而不受其他組件或容器影響就能使代碼很簡潔了。2.相對布局 在編寫圖像界面的程序的時候,我用了很多的布局方式,有FlowLayout,BorderLayout,GrirLayout,null等,其中null布局用的最多。null布局的優點在于可以將組建的位置精準到坐標值,可以把組件放到任意想放置的位置,另外可以通過null去設置組件的大小,相較于其他布局方式來說是一個很大的優勢。但同時它的缺點在于不能根據Frame的大小變化而改變位置。其他三種布局方式雖然實現了歲Frame大小變動而改變組件大小及位置但是不容易控制,如果null布局能實現相對布局就是很好的一種布局方式了。 《Java語言程序設計》 題 目:指導老師:姓 名:專 業:班 級:日 期:課程設計報告 目 錄 一、系統總體設計.......................................1 (一)設計目標及完成功能..............................1 (二)系統結構設計...................................1 二、數據庫設計........................................1 三、詳細設計..........................................1(一)界面設計........................................1(二)系統設計........................................2(三)關鍵技術及算法..................................2 四、測試..............................................2 五、安裝使用說明.......................................2 總結(體會)..........................................2 參考文獻:............................................2 《Java語言程序設計》課程設計報告 一、系統總體設計 (一)設計目標及完成功能 此部分可以包括如下內容: 1.窗口功能說明:設計了幾個窗口,分別實現什么功能.2.菜單欄或工具欄說明:如果設計了菜單或工具欄,分別說明包括哪些項及其各自實現什么操作.…… (二)系統結構設計 說明:要繪出系統結構圖 二、數據庫設計 必須包含以下內容:E-R圖(說明所有實體及其屬性)、表結構列表(字段名、類型、主鍵、備注等)。 沒有用到數據庫的不用寫此部分 三、詳細設計 (一)界面設計 界面抓圖,圖要有編號和標題(格式:圖1 主界面圖),位于圖的下方居中對齊。 《Java語言程序設計》課程設計報告 (二)系統設計 1.共設計了幾個類或接口,是否有繼承關系,各實現什么功能,繪圖(列表)說明。 2.列表說明各類的方法及其功能。 (三)關鍵技術及算法 使用的主要算法分析、關鍵技術說明。如數據庫連接技術、加密解密算法等。 四、測試 五、安裝使用說明 1.系統配置說明。是否需要配置數據源或安裝補丁包、導入數據庫等。 2.登錄的用戶名、密碼。3.其他特殊說明。 總結(體會) (談談本次課程設計的心得體會) 參考文獻: [1] 竇萬峰.軟件工程方法與實踐[M].機械工業出版社,2009 [2] 李兆鋒,張得生.Java Web項目開發案例精粹[M].電子工業出版社,2010 [3] 沈澤剛,秦玉平.Java Web編程技術[M].清華大學出版社,2009 《Java語言程序設計》課程設計報告 包括書籍、網站、論壇等。 說明:(以下內容作為要求) 正文部分一律用小四號字,宋體,1.5倍行距。一級大標題黑體四號字靠左,加粗。二級大標題黑體四號靠左,不加粗。 參考文獻: (要求:五號字,宋體,左對齊,懸掛縮進,單倍行距。按作者、書名、出版社、出版時間格式逐一列出)第二篇:Matlab課程設計報告(簡單計算器)
第三篇:單片機計算器課程設計報告
第四篇:Java課程設計報告
第五篇:java課程設計報告