TP iostream
This commit is contained in:
BIN
DEV2.1/hasard/exo2/Entiexte.class
Normal file
BIN
DEV2.1/hasard/exo2/Entiexte.class
Normal file
Binary file not shown.
85
DEV2.1/hasard/exo2/Entiexte.java
Normal file
85
DEV2.1/hasard/exo2/Entiexte.java
Normal file
@@ -0,0 +1,85 @@
|
||||
import java.lang.Math;
|
||||
|
||||
public class Entiexte
|
||||
{
|
||||
private String repr;
|
||||
|
||||
public Entiexte(int i)
|
||||
{
|
||||
this.repr = Integer.toBinaryString(i);
|
||||
this.repr = this.reverse(this.repr);
|
||||
}
|
||||
|
||||
public Entiexte(String i)
|
||||
{
|
||||
int temp = Integer.parseInt(i);
|
||||
this.repr = Integer.toBinaryString(temp);
|
||||
this.repr = this.reverse(this.repr);
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
return this.repr;
|
||||
}
|
||||
|
||||
private String reverse(String txt)
|
||||
{
|
||||
char temp;
|
||||
char[] txt2 = txt.toCharArray();
|
||||
for (int i = 0;i<txt.length()/2;i++)
|
||||
{
|
||||
temp = txt2[txt.length()-(1+i)];
|
||||
txt2[txt.length()-(1+i)]=txt2[i];
|
||||
txt2[i]=temp;
|
||||
}
|
||||
txt = new String(txt2);
|
||||
return txt;
|
||||
}
|
||||
|
||||
public int toInt()
|
||||
{
|
||||
int out=0;
|
||||
for (int i=0;i<this.repr.length();i++)
|
||||
{
|
||||
if(repr.charAt(i)=='1')
|
||||
{
|
||||
out+= Math.pow(2,i);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public boolean plusGrand(Entiexte e)
|
||||
{
|
||||
int test = 0;
|
||||
if (this.repr.length()>e.repr.length())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this.repr.length()<e.repr.length())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i=this.repr.length()-1;i>=0;i--)
|
||||
{
|
||||
if (this.repr.charAt(i)!=e.repr.charAt(i))
|
||||
{
|
||||
if (this.repr.charAt(i)=='1')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
BIN
DEV2.1/hasard/exo2/test.class
Normal file
BIN
DEV2.1/hasard/exo2/test.class
Normal file
Binary file not shown.
15
DEV2.1/hasard/exo2/test.java
Normal file
15
DEV2.1/hasard/exo2/test.java
Normal file
@@ -0,0 +1,15 @@
|
||||
public class test
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
|
||||
Entiexte test = new Entiexte("11");
|
||||
System.out.println(test.toString());
|
||||
System.out.println(test.toInt());
|
||||
Entiexte test2 = new Entiexte("9");
|
||||
Entiexte test3 = new Entiexte("11");
|
||||
Entiexte test4 = new Entiexte("14");
|
||||
System.out.println(test.plusGrand(test2));
|
||||
System.out.println(test.plusGrand(test3));
|
||||
System.out.println(test.plusGrand(test4));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user