定自已的格式化类

public class Vector:IFormattable
	{
		public double x,y,z;
		public Vector(double dx,double dy,double dz)
		{
			x=dx;
			y=dy;
			z=dz;
		}
		public string ToString(string format,IFormatProvider formatProvider)
		{
			if(format==null)
				return ToString();
			string formatUpper=format.ToUpper();
			switch(formatUpper)
			{
				case "N":
					return "||"+Norm().ToString()+"||";
				case "VE":
					return String.Format("({0:E},{1:E},{2:E})",x,y,z);
				case "IJK":
					StringBuilder sb=new StringBuilder(x.ToString(),30);
					sb.Append("i+");
					sb.Append(y.ToString());
					sb.Append("j+");
					sb.Append(z.ToString());
					sb.Append("k");
					return sb.ToString();
				default:
					return ToString();
			}
				
		}
		public override string ToString()
		{
			return "("+x+","+y+","+z+")";
		}
		public double Norm()
		{
			return x*x+y*y+z*z;
		}
	}

你可能感兴趣的:(C++,c,C#,J#)