实验6函数与预处理1参考答案

上传人:大米 文档编号:431094194 上传时间:2023-10-13 格式:DOC 页数:10 大小:170KB
返回 下载 相关 举报
实验6函数与预处理1参考答案_第1页
第1页 / 共10页
实验6函数与预处理1参考答案_第2页
第2页 / 共10页
实验6函数与预处理1参考答案_第3页
第3页 / 共10页
实验6函数与预处理1参考答案_第4页
第4页 / 共10页
实验6函数与预处理1参考答案_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《实验6函数与预处理1参考答案》由会员分享,可在线阅读,更多相关《实验6函数与预处理1参考答案(10页珍藏版)》请在金锄头文库上搜索。

1、实验6函数与预处理1(2) 自测题一 编写函数,实现删除字符串中字符T的功能,要求使用内联函数#include#include using namespace std;inline char fun(char ch) /内联函数fun首部if (ch = T)return 0; /内联函数fun函数体,语句数量不限int main()char c;cout please input a string: endl;while (c = getchar() != n)if (fun(c) /对于if来说除了0之外都是1cout fun(c);cout n;return 0; 自测题二 编写重载函数

2、Max可分别求取两个整数,三个整数,两个双精度数,三个双精度数的最大值。#include using namespace std;int max(int a, int b)/*功能:求取两个整数的最大值,语句数量不限 */cout b ? a : b;int max(int a, int b, int c)/*求取三个整数的最大值,语句数量不限 */cout b ? (a c ? a : c ) : ( b c ? b : c);double max(double a, double b)/*求取两个双精度的最大值,语句数量不限 */cout b ? a : b;double max(doub

3、le a, double b, double c)/*求取三个双精度的最大值,语句数量不限 */cout b ? (a c ? a : c) : (b c ? b : c);int main()int a, b, c, d, e;double m, n, o, p, q;cout 请输入两个整数: a b;cout 最大值为 max(a, b) endl;cout 请输入三个整数: c d e;cout 最大值为 max(c, d, e) endl;cout 请输入两个双精度数: m n;cout 最大值为 max(m, n) endl;cout 请输入三个双精度数: o p q;cout 最

4、大值为 max(o, p, q) endl;return 0; 自测题三 设计一个打印年历的程序。要求:打印每个月的月历的功能有一个独立的函数完成,程序运行时,主程序通过若干次调用该函数完成年历的输出。注意处理闰年问题。#include #include using namespace std;void printmonth(int m); /5个被调函数声明void printhead(int m);int daysofmonth(int m);int isleap(int y);void firstday(int m);int year, weekday; /定义全局变量int main(

5、)while (true)cout year;cout nn;cout year 年n;for (int i = 1; i = 12; i+)printmonth(i);cout n;cout nn;return 0;void printmonth(int m) /*功能:1、调用printhead(m),输出月历首行形式并确定m月第1天的位置2、从第1天输出该月其它天的日历,注意换行控制格式*/int days;printhead(m);for (int i = 0; i (weekday + 1) % 7; i+)/控制是星期的输出 cout setw(4) ;days = daysofm

6、onth(m);/得到每月的天数 for (int i = 1; i = days; i+)cout setw(4) i;weekday = (weekday + 1) % 7;/为了配合星期的显示if (weekday = 6)cout endl endl;void printhead(int m) /*功能:1、输出月历首行形式: m 月 日 一 二 三 四 五 六2、确定m月第1天的位置,即对齐改天是星期几*/cout endl endl;cout m 月 endl;cout setw(4) 日 setw(4) 一 setw(4) 二 setw(4) 三 setw(4) 四 setw(4

7、) 五 setw(4) 六 endl;firstday(m);int daysofmonth(int m)/*功能:判断m月多少天,返回值是m月的天数,注意闰年*/switch (m)case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;/1,3,5,7,8,10,12,月每月有31天 case 4:case 6:case 9:case 11:return 30;/4,6,9,11月每月31天 case 2:if (isleap(year)return 29;/闰年每年二月29天 elsereturn 28;/非闰年二月28天

8、 default:return 30;int isleap(int y)/*功能:判断y是否为闰年,是返回1,否返回0,闰年判断表达式见上机指导P19 二、填空题第5题*/return (y % 4 = 0 & y % 100 != 0 | y % 400 = 0);void firstday(int m) /*功能:判断该月的第一天是星期几? 思路:这个问题需找一个参照点,如设2000年1月1日为参照点,这天是星期六,则可计算该月的第一天距离参照点有多少天,(注意闰年要多加1天)如2012年4月1日与2000年1月1日相差4474天,4474%7=1,所以2012年4月1日是星期日。返回值是

9、y月第1天是星期几*/long n;n = (year - 1) * 365;/从公元 1 年 1 月 1 日天始算, 到现在所输入年有多少天 for (int i = 1; i year; i+)if (isleap(i)n+;/闰年多一天 for (int i = 1; i m; i+)n += daysofmonth(i);weekday = n % 7; /返回星期数,0表示周一,6表示周日 自测题四 有一个8层灯塔,每层所点灯数都等于该层上一层的两倍,一共有765盏灯,求塔底的灯数。第(1)问跳过,要求:设计一个函数计算塔顶灯数为n盏时,8层灯的总灯数,并在主函数中调用。#inclu

10、de #include int calculate(int);using namespace std;int main()int top;cout top;cout 总灯数是: calculate(top) endl;return 0;int calculate(int top)int total = 0;for (int i = 0; i8; i+)total += pow(2, i) * top;return total; 自测练习五 数字反射。编写一个函数,接收一个整数值,返回这个数中数字逆序后的结果值。例如:给定数7631,函数返回1367.#includeusing namespac

11、e std;int main()int n, rn = 0;cout 输入一个整数值: n;dorn = 10 * rn + n % 10; while (n /= 10);cout 数字逆序后结果为: rn endl;return 0; 自测练习六 猜数字游戏编写一个程序,可以玩“猜数字”的游戏。具体描述如下:程序在11000之间的整数中随机选择需要猜的数。然后显示:I have a number between 1 and 1000.Can you guess my number?Please type your first guess.玩家于是输入猜想的第一个数。程序会做出如下响应之一:Excellent You guessed the number!Would you like to play again (y or n)?Too low. Try again.Too high. Try again.如果玩家的猜测是不正确的,程序应继续循环,直到玩家最终猜对为止。此过程中程序要一直提醒玩家是猜大了(Tool high)还是猜小了(Tool low),这样帮助玩家尽快获得正确的答案。#include#includevoid game();using namespace std;int main()game();char m;cin m;while (m = y

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 习题/试题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号