电脑是怎么算加减法的

电脑是怎么算加减法的,第1张

在电脑内部,所有的运算都是加法。

首先,计算机把数转换成二进制(全是0、1代码,转换都是按现成的程序进行),

如果计算加法,则把两数按位相加。

如果是计算减法,则把减数取补码(就是按位把0换成1,取后八位为 00000010 ,13=00001101 ,取13的补码为 11110010 ,加1 后为 11110011 ,相加有 00001111+11110011=100000010 ,转换都是按现成的程序进行)

二进制是计算技术中广泛采用的一种 数制。 二进制数据是用0和1两个 数码来表示的数。它的基数为2,进位规则是“逢二进一”,借位规则是“借一当二”,由18世纪德国数理哲学大师 莱布尼兹发现。

当前的 计算机系统使用的基本上是 二进制系统,数据在 计算机中主要是以补码的形式存储的。计算机中的二进制则是一个非常微小的开关,用“开”来表示1,“关”来表示0。

static void Main(string[] args)

{

string str = "",math="";

while (true)

{

bool IsOk = true;

ConsoleWriteLine("请输入两个正整数(以空格分开)");

str = ConsoleReadLine();

if (str == "#")

break;

ConsoleWriteLine("请输入一个运算符(+,-,,/)");

math = ConsoleReadLine();

string[] array = strSplit(' ');

int[] a = new int[arrayLength];

for (int i = 0; i < arrayLength; i++)

{

try

{

a[i] = ConvertToInt32(array[i]);

}

catch

{

ConsoleWriteLine("输入的运算数不对");

IsOk = false;

break;

}

}

if (IsOk ==true)

{

int i=0;

switch(math)

{

case "+":

ConsoleWriteLine(a[i] + a[i++]);

break;

case "-":

ConsoleWriteLine(a[i]-a[i++]);

break;

case "":

ConsoleWriteLine(a[i] a[i++]);

break;

case "/":

ConsoleWriteLine(a[i]/a[i++]);

break;

default:

ConsoleWriteLine("输入的运算符不正确");

break;

}

}

else

{

continue;

}

}

}

我这只是一个大概思路,像减法,触发都可能会出现异常,你处理一下就好了,这里就省了

#include <stdioh>

#include <stdlibh>

#include <timeh>

main()

{

int a,b,c,d,result,i;

char f[]={'+','-','','/'};

while(1)

{

srand(time(NULL));

a=rand()%100;

b=rand()%100;

c=rand()%4;

switch (c)

{

case 0: d=a+b;break;

case 1: d=a-b;break;

case 2: d=ab;break;

case 3: d=a/b;break;

}

printf("输入结果:%d%c%d=",a,f[c],b);

i=0;

while (i<2)

{

scanf("%d",&result);

if (result==d&&i<2)

{printf("你答对了^_^\n"); break; }

else i++,printf("你答错了%d次:",i);

if (i==2) {printf("\n答案:%d\n",d);}

}

}

}

#include <stdioh>

#include <stdlibh>

#include <ctypeh>

#define MAXOP 100

#define NUMBER '0'

int getop (char [] );

void push (double);

double pop(void);

int getch(void);

void ungetch(int);

int getop(char s[])

{

int i,c;

while ((s[0]=c=getch())==' '|| c=='\t');

s[1]='\0';

if(!isdigit(c)&&c!='')

return c;

i=0;

if(isdigit(c))

while (isdigit(s[++i]=c=getch()));

if (c=='')

while (isdigit(s[++i]=c=getch()));

s[i]='\0';

if (c!=EOF)

ungetch(c);

return NUMBER;

}

#define BUFSIZE 100

char buf[BUFSIZE];

int bufp=0;

int getch(void)

using System;\x0d\using SystemDrawing;\x0d\using SystemCollections;\x0d\using SystemComponentModel;\x0d\using SystemWindowsForms;\x0d\using SystemData;\x0d\\x0d\namespace jisuan\x0d\{\x0d\ /// \x0d\ /// Form1 的摘要说明。\x0d\ /// \x0d\ public class Form1 : SystemWindowsFormsForm\x0d\ {\x0d\ private SystemWindowsFormsTextBox textBox1;\x0d\ private SystemWindowsFormsTextBox textBox2;\x0d\ private SystemWindowsFormsTextBox textBox3;\x0d\ private SystemWindowsFormsComboBox comboBox1;\x0d\ private SystemWindowsFormsButton button1;\x0d\ private SystemWindowsFormsButton button2;\x0d\ private SystemWindowsFormsButton button3;\x0d\ /// \x0d\ /// 必需的设计器变量。\x0d\ /// \x0d\ private SystemComponentModelContainer components = null;\x0d\\x0d\ public Form1()\x0d\ {\x0d\ //\x0d\ // Windows 窗体设计器支持所必需的\x0d\ //\x0d\ InitializeComponent();\x0d\\x0d\ //\x0d\ // TODO: 在 InitializeComponent 调用后添加任何构造函数代码\x0d\ //\x0d\ }\x0d\\x0d\ /// \x0d\ /// 清理所有正在使用的资源。\x0d\ /// \x0d\ protected override void Dispose( bool disposing )\x0d\ {\x0d\ if( disposing )\x0d\ {\x0d\ if (components != null) \x0d\ {\x0d\ componentsDispose();\x0d\ }\x0d\ }\x0d\ baseDispose( disposing );\x0d\ }\x0d\\x0d\ #region Windows 窗体设计器生成的代码\x0d\ /// \x0d\ /// 设计器支持所需的方法 - 不要使用代码编辑器修改\x0d\ /// 此方法的内容。\x0d\ /// \x0d\ private void InitializeComponent()\x0d\ {\x0d\ thistextBox1 = new SystemWindowsFormsTextBox();\x0d\ thistextBox2 = new SystemWindowsFormsTextBox();\x0d\ thistextBox3 = new SystemWindowsFormsTextBox();\x0d\ thiscomboBox1 = new SystemWindowsFormsComboBox();\x0d\ thisbutton1 = new SystemWindowsFormsButton();\x0d\ thisbutton2 = new SystemWindowsFormsButton();\x0d\ thisbutton3 = new SystemWindowsFormsButton();\x0d\ thisSuspendLayout();\x0d\ // \x0d\ // textBox1\x0d\ // \x0d\ thistextBox1Location = new SystemDrawingPoint(24, 72);\x0d\ thistextBox1Name = "textBox1";\x0d\ thistextBox1Size = new SystemDrawingSize(100, 21);\x0d\ thistextBox1TabIndex = 0;\x0d\ thistextBox1TextChanged += new SystemEventHandler(thistextBox1_TextChanged);\x0d\ // \x0d\ // textBox2\x0d\ // \x0d\ thistextBox2Location = new SystemDrawingPoint(312, 72);\x0d\ thistextBox2Name = "textBox2";\x0d\ thistextBox2Size = new SystemDrawingSize(100, 21);\x0d\ thistextBox2TabIndex = 1;\x0d\ // \x0d\ // textBox3\x0d\ // \x0d\ thistextBox3Location = new SystemDrawingPoint(448, 72);\x0d\ thistextBox3Name = "textBox3";\x0d\ thistextBox3Size = new SystemDrawingSize(88, 21);\x0d\ thistextBox3TabIndex = 2;\x0d\ // \x0d\ // comboBox1\x0d\ // \x0d\ thiscomboBox1ItemsAddRange(new object[] {\x0d\ "+",\x0d\ "-",\x0d\ "",\x0d\ "/"});\x0d\ thiscomboBox1Location = new SystemDrawingPoint(152, 72);\x0d\ thiscomboBox1Name = "comboBox1";\x0d\ thiscomboBox1Size = new SystemDrawingSize(121, 20);\x0d\ thiscomboBox1TabIndex = 3;\x0d\ thiscomboBox1SelectedIndexChanged += new SystemEventHandler(thiscomboBox1_SelectedIndexChanged);\x0d\ // \x0d\ // button1\x0d\ // \x0d\ thisbutton1Location = new SystemDrawingPoint(64, 184);\x0d\ thisbutton1Name = "button1";\x0d\ thisbutton1Size = new SystemDrawingSize(104, 32);\x0d\ thisbutton1TabIndex = 4;\x0d\ thisbutton1Text = "计算";\x0d\ thisbutton1Click += new SystemEventHandler(thisbutton1_Click);\x0d\ // \x0d\ // button2\x0d\ // \x0d\ thisbutton2Location = new SystemDrawingPoint(216, 192);\x0d\ thisbutton2Name = "button2";\x0d\ thisbutton2Size = new SystemDrawingSize(75, 23);\x0d\ thisbutton2TabIndex = 5;\x0d\ thisbutton2Text = "清除";\x0d\ thisbutton2Click += new SystemEventHandler(thisbutton2_Click);\x0d\ // \x0d\ // button3\x0d\ // \x0d\ thisbutton3Location = new SystemDrawingPoint(376, 192);\x0d\ thisbutton3Name = "button3";\x0d\ thisbutton3Size = new SystemDrawingSize(75, 23);\x0d\ thisbutton3TabIndex = 6;\x0d\ thisbutton3Text = "退出";\x0d\ thisbutton3Click += new SystemEventHandler(thisbutton3_Click);\x0d\ // \x0d\ // Form1\x0d\ // \x0d\ thisAutoScaleBaseSize = new SystemDrawingSize(6, 14);\x0d\ thisClientSize = new SystemDrawingSize(656, 366);\x0d\ thisControlsAdd(thisbutton3);\x0d\ thisControlsAdd(thisbutton2);\x0d\ thisControlsAdd(thisbutton1);\x0d\ thisControlsAdd(thiscomboBox1);\x0d\ thisControlsAdd(thistextBox3);\x0d\ thisControlsAdd(thistextBox2);\x0d\ thisControlsAdd(thistextBox1);\x0d\ thisName = "Form1";\x0d\ thisText = "Form1";\x0d\ thisResumeLayout(false);\x0d\ thisPerformLayout();\x0d\\x0d\ }\x0d\ #endregion\x0d\\x0d\ /// \x0d\ /// 应用程序的主入口点。\x0d\ /// \x0d\ [STAThread]\x0d\ static void Main() \x0d\ {\x0d\ ApplicationRun(new Form1());\x0d\ }\x0d\ public double jia(double a,double b)\x0d\ {\x0d\ return a+b;\x0d\ }\x0d\ public double jian(double a,double b)\x0d\ {\x0d\ return a-b;\x0d\ }\x0d\ public double cheng(double a,double b)\x0d\ {\x0d\ return ab;\x0d\ }\x0d\ public double chu(double a,double b)\x0d\ {\x0d\ return a/b;\x0d\ }\x0d\\x0d\private void comboBox1_SelectedIndexChanged(object sender, SystemEventArgs e)\x0d\ {\x0d\\x0d\ }\x0d\\x0d\ private void textBox1_TextChanged(object sender, SystemEventArgs e)\x0d\ {\x0d\\x0d\ }\x0d\\x0d\ private void button1_Click(object sender, SystemEventArgs e)\x0d\ {\x0d\ string i=thiscomboBox1SelectedItemToString();\x0d\\x0d\ switch(i)\x0d\ {\x0d\ \x0d\ case "+":thistextBox3Text=thisjia(doubleParse(thistextBox1Text),doubleParse(thistextBox2Text))ToString();\x0d\ \x0d\ break;\x0d\\x0d\ case "-":thistextBox3Text=thisjian(doubleParse(thistextBox1Text),doubleParse(thistextBox2Text))ToString();\x0d\ break;\x0d\ case "":thistextBox3Text=thischeng(doubleParse(thistextBox1Text),doubleParse(thistextBox2Text))ToString();\x0d\ break;\x0d\ case"/" :thistextBox3Text=thischu(doubleParse(thistextBox1Text),doubleParse(thistextBox2Text))ToString();\x0d\ break;\x0d\ }\x0d\\x0d\ }\x0d\\x0d\ private void button2_Click(object sender, SystemEventArgs e)\x0d\ {\x0d\ thistextBox1Text=null;\x0d\ thistextBox2Text=null;\x0d\ thistextBox3Text = null;\x0d\ }\x0d\\x0d\ private void button3_Click(object sender, EventArgs e)\x0d\ {\x0d\ \x0d\ //thisHide();\x0d\ ApplicationExit();\x0d\ //thisClose();\x0d\ \x0d\ }\x0d\ }\x0d\}

以上就是关于电脑是怎么算加减法的全部的内容,包括:电脑是怎么算加减法的、编写一个进行加减乘除四则运算的程序,要求输入2个数,然后输入个运算符,输出两个数运算的结果,控制台应、用C语言编写一个简单加减乘除的程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/10128192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存