二进制数的算术运算的基本规律和十进制数的运算十分相似。最常用的是加法运算和乘法运算。
1
二进制加法
有四种情况:
0+0=0
0+1=1
1+0=1
1+1=0
进位为1
例1103求
(1101)2+(1011)2
的和
解:
1
1
0
1
+
1
0
1
1
1
1
0
0
2
二进制乘法
有四种情况:
0×0=0
1×0=0
0×1=0
1×1=1
例1104求
(1110)2
乘(101)2
之积
解:
1
1
1
0
×
1
0
1
1
1
1
0
0
0
0
0
+
1
1
1
0
1
0
0
0
1
1
0
你也可以先把二进制的数换算成10进制再相加,得出结果再换算成二进制。
#include <stdioh>
#include <stdlibh>
#include <timeh>
int add(int num1,int num2)//加法
{
return num1+num2;
}
int subtraction(int num1,int num2)//减法
{
return num1-num2;
}
int mul(int num1,int num2)//乘法
{
return num1num2;
}
float divis(int num1,int num2)//除法
{
return ((int)(((float)num1/num2)100+05))/1000;
}
int main()
{
int type = -1;
int data = -1;
int choice,num1,num2,results;
float div_result;
int right = 0;
int wrong = 0;
char ch;
printf("欢迎进入四则运算题,现在开始爆发你的小宇宙吧!\n");
srand((unsigned int)time(NULL));
while(1)
{
printf("请选择运算类型:1加法 2减法 3乘法 4除法(保留两位小数)\n");
scanf("%d",&choice);
printf("请选择位数:1一位数 2两位数\n");
scanf("%d",&data);
switch (choice)
{
case 1:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10;
printf("%d + %d=____\n",num1,num2);
scanf("%d",&results);
if(results == add(num1,num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100;
printf("%d + %d=____\n",num1,num2);
scanf("%d",&results);
if(results == add(num1,num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
case 2:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10;
printf("%d - %d=____\n",num1,num2);
scanf("%d",&results);
if(results == subtraction(num1,num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100;
printf("%d - %d=____\n",num1,num2);
scanf("%d",&results);
if(results == subtraction(num1,num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
case 3:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10;
printf("%d %d=____\n",num1,num2);
scanf("%d",&results);
if(results == mul(num1,num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100;
printf("%d %d=____\n",num1,num2);
scanf("%d",&results);
if(results == mul(num1,num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
case 4:
if(data == 1)
{
num1 = rand()%10;
num2 = rand()%10+1;
printf("%d / %d=____\n",num1,num2);
scanf("%f",&div_result);
if(div_result == divis(num1,num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
else if (data == 2)
{
num1 = rand()%100;
num2 = rand()%100+1;
printf("%d / %d=____\n",num1,num2);
scanf("%f",&div_result);
if(div_result == divis(num1,num2))
{
printf("回答正确!\n");
right++;
}
else
{
printf("回答错误!\n");
wrong++;
}
printf("是否继续?y/n\n");
getchar();
scanf("%c",&ch);
if(ch == 'y')
break;
else if(ch == 'n')
goto end;
}
break;
default :
printf("输入错误!\n");
break;
}
}
end:
printf("回答正确%d题,回答错误%d题\n",right,wrong);
printf("\n====================谢谢光临====================!\n");
system("pause");
return 0;
}
//注意除法是采用四舍五入的,有什么不明白的再问吧,望采纳!
#include<iostream>
#include<ctime>
#include<cstring>
using namespace std;
void main()
{
char ch='/';//+ - / 43,45,42,47
int temp1,temp2,answer;
cout<<"input your answer,quit by -1"<<endl;
srand((unsigned int)time(NULL));
do{
int temp=rand()%8;
if(temp==2||temp==3||temp==5||temp==7)
{
temp1=rand()%10;
temp2=rand()%10;
cout<<temp1<<char(temp+40)<<temp2<<"=";
cin>>answer;
if(answer==-1)break;
else{
switch(char(temp+40))
{
case '+':if(answer==(temp1+temp2))cout<<"right,next title:"<<endl;else cout<<"wrong!"<<endl;break;
case '-':if(answer==(temp1-temp2))cout<<"right,next title:"<<endl;else cout<<"wrong!"<<endl;break;
case '':if(answer==(temp1temp2))cout<<"right,next title:"<<endl;else cout<<"wrong!"<<endl;break;
case '/':if(answer==(temp1/temp2))cout<<"right,next title:"<<endl;else cout<<"wrong!"<<endl;break;
}
}
}
}while(1);
}
以上就是关于用C++编写一个8进制的加减乘除运算程序全部的内容,包括:用C++编写一个8进制的加减乘除运算程序、用C++写一个循环的四则运算程序并且包含子函数、c++四则运算的程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)