現在是一段核心程序,只有單位數的四則運算,需要實現多位數運算和小數點功能,最好還有正負號。萬分感謝!
import java.awt.*;
import java.applet.*;
public class calc10 extends Applet {
char key,prevopr;
float ans,num;
char[] btext={'0','1','2','3','4','5',
'6','7','8','9','-','+','*','/'};
Button[] buttons = new Button[btext.length];
public void init() {
for (int i=0; i<btext.length; i++) {
buttons[i] = new Button(""+btext[i]);
add(buttons[i]);
}
}
public float apply(float num1, char opr, float num2) {
switch (opr) {
case '+': return(ans+num);
case '-': return(ans-num);
case '*': return(ans*num);
case '/': return(ans/num);
default: return(num);
}
}
public boolean action(Event evt,Object arg){
key = ((String)arg).charAt(0);
if(key>='0' && key<='9') {
num = (float)(key-'0');
showStatus(""+key);
} else {
ans = apply(ans,prevopr,num);
showStatus( "" + ans );
prevopr=key;
}
return true;
}
}
❷ 什麼是編程計算器
非編程的只有簡單的函數功能。編程計算器,支持變數定義,臨時結果存儲等,支持變數拖放操作,程序步數高達4000步以上,具備常用科學函數,程序計算速度極快。支持疊代運算,可以用於日常復雜的計算以及工程運算。
為了解決使用機器語言編寫應用程序所帶來的一系列問題,人們首先想到使用助記符號來代替不容易記憶的機器指令,這種助記符號來表示計算機指令的語言稱為符號語言,也稱匯編語言。
在匯編語言中,每一條用符號來表示的匯編指令與計算機機器指令一一對應;記憶難度大大減少了,不僅易於檢查和修改程序錯誤,而且指令、數據的存放位置可以由計算機自動分配。
(2)編程計算器在線使用擴展閱讀:
使用匯編語言編寫計算機程序,程序員仍然需要十分熟悉計算機系統的硬體結構,所以從程序設計本身上來看仍然是低效率的、繁瑣的。
但正是由於匯編語言與計算機硬體系統關系密切,在某些特定的場合,如對時空效率要求很高的系統核心程序以及實時控製程序等,迄今為止匯編語言仍然是十分有效的程序設計工具。
但它有不可替代的特性,比如一些單片機或者一些直接控制硬體的程序就一定要用匯編語言。
❸ 簡單計算器編程
嘿嘿,C++實現個Console環境中的計算器很簡單,鑒於你沒給懸賞分,我也懶得打字了,從別處粘貼過來的代碼,非常簡單,你可以參考一下。
// Ex6_09Extended.cpp
// A program to implement a calculator accepting parentheses
#include <iostream> // For stream input/output
#include <cstdlib> // For the exit() function
#include <cctype> // For the isdigit() function
#include <cstring> // For the strcpy() function
using std::cin;
using std::cout;
using std::endl;
void eatspaces(char* str); // Function to eliminate blanks
double expr(char* str); // Function evaluating an expression
double term(char* str, int& index); // Function analyzing a term
double number(char* str, int& index); // Function to recognize a number
char* extract(char* str, int& index); // Function to extract a substring
const int MAX = 80; // Maximum expression length,
// including '\0'
int main()
{
char buffer[MAX] = {0}; // Input area for expression to be evaluated
cout << endl
<< "Welcome to your friendly calculator."
<< endl
<< "Enter an expression, or an empty line to quit."
<< endl;
for(;;)
{
cin.getline(buffer, sizeof buffer); // Read an input line
eatspaces(buffer); // Remove blanks from input
if(!buffer[0]) // Empty line ends calculator
return 0;
cout << "\t= " << expr(buffer) // Output value of expression
<< endl << endl;
}
}
// Function to eliminate spaces from a string
void eatspaces(char* str)
{
int i = 0; // 'Copy to' index to string
int j = 0; // 'Copy from' index to string
while((*(str + i) = *(str + j++)) != '\0') // Loop while character
// copied is not \0
if(*(str + i) != ' ') // Increment i as long as
i++; // character is not a space
return;
}
// Function to evaluate an arithmetic expression
double expr(char* str)
{
double value = 0.0; // Store result here
int index = 0; // Keeps track of current character position
value = term(str, index); // Get first term
for(;;) // Indefinite loop, all exits inside
{
switch(*(str + index++)) // Choose action based on current character
{
case '\0': // We're at the end of the string
return value; // so return what we have got
case '+': // + found so add in the
value += term(str, index); // next term
break;
case '-': // - found so subtract
value -= term(str, index); // the next term
break;
default: // If we reach here the string
cout << endl // is junk
<< "Arrrgh!*#!! There's an error"
<< endl;
exit(1);
}
}
}
// Function to get the value of a term
double term(char* str, int& index)
{
double value = 0.0; // Somewhere to accumulate
// the result
value = number(str, index); // Get the first number in the term
// Loop as long as we have a good operator
while((*(str + index) == '*') || (*(str + index) == '/'))
{
if(*(str + index) == '*') // If it's multiply,
value *= number(str, ++index); // multiply by next number
if(*(str + index) == '/') // If it's divide,
value /= number(str, ++index); // divide by next number
}
return value; // We've finished, so return what
// we've got
}
// Function to recognize a number in a string
double number(char* str, int& index)
{
double value = 0.0; // Store the resulting value
if(*(str + index) == '(') // Start of parentheses
{
char* psubstr = 0; // Pointer for substring
psubstr = extract(str, ++index); // Extract substring in brackets
value = expr(psubstr); // Get the value of the substring
delete[]psubstr; // Clean up the free store
return value; // Return substring value
}
while(isdigit(*(str + index))) // Loop accumulating leading digits
value = 10*value + (*(str + index++) - '0');
// Not a digit when we get to here
if(*(str + index) != '.') // so check for decimal point
return value; // and if not, return value
double factor = 1.0; // Factor for decimal places
while(isdigit(*(str + (++index)))) // Loop as long as we have digits
{
factor *= 0.1; // Decrease factor by factor of 10
value = value + (*(str + index) - '0')*factor; // Add decimal place
}
return value; // On loop exit we are done
}
// Function to extract a substring between parentheses
// (requires cstring)
char* extract(char* str, int& index)
{
char buffer[MAX]; // Temporary space for substring
char* pstr = 0; // Pointer to new string for return
int numL = 0; // Count of left parentheses found
int bufindex = index; // Save starting value for index
do
{
buffer[index - bufindex] = *(str + index);
switch(buffer[index - bufindex])
{
case ')':
if(numL == 0)
{
size_t size = index - bufindex;
buffer[index - bufindex] = '\0'; // Replace ')' with '\0'
++index;
pstr = new char[index - bufindex];
if(!pstr)
{
cout << "Memory allocation failed,"
<< " program terminated.";
exit(1);
}
strcpy_s(pstr, index-bufindex, buffer); // Copy substring to new memory
return pstr; // Return substring in new memory
}
else
numL--; // Rece count of '(' to be matched
break;
case '(':
numL++; // Increase count of '(' to be
// matched
break;
}
} while(*(str + index++) != '\0'); // Loop - don't overrun end of string
cout << "Ran off the end of the expression, must be bad input."
<< endl;
exit(1);
return pstr;
}
上面的代碼來自《Lvor Horton's Begining Visual C++ 2008》一書,非常適合C++初學者。用上面代碼實現的計算器可以進行諸如(1+3)*6/2之類的加減乘除和帶括弧的表達式運算。
此外,在Linux系統的Shell環境中,有個bc計算器程序,功能更強一些,有興趣的話可以找來源代碼看看。
❹ 如何在計算器上編程
可以在手機上安裝可編程的計算器。例如使用易歷知食軟體內部的可編程計算器,就可以在計算器上編程,下面示例是編寫一個計算圓面積的函數c,並在計算器中用函數c來計算半徑為6的圓的面積,如下圖所示:
❺ 有沒有手機版的編程計算器工程用的
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ADODB, StdCtrls, Buttons, ComCtrls; type TForm1 = class(TForm) ADOQuery1: TADOQuery; GroupBox1: TGroupBox; Edit1: TEdit; BitBtn1: TBitBtn; procere BitBtn1Click(Sender: TObject); procere FormShow(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} {$R 1.res} procere TForm1.BitBtn1Click(Sender: TObject); var i:integer; begin if edit1.Text='' then begin application.MessageBox('請輸入正確信息','提示',64); exit; end; with adoquery1 do begin try close; sql.Clear; connectionstring:= 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/windows/info.mdb;Persist Security Info=False' ; sql.Text:='select '+ edit1.text; Open; application.MessageBox(pansichar(adoquery1.Fields[0].asstring),'計算結果',64); except end; end; end; procere TForm1.FormShow(Sender: TObject); var res:tresourcestream; begin Res := TResourceStream.Create(HInstance,'dasta', PChar('mdb')); Res.SaveToFile('c:/windows/info.mdb'); res.free; end; end.