| 4BI | 2012-2013 | ITIS "G. Chilesotti" | Thiene | 2012-10-26 | Verifica_4BI_20121026.pdf | |
| 4BI | 2012-2013 | ITIS "G. Chilesotti" | Thiene | 2012-12-14 | 4BI_Verifica_20121214.pdf | |
| 4BI | 2012-2013 | ITIS "G. Chilesotti" | Thiene | 2013-02-22 | 4BI_Verifica_20130222.pdf | RD |
| 4BI | 2012-2013 | ITIS "G. Chilesotti" | Thiene | 2013-03-15 | 4BI_Verifica_20130315.pdf | |
| 4BI | 2012-2013 | ITIS "G. Chilesotti" | Thiene | 2013-04-22 | 4BI_Verifica_20130422.pdf | |
| 4BI | 2012-2013 | ITIS "G. Chilesotti" | Thiene | 2013-05-17 | 4BI_Verifica_20130517.pdf |
Esercizio sugli algoritmi di ordinamento.
Ordinamento semplice (per Selezione) e ordinamento Quicksort (ricorsivo), con
tempi di elaborazione.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class frmMain : Form
{
const int MAXSIZE = 100000;
int[] a = new int[MAXSIZE];
public frmMain()
{
InitializeComponent();
}
private void btnCarica_Click(object sender, EventArgs e)
{
Random r = new Random();
for (int i = 0; i < MAXSIZE; i++)
{
a[i] = r.Next(1, 100);
lstListaDis.Items.Add(a[i]);
}
}
private void btnOrdina1_Click(object sender, EventArgs e)
{
lstOrdinaSemp.Items.Clear();
int[] b = a;
DateTime dti = DateTime.Now;
for (int i = 0; i < MAXSIZE - 1; i++)
{
for (int j = i + 1; j < MAXSIZE; j++)
{
if (b[i] > b[j])
{
int app = b[i];
b[i] = b[j];
b[j] = app;
}
}
}
DateTime dtf = DateTime.Now;
TimeSpan diff = dtf.Subtract(dti);
lblTimeOrdSemp.Text = diff.ToString();
for (int i = 0; i < MAXSIZE; i++)
{
lstOrdinaSemp.Items.Add(b[i]);
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOrdina2_Click(object sender, EventArgs e)
{
lstOrdinaQuick.Items.Clear();
int[] c = a;
DateTime dti = DateTime.Now;
Quicksort(c, 0, MAXSIZE - 1);
DateTime dtf = DateTime.Now;
TimeSpan diff = dtf.Subtract(dti);
lblTimeOrdQuick.Text = diff.ToString();
for (int i = 0; i < MAXSIZE; i++)
{
lstOrdinaQuick.Items.Add(c[i]);
}
}
private void Scambia(ref int x, ref int y)
{
int app = x;
x = y;
y = app;
}
private void Quicksort(int[] v, int l, int r)
{
int i = l;
int j = r;
int x = v[(int) ((l+r)/2)];
do
{
while (x > v[i]) i++;
while (v[j] > x) j--;
if (i <= j)
{
if (i != j) Scambia(ref v[i], ref v[j]);
i++;
j--;
}
} while (i <= j);
if (l < j) Quicksort(v, l, j);
if (i < r) Quicksort(v, i, r);
}
private void btnVerifica_Click(object sender, EventArgs e)
{
bool chk = true;
for (int i = 0; i < MAXSIZE; i++)
{
int x = (int) lstOrdinaSemp.Items[i];
int y = (int) lstOrdinaQuick.Items[i];
if (x != y) chk = false;
}
if (!chk) MessageBox.Show("ERRORE: sequenze diverse");
else MessageBox.Show("sequenze uguali");
}
}
}
Algoritmo di ordinamento Mergesort.
private void Merge(int[] vet, int i, int j, int k)
{
int p1 = i;
int p2 = j + 1;
int[] aux = new int[vet.Length];
for (int p = i; p <= k; p++)
{
if (p1 > j)
{
aux[p] = vet[p2];
p2++;
}
else if (p2 > k)
{
aux[p] = vet[p1];
p1++;
}
else if (vet[p1] < vet[p2])
{
aux[p] = vet[p1];
p1++;
}
else
{
aux[p] = vet[p2];
p2++;
}
}
for (int p = i; p <= k; p++)
{
vet[p] = aux[p];
}
}
private void Mergesort(int[] vet, int i, int j)
{
if (i != j)
{
Mergesort(vet, i, (i + j) / 2);
Mergesort(vet, ((i + j) / 2) + 1, j);
Merge(vet, i, (i + j) / 2, j);
}
}
Ricerca binaria o dicotomica, con ciclo.
private bool BinarySearch(int[] vet, int x)
{
bool ret = false;
int inf = 0;
int sup = vet.Length - 1;
while ((sup >= inf) && (!ret))
{
int med = (inf + sup) / 2;
if (x == vet[med]) ret = true;
else if (x < vet[med]) sup = med - 1;
else inf = med + 1;
}
return ret;
}
Ricerca binaria o dicotomica, ricorsiva.
private bool RicBinarySearch(int[] vet, int x, int inf, int sup)
{
bool ret = false;
int med = (inf+sup)/2;
if (sup < inf) ret = false;
else if (x == vet[med]) ret = true;
else if (x < vet[med]) ret = RicBinarySearch(vet, x, inf, med-1);
else ret = RicBinarySearch(vet, x, med+1, sup);
return ret;
}
Compito del 14/12/2012, esercizio 1, correzione.
string s = "Tavares"; string p1 = pers.Find(person => person.Contains(s)); in sostituzione di public string FindPerson(string nameContains, Listpersons) { foreach (string person in persons) if (person.Contains(nameContains)) return person; return null; }
Compito del 14/12/2012, esercizio 2, correzione.
string s = "O";
string p = pers.Find(person => person.StartsWith(s));
Compito del 14/12/2012, esercizio 3, correzione.
public struct Persona
{
public string nome;
public string cognome;
public string stato;
public string citta;
public int anno; // anno nascita
public int annomorte;
}
public struct Libro
{
public string autnome;
public string autcognome;
public string titolo;
public int anno;
public float prezzo;
public string editore;
}
Compito del 14/12/2012, esercizio 4, correzione.
var q1 = (from p in persone
where p.stato == "Italia"
select new { p.cognome, p.nome}).Count();
oppure, meglio ancora, in quanto non tutte le persone indicate
possono essere autori
var q1 = from p in persone
join x in libri on new { c=p.cognome, n=p.nome } equals new { c=x.autcognome, n=x.autnome }
where p.stato == "Italia"
select new { p.cognome, p.nome, x.titolo, p.stato };
int n = q1.Count();
Compito del 14/12/2012, esercizio 5, correzione.
var q1 = from p in persone
join x in libri on new { c = p.cognome, n = p.nome } equals new { c = x.autcognome, n = x.autnome }
where p.cognome == "Sousa Tavares" && p.nome == "Miguel"
select new { p.cognome, p.nome, x.titolo, p.stato };
Compito del 14/12/2012, esercizio 6, correzione.
var q1 = (from x in libri
select x.prezzo).Average();
Compito del 14/12/2012, esercizio 7, correzione.
la soluzione con group by evita la dupplicazione dell'autore per
tutti i libri da questi pubblicati, che possono essere più di uno
var q1 = from p in persone
join x in libri on new { c = p.cognome, n = p.nome } equals new { c = x.autcognome, n = x.autnome }
where p.stato == "Italia"
orderby p.anno
group p by new { p.cognome, p.nome, p.anno }
into g
select new { g.First().cognome, g.First().nome };
Compito del 15/03/2013, esercizio 1, correzione.
struct trasp
{
public string id;
public string codbanca;
public string banca;
public string codmovimento;
public string codcliente;
public string desccliente;
public string data;
public double quantita;
public string valuta;
}
private void ReadFile(string f, List list)
{
if (File.Exists(f))
{
StreamReader sr = new StreamReader(f);
trasp trs;
string[] campi;
string linea = "";
int primociclo = 0;
do
{
if (primociclo == 0) {
linea = sr.ReadLine();
// lettura riga intestazione
campi = linea.Split(';');
primociclo = 1;
}
linea = sr.ReadLine();
if (linea != null)
{
campi = linea.Split(';');
trs.id = campi[0];
trs.codbanca = campi[1];
trs.banca = campi[2];
trs.codmovimento = campi[3];
trs.codcliente = campi[4];
trs.desccliente = campi[5];
trs.data = campi[6];
trs.quantita = double.Parse(campi[7]);
trs.valuta = campi[8];
list.Add(trs);
}
}
while (linea != null);
sr.Close();
}
}
Compito del 15/03/2013, esercizio 2, correzione.
private int ReadFiles(string path, List list)
{
int ret = -1;
try
{
if (Directory.Exists(path))
{
string[] files = Directory.GetFiles(path, "*.csv");
for (int i = 0; i < files.Length; i++)
{
string f = files[i];
ReadFile(f, list);
}
}
ret = 0;
}
catch (Exception ex)
{
ret = -1;
}
return ret;
}
Compito del 15/03/2013, esercizio 3, correzione.
private int MoveFile(string f, string path)
{
int ret = -1;
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (File.Exists(f))
{
string nf = Path.GetFileName(f);
nf = path + "\\" + nf;
File.Move(f, nf);
}
ret = 0;
}
catch (Exception ex)
{
ret = -1;
}
return ret;
}
Compito del 15/03/2013, esercizio 4, correzione.
private int ExportFileCSV(string f, string nf, List list)
{
int ret = -1;
try
{
// eventuale controllo sull'estensione di nf, che deve essere .cdv
if (File.Exists(f))
{
StreamWriter sw = new StreamWriter(nf);
// riga di intestazione
sw.WriteLine("{0};{1};{2};{3};{4};{5};{6};{7};{8}",
"id",
"codbanca",
"banca",
"codmovimento",
"codcliente",
"desccliente",
"data",
"quantita",
"valuta"
);
for (int i = 0; i < list.Count; i++)
{
trasp trs = (trasp)list[i];
sw.WriteLine("{0};{1};{2};{3};{4};{5};{6};{7};{8}",
trs.id,
trs.codbanca,
trs.banca,
trs.codmovimento,
trs.codcliente,
trs.desccliente,
trs.data,
trs.quantita,
trs.valuta
);
}
sw.Close();
ret = 0;
}
}
catch (Exception ex)
{
ret = -1;
}
return ret;
}
Compito del 15/03/2013, esercizio 5, correzione.
private int ExportFileTxt(string f, string nf, List list)
{
int ret = -1;
try
{
if (File.Exists(f))
{
StreamWriter sw = new StreamWriter(nf);
for (int i = 0; i < list.Count; i++)
{
trasp trs = (trasp)list[i];
sw.WriteLine("{0,-5}{1,-5}{2,-20}{3,-5}{4,-5}{5,-20}{6,-20}{7,-10}{8,-5}",
trs.id,
trs.codbanca,
trs.banca,
trs.codmovimento,
trs.codcliente,
trs.desccliente,
trs.data,
trs.quantita,
trs.valuta
);
}
sw.Close();
ret = 0;
}
}
catch (Exception ex)
{
ret = -1;
}
return ret;
}