根据3个数据生成比例值,并求和

go2oracle 2011-04-26 12:18:21
最近开始接触.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代码?
...全文
158 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cjh200102 2011-04-26
  • 打赏
  • 举报
回复
按最小的数为基准就行。

如果是100%为比例分配,则求和并计算每份的值,再去计算所占的比例
go2oracle 2011-04-26
  • 打赏
  • 举报
回复
呵呵,代码都还没写呢
go2oracle 2011-04-26
  • 打赏
  • 举报
回复
9楼的OK,若将比例值化简,则只需变换这里即可:
for (int i = min; i >0 ; i--)

感谢各位的热心参与,此需求成功实现!
vrhero 2011-04-26
  • 打赏
  • 举报
回复
求最大公因数,欧几里得算法...唉,现在的孩子小学都怎么学的...
maxfara 2011-04-26
  • 打赏
  • 举报
回复
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;
}
}
}
go2oracle 2011-04-26
  • 打赏
  • 举报
回复
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();
}

}
}
go2oracle 2011-04-26
  • 打赏
  • 举报
回复
求和代码,验证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();
}
余山水 2011-04-26
  • 打赏
  • 举报
回复
求最大公约数,然后每个数除最大公约数就可以,最大公约数的计算方法参看这个网页
http://baike.baidu.com/view/47637.htm#4
go2oracle 2011-04-26
  • 打赏
  • 举报
回复
比例值,我的想法是先求出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");
}
}
}
}

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧