vs c# 中二进制数相加的代码 5
在textbox1和textbox2中分别输入二进制字符串,将它们相加的结果在textbox3中显示怎么代码怎么写啊?...
在textbox1和textbox2中分别输入二进制字符串,将它们相加的结果在textbox3中显示怎么代码怎么写啊?
展开
展开全部
希望你能从中得到你想要的吧! 对你有用的话,加分啊!!
using System;
using System.Collections.Generic;
using System.Text;
namespace 二进制转换
{
class Program
{
static void Main(string[] args)
{
int mark = 119;
int tem = ToErJin(mark);
Console.WriteLine("转成二进制后:"+tem);
int mark2 = 111;
int tem2 = ToShijin(mark2);
Console.WriteLine("转成十进制后:"+tem2);
}
public static int ToErJin(int value)
{
int temp = 0;
int shang=1;
int yushu;
while(shang!=0){
shang = (int)value / 2;
yushu = value % 2;
value = shang;
temp += yushu;
if(shang!=0){
temp = temp * 10;}
}
return temp;
}
public static int ToShijin(int value)
{
int temp = 0;
int shang = value;
int yushu;
int mark = 0;
while (shang != 0)
{
yushu= shang % 10;
shang = (int)shang/ 10;
temp += yushu * (int)Math.Pow(2,mark);
mark++;
}
return temp;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace 二进制转换
{
class Program
{
static void Main(string[] args)
{
int mark = 119;
int tem = ToErJin(mark);
Console.WriteLine("转成二进制后:"+tem);
int mark2 = 111;
int tem2 = ToShijin(mark2);
Console.WriteLine("转成十进制后:"+tem2);
}
public static int ToErJin(int value)
{
int temp = 0;
int shang=1;
int yushu;
while(shang!=0){
shang = (int)value / 2;
yushu = value % 2;
value = shang;
temp += yushu;
if(shang!=0){
temp = temp * 10;}
}
return temp;
}
public static int ToShijin(int value)
{
int temp = 0;
int shang = value;
int yushu;
int mark = 0;
while (shang != 0)
{
yushu= shang % 10;
shang = (int)shang/ 10;
temp += yushu * (int)Math.Pow(2,mark);
mark++;
}
return temp;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询