1.设计思想:
主要运用switch语句进行选择,运用随机函数避免题目重复,其他思想在上一篇有所陈述。
2.源程序代码:
1 //信1305 郭婷 20132916 2 #include <iostream> 3 #include <time.h> 4 using namespace std; 5 6 int main() 7 { 8 srand(time(NULL)); 9 int x1,x2,flag,i,j; 10 int x3,x4; 11 int a1,a2; 12 char a3,a4,a5,a6; 13 int n; 14 //可定制打印的题目数 15 cout<<"请输入要打印的题目数:"; 16 cin>>a1; 17 while(a1<=0) 18 { 19 cout<<"请重新输入有效的题目数:"; 20 cin>>a1; 21 }; 22 //可定制运算数的范围 23 cout<<"请输入运算数的范围(大于1的数):"; 24 cin>>a2; 25 while(a1<=0) 26 { 27 cout<<"请重新输入有效的范围:"; 28 cin>>a2; 29 }; 30 //可定制题目中是否有乘除法 31 cout<<"请选择否有乘除法(Y/N):"; 32 cin>>a3; 33 while ((a3!=‘Y‘)&(a3!=‘N‘)) 34 { 35 cout<<"请重新输入有效的数值:"; 36 cin>>a3; 37 }; 38 //可定制题目结果是否有负数 39 cout<<"请选择结果有无负数(Y/N):"; 40 cin>>a4; 41 while((a4!=‘Y‘)&(a4!=‘N‘)) 42 { 43 cout<<"请重新输入有效的数值:"; 44 cin>>a4; 45 }; 46 //可定制结果是否有余数 47 cout<<"请选择结果有无余数(Y/N)"; 48 cin>>a5; 49 while((a5!=‘Y‘)&(a5!=‘N‘)) 50 { 51 cout<<"请重新输入有效的数值:"; 52 cin>>a5; 53 }; 54 //可定制是否支持分数 55 cout<<"请选择是否支持分数(Y/N):"; 56 cin>>a6; 57 while((a6!=‘Y‘)&(a6!=‘N‘)) 58 { 59 cout<<"请重新输入有效的数值:"; 60 cin>>a6; 61 }; 62 cout<<"每行输出的题目数:"; 63 cin>>n; 64 //循环打印符合要求的题目 65 for (i=0;i<a1;i++) 66 { 67 switch (a3) 68 { 69 case ‘Y‘:j=rand()%4; 70 break; 71 case ‘N‘:j=rand()%2; 72 break; 73 } 74 x1=rand()%a2+1; 75 x2=rand()%a2+1; 76 //循环打印 77 78 if (j==0) 79 { 80 switch(a6) 81 { 82 case ‘N‘:cout<<i+1<<" "<<x1<<"+"<<x2<<"= "; 83 if ((i+1)%n==0) 84 { 85 cout<<endl; 86 } 87 break; 88 case ‘Y‘:x3=rand()%9+1; 89 x4=rand()%9+1; 90 cout<<i+1<<" "<<x1<<"/"<<x2<<"+"<<x3<<"/"<<x4<<"= "; 91 if ((i+1)%n==0) 92 { 93 cout<<endl; 94 } 95 break; 96 } 97 } 98 if (j==1) 99 { 100 switch(a4) 101 { 102 case ‘Y‘: 103 break; 104 case ‘N‘: 105 if (x1<x2) 106 { 107 flag=x1; 108 x1=x2; 109 x2=flag; 110 } 111 break; 112 } 113 switch (a6) 114 { 115 case ‘N‘:cout<<i+1<<" "<<x1<<"-"<<x2<<"= "; 116 if ((i+1)%n==0) 117 { 118 cout<<endl; 119 } 120 break; 121 case ‘Y‘: 122 x3=rand()%9+1; 123 x4=rand()%9+1; 124 cout<<i+1<<" "<<x1<<"/"<<x2<<"-"<<x3<<"/"<<x4<<"= "; 125 if ((i+1)%n==0) 126 { 127 cout<<endl; 128 } 129 break; 130 } 131 } 132 if (j==2) 133 switch(a6) 134 { 135 case ‘N‘:cout<<i+1<<" "<<x1<<"*"<<x2<<"= "; 136 if ((i+1)%n==0) 137 { 138 cout<<endl; 139 } 140 break; 141 case ‘Y‘: 142 x3=rand()%9+1; 143 x4=rand()%9+1; 144 cout<<i+1<<" "<<x1<<"/"<<x2<<"*"<<"("<<x3<<"/"<<x4<<")"<<"= "; 145 if ((i+1)%n==0) 146 { 147 cout<<endl; 148 } 149 break; 150 } 151 if (j==3) 152 { 153 switch(a5) 154 { 155 case ‘Y‘: 156 break; 157 case ‘N‘: 158 while (x1%x2!=0) 159 { 160 x1=rand()%a2+1; 161 x2=rand()%a2+1; 162 }; 163 break; 164 } 165 switch(a6) 166 { 167 case ‘Y‘:cout<<i+1<<" "<<x1<<"/"<<x2<<"= "; 168 if ((i+1)%n==0) 169 { 170 cout<<endl; 171 } 172 break; 173 case ‘N‘: 174 x3=rand()%9+1; 175 x4=rand()%9+1; 176 cout<<i+1<<" "<<"("<<x1<<"/"<<x2<<")"<<"/"<<"("<<x3<<"/"<<x4<<")"<<"= "; 177 if ((i+1)%n==0) 178 { 179 cout<<endl; 180 } 181 break; 182 } 183 184 } 185 } 186 187 return 0; 188 }
3.程序截图
4.编程总结
刚一开始看到这个题目,感觉有点难,要求挺多。但是经过对每一个要求的分析和理解,将这个程序由大化小,先将一个个小的要求实现,在将这些要求程序连接起来,就可以将一个看似麻烦的程序变得简单了;还有就是对于题目的重复问题,我在网上查过之后,找到了srand(time(NULL));这个函数。其中不足的是是否支持分数,如果选择是,则题目中都是分数。
5.时间记录日志
时间: 2024-09-16 06:41:55