0% found this document useful (0 votes)
38 views9 pages

Programare - Sem

This document contains code for multiple C# programs related to data structures and algorithms seminars: 1. The first seminar code implements a queue data structure and allows adding and removing elements. 2. The second seminar code models a train using an array, queue and stack to sort wagons by class. 3. The third seminar code implements topological sorting on a graph read from a file where nodes are sorted based on dependencies.

Uploaded by

Flavia Mateescu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views9 pages

Programare - Sem

This document contains code for multiple C# programs related to data structures and algorithms seminars: 1. The first seminar code implements a queue data structure and allows adding and removing elements. 2. The second seminar code models a train using an array, queue and stack to sort wagons by class. 3. The third seminar code implements topological sorting on a graph read from a file where nodes are sorted based on dependencies.

Uploaded by

Flavia Mateescu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Seminar I Stive.Liste.

Cozi
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace sem1
{
class Program
{
static Queue<int> queue;
static void Main()
{
queue = new Queue<int>();
do
{
Console.WriteLine();
Console.Write("0: Exit, 1: Add, 2: Delete --> ");
switch (int.Parse(Console.ReadLine()))
{
case 1: Console.Write("el=");
int x = int.Parse(Console.ReadLine());
queue.Enqueue(x);
afis();
break;
case 2: queue.Dequeue();
afis();
break;
case 0: return;
}

}
while (true);

}
static private void afis()
{
int[] array = new int[queue.Count];
queue.CopyTo(array, 0);
Console.Write("Array:");
for (int i = 0; i < array.Length; i++)
{
Console.Write(" " + array[i]);
}
Console.WriteLine();
}
}

Seminar II Stive.Liste.Cozi App trenuri


using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace sem2
{
class Program
{
static void Main(string[] args)
{
int nrv = 0;
//coada
Queue<vagon> tren2 = new Queue<vagon>();
//stiva
Stack<vagon> tren3 = new Stack<vagon>();
Console.WriteLine("Citire date tren:");
nrv = int.Parse(Console.ReadLine());
vagon[] tren1 = new vagon[nrv];
// citire
for (int i = 0; i < nrv; i++)
{
tren1[i] = new vagon();
tren1[i].citire();
}
//afisare tren initial
for (int i = 0; i < nrv; i++)
{
Console.WriteLine(tren1[i].ToString());
}
//triaj tren initial dupa clasa
for (int i = 0; i < nrv; i++)
{
if (tren1[i].clasa == 'I')
tren2.Enqueue(tren1[i]);
else tren3.Push(tren1[i]);
}
//adaugare vagoane clasa a doua
foreach (vagon v in tren3)
{
tren2.Enqueue(tren3.Pop());
}
//afisare tren cu structura finala
foreach (vagon v in tren2)
{
Console.WriteLine(v.ToString());
}
}
class vagon
{
public int id;
public char clasa;
public void citire()
{

Console.WriteLine("Introduceti vagonul:");
this.id = int.Parse(Console.ReadLine());
Console.WriteLine("Introduceti clasa vagonul:");
this.clasa = char.Parse(Console.ReadLine());
}
public override string ToString()
{
return "[" + this.id + "," + this.clasa + "]";
}
}

Seminar III Sortarea topologica. Grafuri. Noduri

using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[]linii = File.ReadAllLines("fis.txt");
int n = int.Parse (linii[0]);
int[] pred = new int[n];
List<int>[]succ= new List<int>[n];
for (int i = 0; i < n; i++)
{ succ[i] = new List<int>();
}
for(int i=1; i<linii.Length; i++)
{
string[] atom = linii[i].Split(',');
int p = int.Parse(atom[0]);
int s = int.Parse(atom[1]);
pred[s]++;
succ[p].Add(s);
}
for(int it=0; it<n; it++)
{
for (int i = 0; i < n; i++)
if (pred[i] == 0)
{
Console.WriteLine(i);
pred[i] = -1;
foreach (int s in succ[i])
pred[s]--;
}
}

}
}

Seminar IV Greedy.Eurustice.Bubble Sort App task-urilor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string [] linii = File.ReadAllLines("fis.txt");
int[] v = new int [linii.Length];
for (int i = 0; i < linii.Length; i++)
v[i] = int.Parse(linii[i]);
Array.Sort(v, descrescator);
int m;
Console.Write("m = "); m=int.Parse(Console.ReadLine());
int[] T = new int [m];
for (int i = 0; i < v.Length; i++)
{
int imin = 0;
for (int j = 1; j < m; j++)
if (T[imin] > T[j])
imin = j;
Console.WriteLine("Task-ul {0} a fost alocat angajatului {1}", i,imin );
T[imin] += i;
foreach (int j in T)
Console.Write(j + " ");
Console.WriteLine();
}
Console.ReadKey();
}
static int descrescator(int x, int y)
{
return -x.CompareTo(y);
}
}
}

Seminar V Greedy App rucsac

using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
float G;//capacitate rucsac
//
, AC, AG; //auxiliare pentru creare obiecte : castig, greutate
//int optiune,
// k = 0; // contor
List<Obiect> rucsac = new List<Obiect>();
String[] lines = File.ReadAllLines("IN.txt");
foreach (String line in lines)
{
rucsac.Add(new Obiect(line));
}
do
{
Console.WriteLine("Care este capacitatea rucsacului ?");
G = Convert.ToInt32(Console.ReadLine());
if (G < 0) Console.WriteLine("Greutate gresita ! ");
} while (G < 0);
//optiune = 1;
//do
//{
// Console.WriteLine("Doriti sa introduceti un obiect nou ? 1 = DA, 0 = NU");
// optiune = Convert.ToInt32(Console.ReadLine());
// if (optiune == 1)
// {
//
Console.WriteLine("Care este greutatea obiectului ?");
//
AG = Convert.ToInt32(Console.ReadLine());
//
Console.WriteLine("Care este castigul obtinut in urma vanzarii obiectului ?");
//
AC = Convert.ToInt32(Console.ReadLine());
//
if ((AG > 0) && (AC > 0))
//
{
//
rucsac.Add(new Obiect(AG, AC));
//
}
// }
//} while (optiune == 1);
rucsac.Sort();
int i = 0;
Console.WriteLine("Am adaugat obiectele : ");
while (G > 0 && i < rucsac.Count)
{
if (G > rucsac[i].G)
{
G -= rucsac[i].G;
Console.WriteLine(rucsac[i]);
}

else
{
Console.WriteLine(rucsac[i] + "-" + (G / rucsac[i].G * 100) + "%");
G = 0;
}
i++;
}
//Console.WriteLine(">> " + Convert.ToString(G)+ " <<");

}
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;

namespace ConsoleApplication1
{
class Obiect:IComparable<Obiect>
{
int _id;
float _g // Greutate obiect
, _c // Castig obtinut in urma vanzarii obiectului, dupa ce este adaugat in rucsac
, _r; // Rentabilitate ( c / g )
public float G
{
get { return _g; }
}
public float C
{
get { return _c; }
}
public float ID
{
get { return _id; }
}
public float R
{
get { return _r; }
}
public Obiect()
{
_g = -1;
_c = -1;
_r = -1;
}
public Obiect(float g, float c)
{
_g = g;
_c = c;

if (g != 0)
_r = c / g;
else _r = -1;

public Obiect(String line)


{
String[] atomi = line.Split(' ');
_id = Int32.Parse(atomi[0]);
_g = Single.Parse(atomi[1]);
_c = Single.Parse(atomi[2]);
if (_g != 0)
_r = _c / _g;
else _r = 0;
}
public int CompareTo(Obiect b)
{
return -this.R.CompareTo(b.R);
}
public override String ToString()
{
return Convert.ToString(this._id) + ' ' + Convert.ToString(this._g) + ' ' +
Convert.ToString(this._c) + ' ' + Convert.ToString(this._r);
}
}
}

Seminar V App Sierpinski


using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;

using
using
using
using
using

System.Drawing;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{ Graphics G;
public Form1()
{
InitializeComponent();
G = pictureBox1.CreateGraphics();
}
void srp(int n, Point a, Point b, Point c, int r, int g, int bl)
{
if (n > 0)
{
Point ma = new Point((b.X + c.X) / 2, (b.Y + c.Y) / 2);
Point mb = new Point((c.X + a.X) / 2, (c.Y + a.Y) / 2);
Point mc = new Point((b.X + a.X) / 2, (b.Y + a.Y) / 2);
srp(n - 1, a, mc, mb,r/2,g,bl);
srp(n - 1, mc, b, ma,r,g/2, bl);
srp(n - 1, mb, ma, c, r, g, bl/2);
}
else
{
Point[]p={a,b,c};
Color col= Color.FromArgb(255, r, g, bl);
SolidBrush br = new SolidBrush(col);
G.FillPolygon(br, p);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Refresh();
srp(comboBox1.SelectedIndex, new Point(pictureBox1.Width/2, 0), new Point(0,
pictureBox1.Height-1), new Point(pictureBox1.Width, pictureBox1.Height-1),255,255,255);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy