C#GUI二进制 - 和*。 [英] C# GUI binary - and *.

查看:120
本文介绍了C#GUI二进制 - 和*。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是编程的新手,目前正在自学C#GUI atm。

如何在不使用padleft方法的情况下进行二进制减法和乘法?

我设法做二进制加法部分,并被告知我可以重复使用加法代码进行乘法。



NVM解决。

Hi guys, I'm kinda new to programming, currently self studying C# GUI atm.
How do I do binary subtraction and multiplication without using padleft method?
I managed to do the binary addition part and was told that I can reuse the addition code for multiplication.

NVM solved.

推荐答案

首先,这是你在那里的一些奇怪的代码:

First off, that is some odd code you have there:
for (int i = textBox1.TextLength - 1; i < textBox2.TextLength - 1; i++)
{ num1 = "0" + textBox1.Text; }

那是做什么的?

它与以下相同:

因为循环内的代码不使用循环变量,或更改除 num1 以外的任何其他值,它与没有循环相同 - 它只是加载相同的价值进入 num1 几次...



我可以看到你想要做什么,有点 - 但是假设num1中的每个字符都是零或一个(或者它不是二进制),结果怎么可能是三个?

What does that do?
It's the same as:
Since the code inside the loop doesn't use the loop variable i, or change anythign other than the num1 value, it's the same as not having a loop at all - it just loads teh same value into num1 several times...

I can see what you are trying to do, sort of - but assuming that each character in num1 is zero or one (or it isn't binary) how can the result ever be three?

0 + 0 == 0
0 + 1 == 1
1 + 0 == 1
1 + 1 == 2

因此,主处理循环中的测试是......嗯。 ..odd。



在你尝试乘法之前我会解决这个问题! :笑:



忽略这一点:我忘记了随身携带! :O [/ edit]



所以,内循环会起作用 - 所以乘法很容易:

想想你是怎么教的做乘法:

So the testing in your main processing loop is...um...odd.

I'd sort that out before you try to get to multiplication! :laugh:

[edit]Ignore that: I forgot the carry! :O [/edit]

So, the inner loop will work - so multiplication is pretty easy:
Think how you were taught to do multiplication:

7 * 5 == 7 + 7 + 7 + 7 + 7



然后为更高级的用户有很长的mutiplication:

123

* 45

---

4920
+615

----

5535


Then for more advanced users there is long mutiplication:
123
*45
---
4920
+615
----
5535


这会将你的两个二进制字符串转换成整数,你然后可以减去,并将其转换回二进制字符串



This will convert your two binary strings to integers, you can then subtract, and return it converted back to a binary string

int i = Convert.ToInt32(textBox1.Text, 2);
int j = Convert.ToInt32(textBox2.Text, 2);
Return Convert.ToString(i - j, 2);


这篇关于C#GUI二进制 - 和*。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆