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

本文提供了几个C#编程实验示例,包括坐标对称计算、求最大值方法、数值交换及路径拆分等,旨在帮助初学者理解C#中的运算符重载、方法定义与输出参数的使用。

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

引用请注明http://blog.youkuaiyun.com/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();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值