Standards

Published on: August 25, 2010
Categories: C# C++ und C
Tags: No Tags
Comments: No Comments

Standards

Mathematische Befehle:
Math.Round(x,3); // Rundet x auf 3 Nachkommastellen
Math.Sqrt(x); Math.Pow(x, y); // Wurzel von x bzw x^y
Math.Exp(x); Math.Log(x);// e^x bzw log(x)
Math.Abs(x);// Absolut –> betrag -=+ und +=+
 
 
Strings:
StringBuilder str = new StringBuilder(“Das ist”); // Erstellt Objekt von Stringbuilder: s2
str.Append(” ein”); // Reiht ” ein” an
str.Append(” String”);// Reiht ” String” an
str[0] = ‘h’; // Setzt erste stelle von s2 auf “h” ( char mit ”)

static StringBuilder[] str = { new StringBuilder(“XXX”), new StringBuilder(“XXX”), }; //zb zugriff mit str[0][1]

str2 = String.Copy(str1); // Kopiert str1 in str2
str = str.Insert(3,”asdf”); // fügt ab der 3ten(3 exkludet) stelle asdf ein
str = str.Remove(3,4); // Löscht ab der 3ten(3 exkludet stelle 4 Zeichen
str2 = str.Substring(3); //Kopiert in str2 alles ab dem 3ten(3 exkludet) Zeichen
str1 = “hallo”; str2 = ” Welt”; str3 = str1 + str2; //str3 = “hallo Welt”

StreamReader sr = new StreamReader(fname); //Erstellt SR auf file
String str = sr.ReadToEnd(); // Liest ganze datei ein
int indexGBP = str.IndexOf(“GBP”); //erstellt die nummer des zu suchenden
String temp = str.Substring(indexGBP); // schneidet alles VOR der nummer weg!
String[] temp_array = temp.Split(‘\”); // Erstellt array nach jedem \
sr.Close();
double x = Convert.ToDouble(temp_array[2].Replace(“.”, “,”)); //hohlt sich das 2te raus! und ersetzt . durch, !
string[] woerter; // Inhalt wort für wort ausgeben !
woerter = string.Split(new char[] { ‘ ‘ });
foreach (string w in woerter)
Console.WriteLine(w);

Arrays:
int [] g = {1,3,4,5,1,2,3,5} //erstellt array g mit festen Werten
int [] f = new int[100]; //erstellt array f mit 100 Werten
double[] [] matrix = new double [3][]; //erstellt array maxtrix
matrix.GetLengh(0) //gibt die länge der 1. Dimension

Foreach:
foreach (int x in f) // Schleife: durchläuft den Array, gibt in x den Wert der jeweiligen Stelle aus.
foreach (char c in s2) ; //Schleife: durchläuft s2, gibt in c das Zeichen der jeweiligen Stelle aus.
Random:
Random rnd = new Random();
x = rnd.Next(); // Zufallswert ( von 0-999999)
x = rnd.Next(10);// Zufallswert ( von 0-9)
x = rnd.NextDouble();// Zufallswert (0,00… – 0,99….)
Zeit:
DateTime t1 = DateTime.Now; // Start Initialisierung
DateTime t2 = new DateTime(1987, 1, 21, 24,00,00); // jjjj,dd,mm,hh,mm,ss(oder einfach nur jjjj,dd,mm)
Console.WriteLine(“Zeit: ” + (DateTime.Now – t1)); // Ausgabe der Rechenzeit
Stopwatch sw = new Stopwatch(); // legt objekt an
sw.Start(); sw.Stop(); // start bzw stop
Console.WriteLine(sw.ElapsedMilliseconds); // Ausgabe

Boxing
double x = 99.99; int y = (int)x; //Überführt den Double in int ( Keine Rundung –> y=99)

Schleifen:
break; // Springt komplett aus der schleife hinaus
continue;// Springt zum Schleifenkopf ( unterbricht nur den einen durchgang)

Ausgabe:
Console.WriteLine(“1.Zahl{0:D8}, 2.Zahl{1}, 3.Zahl{2}”, x, y, z); // D8 Lässt die leern stellen mit 0 auffüllen ( 8 ziff.)
Console.WriteLine(“1.Zahl{0:F3}, 2.Zahl{1}, 3.Zahl{2}”, x, y, z); // F3 Lässt 3 stellen nach dem Komma noch da
 
 

Switch & einfache if ( ?:) :
 
y=(x==0? 1:x); // wenn x == 0 dann y = 1 sonst y=x
switch (i)
{ case ‘A’: Console.WriteLine(“\tSie haben A gewählt”); break;
case ‘B’: Console.WriteLine(“\tSie haben B gewählt”); break;
default: Console.WriteLine(“\tFalsche Eingabe”); break; }
 
LOAD SAVE:
FileStream fs1 = new FileStream(“Text.txt”, FileMode.Create);
StreamWriter w = new StreamWriter(fs1);
w.WriteLine(“Hallo”);
w.Close();
FileStream y = new FileStream(“Text.txt”, FileMode.Open);
StreamReader r = new StreamReader(y);
Console.WriteLine(r.ReadLine());
r.Close();
File.Exists(“Artikel.txt”)

REGEX:
using System.Text.RegularExpressions;
Regex numcheck = new Regex(“^[0-9]*$”);
int x;
bool isnum = numcheck.IsMatch(XX.Text);
if(isnum)
x = Convert.ToInt32(XX.Text);
else
zb: MessageBox.Show(“keine zahl!”)

No Comments - Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Welcome , today is Saturday, May 19, 2012