最近开始接触.NET,有些看似简单的问题,做起来挺费劲的,以前也没有开发的经验。有个小问题再次请教下朋友们:HTML页面代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      A:<asp:TextBox ID="txtA" runat="server"></asp:TextBox>
    <br />
      B:<asp:TextBox ID="txtB" runat="server"></asp:TextBox>
    <br />
      C:<asp:TextBox ID="txtC" runat="server"></asp:TextBox>
    <br />
      Portion:<asp:TextBox ID="txtPortion" runat="server"></asp:TextBox>
      <asp:Button ID="btnPortion" runat="server" Text="点击根据A,B,C的值自动生成比例值" OnClick="btnPortion_OnClick"/>
    <br />
      Total:<asp:TextBox ID="txtTotal" runat="server" Width="165px"></asp:TextBox>
      <asp:Button ID="btnTotal" runat="server" Text="点击根据A,B,C的值求和" OnClick="btnTotal_OnClick"/>
    </div>
    </form>
</body>
</html>比例值,比如说A是40,B是60,C是20,则txtPortion显示的值就是: 2:3:1
                                     txtTotal计算的和就是:120
请问,这样的小需求,该如何写CS代码?

解决方案 »

  1.   

    比例值,我的想法是先求出A,B,C的最大公约数,然后用A,B,C分别去除这个最大公约数,三个值串起来就OK了,主要是代码这块怎么写,如下是我针对A,B的测试代码,是OK的,三个的不知道如何变换:
    using System;
    using System.Collections; 
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;namespace CocisWeb.DevelopHelp
    {
        public partial class TestAppPortion : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                }
            }        public void btnPortion_OnClick(object sender,EventArgs e)
            {
                TestAppPortion m = new TestAppPortion();
                int n1 = Convert.ToInt32(txtA.Text);
                int n2 = Convert.ToInt32(txtB.Text);
                this.txtPortion.Text = m.maxGongYueShu(n1, n2).ToString();
            }        public float maxGongYueShu(int n1, int n2)
            {
                int temp = Math.Max(n1, n2);
                n2 = Math.Min(n1, n2);//n2中存放两个数中最小的
                n1 = temp;//n1中存放两个数中最大的
                while (n2 != 0)
                {
                    n1 = n1 > n2 ? n1 : n2;//使n1中的数大于n2中的数
                    int m = n1 % n2;
                    n1 = n2;
                    n2 = m;
                }
                return n1;
            }        public void Main(string[] args)
            {
                int n1 = Convert.ToInt32(txtA.Text);
                int n2 = Convert.ToInt32(txtB.Text);
                if (n1 * n2 != 0)
                {
                    TestAppPortion m = new TestAppPortion();
                    Console.WriteLine(m.maxGongYueShu(n1, n2));
                }
                else
                {
                    Console.WriteLine("这两个数不能为0");
                }
            }
      }
    }
      

  2.   

    求最大公约数,然后每个数除最大公约数就可以,最大公约数的计算方法参看这个网页
    http://baike.baidu.com/view/47637.htm#4
      

  3.   

    求和代码,验证OK:
    public void btnTotal_OnClick(object sender,EventArgs e)
            {
                int sum=0;
                int S1 = Convert.ToInt32(this.txtA.Text);
                int S2 = Convert.ToInt32(this.txtB.Text);
                int S3 = Convert.ToInt32(this.txtC.Text);
                sum= S1 + S2 + S3;
                this.txtTotal.Text = sum.ToString();
            }
      

  4.   

    A,B,C求和,搞定了。
    计算A,B的比例值,也搞定了,若加上C,改怎么变换呢?
    using System;
    using System.Collections; 
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;namespace CocisWeb.DevelopHelp
    {
        public partial class TestAppPortion : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                }
            }        public void btnPortion_OnClick(object sender,EventArgs e)
            {
                TestAppPortion m = new TestAppPortion();
                int n1 = Convert.ToInt32(txtA.Text);
                int n2 = Convert.ToInt32(txtB.Text);
                this.txtPortion.Text = (n1 / m.maxGongYueShu(n1, n2)).ToString() + ":" + (n2 / m.maxGongYueShu(n1, n2)).ToString();
            }        public float maxGongYueShu(int n1, int n2)
            {
                int temp = Math.Max(n1, n2);
                n2 = Math.Min(n1, n2);//n2中存放两个数中最小的
                n1 = temp;//n1中存放两个数中最大的
                while (n2 != 0)
                {
                    n1 = n1 > n2 ? n1 : n2;//使n1中的数大于n2中的数
                    int m = n1 % n2;
                    n1 = n2;
                    n2 = m;
                }
                return n1;
            }        public void Main(string[] args)
            {
                int n1 = Convert.ToInt32(txtA.Text);
                int n2 = Convert.ToInt32(txtB.Text);
                if (n1 * n2 != 0)
                {
                    TestAppPortion m = new TestAppPortion();
                    Console.WriteLine(m.maxGongYueShu(n1, n2));
                }
                else
                {
                    Console.WriteLine("这两个数不能为0");
                }
            }        public void btnTotal_OnClick(object sender,EventArgs e)
            {
                int sum=0;
                int S1 = Convert.ToInt32(this.txtA.Text);
                int S2 = Convert.ToInt32(this.txtB.Text);
                int S3 = Convert.ToInt32(this.txtC.Text);
                sum= S1 + S2 + S3;
                this.txtTotal.Text = sum.ToString();
            }    }
    }
      

  5.   

    private void button1_Click(object sender, EventArgs e)
            {
                Int32 i1 = Convert.ToInt32(textBox1.Text);
                Int32 i2 = Convert.ToInt32(textBox2.Text);
                Int32 i3 = Convert.ToInt32(textBox3.Text);            Int32 min = Math.Min(i1, i2);
                min = Math.Min(min, i3);            for (int i = min-1; i >0 ; i--)
                {
                    if (
                        (i1 % i == 0) &&
                        (i2 % i == 0) &&
                        (i3 % i == 0) 
                        )
                    {
                        i1 = i1 / i;
                        i2 = i2 / i;
                        i3 = i3 / i;
                        textBox4.Text = i1.ToString() + "/" + i2.ToString() + "/" + i3.ToString();
                        return;
                    }
                }
            }
      

  6.   

    9楼的OK,若将比例值化简,则只需变换这里即可:
    for (int i = min; i >0 ; i--)感谢各位的热心参与,此需求成功实现!