C++程序设计Y.Daniel Liang 第五章课后习题答案.docx_第1页
C++程序设计Y.Daniel Liang 第五章课后习题答案.docx_第2页
C++程序设计Y.Daniel Liang 第五章课后习题答案.docx_第3页
C++程序设计Y.Daniel Liang 第五章课后习题答案.docx_第4页
C++程序设计Y.Daniel Liang 第五章课后习题答案.docx_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Exercise5_2#include #include using namespace std;int sumDigits(long n) int temp = abs(n); int sum = 0; while (temp != 0) int remainder = temp % 10; sum += remainder; temp = temp / 10; return sum;int main() cout The sum of digits for 234 is sumDigits(234) endl; return 0;Exercise5_4#include using namespace std;int reverse(int number) int result = 0; while (number != 0) int remainder = number % 10; result = result * 10 + remainder; number = number / 10; return result;int main() cout reverse(12345) endl; return 0;Exercise5_6#include using namespace std;void displayPattern(int n) for (int row = 1; row = n; row+) / Print spaces for (int i = row; i n; i+) cout = 1; i-) cout i; cout endl; int main() displayPattern(7); return 0;Exercise5_8#include #include #include using namespace std;double celsiusToFahrenheit(double celsius) return (9.0 / 5.0) * celsius + 32;double fahrenheitToCelsius(double fahrenheit) return (5.0 / 9) * (fahrenheit - 32);int main() cout setw(12) Celsius setw(12) Fahrenheit setw(12) | setw(12) Fahrenheit setw(12) Celsius endl; cout - endl; double celsius = 40; double farenheit = 120; for (int i = 1; i = 10; celsius-, farenheit -= 10, i+) cout setw(12) celsius setw(12) celsiusToFahrenheit(celsius) setw(12) | setw(12) farenheit setw(12) setprecision(5) showpoint fahrenheitToCelsius(farenheit) endl; return 0;Exercise5_10#include #include #include using namespace std;int gcd(int m, int n) int gcd = 1; int k = 1; while (k = m & k = n) if (m % k = 0 & n % k = 0) gcd = k; k+; return gcd;int main() cout gcd(24, 16) endl; cout gcd(255, 25) endl; return 0;Exercise5_12#include #include #include using namespace std;void printChars(char ch1, char ch2, int numberPerLine) int count = 1; for (int i = ch1; i = ch2; i+, count+) if (count % numberPerLine = 0) cout (char)i endl; else cout (char)i;int main() printChars(1, Z, 10); return 0;Exercise5_14#include #include #include using namespace std;double m(int n) double pi = 0; double term; int sign = 1; for (int i = 1; i = n; i+) term = sign * 4.0 / (2 * i - 1); pi += term; sign = -1 * sign; return pi;int main() cout ittm(i) endl; for (int i = 2; i = 10000; i+) cout i tt m(i) endl; return 0;Exercise5_16#include #include #include using namespace std;bool isPrime(int num) if (num = 1) | (num = 2) return true; for (int i = 2; i = num / 2; i+) if (num % i = 0) return false; return true;int main() int count = 0; for (int i = 2; count 1000; i+) / Display each number in five positions if (isPrime(i) cout setw(5) i; count+; if (count % 10 = 0) cout endl; return 0;Exercise5_18#include #include RandomCharacter.husing namespace std;int main() for (int i = 0; i 100; i+) cout getRandomUpperCaseLetter(); if (i + 1) % 10 = 0) cout endl; cout endl; for (int i = 0; i 100; i+) cout getRandomDigitCharacter(); if (i + 1) % 10 = 0) cout endl; return 0;Exercise5_20#include #include #include using namespace std;bool isValid( double side1, double side2, double side3) return (side1 + side2 side3) & (side1 + side3 side2) & (side2 + side3 side1);double area( double side1, double side2, double side3) double s = (side1 + side2 + side3) / 2; return sqrt(s * (s - side1) * (s - side2) * (s - side3);int main() / Enter the first edge cout edge1; / Enter the second edge cout edge2; / Enter the third edge cout edge3; / Display results bool valid = (edge1 + edge2 edge3) & (edge1 + edge3 edge2) & (edge2 + edge3 edge1); if (valid) cout The are of the triangle is area(edge1, edge2, edge3) endl; else cout Input is invalid endl; return 0;Exercise5_21#include #include #include using namespace std;int main() cout setw(14) x setw(14) log(x) endl; cout - endl; for (int num = 1; num = 10; num+) cout setw(14) num setw(14) log(num * 1.0) endl; return 0;Exercise5_22#include #include #include using namespace std;int main() const int COUNT = 10; / Total numbers int number = 0; / Store a random number double sum = 0; / Store the sum of the numbers double squareSum = 0; / Store the sum of the squares / Create numbers, find its sum, and its square sum for (int i = 0; i COUNT; i+) / Generate a new random number number = rand(); cout number ; / Add the number to sum sum += number; / Add the square of the number to squareSum squareSum += pow(number * 1.0, 2); / Same as number*number; / Find mean double mean = sum / COUNT; / Find standard deviation double deviation = sqrt(squareSum - sum * sum / COUNT) / (COUNT - 1); / Display result cout nThe mean is mean endl; cout The standard deviation is deviation endl; return 0;Exercise5_24#include #include #include using namespace std;/* Determine if it is a leap year */bool isLeapYear(int year) if (year % 400 = 0) | (year % 4 = 0) & (year % 100 != 0) return true; return false;/* Get the total number of days from Jan 1, 1970 to the specified year */int getTotalDaysInYears(int year) int total = 0; / Get the total days from 1970 to the specified year for (int i = 1970; i = year; i+) if (isLeapYear(i) total = total + 366; else total = total + 365; return total;/* Get the number of days in a month */int getNumOfDaysInMonth(int year, int month) if (month = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10 | month = 12) return 31; if (month = 4 | month = 6 | month = 9 | month = 11) return 30; if (month = 2) if (isLeapYear(year) return 29; else return 28; return 0; / If month is incorrect./* Get the total number of days from Jan 1 to the month in the year */int getTotalDaysInMonths(int year, int month) int total = 0; / Add days from Jan to the month for (int i = 1; i 0) totalDays+; / Obtain Year int currentYear = 2000; do currentYear+; while (getTotalDaysInYears(currentYear) totalDays); / Obtain Month int totalNumOfDaysInTheYear = totalDays - getTotalDaysInYears(currentYear - 1); int currentMonth = 0; do currentMonth+; while (getTotalDaysInMonths(currentYear, currentMonth) totalNumOfDaysInTheYear); / Obtain Day int currentDay = totalNumOfDaysInTheYear - getTotalDaysInMonths(currentYear, currentMonth - 1); / Display results cout Current date and time is currentMonth / currentDay / currentYear currentHour : currentMinute : currentSecond GMT endl; return 0;Exercise5_26#include #include #include using namespace std;bool isPrime(int num) if (num = 1) | (num = 2) return true; for (int i = 2; i = num / 2; i+) if (num % i = 0) return false; return true;int reversal(int num) int result = 0; while

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论