现在的位置: 首页 > 综合 > 正文

学习软件设计——C#练习(6)

2018年01月09日 ⁄ 综合 ⁄ 共 1712字 ⁄ 字号 评论关闭

C#练习(1~8)源代码下载请到http://download.csdn.net/detail/hsttmht/3751088

引用请注明http://blog.csdn.net/hsttmht

 

 

1、运算符重载实验。编写一个类,重载~符号,使它能够实现关于原点对称坐标的计算
2、编写一个类,类中两个求最大值方法,参数分别为数组和整数
3、编写一个类,实现两个数字的交换

4、输出参数的使用

using System;
namespace Operator
{
  class mars
	{
		int a,b,c;
		public mars(int aa,int bb,int cc)
		{
			a=aa;
			b=bb;
			c=cc;
		}
	
		public static mars operator~(mars x)
		{
			mars q=new mars(0,0,0);
			q.a=(-1)*x.a;
			q.b=(-1)*x.b;
			q.c=(-1)*x.c;
			return q;
		}

		class test
		{
			public static void Main(String [] args)
			{
				mars s1=new mars(75,-19,128);
				mars s2=~s1;
				Console.WriteLine("{0},{1},{2}",s2.a,s2.b,s2.c);
				Console.ReadLine();
			}
		}
	}
}

using System;
using System.Collections.Generic;

class GetMax
{
	public static int Max(int a, int b)
	{
		if(a>b)
		return a;
		else
		return b;
	}
	
	public void FindMax(int[] array)
	{
		int max = array[0];
		for(int i=0;i<array.Length;i++)
		{
			if(array[i]>max)
			{
				max=array[i];
			}
		}
		Console.WriteLine(max);
	}

	public static void Main()
	{
		int Result=GetMax.Max(93,4);
		Console.WriteLine(Result);
		
		int[] array = new int[3]{1,77,-43};
		GetMax a=new GetMax();
        a.FindMax(array);
		
		Console.ReadLine();
	}
} 

using System;
using System.Collections.Generic;

class mars
    {
        static void Main(string[] args)
        {
            int a = 1;
            int b = 2;
            Console.WriteLine("mars交换前\ta={0}\tb={1}\t",a,b);
            Swap(ref a,ref b);
            Console.WriteLine("mars交换后\ta={0}\tb={1}\t",a,b);
            Console.Read();
        }
        //交换a,b两个变量的值
        private static void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
            Console.WriteLine("mars方法内\ta={0}\tb={1}\t",a,b);
        }
    }

using System;
using System.Collections.Generic;

public class MyClass
{
	static void SplitPath(string path,out string dir,out string name)
	{
		int i = path.Length;
		while (i>0)
		{
			char ch = path[i-1];
			if(ch=='\\'||ch=='/'||ch==':') break;
			i--;
		}
		dir = path.Substring(0,i);
		name = path.Substring(i);
	}

	public static void Main()
	{
		string dir,name;
		SplitPath("d:\\mars\\1.txt",out dir,out name);
		Console.WriteLine(dir);
		Console.WriteLine(name);
		Console.ReadLine();
	}
}

抱歉!评论已关闭.