实验2选择、循环结构的程序设计 【实验目的】 1掌握向工作区添加项目及向项目添加文件的方法。 2学习if、 switch选择语句的使用及其嵌套的使用方法 3熟悉掌握用 while语句、do… while语句和for语句实现循环的方法 【实验内容】 1.编程实现:输入一个百分制成绩,要求输出成绩等级A、B、C、D、E。 (注:90分以上:为A,81一89分为B,70一79分为C,60一69分为D,60分以 下为E。) 要求:该程序能正确处理任何数据。当输人数据大于:100或小于0的数据通知用户 “输人数据错”,结束程序。 #include <iostream h> void main d float score; cout<"输入一个人的成绩"; if( score>100ore<0)cout<"输入数据有误!n"; else if( score≥=90 cout<<sore<<"分为Aln"; else i( score>=80)cout<<sore<"分为Bn"; else if( score>=70)cout<<sore<"分为Cin"; else if( score>=60)cout<<sore<<"分为Dn" cou<sore<<"分为En"; 2.编写程序由键盘输入年、月,输出该年该月的天数。 #include <iostream h> void main() f int year, month, days; cin>>vear>>month switch(month) f case 1: case 3: case 5: case 7: case 8: case 10: case 12 /*处理“大”月 case 4: case 6: case 9: case 11 处理“小”月* 30: break case 处理“平”月刘 if( year%==0 & year%100=0 ll year%/400==0 days=29; /*如果是闰年* /不是闰年 break: default /*月份错误 cout<<"Input error! \n";
实验 2 选择、循环结构的程序设计 【实验目的】 1 掌握向工作区添加项目及向项目添加文件的方法。 2 学习 if、switch 选择语句的使用及其嵌套的使用方法 3 熟悉掌握用 while 语句、do…while 语句和 for 语句实现循环的方法。 【实验内容】 ⒈编程实现:输入一个百分制成绩,要求输出成绩等级 A、B、C、D、E。 (注:90 分以上:为 A,8l 一 89 分为 B,70 一 79 分为 C,60 一 69 分为 D,60 分以 下为 E。) 要求:该程序能正确处理任何数据。当输人数据大于:100 或小于 0 的数据通知用户 “输人数据错”,结束程序。 #include <iostream.h> void main( ) { float score; cout<<"输入一个人的成绩:"; cin>> score; if (score >100|| score <0 ) cout<<"输入数据有误! \n"; else if (score >=90) cout<< score<< "分为 A \n"; else if (score >=80 ) cout<< score <<"分为 B\n "; else if (score >=70 ) cout<< score <<"分为 C \n"; else if (score >=60 ) cout<< score<< "分为 D\n"; else cout<< score <<"分为 E\n"; } ⒉编写程序由键盘输入年、月,输出该年该月的天数。 #include <iostream.h> void main( ) { int year, month, days; cin>>year>>month; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: /* 处理“大”月 */ days=31; break; case 4: case 6: case 9: case 11: /* 处理“小”月 */ days=30; break; case 2: /* 处理“平”月 */ if ( year%4==0 && year%100!=0 || year%400==0 ) days=29; /* 如果是闰年 */ else days=28; /* 不是闰年 */ break; default: /* 月份错误 */ cout<<"Input error!\n";
days=0; if (days =0) cout<<year<"年的< month<<"是"<<days<"天n"; 3.某商店五一购物打折。每位顾客一次购物: ①满1000元,打九折 ②满2000元,打八折 ③满3000元,打七折 ④满4000元,打六折: ⑤50元,打五折:编写程序,输入购物款,输出实收款 #includesiostream. h> void main i double m, p: cout<<"Input sum of money cin>>m; f(m1000)p=m; else if (m<2000)p=0.9*m; else if(m<3000)p=0.8*m: else if(m<4000)p=0.7*m else if(m<5000)p=0. 6 m else p=0.5* 购物款 cout<<"实收款:"<p<end 4.求1+22+42+62……+502的值 #includesiostream. h> fint i, sum1; for(i=2;i<=50;i+=2) sursum+ii cout<<sum=<<sum<<endl 5编程求出小于n的所有素数(素数又叫质数)。(选作题) #include iostream h> id maino I int i in > for (i=2: i<n;i++) [for(j=2;孓i;j+) if (i %j==0 break if(j〉i-1) cout <i<<
days=0; } if (days!=0) cout<<year<<"年的"<<month<<"是"<<days<<"天\n"; } ⒊某商店五一购物打折。每位顾客一次购物: ①满 1000 元,打九折; ②满 2000 元,打八折; ③满 3000 元,打七折; ④满 4000 元,打六折; ⑤5000 元,打五折;编写程序,输入购物款,输出实收款。 #include<iostream.h> void main() { double m,p; cout<<"Input sum of money:"; cin>>m; if(m<1000) p=m; else if (m<2000)p=0.9*m; else if(m<3000) p=0.8*m; else if(m<4000) p=0.7*m; else if(m<5000) p=0.6*m; else p=0.5*m; cout<<"购物款:"<<m<<endl; cout<<"实收款:"<<p<<endl; } ⒋ 求 1+22+42+62……+502 的值。 #include<iostream.h> void main() {int i,sum=1; for(i=2;i<=50;i+=2) sum=sum+i*i; cout<<"sum="<<sum<<endl; } ⒌编程求出小于 n 的所有素数(素数又叫质数)。(选作题) #include <iostream.h> void main() { int i,j,n; cin >> n; for (i=2; i<n; i++) { for (j=2; j<i; j++) if (i % j = = 0) break; if (j > i-1) cout << i << " "; } }