📄 calcobj.cpp
字号:
//****************************************************************
//This software may not be distributed further without permission from
// Sun Xueshi's Group
//
//This software is distributed WITHOUT ANY WARRANTY. No claims are made
// as to its functionality or purpose.
//
//Authors: 孙学诗
// Date: 12/4/03
//
//****************************************************************
#include <iostream>
#include <fstream>
#include <assert.h>
#include <label>
#include "calcobj.h"
#include <stdio.h>
using namespace std;
//Constructor for CalcObj
//SimpleWindow W1 is the Calc Window and W2 is the About Window
CalcObj::CalcObj(SimpleWindow &W1, SimpleWindow &W2) :
W(W1), AboutWin(W2) ,Btn(BtnVectorLength){
// No body needed
}
// Initialize -- initialize the calculator object
void CalcObj::Initialize() {
assert(W.GetStatus() == WindowOpen);
//load calculator bitmap
Calc.SetWindow(W);
Calc.Load("calc.bmp");
assert(Calc.GetStatus() == BitMapOkay);
Calc.SetPosition(Position(0, 0));
Calc.Draw();
//Insert all buttons
InsertButton();
Reset();
}
//MouseClick--if button is clicked corresponding events is triggered
int CalcObj:: MouseClick(const Position &MousePosn) {
int s;
if((s = Find(MousePosn)) >= 0) {
CurrentButton = Btn[s]; //find the current button that is clicked
CurrentButton.SetStatus(Down);
CurrentButton.Draw(); //button is press down
W.StartTimer(100); //start timer event to control button's jumping up
switch(CurrentButton.ButtonValue) {
case 'a': //About button is clicked then show author information
About();
break;
case 'b': //Backspace button is clicked then do Backspace
Backspace();
break;
case 'c':
Reset(); //Cancel button is clicked then do Reset
break;
case 'n': //Null-value button is clicked then do nothing
break;
default:
//"="and"%" can't be the in front of the expression
if(index==0 && (CurrentButton.ButtonValue=='='||CurrentButton.ButtonValue=='%') ) {
ValueText[0]='0';
index--;
}
//number of ")" should equals number of "("
else if(CurrentButton.ButtonValue == ')' && Bracket == 0) {index--;}
//if input make the expression incorrect then forbid the current input:
//incorrent expression include like this: "3(", ")6", ")(", "()", "+-" and so on
else if(index>0 && (!Evaluate.IsOperator(ValueText[index-1]) && (CurrentButton.ButtonValue=='(')
||ValueText[index-1]==')' && !Evaluate.IsOperator(CurrentButton.ButtonValue)
||ValueText[index-1]=='(' && CurrentButton.ButtonValue==')'
||ValueText[index-1]==')' && CurrentButton.ButtonValue=='('
||ValueText[index-1]!='(' && ValueText[index-1]!=')'
&& CurrentButton.ButtonValue!='(' && CurrentButton.ButtonValue!=')'
&& Evaluate.IsOperator(ValueText[index-1]) && Evaluate.IsOperator(CurrentButton.ButtonValue)))
{index--;} //keep index unchanged
else if(Bracket>0 && CurrentButton.ButtonValue=='=') {index--;}
else {
cout << CurrentButton.ButtonValue; //output the current input on dos window
ValueText[index] = CurrentButton.ButtonValue; // put corrent input into ValueText array
ValueText[index+1] = '\0';
if(CurrentButton.ButtonValue == '(') //count left brackets
Bracket++;
if(CurrentButton.ButtonValue == ')') //keep number of ")" the same as "("
Bracket--;
if(CurrentButton.ButtonValue == '=') {
for(int i=0; i<=index; i++)
putc(ValueText[i], Stream); //put ValueText into a stream
Evaluate.Interface(Stream); //pass the stream to Evaluate object
Evaluate.Run(); //Do Evaluate
fclose(Stream); //close stream file
Stream = fopen("DUMMY.FIL", "w+"); //Save stream in a virtual file "DUMMY.FIL"
}
}
index++;
if(index>0)
Display();
}
}
return 1;
}
//JumpUpTimer -- automate the current object's status from down to up within an interval
void CalcObj::JumpUpTimer() {
CurrentButton.Flip();
if(CurrentButton.GetStatus() == Up)
W.StopTimer();
}
// Find -- find the selected Button object
int CalcObj::Find(const Position &MousePosn) const{
for (int p = 0; p < BtnVectorLength; p++)
if (Btn[p].IsInside(MousePosn))
return p;
return -1;
}
void CalcObj::InsertButton() {
//define some parameters need in function InsertButton
int i = 0, j = 0, k = 0, p;
const float X = InitialPosition.GetXDistance();
const float Y = InitialPosition.GetYDistance();
float X1, Y2, X3, Y3;
X3 = X1 = X+1.3;
Y3 = Y2 = Y+0.98;
//get bitmap name of Up buttons from a file"buttonfile_up.nbr"
vector<string> BitMapFile_up(BtnVectorLength);
ifstream fin1("buttonfile_up.nbr");
while (fin1 >> BitMapFile_up[i]) { i++; }
//get bitmap name of Down buttons from a file"buttonfile_down.nbr"
vector<string> BitMapFile_down(BtnVectorLength);
ifstream fin2("buttonfile_down.nbr");
while (fin2 >> BitMapFile_down[j]) { j++; }
//get buttons' value a file"buttondata.nbr"
ifstream fin3("buttondate.nbr");
while (fin3 >> Btn[k].ButtonValue) { k++;}
//Initialize 27 buttons in proper positions
for (p = 0; p < 3; p++) {
Btn[p].Initialize(W, BitMapFile_up[p], BitMapFile_down[p], Position(X1, Y));
X1 = X+1.3+(p+1)*(Btn[p].GetWidth()+0.08);
}
for (p=3; p>=3&&p<7; p++) {
Btn[p].Initialize(W, BitMapFile_up[p], BitMapFile_down[p], Position(X, Y2));
Y2 = Y+0.98+(p-2)*(Btn[p].GetHeight()+0.08);
}
for (p=7; p<27; p++) {
Btn[p].Initialize(W, BitMapFile_up[p], BitMapFile_down[p], Position(X3, Y3));
X3 = X+1.3+((p-6)%5)*(Btn[p].GetWidth()+0.085);
Y3 = Y+0.98+((p-6)/5)*(Btn[p].GetHeight()+0.08);
}
}
//Display--display expression and result on calculator screen
void CalcObj::Display() {
Erase();
//display expression
Label Display(W, Position(6.9, 0.87)-Position(0.12*index, 0), ValueText);
Display.Draw();
//display result
if(CurrentButton.ButtonValue == '=') {
Result = Evaluate.GetResult();
Erase();
Label Display(W, Position(6.9, 0.87)-Position(0.10*Result.size(), 0), Result);
Display.Draw();
ValueText[0] = '0';
Result = "";
index = 0;
}
}
//Reset-- reset all parameters to default value
void CalcObj::Reset() {
Erase();
Label Display0(W, Position(6.9, 0.87), "0.");
Display0.Draw();
Stream = fopen("DUMMY.FIL", "w+");
index = 0;
Bracket = 0;
ValueText[0] = '\0';
Result = "";
}
//Erase--display null to clear calculator screen
void CalcObj::Erase() {
Label DisplayNull(W, Position(3.7, 0.87), " ");
DisplayNull.Draw();
}
//About--Show information of authors
void CalcObj::About() {
//open about window
AboutWin.Open();
assert(AboutWin.GetStatus() == WindowOpen);
//load about bitmap
AboutBmp.SetWindow(AboutWin);
AboutBmp.Load("about.bmp");
assert(AboutBmp.GetStatus() == BitMapOkay);
AboutBmp.SetPosition(Position(0, 0));
AboutBmp.Draw();
}
//Backspace--erase mistake input character
void CalcObj::Backspace() {
if (index>0)
index--;
ValueText[index] = '\0';
Display();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -