1 "topic Requirements:
Poor two pillars classmate, the teacher again on his automatic question system put forward a new request:
1. Avoid duplication of topics;
2, can be customized (quantity/printing method);
3, the following parameters can be controlled:
* Whether there is a multiplication law; * Whether there are brackets (up to 10 numbers to participate in the calculation); * range of values; * plus or minus negative numbers; * Division has no total number! 2 "Design idea: This programming is mainly on the basis of the previous, make further requirements, and then realize the output of different ways. 1) The problem to avoid duplication of implementation: first define three arrays, and then loop the resulting operator and two operands (three randomly generated) into the array, each cycle, the number in the array and the preceding, if two operators and operands are equal to a previous set of numbers, then re-produce a set of random numbers, Implementation avoids duplication. 2) Customizable implementation: The number of prints, is the number of inputs, then the number of cycles, and then output. Printing mainly can be output on the screen, you can also output to a Notepad file, which uses the input and output stream knowledge. 3) 1. Whether there is a multiplication method, with the IF statement to achieve, if there is multiplication method, then further select whether the surplus number, my program is the choice of the surplus number of direct output, if there is no remainder, is the divisor minus the remainder in the output. 2. Range of values: You can define a number s, with the random number rand ()%s is the random number that produces 1~s. 3. Add and subtract no negative, is to ensure that the latter number is larger than the previous number, there are negative numbers, in which the minus sign is added. PS: Whether the parentheses function is more difficult, the source code has not been implemented for the time being:
Arithmetic 2
3 Sunzhe
#include <fstream>
#include <iostream>
using namespace Std;
void Chengchufa (int m,int s)
{
int V;
int j[100],k[100],l[100];//is used to check for duplicate arrays
cout<< "Please enter your choice: 1.2. No:" <<endl;
cin>>v;
for (int i=0;i<m;i++)
{
int N=rand ()%4; Generate random number, operator
int A=rand ()%s;//generates 1~s random number
int B=rand ()%s;//generates 1~s random number
J[i]=n;
K[i]=a;
L[i]=b;
for (int r=i-1;r>=0;r--)//forward check duplicates, regenerate if duplicate
{
if (J[r]==j[i]&&k[r]==k[i]&&l[r]==l[i])
{
int N=rand ()%4; Generate random number, operator
int A=rand ()%s;//generates 1~s random number
int B=rand ()%s;//generates 1~s random number
}
}
if (n==0)
{
cout<<a<< "+" <<b<< "=" <<endl;
}
else if (n==1) {
cout<<a<< "-" <<b<< "=" <<endl;
}
else if (n==2) {
cout<<a<< "*" <<b<< "=" <<endl;
}
else{
if (A<B)//implementation of the divisor is larger than the divisor
{
int W;
W=a;
A=b;
B=w;
}
if (b==0)
{
b++;
}
if (v==1)//Surplus number
{
cout<<a<< "/" <<b<< "=" <<endl;
}
if (v==2)
{
A=a-a%b;
cout<<a<< "/" <<b<< "=" <<endl;
}
}
}
}
void Jiajianfa (int m,int s) {
int u;
int j[100],k[100],l[100];//is used to check for duplicate arrays
cout<< "Please input select: 1 negative 2 no negative" <<endl;
cin>>u;
for (int i=0;i<m;i++)
{
int N=rand ()%2;//produces a random number, the operator's
int A=rand ()%s;//generates 1~s random number
int B=rand ()%s;//generates 1~s random number
J[i]=n;
K[i]=a;
L[i]=b;
for (int r=i-1;r>=0;r--)
{
if (J[r]==j[i]&&k[r]==k[i]&&l[r]==l[i])
{
int N=rand ()%4; Generate random number, operator
int A=rand ()%s;//generates 1~s random number
int B=rand ()%s;//generates 1~s random number
}
}
if (u==1)//Negative
{
if (n==0)
{
cout<< "(-" <<a<< ") +" <<b<< "=" <<endl;
}
Else
{
cout<<a<< "-" <<b<< "=" <<endl;
}
}
if (u==2)//No negative
{
if (A<B)//implementation subtraction No negative numbers
{
int W;
W=a;
A=b;
B=w;
}
if (n==0)
{
cout<<a<< "+" <<b<< "=" <<endl;
}
Else
{
cout<<a<< "-" <<b<< "=" <<endl;
}
}
}
}
void Dayinfangshi (int m,int s) {
int t;
cout<< "Please enter select 1. On screen output 2. In file output" <<endl;
cin>>t;
if (t==1)
{
for (int i=0;i<m;i++)
{
int N=rand ()%4; Generate random number, operator
int A=rand ()%s;//generates 1~s random number
int B=rand ()%s;//generates 1~s random number
if (n==0) {
cout<<a<< "+" <<b<< "=" <<endl;
}
else if (n==1) {
cout<<a<< "-" <<b<< "=" <<endl;
}
else if (n==2) {
cout<<a<< "*" <<b<< "=" <<endl;
}
else{
if (A<B)//implementation of the divisor is larger than the divisor
{
int W;
W=a;
A=b;
B=w;
}
if (b==0)
{
b++;
}
cout<<a<< "/" <<b<< "=" <<endl;
}
}
}
if (t==2)
{
Ofstream outfile ("F1.dat", ios::out);//define file stream object, open disk File F1.dat
if (! outfile)//If Open failure returns 0 value
{
cerr<< "Open error!" <<endl;
Exit (1);
}
for (int i=0;i<m;i++)
{
int N=rand ()%4; Generate random number, operator
int A=rand ()%s;//generates 1~s random number
int B=rand ()%s;//generates 1~s random number
if (n==0) {
outfile<<a<< "+" <<b<< "=" <<endl;
}
else if (n==1) {
outfile<<a<< "-" <<b<< "=" <<endl;
}
else if (n==2) {
outfile<<a<< "*" <<b<< "=" <<endl;
}
else{
if (A<B)//implementation of the divisor is larger than the divisor
{
int W;
W=a;
A=b;
B=w;
}
if (b==0)
{
b++;
}
outfile<<a<< "/" <<b<< "=" <<endl;
}
}
Outfile.close ();
}
}
void Main ()
{
int x;//the number of selections
q:cout<< "Please select the operation to be performed: 1. Integer arithmetic 2. True fractional arithmetic 3. Exit program <<endl;
cin>>x;
Switch (x)
{
Case 1://An integer operation
{
int m;//Number of topics
cout<< "Please enter the quantity you want to print:" <<endl;
cin>>m;
int s;//range of values
cout<< "Please enter the range of values you want to calculate (1~ the number you entered):" <<endl;
cin>>s;
int t;
cout<< "Please enter selection:" <<endl;
cout<< "1. Multiplication Law" <<endl;
cout<< "2. No multiplication Law" <<endl;
cout<< "3. Printing Mode" <<endl;
cin>>t;
if (t==1) {//With multiplication method
Chengchufa (m,s);
}
if (t==2)//No Multiplication method
{
JIAJIANFA (m,s);
}
if (t==3)
{
Dayinfangshi (m,s);
}
Goto Q;
}
Case 2:
{
for (int i=0;i<30;i++)
{
int C=rand ()%100;//generates 1~100 random number
int D=rand ()%100;//generates 1~100 random number
int E=rand ()%100;//generates 1~100 random number
int F=rand ()%100;//generates 1~100 random number
//Guaranteed True score
if (c>d)
{
int t;
T=d;
D=c;
C=t;
}
if (e>f)
{
int s;
S=f;
F=e;
E=s;
}
if (d==0) {d++;}//Denominator not zero
if (f==0) {f++;}//Denominator not zero
//approximately differentiated to the simplest
for (int a=c;a>0;a--)
{
if (c%a==0&&d%a==0)
{
c=c/a ;
d=d/a;
break;
}
}
for (int b=e;b>0;b--)
{
if (e%b==0&&f %b==0)
{
e=e/b;
f=f/b;
break;
}
}
int N=rand ()% 4; The random number of the generated operator
if (n==0) {
cout<<c<< "/" <<d<< "+" <<e<< "/" <<f<< "=" <<endl;
}
else if (n==1) {
cout<<c<< "/" <<d<< "-" <<e<< "/" <<f<< "=" <<endl;
}
else if (n==2) {
cout<<c<< "/" <<d<< "* (" <<e<< "/" <<f<< ") =" <<endl;
}
Else
{
cout<<c<< "/" <<d<< "/(" <<e<< "/" <<f<< ") =" <<endl;
}
}
Goto Q;
}
Case 3:
{
Exit (0);
}
}
}
3 "Operation Result:
Software Engineering Personal Assignment 2