欢迎来到天天文库
浏览记录
ID:48276525
大小:107.00 KB
页数:8页
时间:2019-11-30
《程序设计教程(机械工业出版社)课后习题答案--第3章-程序的流程控制――语句》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第3章程序的流程控制――语句1、编写一个程序,将华氏温度转换为摄氏温度。转换公式为:c=(f-32),其中,c为摄氏温度,f为华氏温度解:#includeusingnamespacestd;intmain(){doublec,f;cout<<"PleaseinputanF-temperature:"<>f;c=(f-32)*5/9;cout<<"TheC-temperatureis:"<2、换为12小时制表示的时间。例如,输入20和16(20点16分),输出8:16pm;输入8和16(8点16分),输出8:16am。解:#includeusingnamespacestd;intmain(){inthour,minute;charnoon;cout<<"Pleaseinputatimein24-hourformat:"<>hour;if(hour<03、4、hour>23){cout<<"Theinputhouriswrong!"5、<12){hour=hour-12;noon='p';}elsenoon='a';cout<<"minute:";cin>>minute;if(minute<06、7、minute>59){cout<<"Theinputminuteiswrong!"<8、secout<<"am"<usingnamespacestd;intmain(){charc;for(c='a';c<='z';c++)cout<='a';c--)cout<9、iostream>usingnamespacestd;intmain(){unsignedintgzint;intcount=0;while(1){cout<<"Pleaseinputaninteger(greaterthanzero):"<>gzint;if(gzint<=0)cout<<"Yourinputiswrong!Pleaseinputagain..."<10、t<<"Thenumberofdigitsintheintergeris:"<usingnamespacestd;intmain(){intcount=0;charch;cout<<"Pleaseinputanexpression:"<>ch;ch!='#';cin>>ch){if(ch11、=='(')count++;elseif(ch==')')count--;}if(count==0)cout<<"配对!"<0)cout<<"多左括号!"<=”进行计数。解:#includeusingnamespacestd;intmain(){intcount=0;charch1=' ',ch2;cout<12、<"Pleaseinputastring(terminatedwith#):"<>ch2;ch2!='#';cin>>ch2){if(ch2=='='&&ch1=='>')count++;ch1=ch2;}cout<<"Numberof>=:"<
2、换为12小时制表示的时间。例如,输入20和16(20点16分),输出8:16pm;输入8和16(8点16分),输出8:16am。解:#includeusingnamespacestd;intmain(){inthour,minute;charnoon;cout<<"Pleaseinputatimein24-hourformat:"<>hour;if(hour<0
3、
4、hour>23){cout<<"Theinputhouriswrong!"
5、<12){hour=hour-12;noon='p';}elsenoon='a';cout<<"minute:";cin>>minute;if(minute<0
6、
7、minute>59){cout<<"Theinputminuteiswrong!"<8、secout<<"am"<usingnamespacestd;intmain(){charc;for(c='a';c<='z';c++)cout<='a';c--)cout<9、iostream>usingnamespacestd;intmain(){unsignedintgzint;intcount=0;while(1){cout<<"Pleaseinputaninteger(greaterthanzero):"<>gzint;if(gzint<=0)cout<<"Yourinputiswrong!Pleaseinputagain..."<10、t<<"Thenumberofdigitsintheintergeris:"<usingnamespacestd;intmain(){intcount=0;charch;cout<<"Pleaseinputanexpression:"<>ch;ch!='#';cin>>ch){if(ch11、=='(')count++;elseif(ch==')')count--;}if(count==0)cout<<"配对!"<0)cout<<"多左括号!"<=”进行计数。解:#includeusingnamespacestd;intmain(){intcount=0;charch1=' ',ch2;cout<12、<"Pleaseinputastring(terminatedwith#):"<>ch2;ch2!='#';cin>>ch2){if(ch2=='='&&ch1=='>')count++;ch1=ch2;}cout<<"Numberof>=:"<
8、secout<<"am"<usingnamespacestd;intmain(){charc;for(c='a';c<='z';c++)cout<='a';c--)cout<9、iostream>usingnamespacestd;intmain(){unsignedintgzint;intcount=0;while(1){cout<<"Pleaseinputaninteger(greaterthanzero):"<>gzint;if(gzint<=0)cout<<"Yourinputiswrong!Pleaseinputagain..."<10、t<<"Thenumberofdigitsintheintergeris:"<usingnamespacestd;intmain(){intcount=0;charch;cout<<"Pleaseinputanexpression:"<>ch;ch!='#';cin>>ch){if(ch11、=='(')count++;elseif(ch==')')count--;}if(count==0)cout<<"配对!"<0)cout<<"多左括号!"<=”进行计数。解:#includeusingnamespacestd;intmain(){intcount=0;charch1=' ',ch2;cout<12、<"Pleaseinputastring(terminatedwith#):"<>ch2;ch2!='#';cin>>ch2){if(ch2=='='&&ch1=='>')count++;ch1=ch2;}cout<<"Numberof>=:"<
9、iostream>usingnamespacestd;intmain(){unsignedintgzint;intcount=0;while(1){cout<<"Pleaseinputaninteger(greaterthanzero):"<>gzint;if(gzint<=0)cout<<"Yourinputiswrong!Pleaseinputagain..."<10、t<<"Thenumberofdigitsintheintergeris:"<usingnamespacestd;intmain(){intcount=0;charch;cout<<"Pleaseinputanexpression:"<>ch;ch!='#';cin>>ch){if(ch11、=='(')count++;elseif(ch==')')count--;}if(count==0)cout<<"配对!"<0)cout<<"多左括号!"<=”进行计数。解:#includeusingnamespacestd;intmain(){intcount=0;charch1=' ',ch2;cout<12、<"Pleaseinputastring(terminatedwith#):"<>ch2;ch2!='#';cin>>ch2){if(ch2=='='&&ch1=='>')count++;ch1=ch2;}cout<<"Numberof>=:"<
10、t<<"Thenumberofdigitsintheintergeris:"<usingnamespacestd;intmain(){intcount=0;charch;cout<<"Pleaseinputanexpression:"<>ch;ch!='#';cin>>ch){if(ch
11、=='(')count++;elseif(ch==')')count--;}if(count==0)cout<<"配对!"<0)cout<<"多左括号!"<=”进行计数。解:#includeusingnamespacestd;intmain(){intcount=0;charch1=' ',ch2;cout<
12、<"Pleaseinputastring(terminatedwith#):"<>ch2;ch2!='#';cin>>ch2){if(ch2=='='&&ch1=='>')count++;ch1=ch2;}cout<<"Numberof>=:"<
此文档下载收益归作者所有