二柱子四则运算定制版+升级版

题目:在上次程序的基础上,实现对连除的判断,并且实现多个数的混合运算,要求可以输入结果,并进行判断。

思路:基于上次程序,首先思考混合运算,这里运用两个数组,分别记录随机产生的运算数和运算符号。然后对连除进行判断,,检查运算符号数组里是否有连续的除号,有的话加上括号,避免歧义。最后对运算结果进行计算,并对用户输入的结果进行比较,给出相应提示,回答,计算方面,要优先对括号,乘除进行运算,然后再进行加减运算,每次运算完后都要记录下运算的结果。

代码:

//吕广浩 3/19

#include<iostream>
#include<time.h>
using namespace std;
#define N 100
int main()
{
int n = 10;
srand((unsigned)time(NULL));
int num, max, out, d, fushu, kh, yushu;//有无真分数判断,题目数量,最大值,输出方式,是否有乘除运算,加减法是否有负数,有无括号,有无余数
cout << "********二柱子四则运算定制版+升级版********" << endl;
cout << "请输入题目的数量num:" << endl;
cin >> num;
cout << "请选择数值范围最大值(大于0)" << endl;
cin >> max;
cout << "请选择打印方式out(0空行打印 ,1空格打印)" << endl;
cin >> out;
cout << "请选择有无乘除运算d(0没有乘除运算, 1有乘除运算)" << endl;
cin >> d;
cout << "请选择加减运算有无负数fushu(0没有负数 ,1有负数)" << endl;
cin >> fushu;
cout << "请选择有无括号(0没有括号 ,1有括号)" << endl;
cin >> kh;
cout << "除法是否有余数(0没有余数,1有余数)" << endl;
cin >> yushu;
for (int m = 0; m < num; m++)
{
int a = 2 + rand() % 4;
int a1[N] = {};//存运算的随机数
int y;//运算结果
for (int i = 0; i < a; i++)
{
int b = 1 + rand() % max;
a1[i] = b;
}
if (fushu == 1)
{
int v = rand() % 2;
if (v == 1)
{
a1[0] = -a1[0];
}

}
char b1[N] = {};//存进行运算的符号
char b[4] = { '+', '-', '*', '/' };
for (int i = 0; i < a - 1; i++)//判断连续除法
{
if (d == 0)
{
int d = rand() % 2;
b1[i] = b[d];
}
else
{
int d = rand() % 4;
b1[i] = b[d];
if (d == 3)
{
int c = rand() % 10 + 1;
if (yushu == 0)
{
a1[i] = a1[i + 1] * c;
}

if (b1[i - 1] == '/')
{
a1[i] = a1[i + 1] * c;
int c1 = rand() % 10 + 1;
a1[i - 1] = a1[i] * a1[i + 1] * c1;
}
}
for (int i = 0; i < a - 3; i++)
{
if (b1[i] == b1[i + 1] == b1[i + 2] == '/')
{
b1[i] = '+';
}
if (b1[i] == b1[i + 1] == b1[i + 2] == b1[i + 3] == '/')
{
b1[i + 3] = '-';
}
}
}

}
if (kh == 1)
{
for (int i = 0; i < a - 1; i++)//输出运算
{
if (b1[i] == '/'&&b1[i + 1] == '/')//连续除法用括号分开
{
cout << '(' << a1[i] << b1[i];

}
else
if (b1[i] == '/'&&b1[i - 1] == '/'&&i>0)
{
cout << a1[i] << ')' << b1[i];
}

else
{
int e = rand()%10;
cout << '(' << a1[i]<< '+' << e << ')' << b1[i];
}

}
cout << a1[a - 1] << '=' << " ";
}
else
{
for (int i = 0; i < a - 1; i++)//输出运算
{
if (b1[i] == '/'&&b1[i + 1] == '/')//连续除法用括号分开
{
cout << '(' << a1[i] << b1[i];

}
else
if (b1[i] == '/'&&b1[i - 1] == '/'&&i>0)
{
cout << a1[i] << ')' << b1[i];
}

else
cout << a1[i] << b1[i];
}
cout << a1[a - 1] << '=' << " ";
}
y = a1[0];
for (int i = 0; i < a - 1; i++)
{
int y = 0;

if (b1[i] == '/'&&b1[i + 1] == '/')
{
y = a1[i] / a1[i + 1] / a1[i + 2];
a1[i] = y;
a1[i + 1] = 0;
a1[i + 2] = 0;
b1[i] = b1[i + 1] = '+';

}
else
if (b1[i] == '/'&&b1[i + 1] == '*')
{
y = a1[i] / a1[i + 1] * a1[i + 2];
a1[i] = y;
a1[i + 1] = 0;
a1[i + 2] = 0;;
b1[i] = b1[i + 1] = '+';

}
else
if (b1[i] == '*'&&b1[i + 1] == '/')
{
y = a1[i] * a1[i + 1] / a1[i + 2];
a1[i] = y;
a1[i + 1] = 0;
a1[i + 2] = 0;
b1[i] = b1[i + 1] = '+';

}
else
if (b1[i] == '*'&&b1[i + 1] == '*')
{
y = a1[i] * a1[i + 1] * a1[i + 2];
a1[i] = y;
a1[i + 1] = 0;
a1[i + 2] = 0;
b1[i] = b1[i + 1] = '+';

}
else
if (b1[i] == '/')
{
y = a1[i] / a1[i + 1];
a1[i] = y;
a1[i + 1] = 0;
b1[i] = '+';

}
else
if (b1[i] == '*')
{
y = a1[i] * a1[i + 1];
a1[i] = y;
a1[i + 1] = 0;
b1[i] = '+';

}

}
int y1 = a1[0];
for (int i = 0; i < a - 1; i++)//递归计算
{
if (b1[i] == '+')
{
y1 = y1 + a1[i + 1];

}
else
if (b1[i] == '-')
{
y1 = y1 - a1[i + 1];

}
else
if (b1[i] == '*')
{
y1 = y1 * a1[i + 1];

}
else
if (b1[i] == '/')
{
y1 = y1 / a1[i + 1];

}

}
int s;
cout << "请输入你的计算结果";
cin >> s;
if (s == y1)
{
cout << "结果正确" ;
}
else
{
cout << "结果错误,正确答案为" << y1<<" " ;
}
if (out == 0)
{
cout << endl;
}
}

return 0;
}

  

运行结果截图:

宿舍工作照:

开发流程:

日期和任务
听课看书网上查资料编程日总计
周一2  13
周二    0
周三 2  2
周四2 125
周五  134
周六 1215
周日    0
总计434718
日期
开始时间结束时间中断时间净时间活动备注
3/1414:0015:0010110听课软件工程课
 16:3017:30060编程想思路
3/1615:0017:0020100看书读《构建之法》
3/1714:0015:5010100听课软件工程课
 19:0021:0020100结对编程交流思想
3/188:309:30060上网查资料整理思路
 16:3017:30060编程编写代码
 19:3021:3020100结对开发领航,查错
3/1910:0011:00060调试代码检查问题
 14:0016:0020100写博客总结
 19:3020:30060看书预习

缺陷记录日志:结对开发还不太默契,两个人节拍不协调,还需要多磨合。

程序的不足:

没有实现真分数的运算和结果的计算,还需要努力。

 

队友博客:http://home.cnblogs.com/u/apan008/

 

转载于:https://www.cnblogs.com/lvstudy/p/5295298.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值