原创

不使用加减乘除+-*/,实现减法运算

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/dengheCSDN/article/details/77816320

  上篇文章中,解释了不使用加减乘除+-*/,实现加法运算,那么如何实现减法呢?

  方法一、将被减数取反,利用加法运算。

先看程序:

int add(int a, int b)
{
	if (a == 0)
		return b;
	if (b == 0)
		return a;
	int p1 = a&b;//位与。
	p1 = p1 << 1;//这两句只考虑进位 
	int p2 = a^b;//位异或。不考虑进位 
	return add(p2, p1);  
}

int nega_num(int n)
{
	return add(~n, 1);  // 取反
}

int main()
{
	int a, b;
	cin >> a >> b;
	cout << add(a, nega_num(b)) << endl;
}

  说明:对一个整数取负,等于将这个整数按位取反再加 1 。

  方法二、利用位操作,实现减法

步骤如下:

1、首先,如果减数为0,则被减数即为减法的结果,运算结束。
但如果减数不为0,我们可以先把被减数和减数上同为1的位从两个数上去除。至于如何分离出值同为1的位,则可以通过求位与操作来做到;而把这些1分别中被减数和减数中去除,则可以通过按位异或来的操作来实现。
2、经步骤1处理后,被减数和减数在对应的位上,将或者通为0,或者分别为0和1,却不会同为1。此时:
如果对应位被减数=1,而减数=0,则所得结果对应位也为1;
如果对应位被减数=0,而减数=1,则所得结果对应位还是1,但此时须向前一位借1;
即,通过被减数与减数作位异或的操作得到临时结果,和通过对减数左移一位得到需从临时结果中减去的借数。
3、于是,经过步骤2后,原来的减法变成了要求:临时结果 - 借数
很明显,只要以临时结果为被减数,借数为减数,重复步骤1~3即可。

代码:

int Subtract(int a, int b)
{
	while (b != 0)
	{
		int p = a & b;
		a ^= p;
		b ^= p;
		a |= b;
		b <<= 1;
	}

	return a;
}

int main()
{
	int a, b;
	cin >> a >> b;
	cout << Subtract(a,b);
}



展开阅读全文

怎样实现加减乘除的混合运算?

07-25

我编写了一个计算器程序,但不知怎样实现加减乘除的混合运算,请各位高手指点.下面是我的源程序,我想请问一下,如果我想把m:=1;这句加在procedure外面,不知要放在哪里?rnunit Unit1;rnrninterfacernrnusesrn Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,rn Dialogs, StdCtrls;rnrntypern TForm1 = class(TForm)rn Button1: TButton;rn Button2: TButton;rn Button3: TButton;rn Button4: TButton;rn Button5: TButton;rn Button6: TButton;rn Button7: TButton;rn Button8: TButton;rn Button9: TButton;rn Button10: TButton;rn Button11: TButton;rn Button12: TButton;rn Button13: TButton;rn Button14: TButton;rn Button15: TButton;rn Button16: TButton;rn Button17: TButton;rn Button18: TButton;rn Edit1: TEdit;rn procedure Button1Click(Sender: TObject);rn procedure Button2Click(Sender: TObject);rn procedure Button3Click(Sender: TObject);rn procedure Button4Click(Sender: TObject);rn procedure Button5Click(Sender: TObject);rn procedure Button6Click(Sender: TObject);rn procedure Button7Click(Sender: TObject);rn procedure Button8Click(Sender: TObject);rn procedure Button9Click(Sender: TObject);rn procedure Button10Click(Sender: TObject);rn procedure Button11Click(Sender: TObject);rn procedure Button12Click(Sender: TObject);rn procedure Button13Click(Sender: TObject);rn procedure Button14Click(Sender: TObject);rn procedure Button15Click(Sender: TObject);rn procedure Button16Click(Sender: TObject);rn procedure Button17Click(Sender: TObject);rn procedure Button18Click(Sender: TObject);rn privatern Private declarations rn publicrn Public declarations rn end;rnrnvarrn Form1: TForm1;rn x,y,z,m: real;rn flag: char;rnrnimplementationrnrn$R *.dfmrnrnrnrnprocedure TForm1.Button1Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='1'rnelsernedit1.Text:=edit1.Text+'1';rnend;rnrnprocedure TForm1.Button2Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='2' elsernedit1.Text:=edit1.Text+'2';rnend;rnrnprocedure TForm1.Button3Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='3' elsernedit1.Text:=edit1.Text+'3';rnend;rnrnprocedure TForm1.Button4Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='4' elsernedit1.Text:=edit1.Text+'4';rnend;rnrnprocedure TForm1.Button5Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='5' elsernedit1.Text:=edit1.Text+'5';rnend;rnrnprocedure TForm1.Button6Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='6' elsernedit1.Text:=edit1.Text+'6';rnend;rnrnprocedure TForm1.Button7Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='7' elsernedit1.Text:=edit1.Text+'7';rnend;rnrnprocedure TForm1.Button8Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='8' elsernedit1.Text:=edit1.Text+'8';rnend;rnrnprocedure TForm1.Button9Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='9' elsernedit1.Text:=edit1.Text+'9';rnend;rnrnprocedure TForm1.Button10Click(Sender: TObject);rnbeginrnif edit1.Text='0' then edit1.Text:='0' elsernedit1.Text:=edit1.Text+'0';rnend;rnrnprocedure TForm1.Button11Click(Sender: TObject);rnbeginrnif edit1.Text='' then edit1.Text:=''rnelsern beginrn if pos('.',edit1.Text)<>0rn then edit1.Text:=edit1.Textrn else edit1.Text:=edit1.Text+'.';rn end;rnend;rnrnprocedure TForm1.Button12Click(Sender: TObject);rnbeginrnedit1.Clear;rnend;rnrnprocedure TForm1.Button13Click(Sender: TObject);rnbeginrnif edit1.Text='' then edit1.Text:=''rnelse beginrnrnflag:='+';rnx:=strtofloat(edit1.Text);rnz:=x+z;rnrnedit1.Clear;rnend;rnend;rnrnprocedure TForm1.Button14Click(Sender: TObject);rnbeginrnif edit1.Text='' then edit1.Text:=''rnelse beginrnflag:='-';rnx:=strtofloat(edit1.Text);rnz:=x-z;rnedit1.Clear;rnend;rnend;rnrnprocedure TForm1.Button15Click(Sender: TObject);rnbeginrnif edit1.Text='' then edit1.Text:=''rnelse beginrnflag:='*';rnx:=strtofloat(edit1.Text);rnz:=x*m;rnedit1.Clear;rnend;rnend;rnrnprocedure TForm1.Button16Click(Sender: TObject);rnbeginrnif edit1.Text='' then edit1.Text:=''rnelse beginrnflag:='/';rnx:=strtofloat(edit1.Text);rnz:=x/m;rnedit1.Clear;rnend;rnend;rnrnprocedure TForm1.Button17Click(Sender: TObject);rnbeginrnif edit1.Text='' then edit1.Text:=''rnelserncase flag ofrn '+': beginrn edit1.text:=floattostr(z+strtofloat(edit1.Text));rn z:=0;rn end;rn '-':beginrn edit1.text:=floattostr(z-strtofloat(edit1.Text));rn z:=0;rn end;rn '*': beginrn edit1.text:=floattostr(m*strtofloat(edit1.Text));rn m:=1;rn end;rn '/': beginrn y:=strtofloat(edit1.Text);rn if y=0 then edit1.Text:='99999999999999999999999999999999'rn else beginrn edit1.text:=floattostr(m/strtofloat(edit1.Text));rn m:=1;rn end;rn end;rnend;rnrnend;rnprocedure TForm1.Button18Click(Sender: TObject);rnbeginrnclose;rnend;rnrnend. 论坛

没有更多推荐了,返回首页

博客模板©2019 由CSDN提供 模版主题为:skin-yellow by CSDN官方博客 设计