《C++程序设计第五章答案.docx》由会员分享,可在线阅读,更多相关《C++程序设计第五章答案.docx(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、C+程序设计第五章答案 C/C+程序设计实验报告 实验五 练习5.2 代码如下: #include using namespace std; /display the numbers 逆序 int sumDigits(long n) int i,sum=0; while(n0) i = n % 10; n = n / 10; sum = sum + i; return sum; int main() couta; /use the function sumDigits cout0) i = number % 10; number = number / 10; sum = sum*10 + i;
2、 return sum; int main() cout a; /use the function int reverse cout=1;num-) coutinteger; /use the function displayPattern displayPattern(integer); return 0; 练习5.8 代码如下: /以下是头文件 double cTF(double c) double F = (9.0/5)*c + 32; return F; double fTC(double f) double C = (f - 32)/(9.0/5); return C; /以下是ma
3、in函数 #include #include #include tou.h using namespace std; int main() cout = 31) cout 0) cout ch1 ch2 numberPerLine; printChars(ch1, ch2, numberPerLine); return 0; 练习5.14 代码如下: #include using namespace std; /this is a function that used to compute the 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 .) double m(int i
4、) double sum = 1.0; int j = 1; while (j i; if ( 2 i ) cout = i); /display the result. cout s1 s2 s3; /used the function isV alid to judge a triangle can be formed or not. if (isV alid(s1,s2,s3) cout the area of your triangle is area(s1, s2, s3) endl; else cout your entered can not form a triangle.n;
5、 system(pause); return 0; 练习5.22 代码如下:#include #include #include using namespace std; int main() /set a time seed srand( time(0) ); int sum = 0, powSum = 0; /compute the sum of ten random numbers and the sum of per numbers pow for (int i = 1; i = 10; i+) sum += rand()%1001; powSum += (rand()%1001) *
6、 (rand()%1001); /display the result cout the average of ten random numbers is sum/10 endl; cout the deviation of ten random number is sqrt( (powSum - (sum*sum)/10.0)/9.0) endl; system(pause); return 0; 练习5.24 代码如下: #include #include using namespace std; /the function isRunnian is to judge a year is runnian or not . bool isRunnian (int year) bool runnian = false; if ( ( ( 0 = year%4 ) & ( 0 != year%100) ) | ( 0 = year % 400 ) ) runnian = true; return runnian;