四则运算2

悲催的二柱子接到了老师要求给软件增加一些小小的功能,具体要求如下:

1、除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性);

2、一次出的题目避免相互重复;

3、可定制出题的数量。

设计思路: 这次是设计几个满足真分数的情况需要有真分数的加减乘除

是先进行计算数目的选择,然后是对整数和真分数的选择,最后可输出答案。

 

源代码

 

  1 #include<iostream.h>

  2 #include<stdlib.h>

  3 #include<conio.h>

  4

  5 void DealFenshu(int m, int a[][2])

  6 {

  7     for(int p=0;p<m;p++)

  8     {

  9         int i=(int)rand()%10;

 10         int j=(int)rand()%10;

 11         while(j==0||i>=j)

 12         {

 13             i=(int)rand()%10;

 14             j=(int)rand()%10;

 15         }

 16         int x=(int)rand()%10;

 17         int y=(int)rand()%10;

 18         while(y==0||x>=y)

 19         {

 20             x=(int)rand()%10;

 21             y=(int)rand()%10;

 22         }

 23         int k=(int)rand()%100/25;

 24         switch(k)

 25         {

 26             case 0:

 27                 cout<<"("<<i<<"/"<<j<<")"<<"+"<<"("<<x<<"/"<<y<<")"<<"=";

 28                 a[p][0]=i*y+x*j;

 29                 a[p][1]=j*y;

 30                 break;

 31             case 1:

 32                 cout<<"("<<i<<"/"<<j<<")"<<"-"<<"("<<x<<"/"<<y<<")"<<"=";

 33                 a[p][0]=i*y-x*j;

 34                 a[p][1]=j*y;

 35                 break;

 36             case 2:

 37                 cout<<"("<<i<<"/"<<j<<")"<<"*"<<"("<<x<<"/"<<y<<")"<<"=";

 38                 a[p][0]=i*x;

 39                 a[p][1]=j*y;

 40                 break;

 41             case 3:

 42                 a[p][0]=i*y;

 43                 a[p][1]=j*x;

 44                 cout<<"("<<i<<"/"<<j<<")"<<"/"<<"("<<x<<"/"<<y<<")"<<"=";

 45             }

 46            

 47             if(p%5==4)

 48             {

 49                 cout<<endl;

 50             }

 51             else

 52             {

 53                 cout<<'\t';

 54             }

 55     }

 56

 57 }

 58 void DisplayFenshu(int a[][2],int w,int m)

 59 {

 60     if(w==1)

 61     {

 62         for(int q=0;q<m;q++)

 63         {

 64             if(a[q][0]==0)

 65                 cout<<"0"<<'\t';

 66             else

 67                 cout<<a[q][0]<<"/"<<a[q][1]<<'\t';

 68             if(q%5==4)

 69             {

 70                 cout<<endl;

 71             }

 72         }

 73     }

 74     else

 75     {};

 76 }

 77 void DealInt(int m,int a[])

 78 {

 79        

 80     for(int p=0;p<m;p++)

 81     {

 82     int i=(int)rand()%10;

 83     int j=(int)rand()%10;

 84     int k=(int)rand()%100/25;

 85     switch(k)

 86     {

 87     case 0:

 88         cout<<i<<"+"<<j<<"=";

 89             a[p]=i+j;

 90         break;

 91     case 1:

 92         cout<<i<<"-"<<j<<"=";

 93         a[p]=i-j;

 94         break;

 95     case 2:

 96         cout<<i<<"*"<<j<<"=";

 97         a[p]=i*j;

 98         break;

 99     case 3:

100         try

101         {

102         a[p]=i/j;

103         cout<<i<<"/"<<j<<"=";

104         }

105         catch(...)

106         {

107             p--;

108         }

109        

110

111     }

112        

113         if(p%5==4)

114         {

115             cout<<endl;

116         }

117         else

118         {

119             cout<<'\t';

120         }

121     }

122 }

123 void DisplayInt(int a[],int w,int m)

124 {

125     if(w==1)

126     {

127         for(int q=0;q<m;q++)

128         {

129             cout<<a[q]<<'\t';

130             if(q%5==4)

131             {

132                 cout<<endl;

133             }

134         }

135     }

136     else

137     {};

138 }

139 void main()

140 {

141     int p;

142     do

143     {

144         system("cls");

145         int a[1000],b[1000][2];

146         int m,n,w;

147         cout<<"请输入生成的四则运算题个数:";

148         cin>>m;

149         cout<<endl;

150         cout<<"请输入要生成的四则运算种类(输入1为整数,否则为真分数):";

151         cin>>n;

152         cout<<endl;

153         if(n==1)

154         {

155             DealInt(m,a);

156             cout<<endl;

157         }

158         else

159         {

160             DealFenshu(m,b);

161             cout<<endl;

162         }

163         cout<<"是否输出答案(输入1则输出答案否则不输出答案)"<<endl;

164         cin>>w;

165         if(n==1)

166         {

167             DisplayInt(a,w,m);       

168         }

169         else

170         {

171             DisplayFenshu(b,w,m);

172         }

173         cout<<endl;

174         cout<<"是否继续生成运算题(输入1则生成否则不生成)"<<endl;

175         cin>>p;

176         cout<<endl;

177     }while(1==p);

178

179 }

结构截屏

 

实验心得:有时候对代码的使用认识还是不够清楚,还需要更多的去编写才能够了解。

猜你喜欢

转载自www.cnblogs.com/renhao46707633/p/10247624.html