import java.io.DataInputStream;
import java.lang.String;
import java.util.StringTokenizer;
import java.util.Random;
import java.awt.*;
import java.lang.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.awt.List;
import java.lang.Math;
import java.lang.String;
import java.io.*;
import java.text.DateFormat;
import java.lang.NumberFormatException;
public class algoritm extends Applet
{
int i=0;
int cond=2;
int step=-1;
Button btn1;
Button btn2;
Label about_A;
int rez[] = new int[5];
al alg;
TextField txt_A;
int a;
Integer A=new Integer(0);
int x1=150,y1=100;
boolean cnd=false;
//=======================algoritm=====================
//====================================================
public class al
{
int pos_x;
int pos_y;
int ost;
al (int x, int y)
{
this.pos_x=x;
this.pos_y=y;
}
void work()
{cond=2;
switch(step)
{case 0: break;
case 1: break;
case 2: ost=a-(a/2)*2;
a=a >> 1;
rez[i]=ost;
i++;
break;
case 3: if(a<2)
cond=1;
else
{
cond=0;
}
break;
case 4:
rez[i]=a;
case 5: break;
case 6: break;
case 7: step=-1;
cnd=false;
break;
}
}
void show(Graphics pole)
{String ch = new String();
int j;
Polygon p1 = new Polygon();
Polygon p2 = new Polygon();
Polygon p3 = new Polygon();
Polygon p4 = new Polygon();
Polygon p5 = new Polygon();
Polygon p6 = new Polygon();
pole.setColor(Color.black);
p1.addPoint(pos_x+30,pos_y);
p1.addPoint(pos_x+100+30,pos_y);
p1.addPoint(pos_x+100-30,pos_y+50);
p1.addPoint(pos_x-30,pos_y+50);
p2.addPoint(pos_x,pos_y+70);
p2.addPoint(pos_x+100,pos_y+70);
p2.addPoint(pos_x+100,pos_y+120);
p2.addPoint(pos_x,pos_y+120);
// p3.addPoint(pos_x,pos_y+140);
// p3.addPoint(pos_x+100,pos_y+140);
// p3.addPoint(pos_x+100,pos_y+190);
// p3.addPoint(pos_x,pos_y+190);
p4.addPoint(pos_x+50,pos_y+140);
p4.addPoint(pos_x+100,pos_y+165);
p4.addPoint(pos_x+50,pos_y+190);
p4.addPoint(pos_x,pos_y+165);
p5.addPoint(pos_x,pos_y+210);
p5.addPoint(pos_x+100,pos_y+210);
p5.addPoint(pos_x+100,pos_y+260);
p5.addPoint(pos_x,pos_y+260);
p6.addPoint(pos_x+30,pos_y+280);
p6.addPoint(pos_x+100+30,pos_y+280);
p6.addPoint(pos_x+100-30,pos_y+330);
p6.addPoint(pos_x-30,pos_y+330);
pole.setColor(Color.green);
pole.fillPolygon(p1);
pole.fillPolygon(p2);
pole.fillPolygon(p3);
pole.fillPolygon(p4);
pole.fillPolygon(p5);
pole.fillPolygon(p6);
pole.fillOval(pos_x,pos_y-50,100,30);
pole.fillOval(pos_x,pos_y+350,100,30);
pole.setColor(Color.red);
if(step==0)
pole.fillOval(pos_x,pos_y-50,100,30);
if(step==1)
{pole.fillPolygon(p1);
pole.setColor(Color.white);
pole.fillRect(pos_x+140,pos_y,100,50);
pole.setColor(Color.black);
ch=ch.valueOf(a);
pole.drawString("A =",pos_x+145,pos_y+20);
pole.drawString(ch,pos_x+175,pos_y+20);
pole.setColor(Color.red);
}
if(step==2)
{pole.fillPolygon(p2);
pole.setColor(Color.white);
pole.fillRect(pos_x+140,pos_y+70,100,50);
pole.setColor(Color.black);
ch=ch.valueOf(ost);
pole.drawString("ost=",pos_x+145,pos_y+10+80);
pole.drawString(ch,pos_x+175,pos_y+10+80);
ch=ch.valueOf(a);
pole.drawString("A =",pos_x+145,pos_y+10+95);
pole.drawString(ch,pos_x+175,pos_y+10+95);
pole.setColor(Color.red);
}
if(step==3)
pole.fillPolygon(p4);
if(step==4)
pole.fillPolygon(p5);
if(step==5)
{
pole.fillPolygon(p6);
pole.setColor(Color.white);
pole.fillRect(pos_x+140,pos_y+280,100,50);
pole.setColor(Color.black);
pole.drawString("REZ=",pos_x+145,pos_y+10+280);
for(j=0;j<5;j++)
{ch=ch.valueOf(rez[j]);
pole.drawString(ch,pos_x+180+(5-j)*10,pos_y+10+280);
}
pole.setColor(Color.red);
}
if(step==6)
pole.fillOval(pos_x,pos_y+350,100,30);
pole.setColor(Color.black);
pole.drawPolygon(p1);
pole.drawPolygon(p2);
pole.drawPolygon(p3);
pole.drawPolygon(p4);
pole.drawPolygon(p5);
pole.drawPolygon(p6);
pole.drawString("Begin",pos_x+40,pos_y-35);
pole.drawString("Input A",pos_x+30,pos_y+25);
pole.drawString("Ost=A mod 2",pos_x+10,pos_y+90);
pole.drawString("A=A/2",pos_x+10,pos_y+100);
pole.drawString("rez[i]=ost",pos_x+10,pos_y+110);
pole.drawString("A>2",pos_x+40,pos_y+165);
pole.drawString("rez[i]=A",pos_x+40,pos_y+235);
pole.drawString("Output rez",pos_x+30,pos_y+305);
pole.drawString("End",pos_x+40,pos_y+375);
}
}
//=======================initialization=================
//======================================================
public void init()
{
about_A= new Label("Input number 0..31");
add(about_A);
txt_A = new TextField( "0",2);
add(txt_A);
btn1 = new Button(" Enter A ");
add(btn1);
btn2 = new Button(" Work ");
add(btn2);
alg= new al(x1,y1);
drawLincs(getGraphics());
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ boolean p=false;
String ch=new String();
ch=txt_A.getText();
try{A=A.decode(ch);}
catch(Exception s){p=true;}
a=A.intValue();
if(p)
{
return;}
if((a<0)||(a>31))
{
return;}
for(i=0;i<5;i++)
rez[i]=0;
cnd=true;i=0;
paint(getGraphics());
}
});
btn2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{ if(cnd)
{step++;
alg.work();
paint(getGraphics());
if(cond==0)
{step=1;
cond=2;
}
}
}
});
}
public void paint(Graphics g)
{
drawLincs(g);
alg.show(g);
}
private void drawLincs(Graphics g)
{
g.setColor(Color.black);
g.drawLine(x1+50,y1-20,x1+50,y1);
g.drawLine(x1+50,y1+50,x1+50,y1+70);
g.drawLine(x1+50,y1+120,x1+50,y1+140);
g.drawLine(x1+50,y1+190,x1+50,y1+210);
g.drawLine(x1+50,y1+260,x1+50,y1+280);
g.drawLine(x1+50,y1+330,x1+50,y1+350);
g.drawLine(x1,y1+165,x1-50,y1+165);
g.drawLine(x1-50,y1+165,x1-50,y1+60);
g.drawLine(x1-50,y1+60,x1+50,y1+60);
if(cond==0)
{ g.setColor(Color.red);
g.drawString("Yes",x1-30,y1+145);
g.setColor(Color.black);
g.drawString("No",x1+20,y1+200);
}
else
if(cond==1)
{ g.setColor(Color.black);
g.drawString("Yes",x1-30,y1+145);
g.setColor(Color.red);
g.drawString("No",x1+20,y1+200);
}
else
if(cond==2)
{ g.setColor(Color.black);
g.drawString("Yes",x1-30,y1+145);
g.drawString("No",x1+20,y1+200);
}
}
}
|
|
||