好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

双语版C++程序设计(第2版)练习题及答案.doc

100页
  • 卖家[上传人]:sat****105
  • 文档编号:614937197
  • 上传时间:2025-09-16
  • 文档格式:DOC
  • 文档大小:1.18MB
  • / 100 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • Chapter Three Keyboard Input and Screen Output Exercises1. Write a program to input four numbers and display them in reverse order.#include int main(){printf("Please input four numbers:"); int a,b,c,d; scanf("%d%d%d%d",&d,&c,&b,&a); printf("%d %d %d %d\n",a,b,c,d); return 0;}2. Write a program that inputs a number of hours and displays the equivalent number of weeks, days and hours. For example, an input of 553 should display 3 weeks, 2 days and 1 hour.#include int main(){ int hours; int week,day,hourLeft; printf("Please input the hours:"); scanf("%d",&hours); week=hours/24/7; day=hours/24%7; hourLeft=hours-hours/24*24; printf("%d weeks,%d days,%d hours\n",week,day,hourLeft); return 0;}3. Assuming the human heart rate is seventy-five beats per minute; write a program to ask a user their age in years and to calculate the number of beats their heart has made so far in their life. Ignore leap years.(leap years:闰年)#include int main(){ int years; printf("Please input your age:"); scanf("%d",&years); printf("the number of beats your heart has made so far in your life is %d\n",years*75*60*24*365); return 0;}4. Write a program to accept a temperature in degrees Fahrenheit and convert it to degrees Celsius. Your program should display the following prompt:Enter a temperature in degrees Fahrenheit:You will then enter a decimal number followed by the Enter key.The program will then convert the temperature by using the formulaCelsius = (Fahrenheit - 32.0 ) * (5.0 / 9.0)Your program should then display the temperature in degrees Celsius using an appropriate message.#include int main(){ double FahrenheitT; printf("Enter a temperature in degrees Fahrenheit:"); scanf("%lf",&FahrenheitT); double CelsiusT= (FahrenheitT - 32.0 ) * (5.0 / 9.0); printf("the temperature in degrees Celsius is:%lf\n",CelsiusT); return 0;}5. Make changes to the program in exercise 4 to accept the temperature in degrees Celsius and convert it to degrees Fahrenheit.(Fahrenheit:华氏温度;Celsius:摄氏温度)#include int main(){ double CelsiusT; printf("Enter a temperature in degrees Celsius:"); scanf("%lf",&CelsiusT); double FahrenheitT=9.0*CelsiusT/5.0+32; printf("the temperature in degrees Fahrenheit is:%lf\n",FahrenheitT); return 0;}6. Write a program to accept a distance in kilometres and display the equivalent distance in miles. (1 mile =1.609344 kilometres.)#include int main(){ double kilometres; printf("Enter a distance in kilometres:"); scanf("%lf",&kilometres); double miles=kilometres/1.609344; printf("the equivalent distance in miles:%lf\n",miles); return 0;}7. Write a program to input three floating-point numbers from the keyboard and to calculate(a) their sum and(b) their averageDisplay the results to three decimal places.#include int main(){ float a,b,c; float sum,avg; printf("Please input three floating-point numbers:\n"); scanf("%f%f%f",&a,&b,&c); sum=a+b+c; avg=sum/3; printf("their sum is %.3f and their average is: %.3f\n",sum,avg); return 0;}8. Write a program to read in two numbers from the keyboard and to display the result of dividing the second number into the first.For example, if the input is 123 and 12, the result should be displayed in the following format:123 divided by 12 = 10 Remainder = 3(Hint: use the modulus operator % to get the remainder 3, and use integer division to get the quotient 123.)(modulus operator:求余运算符)#include int main(){ int a,b; printf("Please input two int number:\n"); scanf("%d%d",&a,&b); printf("%d divided by %d = %d Remainder = %d\n",a,b,a/b,a%b); return 0;}Chapter Four Selection and Iteration Exercises1. Rewrite the following if-else using a switch statement:if ( marriage_status == 'S' )cout << "single" ;else if ( marriage_status == 'M' )cout << "married" ;else if ( marriage_status == 'W' )cout << "widowed" ;else if ( marriage_status == 'E' )cout << "separated" ;else if ( marriage_status == 'D' )cout << "divorced" ;elsecout << "error: invalid code" ;解答:switch (marriage_status){case ‘S’:cout << "single" ;break;case ‘M’: cout << "married" ;break;case ‘W’: cout << "widowed" ;break;case ‘E’: cout << "separated" ;break;case ‘D’:cout << "divorced" ;break;default:cout << "error: invalid code" ;}2. The following program segment displays an appropriate message depending on the values of three integers: n1, n2, and n3.if ( n1== n2 )if (n1 == n3 )cout << "n1, n2 and n3 have the same value" << endl ;elsecout << "n1 and n2 have the same value" << endl ;else if ( n1 == n3 )cout << "n1 and n3 have the same value" << endl ;else if ( n2 == n3 )cout << "n2 and n3 have the same value" << endl ;elsecout << "n1, n2 and 。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.