日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

四则运算2单元测试

發布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 四则运算2单元测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 #include<iostream.h> 2 #include<stdlib.h> 3 #include<time.h> 4 void display(int number,int l,int mul,int num,int neg,int remainder)//打印方式控制輸出列數 5 { 6 int a[1000],b[1000],c[1000]; 7 int m;//控制題目避免重復 8 int t;//中間變量 9 for(int i=0;i<number;i++)//隨機兩個生成運算操作數 10 { 11 m=1;//初始化 12 a[i]=rand()%num; 13 b[i]=rand()%num; 14 if(mul==0)//沒有乘除法 15 { 16 c[i]=rand()%2;//隨機生成0-1的數字,分別表示加減 17 } 18 else if(mul==1)//有乘除法 19 { 20 c[i]=rand()%4;//隨機生成0-3的數字,分別表示加減乘 21 } 22 for(int j=0;j<i;j++) 23 { 24 if(a[j]==a[i]&&b[j]==b[i]&&c[j]==c[i])//比較新生成的操作數與原來的是否相同 25 { 26 i=i-1; 27 m=0; 28 } 29 } 30 while(m)//若不同則輸出 31 { 32 switch(c[i]) 33 { 34 case 0: 35 cout<<a[i]<<"+"<<b[i]<<"=";break; 36 case 1: 37 if(neg==0)//減法沒有負數 38 { 39 if(a[i]<b[i]) 40 { 41 t=a[i]; 42 a[i]=b[i]; 43 b[i]=t; 44 } 45 cout<<a[i]<<"-"<<b[i]<<"=";break; 46 } 47 else if(neg==1)//減法有負數 48 { 49 cout<<a[i]<<"-"<<b[i]<<"=";break; 50 } 51 case 2: 52 cout<<a[i]<<"*"<<b[i]<<"=";break; 53 case 3: 54 if(b[i]==0)//分母為零則不計入總數 55 { 56 i=i-1;break; 57 } 58 else if(remainder==0)//除法沒有余數 59 { 60 if(a[i]%b[i]==0) 61 { 62 cout<<a[i]<<"/"<<b[i]<<"=";break; 63 } 64 else 65 { 66 i=i-1;break; 67 } 68 } 69 else if(remainder==1)//除法有余數 70 { 71 if(a[i]%b[i]!=0) 72 { 73 cout<<a[i]<<"/"<<b[i]<<"=";break; 74 } 75 else 76 { 77 i=i-1;break; 78 } 79 } 80 } 81 if((i+1)%l==0) 82 { 83 cout<<endl; 84 } 85 else 86 { 87 cout<<"\t"; 88 } 89 break;//跳出循環 90 } 91 } 92 } 93 void main() 94 { 95 int number;//題目數量 96 int l;//輸出的列數 97 int mul;//乘除法 98 int num;//數值范圍 99 int neg;//負數 100 int remainder;//余數 101 int c;//循環變量 102 while(c) 103 { 104 srand((unsigned) time(NULL));//調用隨機函數發生器 105 cout<<"---------------------------------"<<endl; 106 cout<<"| 四則運算 |"<<endl; 107 cout<<"---------------------------------"<<endl; 108 cout<<"請輸入要打印的題目數量:"<<endl; 109 cin>>number; 110 while(number<0) 111 { 112 cout<<"輸入錯誤!"<<endl; 113 cout<<"請輸入要打印的題目數量:"<<endl; 114 cin>>number; 115 } 116 cout<<"請輸入要輸出的列數(1-10):"<<endl; 117 cin>>l; 118 cout<<"是否有乘除法(0表示沒有;1表示有)"<<endl; 119 cin>>mul; 120 while(mul!=0&&mul!=1) 121 { 122 cout<<"輸入錯誤,請重新輸入!是否有乘除法(0表示沒有;1表示有)"<<endl; 123 cin>>mul; 124 } 125 if(mul==1) 126 { 127 cout<<"除法有無余數(0表示沒有;1表示有)"<<endl; 128 cin>>remainder; 129 while(remainder!=0&&remainder!=1) 130 { 131 cout<<"輸入錯誤,請重新輸入!除法有無余數(0表示沒有;1表示有)"<<endl; 132 cin>>remainder; 133 } 134 } 135 /* 136 switch(mul) 137 { 138 default: 139 { 140 cout<<"輸入錯誤,請重新輸入!是否有乘除法(0表示沒有;1表示有)"<<endl; 141 cin>>mul; 142 } 143 break; 144 case 0:break; 145 case 1: 146 { 147 cout<<"除法有無余數(0表示沒有;1表示有)"<<endl; 148 cin>>remainder; 149 switch(remainder) 150 { 151 default: 152 { 153 cout<<"輸入錯誤,請重新輸入!除法有無余數(0表示沒有;1表示有)"<<endl; 154 cin>>remainder; 155 } 156 break; 157 case 0:break; 158 case 1:break; 159 } 160 }break; 161 } 162 */ 163 cout<<"請輸入正整數的數值范圍(即最大數):"<<endl; 164 cin>>num; 165 cout<<"減法有無負數(0表示沒有;1表示有)"<<endl; 166 cin>>neg; 167 while(neg!=0&&neg!=1) 168 { 169 cout<<"輸入錯誤,請重新輸入!"<<endl; 170 cout<<"減法有無負數(0表示沒有;1表示有)"<<endl; 171 cin>>neg; 172 } 173 display(number,l,mul,num,neg,remainder); 174 cout<<"繼續請輸入1,退出請輸入0"<<endl; 175 cin>>c; 176 } 177 }

?

?

測試截圖:

輸入正確時:20?? 2?? 1?? 1?? 100??? 1

輸入200? 20? 1? 1??? 200 1

此時由于頁面大小限制,將不能在輸出所輸入的列數

輸入字母時:a

輸入10000?? 10 2 0 190 1 ? ??可能超出范圍了,程序運行最后會報錯。停止運行。。。

輸入20 ?4 ? 2 ?1 ?0 100 ? ?1

輸出題目數量為負數時,提示出錯

輸入錯誤時提示錯誤并重新輸入

由于程序寫得不太完善,測試時出現很多問題,有待進一步的改進。

轉載于:https://www.cnblogs.com/czl123/p/4341630.html

總結

以上是生活随笔為你收集整理的四则运算2单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。