C# introduktion. using System; Namespace MyFirst { class eks1 { public static void Main() { Console.WriteLine("Hej!"); Console.Write("Skriv dit navn her: "); string navn = Console.ReadLine(); Console.WriteLine("Hej igen - " + navn + ". Jeg er en meget nyttig og arbejdsom medhjælp."); }
C# program struktur. De eksisterende systemer der bruges. Navnet på dette system Komponet1(class) Data og kode Komponet2(class) Data og kode Komponet3(class) Data og kode Komponet4(class) Data og kode
Using System; Namespace { classe komponentnavn1 { Data. metoder. } classe komponentnavn2 { Data. metoder. } class startkomponent { static void Main() { kode; }
SimpelPlot
Main vindue Vælge fil Indlæse fil Plotedata Datafil Databuffer Plot vindue Replot Print color graf read()
namespace SimpelPlot { public class Databuf { private float[] data_val; public int antelm = 0; public Databuf(int size) { data_val = new float[size]; } public void putdata(float val) { data_val[antelm]=val; antelm= antelm + 1; } public float getdata(int x) { return data_val[x]; }
using System; namespace SimpelPlot { public class start { private int heltal1= 3; public static void Main() { Databuf buffer= new Databuf(10); buffer.putdata(3.4); buffer.putdata(4.4); float a; a= (float)Math.Sqrt(3.4*4/5); buffer.putdata(a); a= buffer.getdata(0); }
Typer
Kontrolstrukturer if (x < y) { min = x; max = y; } else { min = y; max = x; } > < >= <= == != || && ! (x<=y) && (y< z)
Kontrolstrukturer int x= 4; switch (x) { case 1: Console.WriteLine("x=1"); break; case 2: case 3: Console.WriteLine("x= 2 eller 3"); break; default: Console.WriteLine("x er ikke lig 1,2 eller 3"); break; }
Kontrolstrukturer: Loop double[] databuf = new double[5]; for (int i = 0; i < 5; i++) { databuf[i] = 0.0; } int i=0; while (i<5) { databuf[i] = 0.0; i=i+1; }
Tekst strenge float b = 5; Console.WriteLine("a= {0,5:f2}",b); Console.WriteLine("a= {0,-5:f2}", b); b =.678e2F; string str = string.Format("a= {0,7:f2}", b); String str2 = "her er en tekst"; str = str + " her er en tekst - " + str2; Console.WriteLine(str); a= 5,00 a= 67,80 her er en tekst – her er en tekst
Skrivning til fil FileStream stream = new FileStream("udfil.txt", FileMode.Create); StreamWriter filoutput = new StreamWriter(stream); float b = 5; filoutput.WriteLine("a= {0,5:f2}", b); filoutput.WriteLine("a= {0,-5:f2}", b); float b =.678e2F; str = string.Format("a= {0,7:f2}", b); str2 = "her er en tekst"; str = str + " her er en tekst - " + str2; filoutput.WriteLine(str); filoutput.Flush(); filoutput.Close(); a= 5,00 a= 67,80 her er en tekst – her er en tekst Filen: udfil.txt
Binær fil FileStream bstream= new FileStream("udfil.bin", FileMode.Create); BinaryWriter bfil = new BinaryWriter(bstream); BinaryReader bin = new BinaryReader(bstream); bfil.Write((double)b); bfil.Write((double)b + 1); bfil.Write((double)b + 2); bstream.Seek(0, SeekOrigin.Begin); Boolean eof = false; while (!eof) { try { Console.WriteLine("in værdi= {0,7:f2}",bin.ReadDouble()); } catch (EndOfStreamException) { eof = true; } bfil.Close();
Grafik. Skaf ’’Graphics’’ objektet for det du vil tegne på. Fx: Graphics g= grafBox.CreateGraphics(); Pen pen = new Pen(Color.Black,3); g.DrawLine(pen, 0, 0, 100, 100); 0,0
Grafik. Skal gentegnes når: Vindue flyttes, foran liggende vindue fjernes, osv. Løsning: Re-tegn hver gang der komme et Paint-event. private void plot_but_Click( object sender, EventArgs e) { PlotForm pwin = new PlotForm(); pwin.Show(); pwin.setfilname(filename); pwin.setData(databuf); } private void grafBox_Paint(object sender, PaintEventArgs e) { g = e.Graphics; gpen= new Pen(gcolor,1); Pen axpen = new Pen(Color.Black); graf = new Graf(g, grafBox.Width, grafBox.Height); graf.axis(axpen, (float)0.0, (float)10.0); if (data != null) { for (int i = 0; i <= data.antset; i++) graf.curve(g, gpen, data.antelm, data.data_val[i]); }
DataFil var1 3,4 5, e var
SimpelPlot Main vindue Vælge fil Indlæse fil Plotedata Datafil Databuffer Plot vindue Replot Print color graf read()
Klasser MainForm Public Databuf databuf; Private void fil_but_Click(); Private void in_but_Click(); Private void plot_but_Click(); Filclass Public Filclass(); Public void read(); Databuf Public float[][] data_val; Public int antset; Public int antem; Public Databuf(int,int); Public void putdata(int,int,float); Public float getdata(int,int); PlotForm Private Graf graf; Public PlotForm(); Public void setData(Databuf); Private void replot_Click(); Private void print_Click(); Private void color_Click(); Graf Public void axis(); Public void curve();