#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct date
{
int yr;
int mon;
int date;
};
typedef struct bookdate
{
char bname[20];
int bnum;
char nevi[30];
};
typedef struct pf
{
char name[30];
int idnum;
int bama[3];
date bdate[3];
};
void main()
{
bookdate book[30]={"",0,""};
pf stu[30]={{"",0,{0},{0}}};
void stud();
void stud1();
void stud2();
void stud3();
void stud4(bookdate *b,pf *s);
clrscr();
a:
int d=0;
clrscr();
printf("
-------------------------------------------");
printf("
| enter number (1) for daryafte ketab |""
| enter number (2) for sabte name danesamoz |""
| enter number (3) for sabte mojodi ketab |");
printf("
| enter number (4) for mahale ketab |""
| enter number (5) for list amaniti ketab |""
| enter number (6) for exit |");
printf("
-------------------------------------------");
scanf("%d",&d);
switch (d)
{
case 1:
stud();
break;
case 2:
stud1();
break;
case 3:
stud2();
break;
case 4:
stud3();
break;
case 5:
stud4(book,stu);
break;
case 6:
exit(0);
default:
{
printf("
--------------------------
");
printf(" menu antekhabi dorost nist");
printf("
--------------------------
");
}
}
getch();
goto a;
}
void stud()
{
bookdate b[30]={{"",0,""}};
pf s[30]={0,0,{0},{0}};
FILE *fs;
FILE *fb;
int m=1110,j=2,i=0,cbook=0;
fs = fopen ("d:\lib\student.txt","r+w");
if (fs==NULL)
{
printf("
cannot read file student");
getch();
exit(1);
}
fread(&s,sizeof(s[0]),30,fs);
idnum:
printf("
enter idnum for student:");
scanf("%d",&i);
for (j=30;j >= 0;j--)
{
if (i==s[j].idnum)
{
m=j;
printf("
name student: %s",s[j].name);
}
}
if (m==1110)
{
printf("
idnum vojod nadaran");
goto idnum;
}
// darkhaste daryafte ketab
fb = fopen ("d:\lib\books.txt","rb");
if (fs==NULL)
{
printf("
cannot read file books");
getch();
exit(1);
}
fread(&b,sizeof(b[0]),30,fb);
i=1110;
booknum:
printf("
enter number book: ");
scanf("%d",&cbook);
for (j=0;j < 30;j++)
{
if (cbook==b[j].bnum)
{
i=j;
printf("
name book: %s , %s",b[i].bname,b[i].nevi);
}
}
if (i==1110)
{
printf("
number book vojod nadaran");
goto booknum;
}
rewind(fs);
if ((s[m].bama[0]!=0) && (s[m].bama[1]!=0) && (s[m].bama[2]!=0))
printf("
!!!!!! 3 ketab ghabli avorde nashode ast ");
rewind(fs);
if ((s[m].bama[0])==0 )
{
printf("
enter date(sal,mah,roz):");
scanf("%d %d %d",&s[m].bdate[0].yr,&s[m].bdate[0].mon,&s[m].bdate[0].date);
s[m].bama[0]=cbook;
fseek(fs,m*(sizeof(s[m])),0);
fwrite(&s[m],sizeof(s[m]),1,fs);
goto a;
}
if (s[m].bama[1]==0 )
{
printf("
enter date(sal,mah,roz):");
scanf("%d %d %d",&s[m].bdate[1].yr,&s[m].bdate[1].mon,&s[m].bdate[1].date);
s[m].bama[1]=cbook;
fseek(fs,m*(sizeof(s[m])),0);
fwrite(&s[m],sizeof(s[m]),1,fs);
goto a;
}
if (s[m].bama[2]==0 )
{
printf("
enter date(sal,mah,roz):");
scanf("%d %d %d",&s[m].bdate[2].yr,&s[m].bdate[2].mon,&s[m].bdate[2].date);
s[m].bama[2]=cbook;
fseek(fs,m*(sizeof(s[m])),0);
fwrite(&s[m],sizeof(s[m]),1,fs);
}
a:
fclose(fs);
fclose(fb);
}
void stud1()
{
pf s[30]={0,0,{0},{0}};
FILE *fs;
fs = fopen ("d:\lib\student.txt","a+b");
if (fs==NULL)
{
printf("
cannot open file student");
getch();
exit(1);
}
int i=0,j=0;
fread(&s,sizeof(s[0]),30,fs);
for (i=0;i<30;i++)
{
if (s[i].idnum == 0)
{
printf("
enter name and idcode for student: ");
scanf("%s %d",&s[i].name,&s[i].idnum);
fwrite(&s[i],sizeof(s[0]),1,fs);
goto stexit;
}
else
printf("
%-15s = %d",s[i].name,s[i].idnum);
}
stexit:
fclose(fs);
}
void stud2()
{
bookdate b[30]={"",0,""};
FILE *fb;
fb = fopen ("d:\lib\books.txt","a+b");
if (fb==NULL)
{
printf("
cannot open file books");
getch();
exit(1);
}
fread(&b,sizeof(b[0]),30,fb);
int i=0,j=0;
for (i=0;i<30;i++)
{
if (b[i].bnum == 0)
{
printf("
enter name , code and navisandeh for book: ");
scanf("%s %d %s",&b[i].bname,&b[i].bnum,&b[i].nevi);
fwrite(&b[i],sizeof(b[0]),1,fb);
goto bexit;
}
else
printf("
%-15s = %d = %s",b[i].bname,b[i].bnum,b[i].nevi);
}
bexit:
fclose(fb);
}
void stud3()
{
FILE *fs;
FILE *fb;
int m=1110,j=2,i=1110,cbook=0;
bookdate b[30]={{"",0,""}};
pf s[30]={0,0,{0},{0}};
fs = fopen("d:\lib\student.txt","rb");
if (fs==NULL)
{
printf("
cannot open file student");
getch();
exit(1);
}
fb = fopen ("d:\lib\books.txt","rb");
if (fs==NULL)
{
printf("
cannot open file books");
getch();
exit(1);
}
booknum:
fread(&s,sizeof(s[0]),30,fs);
fread(&b,sizeof(b[0]),30,fb);
printf("
enter number book: ");
scanf("%d",&cbook);
for (j=30;j >= 0;j--)
{
if (cbook==b[j].bnum)
{
i=j;
printf("
name book: %s , %s",b[i].bname,b[i].nevi);
}
}
if (i==1110)
{
printf("
number book vojod nadaran");
goto booknum;
}
for (j=0;j < 30 ;j++)
{
if ((b[i].bnum == s[j].bama[0]) || (b[i].bnum == s[j].bama[1]) || (b[i].bnum == s[j].bama[2]))
{
printf("
ketab hast nazde: %s",s[j].name);
m=1;
goto ke;
}
}
if (m==1110)
printf("
-------------------------");
printf("
ketab hast dar ketabkhane");
printf("
-------------------------");
ke:
fcloseall();
getch();
}
void stud4(bookdate *b,pf *s)
{
int m=1110,j=2,i=0;
idnum:
printf("
enter idnum for student:");
scanf("%d",&i);
for (j=30;j >= 0;j--)
{
if (i==s[j].idnum)
{
m=j;
printf("
name student: %s",s[m].name);
}
}
if (m==1110)
{
printf("
idnum vojod nadaran");
goto idnum;
}
i=1110;
for (j=0;j < 30;j++)
{
if ((b[j].bnum==s[m].bama[0]) && (s[m].bdate[0].yr !=0))
{
i=j;
printf("
%-10s %-10s date %d/%d/%d ",b[i].bname,b[i].nevi,s[m].bdate[0].yr,s[m].bdate[0].mon,s[m].bdate[0].date);
}
if ((b[j].bnum==s[m].bama[1]) && (s[m].bdate[1].yr !=0))
{
i=j;
printf("
%-10s %-10s date %d/%d/%d ",b[i].bname,b[i].nevi,s[m].bdate[1].yr,s[m].bdate[1].mon,s[m].bdate[1].date);
}
if ((b[j].bnum==s[m].bama[2]) && (s[m].bdate[2].yr !=0))
{
i=j;
printf("
%-10s %-10s date %d/%d/%d ",b[i].bname,b[i].nevi,s[m].bdate[2].yr,s[m].bdate[2].mon,s[m].bdate[2].date);
}
}
if (i==1110)
printf("
ketabi dade nashode ast");
getch();
}
با سلام
دوست عزیز قالبی تحت عنوان تخت جمشید با در نظر گرفتن سرعت بالا برای آن جهت سیستم بلاگ اسکی آماده نموده ام و هم اکنون در تارنمای قالبکده موجود می باشد .
چنانچه مایل به استفاده از آن هستید به قالبکده سری بزنید .
سورس ماشین حساب زیبا در جاوا : ( البته بعضی قسمتا رو پاک کردم اگه خواستین ایمیل بزنید)
import java.awt.*;
import java.awt.event.*;
import javax.swing.* ;
import javax.swing.JOptionPane ;
import java.io.*;
@SuppressWarnings({"ALL"})
public class BasicCalc extends JFrame
implements ActionListener {
private JButton bOne, bTwo, bThree, bFour, bFive,
bSix, bSeven, bEight, bNine, bZero, bExit,
bMult, bDiv, bSub, bPlus, bEquals, bHelp,
bAbout, bDecPt, bClear ;
private JTextField enterBox ;
private Container c ;
private Font font;
private String firstNum = "";
{
}
private String secondNum = "";
private String tempStr ;
private boolean myDiag = false;
private boolean result = true;
private boolean firstNumSet = false;
private boolean pendingOp = false ;
private double dNum1 = 0.0;
private double dNum2 = 0.0;
private double answer = 0.0;
{
}
private int opCode = -1;
{
}
{
}
private int pendingOpCode = -1 ;
BasicCalc() {
super( "Basic Calculator" );
setup() ;
setSize( 350, 300 ); /** sets the size of the frame */
setLocation( 200, 200 ) ; /** sets the location of the frame on the screen*/
show(); /** makes the frame visible */
int dArrayPtr;
dArrayPtr = 0;
}
/**
* The setup() method is used to allocate the panel and
* button objects using the new function.
*/
private void setup() {
c = getContentPane() ;
setUpTextField() ;
/** setup button Panel and the number buttons */
setUpButtons() ;
/** assign an event handler to the button and
* indicate that the responses will be handled
* inside this class, MyCalc. */
/** setup function panel and its buttons*/
setUpFunctionPanel() ;
/** setup exit panel and its buttons */
setUpExitPanel() ;
setUpActionListener() ;
}
/** setUpTextField() */
public void setUpTextField() {
/** setup text field and panel */
enterBox = new JTextField( 15 ) ;
enterBox.setText( "..........." );
enterBox.setEditable( false );
enterBox.setBackground( Color.white );
enterBox.setHorizontalAlignment( JTextField.RIGHT );
enterBox.addKeyListener(
/** this code reprimes the enterbox and avoids exceptions */
new KeyAdapter() {
public void keyPressed ( KeyEvent e ) {
if ( result ) {
MyPrint( "The value of result is " + result +
"this is from the enterbox keylistener." );
result = false ;
enterBox.setText( "" );
}
}
}
);
JPanel textPanel = new JPanel();
textPanel.add(enterBox ) ;
c.add(textPanel, BorderLayout.NORTH ) ;
}
public void setUpButtons() {
JPanel buttonPanel = new JPanel();
c.add(buttonPanel, BorderLayout.CENTER ) ;
bZero = new JButton( "0" ) ;
bZero.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bOne = new JButton( "1" ) ;
bTwo = new JButton( "2" ) ;
bThree = new JButton( "3" ) ;
bFour = new JButton( "4" ) ;
bFive = new JButton( "5" ) ;
bSix = new JButton( "6" ) ;
bSeven = new JButton( "7" ) ;
bEight = new JButton( "8" ) ;
bNine = new JButton( "9" ) ;
bExit = new JButton( "Exit" ) ;
bClear = new JButton( "Clear" ) ;
bClear.setBackground( Color.blue ) ;
bClear.setForeground( Color.white );
bMult = new JButton( "*" ) ;
bMult.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
bMult.setBackground( Color.blue ) ;
bMult.setForeground( Color.white );
bDiv = new JButton( "/" ) ;
bDiv.setFont( new Font("Sanserif", Font.BOLD, 20 ) );
bDiv.setBackground( Color.blue ) ;
bDiv.setForeground( Color.white );
bSub = new JButton( "-" ) ;
bSub.setFont( new Font("Sanserif", Font.BOLD, 24 ) );
bSub.setBackground( Color.blue ) ;
bSub.setForeground( Color.white );
bPlus = new JButton( "+" ) ;
bPlus.setBackground( Color.blue ) ;
bPlus.setForeground( Color.white );
bEquals = new JButton( "=" ) ;
bEquals.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bEquals.setBackground( Color.red ) ;
bEquals.setForeground( Color.white );
bDecPt = new JButton( "." ) ;
bDecPt.setFont( new Font("Sanserif", Font.BOLD, 22 ) );
/** add the following buttons to the buttonPanel panel. */
buttonPanel.add( bSeven ) ;
buttonPanel.add( bEight ) ;
buttonPanel.add( bNine ) ;
buttonPanel.add( bFour ) ;
buttonPanel.add( bFive ) ;
buttonPanel.add( bSix ) ;
buttonPanel.add( bOne ) ;
buttonPanel.add( bTwo ) ;
buttonPanel.add( bThree ) ;
buttonPanel.add( bZero ) ;
buttonPanel.add( bDecPt ) ;
buttonPanel.add( bEquals ) ;
buttonPanel.setLayout( new GridLayout( 5, 3, 5, 5 ) );
}
public void setUpActionListener() {
/** assign an event handler to the button and
* indicate that the responses will be handled
* inside this class, MyCalc. */
bDecPt.addActionListener( this );
bZero.addActionListener( this );
bOne.addActionListener( this );
bTwo.addActionListener( this );
bThree.addActionListener( this );
bFour.addActionListener( this );
bFive.addActionListener( this );
bSix.addActionListener( this );
bSeven.addActionListener( this );
bEight.addActionListener( this );
bNine.addActionListener( this );
bExit.addActionListener( this );
bMult.addActionListener( this );
bDiv.addActionListener( this );
bSub.addActionListener( this );
bEquals.addActionListener( this );
bClear.addActionListener( this );
bExit.addActionListener( this );
bHelp.addActionListener( this );
bAbout.addActionListener( this );
}
public void setUpFunctionPanel() {
/** setup function panel and its buttons*/
JPanel functionPanel;
functionPanel = new JPanel();
c.add(functionPanel, BorderLayout.EAST ) ;
functionPanel.setLayout( new GridLayout( 6, 1, 5, 3 ) );
functionPanel.add( bMult ) ;
functionPanel.add( bDiv ) ;
functionPanel.add( bSub ) ;
functionPanel.add( bPlus ) ;
functionPanel.add( bClear ) ;
}
public void setUpExitPanel() {
/** setup exit panel and its buttons */
JPanel exitPanel = new JPanel();
c.add(exitPanel, BorderLayout.SOUTH ) ;
bExit = new JButton( "Exit" ) ;
bExit.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bExit.setBackground( Color.red ) ;
bExit.setForeground( Color.white );
bHelp = new JButton( "Help" ) ;
bHelp.setFont( new Font("Sanserif", Font.BOLD, 16 ) );
bHelp.setBackground( Color.blue ) ;
bHelp.setForeground( Color.white );
bAbout = new JButton( "About" ) ;
bAbout.setFont( new Font("Sanserif", Font.BOLD, 14 ) );
bAbout.setBackground( Color.darkGray ) ;
/** add the following buttons to the exitPanel panel. */
exitPanel.add( bExit ) ;
exitPanel.add( bHelp ) ;
exitPanel.add( bAbout ) ;
}
public void actionPerformed( ActionEvent e ) {
if ( result ) {
MyPrint( "The value of result is " + result );
result = false ;
enterBox.setText( "" );
}
if ( e.getSource() == bDecPt ) {
MyPrint( "The decimal point button was pressed." );
enterBox.setText( enterBox.getText() + "." ) ;
}
else if ( e.getSource() == bZero ) {
MyPrint( "The zero button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "0" ) ;
}
else if ( e.getSource() == bOne ) {
MyPrint( "The one button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "1" ) ;
}
else if ( e.getSource() == bTwo ) {
MyPrint( "The two button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "2" ) ;
}
else if ( e.getSource() == bThree ) {
MyPrint( "The Three button was pressed. And pendingOp is " + pendingOp);
enterBox.setText( enterBox.getText() + "3" ) ;
}
else if ( e.getSource() == bFour ) {
MyPrint( "The Four button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "4" ) ;
}
else if ( e.getSource() == bFive ) {
MyPrint( "The Five button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "5" ) ;
}
else if ( e.getSource() == bSix ) {
MyPrint( "The Six button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "6" ) ;
}
else if ( e.getSource() == bSeven ) {
MyPrint( "The Seven button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "7" ) ;
}
else if ( e.getSource() == bEight ) {
MyPrint( "The Eight button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "8" ) ;
}
else if ( e.getSource() == bNine ) {
MyPrint( "The Nine button was pressed. And pendingOp is " + pendingOp );
enterBox.setText( enterBox.getText() + "9" ) ;
}
else if ( e.getSource() == bExit ) {
MyPrint( "The Exit button was pressed." );
sysExit() ;
}
else if ( e.getSource() == bMult ) {
if ( !pendingOp ) {
if ( !getFirstNum( ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else {
opCode = 0 ;
pendingOpCode = 0 ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
MyPrint( "The pendingOp is " + pendingOp );
}
}
else {
JOptionPane.showMessageDialog( this, "You have a pending operation." ,
"Pending Operation",
JOptionPane.ERROR_MESSAGE );
opCode = pendingOpCode ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
MyPrint( "The Mult button was pressed. And pendingOp is " + pendingOp );
}
else if ( e.getSource() == bDiv ) {
if ( !pendingOp ) {
if ( !getFirstNum( ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else if ( getFirstNum( ) ) {
opCode = 1 ;
pendingOpCode = 1 ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
}
else {
JOptionPane.showMessageDialog( this, "You have a pending operation." ,
"Pending Operation",
JOptionPane.ERROR_MESSAGE );
opCode = pendingOpCode ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
MyPrint( "The Div button was pressed. And pendingOp is " + pendingOp );
}
else if ( e.getSource() == bSub ) {
if ( !pendingOp ) {
if ( !getFirstNum( ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else if ( getFirstNum( ) ) {
opCode = 2 ;
pendingOpCode = 2 ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
}
else {
JOptionPane.showMessageDialog( this, "You have a pending operation." ,
"Pending Operation",
JOptionPane.ERROR_MESSAGE );
opCode = pendingOpCode ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
}
else if ( e.getSource() == bPlus ) {
if ( !pendingOp ) {
MyPrint( "The Plus button was pressed. And pendingOp is " + pendingOp );
if ( !getFirstNum( ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else if ( getFirstNum( ) ) {
opCode = 3 ;
pendingOpCode = 3 ;
pendingOp = true ;
result = true ;
}
}
else {
JOptionPane.showMessageDialog( this, "You have a pending operation." ,
"Pending Operation",
JOptionPane.ERROR_MESSAGE );
opCode = pendingOpCode ;
pendingOp = true ;
firstNumSet = true ;
result = true ;
}
}
else if ( e.getSource() == bEquals ) {
MyPrint( "The Equals button was pressed. And pendingOp is " + pendingOp +
" The opCode value is " + opCode );
if ( enterBox.getText().equals( "" ) || !firstNumSet ) {
JOptionPane.showMessageDialog( this, "Equals a: You have not entered a first number." ,
"First number blank",
JOptionPane.ERROR_MESSAGE );
}
else if ( !pendingOp ) {
enterBox.setText( "" ) ;
JOptionPane.showMessageDialog( this, "Equals b: You have not entered a first number and/or an operation." ,
"No operation entered",
JOptionPane.ERROR_MESSAGE );
}
else if ( pendingOp ) {
opCodeMethod() ;
}
firstNum = "" + answer ;
firstNumSet = true ;
}
else if ( e.getSource() == bClear ) {
MyPrint( "The Clear button was pressed." );
firstNum = "" ;
secondNum = "" ;
firstNumSet = false ;
opCode = -1 ;
pendingOpCode = -1 ;
pendingOp = false ;
}
else if ( e.getSource() == bHelp ) {
MyPrint( "The Help button was pressed." );
File hd = new File("help_doc.html");
//File net = new File("Netscp.exe");
String helpStr = hd.getAbsolutePath() ;
Runtime rt = Runtime.getRuntime();
/* String[] callAndArgs = { "c:\Program Files\Internet Explorer\IExplore.exe" ,
"" +
E:MyExamplesSimpleCalcCalchelp_doc.html }; */
String[] callAndArgs = { "c:\Program Files\Internet Explorer\IExplore.exe" ,
"" + helpStr };
try {
Process child = rt.exec( callAndArgs );
child.waitFor();
MyPrint ("Process exit code is: " +
child.exitValue());
}
catch(IOException e2) {
MyPrint (
"IOException starting process!");
}
System.err.println(
"Interrupted waiting for process!");
}
}
else if ( e.getSource() == bAbout ) {
MyPrint( "The About button was pressed." );
Runtime rt = Runtime.getRuntime();
String[] callAndArgs = { "c:\Program Files\Internet Explorer\IExplore.exe" ,
"http://sumtotalz.com/TotalAppsWorks/index.html " };
try {
Process child = rt.exec(callAndArgs);
child.waitFor();
MyPrint ("Process exit code is: " +
child.exitValue());
}
catch(IOException e2) {
MyPrint( "IOException starting process!");
}
catch(InterruptedException e3) {
MyPrint( "Interrupted waiting for process!");
}
}
} /** ***** End of actionPerformed() ****************/
/** getFirstNum()
* @return*/
@SuppressWarnings({"JavaDoc"})
private boolean getFirstNum( ) {
boolean retCode = false ;
if ( !firstNumSet ) {
if ( !enterBox.getText().equals( "" ) ) {
firstNum = enterBox.getText() ;
firstNumSet = true ;
result = true ;
retCode = true ;
MyPrint( "1a- In checkFirstNum( ) -- firstNum =s " + firstNum);
}
else if ( enterBox.getText().equals( "" ) ) {
MyPrint( "1b- In checkFirstNum( ) firstNumSet is " + enterBox.getText() );
JOptionPane.showMessageDialog( this, "The first number cannot be blank." ,
"Invalid Number",
JOptionPane.ERROR_MESSAGE );
retCode = false ;
}
}
else if (!firstNumSet) {
if ( enterBox.getText().equals( "" ) ) {
MyPrint( "1c- In checkFirstNum( ) firstNumSet is " + enterBox.getText() );
JOptionPane.showMessageDialog( this, "The first number =s " +
en
"Invalid Number",
JOptionPane.ERROR_MESSAGE );
retCode = false ;
}
} else {
firstNumSet = true ;
result = true ;
retCode = true ;
}
MyPrint( "1d- In checkFirstNum( ): retCode is " + retCode ) ;
return retCode ;
} /** ***** End of checkFirstNum() ****************/
private void setResults() {
enterBox.setText( "" + answer );
firstNum = "" + answer ;
firstNumSet = true ;
result = true ;
pendingOp = false ;
} /** ***** End of checkFirstNum() ****************/
/** opCodeMethod() */
private void opCodeMethod() {
int currentOpCode;
MyPrint( "In opCodeMethod() with an opCode of " + opCode );
/** Check if there is a pending operation that must
* be performed before the current operation is
* performed. */
if ( pendingOp ) {
currentOpCode = pendingOpCode ;
pendingOp = false ;
}
else {
currentOpCode = opCode ;
}
case 0 :
multOp() ;
break ;
case 1 :
//convertNumsToDouble() ;
divOp() ;
break ;
case 2 :
MyPrint( "opCodeMethod(): opCode =s " + currentOpCode ) ;
//convertNumsToDouble() ;
subOp() ;
break ;
case 3 :
//convertNumsToDouble() ;
plusOp() ;
break ;
case 4 :
break ;
case 5 :
break ;
default :
setResults() ;
secondNum = "0.0" ;
dNum2 = 0.0 ;
MyPrint( "Case default: firstNum =s " + firstNum);
//JOptionPane.showMessageDialog( this, "The first number =s " +
// firstNum + " cannot be used as a denometer" ,
// "Case default",
// JOptionPane.ERROR_MESSAGE );
break ;
}
opCode = -1 ;
} /** ********** End of opCodeMethod() **************/
/**
* multOp()
* Version 1.0
* Changed 9/10/2003 - Fixed chained operation errors
*/
private void multOp() {
if ( opCode == 0 ) {
pendingOp = false ;
secondNum = enterBox.getText() ;
MyPrint( "2a- multOp(): opCode is " + opCode + " The value of secondNum is " + secondNum );
if ( secondNum.equals( "" ) ) {
secondNum = "1.0" ;
MyPrint( "2b- multOp(): opCode is " + opCode + " The value of secondNum is " + secondNum );
}
MyPrint( "3a- multOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 * dNum2 ;
setResults() ;
MyPrint( "3b- multOp(): opCode is " + opCode + " The value of answer is " + answer );
dNum1 = answer ;
secondNum = "0.0" ;
}
} /** ********** End of multOp() **************/
/** /**
* divOp()
* Version 1.1
* Changed 9/10/2003 - Fixed chained operation errors
*/
private void divOp() {
secondNum = enterBox.getText() ;
MyPrint( "2a- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
if ( secondNum.equals( "" ) || opCode < 0 ) {
firstNum + " secondNum is " + secondNum ) ;
}
else {
convertNumsToDouble() ;
if (dNum2 != 0.0 ) {
answer = dNum1 / dNum2 ;
setResults() ;
MyPrint( "2- divOp(): opCode is " + opCode + " firstNum is " +
firstNum + " secondNum is " + secondNum ) ;
}
else {
JOptionPane.showMessageDialog( this, "The number " +
dNum2 + " cannot be used as a denometer" ,
"Attempted to Divide by Zero",
JOptionPane.ERROR_MESSAGE );
firstNumSet = false ;
firstNum = "" ;
opCode = -1 ;
}
}
opCode = 1 ;
result = true ;
} /** ********** End of divOp() **************/
/** subOp()
*
* Version 1.1
* Changed 9/10/2003 - Fixed chained operation errors
* */
private void subOp() {
if ( opCode == 2 ) {
pendingOp = false ;
secondNum = enterBox.getText() ;
if ( secondNum.equals( "" ) ) {
secondNum = "0.0" ;
}
MyPrint( "2a- subOp(): opCode is " + opCode + " secondNum is " + secondNum ) ;
convertNumsToDouble() ;
answer = dNum1 - dNum2 ;
setResults() ;
MyPrint( "2b- subOp(): opCode is " + opCode + " The value of answer is " + answer );
secondNum = "0.0" ;
opCode = -1 ;
}
} /** ********** End of subOp() **************/
/**
* plusOp()
* Version 1.1
* Changed 9/10/2003 - Fixed chained operation errors
*/
/** ********** End of plusOp() **************/
/** convertNumsToDouble() */
private void convertNumsToDouble() {
if ( !firstNum.equals( "" ) )
dNum1 = Double.parseDouble( firstNum ) ;
if ( !secondNum.equals( "" ) )
dNum2 = Double.parseDouble( secondNum ) ;
MyPrint( "convertNumsToDouble(): The value of dNum1 is " + dNum1 );
MyPrint( "convertNumsToDouble(): The value of dNum2 is " + dNum2 );
}
/** ********** End of convertNumsToDouble() **************/
/** sysExit()
* - closes sysExit() and ceases execution
*/
private void sysExit() {
System.exit( 0 );
} /** ********** End of sysExit() **************/
/** MyPrint()
* - is used for diagnostics
*
* */
private void MyPrint( String str ) {
if ( myDiag )
System.out.println( str );
} /** ********** End of MyPrint() **************/
/** main() */
public static void main( String args[] ) {
final BasicCalc app = new BasicCalc () ; /** creates an instance of MyCalc */
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
app.sysExit() ;
}
}
);
}
/** ********** End of main() **************/
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
}
public String getTempStr() {
return tempStr;
}
public void setTempStr(String tempStr) {
this.tempStr = tempStr;
}
}