Start
Mandelbrot
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Sockets; using System.Text; //delegate void Writer(string s); namespace SuperVCD2 { public class Class1 { static void Main(string[] args) { // newway(); Mandelbrot(); Console.WriteLine("1"); Console.ReadKey(); } private static void newway() { // “Mandelbrot图像中的每个位置都对应于公式N=x+y*i中的一个复数。其实数部分是x,虚数部分是y,i是-1的平方根。图像中各个位置的x和y坐标对应于虚数的x和y部分。 //图像中的每个位置用参数N来表示,它是x*x+y*y的平方根。如果这个值大于或等于2,则这个数字对应的位置值是0。如果参数N的值小于2,就把N的值改为N*N- //“Mandelbrot 图像 //“Mandelbrot 图像 (3张) // N(N=(x*x-y*y-x)+(2*x*y-y)*i)),并再次测试这个新N值。如果这个值大于或等于2,则这个数字对应的位置值是1。这个过程一直继续下去,直到我们给图像中的位置赋一个值,或迭代执行的次数多于指定的次数为止。” double realCoord, imagCoord;//实数,虚数 double realTemp, imagTemp, realtemp2, arg; int iterations; for (imagCoord = 1.2; imagCoord >= -1.2; imagCoord -= 0.05)//48 { for (realCoord = -0.6; realCoord < 1.77; realCoord += 0.03) { iterations = 0; realTemp = realCoord; imagTemp = imagCoord; arg = (realCoord * realCoord) + (imagCoord * imagCoord); while ((arg < 4) && (iterations < 340)) { realtemp2 = (realTemp * realTemp) - (imagTemp * imagTemp) - realCoord; imagTemp = (2 * realTemp * imagTemp) - imagCoord; realTemp = realtemp2; arg = (realTemp * realTemp) + (imagTemp * imagTemp); iterations += 1; } switch (iterations % 4) { case 0: Console.Write("."); break; case 1: Console.Write("o"); break; case 2: Console.Write("O"); break; case 3: Console.Write("@"); break; } } Console.Write("\n"); } Console.ReadKey(); } public static void Mandelbrot() { double realCoord, imagCoord; double realMax = 1.77; double realMin = -0.6; double imagMax = -1.2; double imagMin = 1.2; double realStep; double imagStep; double realTemp, imagTemp, realTemp2, arg; int iterations; while (true) { realStep = (realMax - realMin) / 79; imagStep = (imagMax - imagMin) / 48; for (imagCoord = imagMin; imagCoord >= imagMax; imagCoord += imagStep) { for (realCoord = realMin; realCoord <= realMax; realCoord += realStep) { iterations = 0; realTemp = realCoord; imagTemp = imagCoord; arg = (realCoord * realCoord) + (imagCoord * imagCoord); while ((arg < 4) && (iterations < 40)) { realTemp2 = (realTemp * realTemp) - (imagTemp * imagTemp) - realCoord; imagTemp = (2 * realTemp * imagTemp) - imagCoord; realTemp = realTemp2; arg = (realTemp * realTemp) + (imagTemp * imagTemp); iterations += 1; } switch (iterations % 4) { case 0: Console.Write("."); break; case 1: Console.Write("o"); break; case 2: Console.Write("O"); break; case 3: Console.Write("@"); break; } } Console.Write("\n"); } Console.WriteLine("Current limits:"); Console.WriteLine("realCoord: from {0} to {1}", realMin, realMax); Console.WriteLine("imagCoord: from {0} to {1}", imagMin, imagMax); Console.WriteLine("Enter new limits:"); Console.WriteLine("realCoord: from:"); realMin = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("realCoord: to:"); realMax = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("imagCoord: from:"); imagMin = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("imagCoord: to:"); imagMax = Convert.ToDouble(Console.ReadLine()); } } } }
checked
short a=281;
byte b;
b = checked((byte )a);
static void Main(string[] args) { short shorResult, shortVal = 4; int integerVal = 67; long longResult; float floatVal=10.5F; double doubleResult, doubleVal = 99.999; string stringResult, stringVal = "17"; bool boolval = true; Console.WriteLine("Variable Conversion Example\n"); doubleResult = floatVal * shortVal; Console.WriteLine("Implicit,->double:{0}*{1}->{2}",floatVal ,shortVal ,doubleResult ); shorResult = (short)floatVal; Console.WriteLine("Explict,->short:{0}->{1}",floatVal ,shorResult); stringResult = Convert.ToString(boolval ) + Convert.ToString(doubleVal ); Console.WriteLine("Explict,-》string:\"{0}\"+\"{1}\"->{2}",boolval ,doubleVal ,stringResult ); longResult = integerVal + Convert.ToInt32(stringVal ); Console.WriteLine("Mixed,->long:{0}+{1}->{2}",integerVal ,stringVal ,longResult ); Console.ReadKey(); }
public class Class1 { static void Main(string[] args) { orientation myDirection = orientation.north; Console.WriteLine("myDirction={0}",myDirection ); Console.ReadKey(); } } enum orientation : byte { north=1, south=2, east=3, west=4 }
enum测试:
static void Main(string[] args)
{
byte directionByte;
string directionString;
orientation myDirection = orientation.north;
Console.WriteLine("myDirction={0}",myDirection );
directionByte = (byte)myDirection;
directionString = Convert.ToString(myDirection );//不能(string)转换,可以string.Tostring()
Console.WriteLine("byte equivalent={0}",directionByte);
Console.WriteLine("string equivalent={0}",directionString );
string myString = "north";
orientation myDirection1 = (orientation)Enum.Parse(typeof (orientation ),myString );
Console.WriteLine(myDirection1 );
int myDirection2 = Convert.ToInt32( Enum.Parse(typeof(orientation), myString));
Console.WriteLine(myDirection2);
Console.ReadKey();
}
public class Class1 { static void Main(string[] args) { route myRoute; int myDirection = -1; double myDistance; Console.WriteLine("1)North\n2)South\n3)East\n4) West"); end: do { Console .WriteLine("Select a direction:"); myDirection =Convert .ToInt32(Console .ReadLine()); } while ((myDirection <1) || (myDirection >4)); Console.WriteLine("Input a distance:"); myDistance = Convert.ToDouble(Console.ReadLine()); myRoute.direction =(orientation ) myDirection; myRoute.distance = myDistance; Console.WriteLine("myRoute specifies a direction of{0} and a"+ "distance of {1}",myRoute .direction ,myRoute.distance ); goto end; Console.ReadKey(); } } enum orientation : byte { north=1, south=2, east=3, west=4 } struct route { public orientation direction; public double distance; }
static void Main(string[] args) { //Ctrl 可透明化intelliSense string zhaoziming = "zhao zi ming"; char zhao = zhaoziming[0]; char[] zi = zhaoziming.ToCharArray(); foreach (char character in zi) { Console.WriteLine("{0}",character); } //length string myString = Console.ReadLine(); Console.WriteLine("Your string leng is {0}",myString.Length); //ToLower() ToUpper() string userResPonse = Console.ReadLine(); char[] trimChars = { ' ','e','s'}; userResPonse = userResPonse.Trim(trimChars); if (userResPonse.ToLower() == "y") { //删除空格 TrimStart() TrimEnd() //添加空格 PadLeft() PadRight() userResPonse = userResPonse.PadLeft(8, '_'); Console.WriteLine(userResPonse ); userResPonse = userResPonse.PadLeft(10);//左边多了9个空格 Console.WriteLine(userResPonse); //删除空格 TrimStart() TrimEnd() userResPonse = userResPonse.TrimStart(); Console.WriteLine(userResPonse); } char[] myseparator = { ' '}; string[] myword; myword = myString.Split(myseparator); foreach(string word in myword ) { Console.WriteLine("{0}",word ); } Console.ReadKey(); } }
//void 函数 return 是结束函数,return;不能加返回 内容; int[] myArray = { 1,23,3,2,3,5,6,7,1,3}; int maxVal = MaxValue(myArray ); Console.WriteLine("Max:{0}",maxVal ); //params 参数 strsearch("a good", myArray ); strsearch("a good", 1,2,3); strsearch("good a"); //ref int myNumver = 4;//ref参数必需初始化 Console.WriteLine("myNumver ={0}",myNumver ); ShowDouble(ref myNumver ); Console.WriteLine("myNumver ={0}", myNumver); //out int mynumber =9; outNum(myArray,out mynumber); Console.ReadLine(); } static void outNum(int [] xx,out int x) { x = 0;//out参数必需在这赋值 return; } static void ShowDouble(ref int val) { val *=2; } static string strsearch(string str,params int[] x) { return str; } static int MaxValue(int[] intArray) { int maxVal=intArray[0]; for(int i=1;i<intArray.Length ;i++) { if (intArray[i] > maxVal) { maxVal =intArray[i]; } } return maxVal; }
static void Main(string[] args) { //命令行可心输入 n个参数,以空格分开,包含空格 用“” 属性-》debug->Command line arguments 可添加参数 Console.WriteLine("{0}comand line :", args.Length); foreach(string arg in args ) { Console.WriteLine(arg); } Console.ReadKey(); }
static void Main(string[] args) { customerName myCustomer; myCustomer.firstname = "John"; myCustomer.lastName = "Frankin"; Console.WriteLine(myCustomer .Name()); Console.ReadKey(); } } struct customerName { public string firstname, lastName; public string Name() { return firstname + "" + lastName; } }
delegate double ProcessDelegate(double param1,double param2,double parame3); static double Multiply(double param1,double param2,double param3) { return param1 * param2 * param3; } static double Divide(double param1,double param2,double param3) { return param3 / param3 / param1; } static double zhu(ProcessDelegate process) { return process(1,2,3); } static void Main(string[] args) { //函数签名:函数名和参数共同构成,返回类型 不在签名中。重载看签名,不看返回类型。 //委托 把引用存储为函数的类型 ProcessDelegate process; string input = Console.ReadLine(); int commaPos = input.IndexOf(','); int comap = input.LastIndexOf(','); double param1 = Convert.ToDouble(input .Substring(0,commaPos )); string str = input.Substring(commaPos + 1, comap - commaPos); string str1 = input.Substring(commaPos + 1, comap - commaPos-1); double p = Convert.ToDouble("123,2,3"); double param2 = Convert.ToDouble(input.Substring(commaPos +1, comap-commaPos+1 )); double param3 = Convert.ToDouble(input.Substring(comap +1, input .Length-comap-1 )); Console.WriteLine(param3 + ":" + param2 + ":" + param1); Console.WriteLine("Enter M to multiply or d divide"); input = Console.ReadLine(); if (input == "M") { process = new ProcessDelegate(Multiply); } else { process = new ProcessDelegate(Divide); } Console.WriteLine("Result:{0}",process(param1 ,param2 ,param3 )); Console.WriteLine("Result1:{0}", zhu(process )); Console.ReadKey(); }
static void Main(string[] args) { int[] testArray = { 4,11,11,11,4,2,17,3,17,12,17,9}; int[] maxValIndices; int maxVal = Maxima(testArray ,out maxValIndices ); Console.WriteLine("Maximum value{0}indices:",maxVal ); foreach (int index in maxValIndices) { Console.WriteLine(index ); } Console.ReadKey(); } static int Maxima(int[] integers, out int[] indices) { Debug.WriteLine("Maximum value search started."); indices = new int[1]; int maxVal = integers[0]; indices[0] = 0; int count = 1; Debug.WriteLine(string .Format("Maxinum value initialized to {0} element 0.",maxVal )); for (int i = 1; i < integers.Length; i++) { Debug.WriteLine(string .Format("NOW ELMENT AT INDEX{0}.",i)); if (integers[i] > maxVal) { maxVal = integers[i]; count = 1; indices = new int[1]; indices[count -1] = i; Debug.WriteLine(string.Format("new maximum fond. new valueis{0},at elemnt{1}", maxVal, i)); } else { if (integers[i] == maxVal) { count++; int[] oldIndices = indices; indices = new int[count]; oldIndices.CopyTo(indices, 0); indices[count - 1] = i; Debug.WriteLine(string .Format("Duplicate fond atelement{0}.",i)); } } } Trace.WriteLine(string .Format("maxmum value{0}found,wite with{1}",maxVal ,count )); Debug.WriteLine("completed"); return maxVal; }
static string[] eTypes = { "none","simple","index","nested index"}; static void Main(string[] args) { foreach (string eType in eTypes) { try { Console.WriteLine("Main() try block reached."); Console.WriteLine("ThrowException(\"{0}\")called.", eType); ThrowException(eType); Console.WriteLine("Main()try block continues."); } catch (System.IndexOutOfRangeException e) { Console.WriteLine("mAIN() system.IndexOutofrangeException catch" + "block reached.Message:\n\"{0}\"", e.Message); } catch { Console.WriteLine("Main() general catch blick reached."); } finally { Console.WriteLine("Main() finally block reached."); } Console.WriteLine(); } Console.ReadKey(); } private static void ThrowException(string eType) { Console.WriteLine("ThrowException(\"{0}\") reached.",eType ); switch (eType ) { case "none": Console.WriteLine("Not throwing an exception."); break; case "simple": Console.WriteLine("Throwing system.Exception."); throw (new System .Exception ()); case "index": Console.WriteLine("Throwing System.IndexOutofRangeException."); eTypes[4] = "error"; break; case "nested index": try { Console.WriteLine("ThrowException(\"nested index\")" + "try block reached"); Console.WriteLine("ThrowException(\"index\")called."); ThrowException("index"); } catch { Console.WriteLine("ThrowException (\"nested index\")general" + "catch block reached."); throw; } finally { Console.WriteLine("ThrowException(\"nested index\")finally block reached."); } break; } }
浙公网安备 33010602011771号