Anteprima
Vedrai una selezione di 3 pagine su 6
Soluzioni appello programmazione orientata ad oggetti - 12-07-17 Pag. 1 Soluzioni appello programmazione orientata ad oggetti - 12-07-17 Pag. 2
Anteprima di 3 pagg. su 6.
Scarica il documento per vederlo tutto.
Soluzioni appello programmazione orientata ad oggetti - 12-07-17 Pag. 6
1 su 6
D/illustrazione/soddisfatti o rimborsati
Disdici quando
vuoi
Acquista con carta
o PayPal
Scarica i documenti
tutte le volte che vuoi
Estratto del documento

Implementazione della classe BigInt

UNO=new DUE=newBigInt("1"), BigInt("2");

private void trim( ArrayList<Integer> d ){
    //elimina gli zeri più significativi in d
    boolean flag=true;
    Iterator<Integer> it=d.iterator();
    while( it.hasNext() && flag ){
        int c=it.next();
        if( flag && c==0 ) it.remove();
        if( flag && c!=0 ) flag=false;
    }
    if( d.isEmpty() ) d.add(0);
}//trim

//costruttore di default - utile solo ai fini di implementazione della classe
//costruisce un BigInt vuoto, cioè senza cifre
private BigInt(){};

public BigInt( String s ){
    if( !s.matches("\\d+") ) throw new IllegalArgumentException();
    for( int i=0; i<s.length(); i++ )
        cifre.add(s.charAt(i)-'0');
    //eliminazione zeri più significativi
    trim( cifre );
}

public BigInt( BigInt b ){
    for( int i=0; i<b.cifre.size(); i++ )
        cifre.add( b.cifre.get(i) );
}

public BigInt add( BigInt b ){
    BigInt s=new BigInt();
    int j=cifre.size()-1, k=b.cifre.size()-1;
    int c1, c2, c, riporto=0;
    while( j>=0 || k>=0
{if( j>=0 ){ c1=cifre.get(j); j--; }else c1=0;if( k>=0 ){ c2=b.cifre.get(k); k--; }else c2=0;c=c1+c2+riporto;if( c>9 ){ c=c-10; riporto=1; }else riporto=0;s.cifre.add( 0, c );}if( riporto>0 ) s.cifre.add( 0, riporto );return s;}//add
public BigInt sub( BigInt b ){if( this.compareTo(b)<0 ) throw new RuntimeException("Sub invalida!");BigInt d=new BigInt(); //differenzaint j=cifre.size()-1, k=b.cifre.size()-1;int c1, c2, c, prestito=0;while( j>=0 ){ //o this è più lungo di b o hanno la stessa lunghezzac1=cifre.get(j); j--;if( k>=0 ){ c2=b.cifre.get(k); k--; }else c2=0;if( c1-prestito<c2 ){ c=c1-prestito+10-c2; prestito=1; }else{ c=c1-prestito-c2; prestito=0; }d.cifre.add( 0, c );}trim(d.cifre);return d;}//sub
public BigInt mul( BigInt b ){c=ZERO, p=ZERO;BigIntwhile( c.compareTo(b)<0 ){p=p.add(this);c=c.add(UNO);}return p;}//mul
public BigInt div( BigInt b ){if( this.compareTo(b)<0 )throw new RuntimeException("Div
invalida!");b.equals(ZERO)if( )throw new RuntimeException("Division by zero or indeterminate form!");q=ZERO;BigInt //quozienteBigInt d=new BigInt(this);while( d.compareTo(b)>=0 ){q=q.add(UNO);d=d.sub(b);}return q;}//divpublic int compareTo( BigInt b ){if( cifre.size()>b.cifre.size() ) return 1;if( cifre.size()<b.cifre.size() ) return -1;int i=0;while( i<cifre.size() ){if( cifre.get(i)>b.cifre.get(i) ) return 1;if( cifre.get(i)<b.cifre.get(i) ) return -1;i++;}return 0;}//compareTopublic boolean equals( Object x ){if( !(x instanceof BigInt) ) return false;if( x==this ) return true;BigInt b=(BigInt)x;if( !cifre.equals(b.cifre) ) return false;return true;}//equalspublic int hashCode(){return cifre.hashCode();}//hashCodepublic String toString(){StringBuilder sb=new StringBuilder(1000);for( int i=0; i<cifre.size(); i++ )sb.append( cifre.get(i) );return sb.toString();}//toStringpublic static void main( String[] args ){//DEMOBigInt b=new
BigInt("234567");
BigInt c=new BigInt("3");
System.out.println(b+"+"+c+"="+b.add(c));
System.out.println(b+"-"+c+"="+b.sub(c));
System.out.println(b+"*"+c+"="+b.mul(c));
System.out.println(b+"/"+c+"="+b.div(c));

p=BigInt.UNO;
BigInt p=p.mul(BigInt.DUE);
for( int i=0; i<128; i++ )
    System.out.println("2^128="+p);

//convalida mediante BigInteger di Java
BigInteger due=new BigInteger("2");
BigInteger pw=new BigInteger("1");
for( int i=0; i<128; i++ )
    pw=pw.multiply(due);
System.out.println("2^128="+pw);

//test equals
BigInt bi=new BigInt( pw.toString() );
System.out.println( p.equals(bi) );
}//main
}//BigInt

Esercizio 2 - Radice numerica rN(x)
Metodi aggiunti alla classe poo.util.Mat

public static int rN( int x ){
    if( x<0 ) throw new IllegalArgumentException();
    rn(x);
    return;
}//rN

private static int rn( int x ){
    sc=sommaCifre(x);
    int;
    if( sc==9 ) return 0;
    if( sc<9 )
Verifica File Matrice
return sc;
rn(sc);
return}

private static int sommaCifre( int x ){
if( x<=9 ) return x;
return x%10+sommaCifre(x/10);
}

//sommaCifreEsercizio 3 – Verifica file e caricamento matrice
package poo.bigint;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeSet;
import poo.util.Matrix;

public class VerificaFileMatrice {
static double[][] verificaECarica( File f ) throws IOException{
BufferedReader br=new BufferedReader( new FileReader(f) );
String REALE="\\-?(\\d+|(\\d+)?\\.\\d+)([eE][\\-\\+]\\d{1,3})?[DdFf]?";
String

LINEA="(i=\\d+\\s+j=\\d+\\s+v="+REALE+"|"+"i=\\d+\\s+v="+REALE+"\\s+j=\\d+|"+"j=\\d+\\s+i=\\d+\\s+v="+REALE+"|"+"j=\\d+\\s+v="+REALE+"\\s+i=\\d+|"+"v="+REALE+"\\s+i=\\d+\\s+j=\\d+|"+"v="+REALE+"\\s+j=\\d+\\s+i=\\d+)"; //6 possibilita'String linea=null;TreeSet<Integer> rig=new TreeSet<>(); //per conservare gli indici di rigaTreeSet<Integer> col=new TreeSet<>(); //per conservare gli indici di colonnaMap<Integer,Map<Integer,Double>> mv=new HashMap<>(); //matrice virtualefor(;;){linea=br.readLine();if( linea==null ) break;if( !linea.matches(LINEA) ) return null;//estrai campi dalla lineaStringTokenizer st=new StringTokenizer(linea," ");int i=0, j=0; double v=0;while( st.hasMoreTokens() ){String tk=st.nextToken();StringTokenizer stt=new StringTokenizer(tk,"=");String
Il testo formattato con i tag HTML è il seguente: ```html

campo=stt.nextToken();

i=Integer.parseInt(if( campo.equals("i") ){ stt.nextToken() ); rig.add(i); }

j=Integer.parseInt(if( campo.equals("j") ){ stt.nextToken() ); col.add(j); }

v=Double.parseDouble(if( campo.equals("v") ){ stt.nextToken() ); }}

if( !mv.containsKey(i) ) mv.put(i, new HashMap<Integer,Double>());

mv.get(i).put(j, v);}

//trova dimensioni della matrice

int x=-1, y=-1, r=0, c=0;

for( int i: rig )

if( i!=x+1 ) return null;

else{ x=i; r=i; }

for( int j: col )

if( j!=y+1 ) return null;

else{ y=j; c=j; }

if( r!=c ) return null; //matrice non quadrata

double[][] a=new double[r+1][c+1]; //cio' in quanto gli indici partono da 0

System.out.println("Dimensioni="+a.length+"x"+a[0].length);

```
Dettagli
Publisher
A.A. 2019-2020
6 pagine
2 download
SSD Scienze matematiche e informatiche INF/01 Informatica

I contenuti di questa pagina costituiscono rielaborazioni personali del Publisher antonio199696 di informazioni apprese con la frequenza delle lezioni di Programmazione orientata agli oggetti e studio autonomo di eventuali libri di riferimento in preparazione dell'esame finale o della tesi. Non devono intendersi come materiale ufficiale dell'università Università della Calabria o del prof Nigro Libero.