作业要求
作业一
预习
用软件写代码要保证正确性,有效性。
一个错误会导致漏洞。低效能运行速度会减慢。
所以要考虑WordFreq算法的时间复杂度,能否优化,加以验证。
代码
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void function1()
{
int a,b,d;
char A[2]={'+','-'};
float D[30];
int n=0;
for(int i=0;n<30;i++)
{
a=rand()%10;
b=rand()%10;
d=rand()%2+1;
cout<<a<<A[d]<<b<<"="<<endl;
if(d=1)
{
D[n]=a+b;
n++;
}
else
{
D[n]=a-b;
n++;
}
}
cout<<"********************分割线*********************"<<endl;
cout<<"******************以下是答案*******************"<<endl;
for(int i=0;i<=30;i++)
{
cout<<D[i]<<endl;
}
}
void function2()
{
int a,b,d;
char A[2]={'+','-'};
float D[30];
int n=0;
for(int i=0;n<30;i++)
{
a=rand()%10;
b=rand()%10;
d=rand()%2+1;
cout<<a<<A[d]<<b<<"="<<endl;
if(d=1)
{
D[n]=a+b;
n++;
}
else
{
D[n]=a-b;
n++;
}
}
cout<<"********************分割线*********************"<<endl;
cout<<"******************以下是答案*******************"<<endl;
for(int i=0;i<=30;i++)
{
cout<<D[i]<<endl;
}
}
void function3()
{
int a,b,c,d,r;
char A[2]={'+','-','*','/'};
char B[2]={'+','-','*','/'};
float D[30];
int n=0;
for(int i=0;n<30;i++)
{
a=rand()%20;
b=rand()%20;
c=rand()%20;
d=rand()%4+1;
r=rand()%4+1;
cout<<a<<A[d]<<b<<B[r]<<c<<"="<<endl;
if(d=1)
{
if(r=1)
{
D[n]=a+b+c;
n++;
}
else if(r=2)
{
D[n]=a+b-c;
n++;
}
else if(r=3)
{
D[n]=a+b*c;
n++;
}
else
{
D[n]=a+b/c;
n++;
}
}
else if(d=2)
{
if(r=1)
{
D[n]=a-b+c;
n++;
}
else if(r=2)
{
D[n]=a-b-c;
n++;
}
else if(r=3)
{
D[n]=a-b*c;
n++;
}
else
{
D[n]=a-b/c;
n++;
}
}
else if(d=3)
{
if(r=1)
{
D[n]=a*b+c;
n++;
}
else if(r=2)
{
D[n]=a*b-c;
n++;
}
else if(r=3)
{
D[n]=a*b*c;
n++;
}
else
{
D[n]=a*b/c;
n++;
}
}
else(d=4)
{
if(r=1)
{
D[n]=a/b+c;
n++;
}
else if(r=2)
{
D[n]=a/b-c;
n++;
}
else if(r=3)
{
D[n]=a/b*c;
n++;
}
else
{
D[n]=a/b/c;
n++;
}
}
}
cout<<"********************分割线*********************"<<endl;
cout<<"******************以下是答案*******************"<<endl;
for(int i=0;i<=30;i++)
{
cout<<D[i]<<endl;
}
}
void function4()
{
int a,b,c,d,r;
char A[2]={'+','-','*','/'};
char B[2]={'+','-','*','/'};
float D[30];
int n=0;
for(int i=0;n<30;i++)
{
a=rand()%100;
b=rand()%100;
c=rand()%100;
d=rand()%4+1;
r=rand()%4+1;
cout<<a<<A[d]<<b<<B[r]<<c<<"="<<endl;
if(d=1)
{
if(r=1)
{
D[n]=a+b+c;
n++;
}
else if(r=2)
{
D[n]=a+b-c;
n++;
}
else if(r=3)
{
D[n]=a+b*c;
n++;
}
else
{
D[n]=a+b/c;
n++;
}
}
else if(d=2)
{
if(r=1)
{
D[n]=a-b+c;
n++;
}
else if(r=2)
{
D[n]=a-b-c;
n++;
}
else if(r=3)
{
D[n]=a-b*c;
n++;
}
else
{
D[n]=a-b/c;
n++;
}
}
else if(d=3)
{
if(r=1)
{
D[n]=a*b+c;
n++;
}
else if(r=2)
{
D[n]=a*b-c;
n++;
}
else if(r=3)
{
D[n]=a*b*c;
n++;
}
else
{
D[n]=a*b/c;
n++;
}
}
else(d=4)
{
if(r=1)
{
D[n]=a/b+c;
n++;
}
else if(r=2)
{
D[n]=a/b-c;
n++;
}
else if(r=3)
{
D[n]=a/b*c;
n++;
}
else
{
D[n]=a/b/c;
n++;
}
}
}
cout<<"********************分割线*********************"<<endl;
cout<<"******************以下是答案*******************"<<endl;
for(int i=0;i<=30;i++)
{
cout<<D[i]<<endl;
}
}
void function5()
{
int a,b,c,d,r,s;
char A[2]={'+','-','*','/'};
char B[2]={'+','-','*','/'};
float D[30];
int n=0;
for(int i=0;n<30;i++)
{
a=rand()%100;
b=rand()%100;
c=rand()%100;
d=rand()%4+1;
r=rand()%4+1;
s=rand()%2+1;
if(s==1){
cout<<"("<<a<<A[d]<<b<<")"<<B[r]<<c<<"="<<endl;
if(d=1)
{
if(r=1)
{
D[n]=(a+b)+c;
n++;
}
else if(r=2)
{
D[n]=(a+b)-c;
n++;
}
else if(r=3)
{
D[n]=(a+b)*c;
n++;
}
else
{
D[n]=(a+b)/c;
n++;
}
}
else if(d=2)
{
if(r=1)
{
D[n]=(a-b)+c;
n++;
}
else if(r=2)
{
D[n]=(a-b)-c;
n++;
}
else if(r=3)
{
D[n]=(a-b)*c;
n++;
}
else
{
D[n]=(a-b)/c;
n++;
}
}
else if(d=3)
{
if(r=1)
{
D[n]=(a*b)+c;
n++;
}
else if(r=2)
{
D[n]=(a*b)-c;
n++;
}
else if(r=3)
{
D[n]=(a*b)*c;
n++;
}
else
{
D[n]=(a*b)/c;
n++;
}
}
else(d=4)
{
if(r=1)
{
D[n]=(a/b)+c;
n++;
}
else if(r=2)
{
D[n]=(a/b)-c;
n++;
}
else if(r=3)
{
D[n]=(a/b)*c;
n++;
}
else
{
D[n]=(a/b)/c;
n++;
}
}
}
if(s==1){
cout<<a<<A[d]<<"("<<b<<B[r]<<c<<")="<<endl;
if(d=1)
{
if(r=1)
{
D[n]=a+(b+c);
n++;
}
else if(r=2)
{
D[n]=a+(b-c);
n++;
}
else if(r=3)
{
D[n]=a+(b*c);
n++;
}
else
{
D[n]=a+(b/c);
n++;
}
}
else if(d=2)
{
if(r=1)
{
D[n]=a-(b+c);
n++;
}
else if(r=2)
{
D[n]=a-(b-c);
n++;
}
else if(r=3)
{
D[n]=a-(b*c);
n++;
}
else
{
D[n]=a-(b/c);
n++;
}
}
else if(d=3)
{
if(r=1)
{
D[n]=a*(b+c);
n++;
}
else if(r=2)
{
D[n]=a*(b-c);
n++;
}
else if(r=3)
{
D[n]=a*(b*c);
n++;
}
else
{
D[n]=a*(b/c);
n++;
}
}
else(d=4)
{
if(r=1)
{
D[n]=a/(b+c);
n++;
}
else if(r=2)
{
D[n]=a/(b-c);
n++;
}
else if(r=3)
{
D[n]=a/(b*c);
n++;
}
else
{
D[n]=a/(b/c);
n++;
}
}
}
cout<<"********************分割线*********************"<<endl;
cout<<"******************以下是答案*******************"<<endl;
for(int i=0;i<=30;i++)
{
cout<<D[i]<<endl;
}
}
int main() //随机产生四则运算
{
int m;
cout<<"请选择年级"<<endl;
cout<<"输入1,一年级计算题(20以内两个数字的加减法)"<<endl;
cout<<"输入2,二年级计算题(10以内两个数字的四则运算"<<endl;
cout<<"输入3,三年级计算题(20以内的三个数字的四则运算)"<<endl;
cout<<"输入4,四年级计算题(100以内的三个数字的四则运算)"<<endl;
cout<<"输入5,五年级计算题(100以内的三个数字加括号的四则运算)"<<endl;
cout<<"请输入:"<<endl;
cin>>m;
if(m==1)
function1();
else if(m==2)
function2();
else if(m==3)
function3();
else if(m==4)
function4();
else
function5();
return 0;
}
问题
电脑上的Microsoft Visual Studio出现错误,所以没运行。代码是自己写的,没写出分数形式的计算题。