

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、海姓名程序设计基础( 1)选择填空:(每题 1分,共 10分)C+语言语法的表达式是(( C ) break海姓名程序设计基础( 1)选择填空:(每题 1分,共 10分)C+语言语法的表达式是(( C ) break和 continue 说法正确的是 ( B :continue 不是a的第一个元素p ( B )。B ios:out|ios:trunc 交 通 大 学 试 卷( A 卷)成绩D )C ios:in | ios:out D 没有上( 2017 至 2018 学年 第_1学期 )班级号_ 学号_ 课程名称一、1 已知各变量的类型说明如下:int a=1, b=2; double x=
2、1.42; 则以下不符合A b=xa?3:4; B x+=b; C x=(a=2, a+b); D x%3; 2下面的循环体哪个执行的次数与其他不同A for(i=0; i10; i+) couti=1; i-) couti ; C i=10; do couti0); D i=0; while(+i=10) couti ; 3 C+语言的跳转语句中,对于A break 语句只应用于循环体中B continue 语句只应用于循环体中C break 是无条件跳转语句,D break 和continue 的跳转范围不够明确,容易产生问题4 有如下定义语句: int a=1,2,3,4,5; ,则对语
3、句 int *p=a; 正确的描述是 ( B )。A 语句 int *p=a; 定义不正确B 语句 int *p=a; 初始化变量 p,使其指向数组对象C 语句 int *p=a; 是把 a0的值赋给变量 p D 语句 int *p=a; 是把 a1的值赋给变量5当使用 ofstream流类定义一个流对象并打开一个磁盘文件时,文件的隐含打开方式为A ios:in|ios:app A 卷 总15页 第 1 页a数组元素的是: ( D ) B ap-a ( C a数组元素的是: ( D ) B ap-a ( C )/A /B /C /D ( D ) A )C *a D a8 6 若有以下定义和语句
4、,则不能合法表示char a= abcdefg” ”;int *p=a; A p7 7 以下所列的各函数原型中,正确的是:A void play(int a, b); B void* play(int a, int b=1, double c); C int* play(int *a, int *b); D int* play(int a; int b ); 8 下列程序中错误的语句是: (B)#include #include using namespace std; main( ) char *pt1=1234; char pt2 =12; char *pt3=34; pt3=pt2; s
5、trcpy(pt1, pt2); strcpy(pt2, pt3); couta) coutaendl; i+; fin.close(); couti=iendl; 答案:打开一个文件,读出其中的字符串,并输出字符串到屏幕,每行一个,最后统计输出一共有多少个字符串在文件中2 写出运行结果#include stdafx.h #include using namespace std; main() int a3=1, 2 , 3, *p; int i, b6; p=a; b0=*p; b1=*p+; b2=*p; b3=*-p; b4=(*p)+1; A 卷 总15页 第 3 页上海交通大学试卷b
6、5=*(p+2); for(i=0; i6; i+) coutbi ; 答案: 1 1 2 1 2 3 3 写出运行结果:#include using namespace std; void foo( int &c) static a=1; c+=a+; void main() int a; int &b=a; a=10; foo(a); coutaendl; foo(+b); coutaendl; 答案:11 14 4 写出运行结果#include template T max(T x,int n); void main() int a=4,5,2,8,9,3; double b=3.5,6
7、.7,2,5.2,9.2; A 卷 总15页 第 4 页写出运行结果上海交通大学试卷写出运行结果coutmax of A: max(a,6)endl; coutmax of B: max(b,5)endl; template T max(T x,int n) int i; T maxv=x0; for(i=1;in;i+) if(maxvxi) maxv=xi; return maxv; 答案: max of A: 9 max of B: 9.2 5.#include using namespace std; int f(int n) int p; if (n=0) return 1; els
8、e if(n=1) return 1; else p= 2*f(n-1)+f(n-2); coutp ; return p; int main() f(4); coutendl; return 0; 答案: 3 7 3 17 A 卷 总15页 第 5 页5,第 25,第 2个输入为 2的程序运行结果6 写出输入为第一个输入为#include using namespace std; void DrawTriangle(int height, int location); void DrawRevTriangle(int height, int location); void DrawDiamo
9、nd(int height, int locatio); int main() int height, location; while(1) coutheight; if(height%2 != 0) break; coutlocation; DrawDiamond(height, location); return 0; void DrawDiamond(int height, int location) DrawTriangle(height+1)/2, location); DrawRevTriangle(height-1)/2, location+1); void DrawTriang
10、le(int height, int location) int i, j, k; for(i=0; iheight; i+) for(j = -location; jheight-i-1; j+) cout ; A 卷 总15页 第 6 页30 时的输出结果。30 时的输出结果。for(k = 0; ki*2+1; k+) cout*; coutendl; void DrawRevTriangle(int height, int location) int i, j, k; for(i=0; iheight; i+) for(j = -location; ji; j+) cout ; for
11、(k = 0; k2*(height-i)-1; k+) cout*; coutendl; 答案:* * * * * 7请写出下面的程序在输入为#include #include using namespace std; int main() int n; cout oct n; cout octal oct n in hexdecimal is: hex n n ; cout hexdecimal n in decimal is: dec n n ; cout setbase(8) octal n in octal is: n endl; return 0; A 卷 总15页 第 7 页12
12、345”时的输出结果。12345 ”abcba”。以下是判断一个字符串是否为回文的函数上海交通大学试卷12345”时的输出结果。12345 ”abcba”。以下是判断一个字符串是否为回文的函数答:Enter a octal number: 30 Octal 30 in hexdecimal is: 18 Hexdecimal 18 in decimal is: 24 Octal 30 in octal is: 308请写出下列程序的功能,以及当输入为“#include using namespace std; int main() int num = 0, i; char ch10; cin
13、ch; for (i = 0; chi != 0; +i) num = num * 10 + chi - 0; cout num endl; return 0; 答:将字符串表示的数字转换成整型数当输入为“ 12345”时,输出为三。程序填空(每空 2分,共 34分)1所谓的回文,就是正读和反读都一样的字符串,如的实现,请填空。bool f(char *s, int n) if (n = 1) _return true_; else if (s0=sn-1) _return_f(s+1, n-2)_; else _return false_; A 卷 总15页 第 8 页fill 有三个参数:
14、填入数字的初值、起始位置、矩阵规模。如要20212223247; data。请填空。1932333425818313635269173029282710161514131211fill 有三个参数:填入数字的初值、起始位置、矩阵规模。如要20212223247; data。请填空。19323334258183136352691730292827101615141312112如下是 数字旋转方阵的递归实现。函数填写下列矩阵,可调用 fill(1, 0, 6) 。请填空123456void fill( int number, int begin, int size) int i, row = be
15、gin, col = begin ; if (size = 0) return ; if (size = 1) pbeginbegin = number; return; prowcol = number; +number; for (i=0; isize-1; +i) +row; prowcol = number; +number; for (i=0; isize-1; +i) +col ; prowcol = number; +number; for (i=0; isize-1; +i) -row; prowcol = number; +number; for (i=0; isize-2;
16、 +i) -col ; prowcol = number; +number; fill(number, begin+1. size 2) 3下面程序是将终端输入的信息原式原样写入文件#include #include using namespace std; int main() char ch; ofstream out; A 卷 总15页 第 9 页“data”) ; 0个位置C+ how to program! “data”) ; 0个位置C+ how to program! 中将“ to”这个单词提取出来,并显示在屏幕上,其, 请补充完整。 out.open( while ( ( ch
17、 = cin.get() != EOF) out.put(ch); out.close() ; return 0; 4直接选择排序是一种常用的排序方法。它的思想是:在所有元素中找到最小的元素放在数组的第在剩余元素中找出最小的放在第一个位置。以此类推,直到所有元素都放在适当的位置请填空。int main( ) int lh, rh, k, tmp; int array = 2, 5, 1, 9, 10, 0, 4, 8, 7, 6; for (lh = 0; lh 10; lh+) rh = lh; for ( k = lh ; k 10; +k) if ( arrayk arrayrh ) r
18、h = k ; tmp = arraylh; arraylh = arrayrh ; arrayrh = tmp; for (lh =0; lh10; +lh) cout arraylh ; return 0; 5下面程序的功能是从字符串中每个空格部分需要填写一条语句或者表达式#include #include using namespace std; bool GetToken(char* str, char *token, int num, char delim); void main() char *str=C+ how to program!; char tok20; A 卷 总15页
19、 第 10 页N的值为多少,计算时ex 1 x2分x2 N的值为多少,计算时ex 1 x2分x2 x32! 3!.xnn!xnn!0.000001的值。(7分)if(GetToken(str, tok, 2, ) couttokendl; else couterror!endl; bool GetToken(char* str, char *token, int num, char delim) int start=0, end=0; int stringLen, tokenLen; int count=-1; int i; stringLen = strlen(str); for(i=0;
20、istringLen; i+) if(stri=delim) count += 1; end = i-1; if(count = num) tokenLen = end-start+1; strncpy(token, str+start, tokenLen); tokentokenLen = 0; return true; else start = end+2; return false; 四 编程题(共 24分)1编一程序计算数列: 1 2 + 3 4 + 5 6 + 的前 N项的和。要求不管间是一个常量。 (7 分)评判标准:用循环扣 2分2编一程序,计算评判标准:总体结构 2分用前一项的
21、值计算后一项的值:其他 3分A 卷 总15页 第 11 页0 或者0 或者 1 分别表3编写一个与计算机猜硬币的小游戏。计算机扔硬币后让用户猜正反面,用户输入示正面( HEAD )或者反面( TAIL )。每次用户猜后,计算机给出判断是否正确,游戏每轮采用三局两胜制判断最终胜负。每轮结束后用户可以选择是否再玩下去。每轮的胜负结果最后按如下格式保存在record.dat 文件中:(10分)Round 0 user win Round 1 user lose Round 2 user win 对应上面记录的游戏运行过程如下所示,你可以从中获得有关程序输入输出的要求。Round 0 Your gue
22、ss is(0 for HEAD, 1 for TAIL): 0 You are wrong Your guess is(0 for HEAD, 1 for TAIL): 1 You are right Your guess is(0 for HEAD, 1 for TAIL): 1 You are right You won this round! Play again? y Round 1 Your guess is(0 for HEAD, 1 for TAIL): 0 You are wrong Your guess is(0 for HEAD, 1 for TAIL): 0 You a
23、re wrong You lose this round Play again? y Round 2 Your guess is(0 for HEAD, 1 for TAIL): 0 You are right Your guess is(0 for HEAD, 1 for TAIL): 0 You are right You won this round! Play again? N 参考答案:#include #include #include #include A 卷 总15页 第 12 页/变量定义和函数定义/文件操作 2分/随机数操作 2/变量定义和函数定义/文件操作 2分/随机数操
24、作 2分/游戏流程控制 4分/输入输出控制1分1分using namespace std; bool GetYesOrNoResponse(char* prompt); char GetAGuess(); char ThrowCoin(); main() ofstream outfile(record.dat); int coin; int playtimes=0; int hit=0, miss=0; int i, round=0; srand(time(NULL); while(1) coutRound roundendl; hit=miss=0; for(i=0;i3;i+) coin = ThrowCoin(); if(coin=GetAGuess() coutYou are rightendl; hit+;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工作与生活两不误的策略
- 工作中的自律性对提高效率的影响
- 工业自动化发展趋势及技术革新
- 工业设计的美学与产品设计实践
- 工作效率提升技巧与策略
- 工业领域物联网环保应用研究
- 工作模式变革下的组织文化创新
- 工作流程优化与持续改进方法
- 工厂安全生产与应急预案制作
- 工程建筑中岩土工程的实践应用
- 网络安全项目授权委托书范本
- (高清版)DB43∕T 2428-2022 水利工程管理与保护范围划定技术规范
- 个人诚信承诺书模板(共4篇)
- 供水公司自来水厂隐患排查和风险分级管控管理制度
- 企业一季一课安全教育内容
- 《乘风破浪扬帆起航》高三主题班会 高中主题班会
- GB/T 44543-2024预制混凝土节段拼装用环氧胶粘剂
- 附着式升降脚手架维护保养管理制度
- 四川凉山历年中考语文现代文之说明文阅读7篇(含答案)(2003-2023)
- 基本公共卫生服务项目糖尿病患者管理考核反馈表
- 山东省潍坊市2024-2025学年高二地理下学期期末考试试题
评论
0/150
提交评论