Posts Tagged ‘c#’
Date : July 9th, 2009Author : saiq
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;//注意,调试模式
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int[] testArray ={ 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
int[] maxValIndices;
int maxVal = Maxima(testArray, out maxValIndices);
Console.WriteLine("Maximum value {0} found at element 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 start.");
indices=new int[1];//数组长度1
int maxVal=integers[0];
indices[0]=0;
int count=1;
Debug.WriteLine("Maximum value initilized to" + maxVal + ", at element index 0.");
for (int i=1;i<integers.Length;i++)
{
Debug.WriteLine("Now looking at element at index"+i+".");
if (integers[i]>maxVal)
{
maxVal=integers[i];
count=1;
indices=new int[1];
indices[0]=i;//重新对数组中的元素赋值
Debug.WriteLine("New maximum found. New value is "+maxVal+", at element index "+i+".");
}
else
{
if (integers[i]==maxVal)
{
count++;//计数器加1
int[] oldIndices=indices;
indices=new int[count];//数组长度变化
oldIndices.CopyTo(indices,0);//将旧数组中的元素复制进新的数组,位置从0开始
indices[count-1]=i;
Debug.WriteLine("Duplicate maximum found at element index"+i+".");
}
}
}
Trace.WriteLine("Maximum value "+maxVal+" found, with "+count+" occurrences.");
Debug.WriteLine("Maximum value search completed.");
return maxVal;
}
}
}
Tags : c#
Date : July 9th, 2009Author : saiq
struct order
{
public string itemName;
public int unitCount;
public double unitCost;
public double SumCost()
{
return unitCount * unitCost;
}
public string OrderInformation()
{
return "Order information: " + unitCount + " " + itemName + " " + "item at $" + unitCost + " each, total cost $" + SumCost();
}
}
static void Main(string[] args)
{
order bill;
bill.itemName = "Milk";
bill.unitCount = 10;
bill.unitCost = 0.3;
Console.WriteLine(bill.SumCost());
Console.WriteLine(bill.OrderInformation());
Console.ReadKey();
}
第6章练习题5
Tags : c#
Date : July 9th, 2009Author : saiq
delegate double ProcessDelegate(double param1, double param2);
static double Multiply(double param1, double param2)
{
return param1 * param2; //返回乘积
}
static double Divide(double param1, double param2)
{
return param1 / param2; //两数相除
}
static void Main(string[] args)
{
ProcessDelegate process;
Console.WriteLine("Enter 2 numbers separated with a comma:"); //输入两个数字并且以逗号隔开
string input = Console.ReadLine();
int commaPos = input.IndexOf(',');//indexof:查找第一次出现逗号的位置;indexof(','2)第二次出现逗号的位置;indexof(',',3,2)从第三开始查,查两位;返回值是绝对位置,为空则为 -1
double param1 = Convert.ToDouble(input.Substring(0, commaPos));//Substring:截取从0-commaPos(2)的字符串
double param2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1));
Console.WriteLine("Enter M to Multiply or D to divide:");
input = Console.ReadLine();
if (input == "M")
process = new ProcessDelegate(Multiply);
else
process = new ProcessDelegate(Divide);
Console.WriteLine("Result: {0}", process(param1, param2));
Console.ReadKey();
}
Tags : c#
Date : July 7th, 2009Author : saiq
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int var1, var2;
//录入开始
start:
Console.WriteLine("Enter a number:");
var1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter another number");
var2 = Convert.ToInt32(Console.ReadLine());
//录入结束
if (var1>10)
{
if (var2 > 10)
{
Console.WriteLine("都超过10,重新输入");
goto start;
}
else
{
Console.WriteLine("{0}", var1);
}
}
else {
if (var2>10) {
Console.WriteLine("{0}",var2);
}
else
Console.WriteLine("两数都不超过10");
}
Console.ReadKey();
}
}
}
这段代码码得我全身发抖了
Tags : asp.net, c#
Date : July 6th, 2009Author : saiq
一段C#的数组示例,看得我头大
using System;
class Program
{
public static void Main()
{
int[] myInts ={ 5, 10, 15 };
bool[][] myBools = new bool[2] [];
myBools[0] = new bool[2];
myBools[1] = new bool[1];
double[,] myDoubles = new double[2, 2];
string[] myString = new string[3];
Console.WriteLine("myInts[0]:{0}, myInts[1]:{1}, myInts[2]:{2}", myInts[0], myInts[1], myInts[2]);
myBools[0][0]=true;
myBools[0][1] = false;
myBools[1][0]=true;
Console.WriteLine("myBools[0][0]: {0}, myBools[1][0]:{1}", myBools[0][0], myBools[1][0]);
myDoubles[0, 0] = 3.147;
myDoubles[0, 1] = 7.157;
myDoubles[1, 1] = 2.117;
myDoubles[1, 0] = 56.000123234234;
Console.WriteLine("myDoubles[0,0]:{0}, myDoubles[1,0]:{1}", myDoubles[0,0], myDoubles[1,0]);
myString[0] = "Joe";
myString[1] = "Matt";
myString[2] = "Robert";
Console.WriteLine("myString[0]:{0}, myString[1]:{1}, myString[2]:{2}", myString[0], myString[1], myString[2]);
}
}
Tags : .net, c#
|
|