c#想要过程。,!~.…

定义一个人民币RMB类,其中成员包括
1.定义元、角、分三个字段,以及相应的构造函数。
2.对三个字段设置读写属性;
3.定义Print函数,用于输出RMB类数值:元角*分;
4.在类中定义+、-两个操作符的重载。对两个RMB对象进行加减操作,如3元5角8分+4元2角3分=7元8角1分


class RMB
{
    private int _y, _j, _f;
    public RMB(int y = 0, int j = 0, int f = 0)
    {
        _y = y;
        _j = j;
        _f = f;
    }
    public int Yuan
    {
        get { return _y; }
        set { _y = value; }
    }
    public int Jiao
    {
        get { return _j; }
        set { _j = value; }
    }
    public int Fen
    {
        get { return _f; }
        set { _f = value; }
    }

    public static RMB operator +(RMB a, RMB b)
    {
        a.Fen += b.Fen;
        a.Jiao += b.Jiao;
        a.Yuan += b.Yuan;
        if (a.Fen > 9)
        {
            a.Fen -= 10;
            a.Jiao += 1;
        }
        if (a.Jiao > 10)
        {
            a.Jiao -= 10;
            a.Yuan += 1;
        }

        return a;
    }
    public static RMB operator -(RMB a, RMB b)
    {
        a.Fen -= b.Fen;
        a.Jiao -= b.Jiao;
        a.Yuan -= b.Yuan;
        if (a.Fen < 0)
        {
            a.Fen += 10;
            a.Jiao -= 1;
        }
        if (a.Jiao < 0)
        {
            a.Jiao += 10;
            a.Yuan -= 1;
        }
        return a;

    }
    public void Print()
    {
        Console.WriteLine($"{Yuan}元{Jiao}角{Fen}分");
    }
}

class Program
{
    static void Main(string[] args)
    {
        RMB a = new RMB(3, 2, 0);
        RMB b = new RMB(6, 1, 2);
        Console.Write("a:");
        a.Print();
        Console.Write("b:");
        b.Print();
        a += b;
        Console.Write("a+b:");
        a.Print();
        RMB c = new RMB(3, 4, 5);
        Console.Write("c:");
        c.Print();
        a -= c;
        Console.Write("a-c:");
        a.Print();
        Console.ReadLine();

    }
}

可参考

C#是微软公司发布的一种由C和C++衍生出来的面向对象的编程语言
可以看看这位博主写的,用C++实现的思路,对用C#编程该问题会有帮助。
https://blog.csdn.net/qq_57987156/article/details/124584852

运行结果

img

using System;

namespace rmb
{
    class CRmb
    {
        public CRmb(double yuan, double jiao, double fen)
        {
            this.y = yuan;
            this.j = jiao;
            this.f = fen;
        }
        public CRmb()
        {
        }

        public double y, j, f;//元、角、分

        public void sety(double yuan)
        {
            this.y = yuan;
        }

        public void setj(double jiao)
        {
            this.j = jiao;
        }

        public void setf(double fen)
        {
            this.f = fen;
        }

        //重载 + 运算符来把两个rmb对象相加
        public static CRmb operator +(CRmb p1, CRmb p2)
        {
            p1.y = p1.y + p2.y;
            p1.j = p1.j + p2.j;
            p1.f = p1.f + p2.f;
            //需要进行判断以进位
            while (p1.f >= 10)
            {
                p1.j++;
                p1.f -= 10;
            }
            while (p1.j >= 10)
            {
                p1.y++;
                p1.j -= 10;
            }
            return p1;
        }
        //重载 - 运算符来把两个rmb对象相减
        public static CRmb operator -(CRmb p1, CRmb p2)
        {
            if (p1.f < p2.f)
            {
                p1.j--;//借位相减,相当于竖式减法
                p1.f = p1.f + 10 - p2.f;
            }
            else
            {
                p1.f -= p2.f;
            }
            if (p1.j < p2.j)
            {
                p1.y--;
                p1.j = p1.j + 10 - p2.j;
            }
            else
            {
                p1.j -= p2.j;
            }
            p1.y -= p2.y;

            return p1;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //定义两个人民币的对象
            CRmb r1 = new CRmb();
            CRmb r2 = new CRmb();

            r1.sety(3);
            r1.setj(5);
            r1.setf(8);

            r2.sety(4);
            r2.setj(2);
            r2.setf(3);

            r1 += r2;
            Console.WriteLine("{0}元{1}角{2}分", r1.y, r1.j, r1.f);
        }
    }
}

代码不难 无非就是逢10进1的基础换算, 注释都给你写了,并不是唯一解 你可以参考下思路

img


/// <summary>
    /// 人民币RMB类
    /// </summary>
    public class RMB
    {
        #region 定义元、角、分三个字段 并设置读写属性
        /// <summary>
        ///
        /// </summary>
        public int Y { get; set; }
        /// <summary>
        ///
        /// </summary>
        public int J { get; set; }
        /// <summary>
        ///
        /// </summary>
        public int F { get; set; }
        #endregion

        #region 构造函数
        public RMB(int Y,int J,int F) {
            this.Y = Y;
            this.J = J;
            this.F = F;
        }
        #endregion

        /// <summary>
        /// Print函数,用于输出RMB类数值:元角*分;
        /// </summary>
        public void Print()
        {
            Console.WriteLine($"人民币计算结果为:{Y}{J}{F}分");
        }

        #region 定义+、-两个操作符的重载
        /// <summary>
        /// +
        /// </summary>
        /// <param name="Y"></param>
        /// <param name="J"></param>
        /// <param name="F"></param>
        public void Plus(int Y, int J, int F)
        {
            //计算换算成分
            int F1 = ConvertToF(this.Y, this.J, this.F);//基数
            int F2 = ConvertToF(Y, J, F);//加数
            //求和
            int sum = F1 + F2;
            //结果转换为元角分
            this.ConvertToYJF(sum);

        }
        /// <summary>
        /// -
        /// </summary>
        /// <param name="Y"></param>
        /// <param name="J"></param>
        /// <param name="F"></param>
        public void Reduce(int Y, int J, int F)
        {
            //计算换算成分
            int F1 = ConvertToF(this.Y, this.J, this.F);//基数
            int F2 = ConvertToF(Y, J, F);//加数
            int differ = F1 - F2;
            //结果转换为元角分
            this.ConvertToYJF(differ);
        }
        #endregion

        /// <summary>
        /// 将元角分换算成分
        /// </summary>
        /// <param name="Y"></param>
        /// <param name="J"></param>
        /// <param name="F"></param>
        /// <returns></returns>
        private int ConvertToF(int Y, int J, int F)
        {
            int f0 = F;//分
            int f1 = J * 10;//角转分
            int f2 = Y * 10 * 10;//元转分
            int f = f0 + f1 + f2;//分值总和
            return f;
        }

        /// <summary>
        /// 将分转换为元角分
        /// </summary>
        /// <param name="TotalF"></param>
        private void ConvertToYJF(int TotalF)
        {
            int J = TotalF / 10;
            int F = TotalF - (J * 10);
            int Y = J / 10;
            J = J - (Y * 10);

            this.Y = Y;
            this.J = J;
            this.F = F;
        }
    }

运行结果及代码如下:

img

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace CsharpConsoleApplication1
{

    public class RMB
    {
        private int _y;
        private int _j;
        private int _f;

        public void sety(int y) { _y = y; }
        public void setj(int j) { _j = j; }
        public void setf(int f) { _f = f; }
        public int gety() { return _y; }
        public int getj() { return _j; }
        public int getf() { return _f; }

        public void Print()
        {
            Console.Write("{0}元{1}角{2}分", _y, _j, _f);
        }

        //重载+
        public static RMB operator+(RMB a,RMB b)
        {
            RMB c = new RMB();
            int y, j, f;
            int flag = 0;
            f = a.getf() + b.getf();
            if (f >= 10)
            {
                f -= 10;
                flag = 1;
            }
            j = a.getj() + b.getj()+flag;
            if (j >= 10)
            {
                j -= 10;
                flag = 1;
            }
            else
                flag = 0;


            y = a.gety() + b.gety()+flag;
            c.sety(y);
            c.setj(j);
            c.setf(f);
            return c;
        }
        //重载-
        public static RMB operator -(RMB a, RMB b)
        {
            RMB c = new RMB();
            int y, j, f;
            int flag = 0;
            f = a.getf() - b.getf();

            if (f < 0)
            {
                f += 10;
                flag = 1;
            }

            j = a.getj() - b.getj() -flag;

            if (j < 0)
            {
                j += 10;
                flag = 1;
            }
            else
                flag = 0;

            y = a.gety() - b.gety() - flag;

            c.sety(y);
            c.setj(j);
            c.setf(f);
            return c;
        }
        
    }

    class Program
    {
        static void Main(string[] args)
        {
            RMB a = new RMB();
            RMB b = new RMB();
            a.sety(3); a.setj(5); a.setf(8);
            b.sety(4); b.setj(2); b.setf(3);
            RMB c = a + b;
            a.Print();
            Console.Write("+");
            b.Print();
            Console.Write("=");
            c.Print();
            Console.WriteLine("");

            RMB d = b - a;
            b.Print();
            Console.Write("-");
            a.Print();
            Console.Write("=");
            d.Print();
        }
    }
}


C# 还是C++ 还是c

https://blog.csdn.net/m0_63064861/article/details/125522594
这里有一个 c++的,你自己移植一下,很简单的

先打开 vs ide,然后写上一句代码 DateTime,然后鼠标右键,选择转到实现。

这就是你要的处理过程。我们如果要写他可以参考的模型就是Datetime。你是圆角分,他是年月日
你要print,他有ToString

你要+,-操作,他有 public static DateTime operator +(DateTime d, TimeSpan t) 加减操作符重载。虽然官方代码比较多,但是如果你有目标,我们就可以选择性裁剪这份代码,照猫画虎还是可以的。同时你看过虎的,就不会写出“反类犬”的代码了