四则运算
Posted guoquancai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算相关的知识,希望对你有一定的参考价值。
include
include <string.h>
define size 10
using namespace std;
//含有一个二元算数式的三个值
typedef struct{
float x = 0,y = 0; //x,y为给定的随机数
float r = 0; //r为算数式结果
}Arithmetic;
Arithmetic choose(Arithmetic n);//选择函数,根据用户选择 识别将要进行的四则运算类型
float conversion(string input);//用户输入转换
bool judge(Arithmetic n);//结果判断
Arithmetic Addition();//加法运算
Arithmetic Subtraction();//减法运算
Arithmetic Multiplication();//乘法运算
Arithmetic Division();//除法运算
int main(int argc, const char * argv[]) {
int score = 0, cnt = 0;
// printf("请设置题目数量
");
// scanf("%d",&cnt);
if (string(argv[1])=="-n") { //在terminals或者command中运行的时候,参数顺序存放在argv数组中
string c = argv[2];
cnt = stoi(c);
}
for (int i = 0; i<cnt; i++) {
Arithmetic t;
t = choose(t);
if (judge(t) == true) {
score+=10;
cout<<"恭喜您回答正确!
";
} else {
cout<<"很抱歉您的答案不正确,再想想哦~
";
}
}
printf("满分为:%d 您的得分为:%d分
",cnt*10,score);
return 0;
}
//函数
//选择函数,根据用户选择识别将要进行的四则运算类型
Arithmetic choose(Arithmetic n){
int choose = 0;
cout<<"请选择您需要进行的四则运算类型
"<<"1.加法 2.减法 3.乘法 4.除法
";
cin>>choose;
switch (choose) {
case 1:
n = Addition();
break;
case 2:
n = Subtraction();
break;
case 3:
n = Multiplication();
break;
case 4:
n = Division();
break;
default:
break;
}
return n;
}
//转译用户输入结果
float conversion(string input){
bool flag = false, sign = false;
//flag标记用户输入中是否存在/号,有/号则true否则false
//sign标记用户输入是否为负数,是负数则true否则false
float a = 0, b = 0, result = 0;
for (int i = 0; i<input.length(); i++) {
if (input[i] != ‘/‘ && input[i] != ‘-‘) {
int num = input[i]-‘0‘; //将char型转换为int型
if (!flag) { //若果flag为false,则说明还未遇到/号
a *= 10; //a记录/号前的数值
a += num;
} else {
b *= 10; //b记录/号后的数值
b += num;
}
} else if (input[i] == ‘/‘){
flag = true;
} else {
sign = true;
}
}
if (flag && sign) { //既有/号,又是负值
result = -a/b;
} else if (flag && !sign){ //有/号,但为正值
result = a/b;
} else if (!flag && sign){ //没有有/号,但为负值
result = -a;
} else { //没有有/号,且为正值
result = a;
}
return result;
}
//判断对错函数
bool judge(Arithmetic n){
bool flag = false; //flag用于记录用户输入答案是否正确,正确return true,否则return false
string input;
cin>>input;
if (n.r == conversion(input))
flag = true;
return flag;
}
//Addition加法
Arithmetic Addition(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为0则进行整数四则运算,若为1则进行真分数的四则运算
n.x = rand()%size+1;
n.y = rand()%size+1;
n.r = n.x + n.y;
printf("%.0f + %.0f = ?
",n.x,n.y);
} else {
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) { //需要保证数值是真分数
n.x = a/b;
} else if (a==b){ //分子分母如果相同的话直接赋值为1
n.x = 1;
}else { //需要保证数值是真分数
n.x = b/a;
}
if (c<d) { //需要保证数值是真分数
n.y = c/d;
} else if (c==d){ //分子分母如果相同的话直接赋值为1
n.y = 1;
}else { //需要保证数值是真分数
n.y = d/c;
}
n.r = n.x + n.y;
if (n.r==0) { //结果为0说明两个真分数都是1/1
printf("%.0f + %.0f = ?
",n.x,n.y);
} else if (n.x==1){ //仅有一个真分数,一个1/1
printf("%.0f + %.0f/%.0f = ?
",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f + %.0f = ?
",a<b?a:b,a>b?a:b,n.y);
} else { //除去其他可能,仅剩下两个数都是真分数的情况
printf("%.0f/%.0f + %.0f/%.0f = ?
",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
}
return n;
}
//Subtraction减法
Arithmetic Subtraction(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x = rand()%size+1;
n.y = rand()%size+1;
n.r = n.x - n.y;
printf("%.0f - %.0f = ?
",n.x,n.y);
} else {
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) {
n.x = a/b;
} else if (a==b){
n.x = 1;
}else {
n.x = b/a;
}
if (c<d) {
n.y = c/d;
} else if (c==d){
n.y = 1;
}else {
n.y = d/c;
}
n.r = n.x - n.y;
if (n.r==0) {
printf("%.0f - %.0f = ?
",n.x,n.y);
} else if (n.x==1){
printf("%.0f - %.0f/%.0f = ?
",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f - %.0f = ?
",a<b?a:b,a>b?a:b,n.y);
} else {
printf("%.0f/%.0f - %.0f/%.0f = ?
",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
}
return n;
}
//Multiplication乘法
Arithmetic Multiplication(){
Arithmetic n;
srand((unsigned)time(NULL));
if (rand()%2 == 0) {//若为偶数则进行整数四则运算,若为奇数则进行真分数的四则运算
n.x= rand()%size+1;
n.y= rand()%size+1;
n.r = n.x * n.y;
printf("%.0f * %.0f = ?
",n.x,n.y);
} else {
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) {
n.x = a/b;
} else if (a==b){
n.x = 1;
}else {
n.x = b/a;
}
if (c<d) {
n.y = c/d;
} else if (c==d){
n.y = 1;
}else {
n.y = d/c;
}
n.r = n.x * n.y;
if (n.r==0) {
printf("%.0f * %.0f = ?
",n.x,n.y);
} else if (n.x==1){
printf("%.0f * %.0f/%.0f = ?
",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f * %.0f = ?
",a<b?a:b,a>b?a:b,n.y);
} else {
printf("%.0f/%.0f * %.0f/%.0f = ?
",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
}
return n;
}
//Division除法
Arithmetic Division(){ //除法不需要考虑两个整数的,没有意义,直接两个真分数的除法
Arithmetic n;
srand((unsigned)time(NULL));
float a,b,c,d;
a = rand()%size+1;
b = rand()%size+1;
c = rand()%size+1;
d = rand()%size+1;
if (a<b) {
n.x = a/b;
} else if (a==b){
n.x = 1;
}else {
n.x = b/a;
}
if (c<d) {
n.y = c/d;
} else if (c==d){
n.y = 1;
}else {
n.y = d/c;
}
n.r = n.x / n.y;
if (n.r==0) {
printf("%.0f / %.0f = ?
",n.x,n.y);
} else if (n.x==1){
printf("%.0f / %.0f/%.0f = ?
",n.x,c<d?c:d,c>d?c:d);
} else if (n.y==1){
printf("%.0f/%.0f / %.0f = ?
",a<b?a:b,a>b?a:b,n.y);
} else {
printf("%.0f/%.0f / %.0f/%.0f = ?
",a<b?a:b,a>b?a:b,c<d?c:d,c>d?c:d);
}
return n;
}
以上是关于四则运算的主要内容,如果未能解决你的问题,请参考以下文章
获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算