第七章 运算符和类型强制转换

Posted liuslayer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第七章 运算符和类型强制转换相关的知识,希望对你有一定的参考价值。

SimpleCurrency

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleCurrency
{
    class Program
    {
           struct Currency
           {
              public uint Dollars;
              public ushort Cents;
              
              public Currency(uint dollars, ushort cents)
              {
                 this.Dollars = dollars;
                 this.Cents = cents;
              }
              
              public override string ToString()
              {
                 return string.Format("${0}.{1,-2:00}", Dollars,Cents);
              }

              public static implicit operator float(Currency value)
              {
                  return value.Dollars + (value.Cents / 100.0f);
              }

              public static explicit operator Currency(float value)
              {
                  uint dollars = (uint)value;
                  ushort cents = (ushort)((value - dollars) * 100);
                  return new Currency(dollars, cents);
              }

           }


        static void Main(string[] args)
        {
           try
           {
              Currency balance = new Currency(50,35);
              
              Console.WriteLine(balance);
              Console.WriteLine("balance is " + balance);
              Console.WriteLine("balance is (using ToString()) " + balance.ToString());
              
              float balance2= balance;
              
              Console.WriteLine("After converting to float, = " + balance2);
              
              balance = (Currency) balance2;
              
              Console.WriteLine("After converting back to Currency, = " + balance);
              Console.WriteLine("Now attempt to convert out of range value of " +
                                "-$50.50 to a Currency:");
              
              checked
              {
                 balance = (Currency) (-50.50);
                 Console.WriteLine("Result is " + balance.ToString());
              }
           }
           catch(Exception e)
           {
              Console.WriteLine("Exception occurred: " + e.Message);
           }

           Console.ReadLine();
        }
    }
}
View Code

SimpleCurrency2

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleCurrency
{
    class Program
    {
        struct Currency
        {
            public uint Dollars;
            public ushort Cents;

            public Currency(uint dollars, ushort cents)
            {
                this.Dollars = dollars;
                this.Cents = cents;
            }

            public override string ToString()
            {
                return string.Format("${0}.{1,-2:00}", Dollars, Cents);
            }

            public static implicit operator float(Currency value)
            {
                return value.Dollars + (value.Cents / 100.0f);
            }

            public static explicit operator Currency(float value)
            {
                uint dollars = (uint)value;
                ushort cents = (ushort)((value - dollars) * 100);
                return new Currency(dollars, cents);
            }

            public static implicit operator Currency(uint value)
            {
                return new Currency(value, 0);
            }

            public static implicit operator uint(Currency value)
            {
                return value.Dollars;
            }


        }


        static void Main(string[] args)
        {
            try
            {
                   Currency balance = new Currency(50,35);
      
                   Console.WriteLine(balance);
                   Console.WriteLine("balance is " + balance);
                   Console.WriteLine("balance is (using ToString()) " + balance.ToString());                    

                   float balance2 = balance;

                   Console.WriteLine("After converting to float, = " + balance2);

                   balance = (Currency)balance2;

                   Console.WriteLine("After converting back to Currency, = " + balance);
                   Console.WriteLine("Now attempt to convert out of range value of " +
                                     "-$50.50 to a Currency:");
              
                checked
                {
                    balance = (Currency)(-50.50);
                    Console.WriteLine("Result is " + balance.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occurred: " + e.Message);
            }

            Console.ReadLine();
        }
    }
}
View Code

VectorStruct

技术分享
using System;
using Wrox.ProCSharp.OOCSharp;

namespace VectorStruct
{
    internal class MainEntryPoint
    {
        private static void Main()
        {
            Vector vect1, vect2, vect3;

            vect1 = new Vector(3.0, 3.0, 1.0);
            vect2 = new Vector(2.0, -4.0, -4.0);
            vect3 = vect1 + vect2;

            Console.WriteLine("vect1 = " + vect1.ToString());
            Console.WriteLine("vect2 = " + vect2.ToString());
            Console.WriteLine("vect3 = " + vect3.ToString());

            Console.ReadLine();
        }
    }
}

namespace Wrox.ProCSharp.OOCSharp
{
    struct Vector
    {
        public double x, y, z;

        public Vector(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public Vector(Vector rhs)
        {
            x = rhs.x;
            y = rhs.y;
            z = rhs.z;
        }

        public override string ToString()
        {
            return "( " + x + " , " + y + " , " + z + " )";
        }

        public static Vector operator +(Vector lhs, Vector rhs)
        {
            Vector result = new Vector(lhs);
            result.x += rhs.x;
            result.y += rhs.y;
            result.z += rhs.z;

            return result;
        }
    }
}
View Code

VectorStructMoreOverloads

技术分享
using System;
using Wrox.ProCSharp.OOCSharp;

namespace VectorStruct
{
    internal class MainEntryPoint
    {
        private static void Main()
        {
            // stuff to demonstrate arithmetic operations
            Vector vect1, vect2, vect3;
            vect1 = new Vector(1.0, 1.5, 2.0);
            vect2 = new Vector(0.0, 0.0, -10.0);

            vect3 = vect1 + vect2;

            Console.WriteLine("vect1 = " + vect1);
            Console.WriteLine("vect2 = " + vect2);
            Console.WriteLine("vect3 = vect1 + vect2 = " + vect3);
            Console.WriteLine("2*vect3 = " + 2 * vect3);

            vect3 += vect2;

            Console.WriteLine("vect3+=vect2 gives " + vect3);

            vect3 = vect1 * 2;

            Console.WriteLine("Setting vect3=vect1*2 gives " + vect3);

            double dot = vect1*vect3;

            Console.WriteLine("vect1*vect3 = " + dot);

            Console.ReadLine();
        }
    }
}

namespace Wrox.ProCSharp.OOCSharp
{
    struct Vector
    {
        public double x, y, z;

        public Vector(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public Vector(Vector rhs)
        {
            x = rhs.x;
            y = rhs.y;
            z = rhs.z;
        }

        public override string ToString()
        {
            return "( " + x + " , " + y + " , " + z + " )";
        }

        public static Vector operator +(Vector lhs, Vector rhs)
        {
            Vector result = new Vector(lhs);
            result.x += rhs.x;
            result.y += rhs.y;
            result.z += rhs.z;

            return result;
        }

        public static Vector operator *(double lhs, Vector rhs)
        {
            return new Vector(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
        }

        public static Vector operator *(Vector lhs, double rhs)
        {
            return rhs * lhs;
        }

        public static double operator *(Vector lhs, Vector rhs)
        {
            return lhs.x * rhs.x + lhs.y + rhs.y + lhs.z * rhs.z;
        }

    }
}
View Code

 

以上是关于第七章 运算符和类型强制转换的主要内容,如果未能解决你的问题,请参考以下文章

重载()运算符和重载强制类型转换

__x__(53)0918第七天__ JavaScript 强制类型转换

python数据类型强制转换和运算符

SV强制类型转换和常数

C++ --- 强制转换运算符

java加法运算 (强制类型转换)