




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、上海交通大学试卷 上 海 交 通 大 学 试 卷( a 卷) ( 2007 至 2008 学年 第_1学期 )班级号_ 学号_ 姓名 课程名称 程序设计基础(1) 成绩 一、 选择填空:(每题1分,共10分)1 已知各变量的类型说明如下: int a=1, b=2; double x=1.42; 则以下不符合c+语言语法的表达式是( d ) a b=x>a?3:4;b x+=b; c x=(a=2, a+b); d x%3; 2下面的循环体哪个执行的次数与其他不同( c )a for(i=0; i<10; i+) cout<<i<<" "
2、 b for(i=10; i>=1; i-) cout<<i<<" " c i=10; do cout<<i<<" " while(i->0);d i=0; while(+i<=10) cout<<i<<" " 3 c+语言的跳转语句中,对于break和continue说法正确的是( b ): a break语句只应用于循环体中 b continue语句只应用于循环体中 c break是无条件跳转语句,continue不是 d break和con
3、tinue的跳转范围不够明确,容易产生问题 4 有如下定义语句:int a=1,2,3,4,5;,则对语句int *p=a;正确的描述是( b )。a 语句 int *p=a;定义不正确b 语句 int *p=a;初始化变量p,使其指向数组对象a的第一个元素c 语句int *p=a; 是把a0的值赋给变量pd 语句int *p=a; 是把a1的值赋给变量p5当使用ofstream流类定义一个流对象并打开一个磁盘文件时,文件的隐含打开方式为( b )。a ios:in|ios:app b ios:out|ios:trunc c ios:in | ios:out d 没有6 若有以下定义和语句,则
4、不能合法表示a数组元素的是:( d ) char a=”abcdefg”;int *p=a; a p7 b ap-a c *a d a87 以下所列的各函数原型中,正确的是:( c ) 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<iostream>#include<cstring>using namespace std;main(
5、 )char *pt1="1234"char pt2 ="12"char *pt3="34"pt3=pt2; /astrcpy(pt1, pt2); /b strcpy(pt2, pt3); /ccout<<pt2; /d9 下列定义或者声明语句哪个是不正确的( d )a void* f(int a, int n); b void* (*f)(int a, int n); c void* (*f10)(int a, int n ); d void f (int a, int n) 10;10 下列语句正确的是( a )a
6、char a2=a; b char 3a=0;c char a ='abc'd char a= "0"二、 程序理解:(每题4分,共32分)1 写出下面函数的功能void foo(char* fname) /可把以fname所指字符串作为文件标识符的文件称为fname文件,/假定该文件中保存着一批字符串,每个字符串的长度均小于20。ifstream fin(fname);char a20;int i=0;while(fin>>a) cout<<a<<endl;i+;fin.close();cout<<"
7、;i="<<i<<endl;答案:打开一个文件,读出其中的字符串,并输出字符串到屏幕,每行一个,最后统计输出一共有多少个字符串在文件中2 写出运行结果#include "stdafx.h"#include<iostream>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;b5=*(p+2);for(i=0; i<6; i+)cout<<bi<<"
8、"答案: 1 1 2 1 2 33 写出运行结果:#include<iostream>using namespace std;void foo( int &c) static a=1; c+=a+;void main() int a; int &b=a; a=10; foo(a);cout<<a<<endl;foo(+b);cout<<a<<endl;答案:11144 写出运行结果#include<iostream.h> template <class t> t max(t x,int
9、 n); void main() int a=4,5,2,8,9,3; double b=3.5,6.7,2,5.2,9.2; cout<<"max of a: "<<max(a,6)<<endl; cout<<"max of b: "<<max(b,5)<<endl; template <class t> t max(t x,int n) int i; t maxv=x0; for(i=1;i<n;i+) if(maxv<xi) maxv=xi; retur
10、n maxv; 答案:max of a: 9max of b: 9.25. 写出运行结果#include<iostream>using namespace std;int f(int n) int p; if (n=0) return 1; else if(n=1) return 1; else p= 2*f(n-1)+f(n-2); cout<<p<<" " return p; int main() f(4); cout<<endl; return 0;答案:3 7 3 176 写出输入为第一个输入为5,第2个输入为2的程序
11、运行结果#include<iostream>using namespace std;void drawtriangle(int height, int location);void drawrevtriangle(int height, int location);void drawdiamond(int height, int locatio);int main()int height, location;while(1) cout<<"please input an odd number):n"cin>>height;if(heigh
12、t%2 != 0)break;cout<<"please input location:n"cin>>location;drawdiamond(height, location);return 0;void drawdiamond(int height, int location)drawtriangle(height+1)/2, location);drawrevtriangle(height-1)/2, location+1);void drawtriangle(int height, int location)int i, j, k;for(i
13、=0; i<height; i+)for(j = -location; j<height-i-1; j+) cout<<' 'for(k = 0; k<i*2+1; k+)cout<<'*'cout<<endl;void drawrevtriangle(int height, int location)int i, j, k;for(i=0; i<height; i+)for(j = -location; j<i; j+)cout<<' 'for(k = 0; k<
14、;2*(height-i)-1; k+) cout<<'*'cout<<endl;答案: * * * *7请写出下面的程序在输入为30时的输出结果。#include <iostream>#include <iomanip>using namespace std;int main()int n; cout << "enter a octal number: " cin >> oct >> n; cout << "octal " <<
15、oct << n << " in hexdecimal is:" << hex << n << 'n' ; cout << "hexdecimal " << n << " in decimal is:" << dec << n << 'n' ; cout << setbase(8) << "octal " << n &
16、lt;<" in octal is:" << n << endl; return 0;答:enter a octal number: 30octal 30 in hexdecimal is: 18hexdecimal 18 in decimal is: 24octal 30 in octal is: 308请写出下列程序的功能,以及当输入为“12345”时的输出结果。#include <iostream>using namespace std;int main()int num = 0, i; char ch10; cin >
17、> ch; for (i = 0; chi != '0' +i) num = num * 10 + chi - '0' cout << num << endl; return 0; 答:将字符串表示的数字转换成整型数当输入为“12345”时,输出为12345三。程序填空(每空2分,共34分)1所谓的回文,就是正读和反读都一样的字符串,如”abcba”。以下是判断一个字符串是否为回文的函数的实现,请填空。bool f(char *s, int n) if (n <= 1)_return true_; else if (s0=sn
18、-1) _return_f(s+1, n-2)_; else_return false_; 2如下是数字旋转方阵的递归实现。函数fill有三个参数:填入数字的初值、起始位置、矩阵规模。如要填写下列矩阵,可调用fill(1, 0, 6)。请填空120191817162213231301532233362914423343528135242526271267891011void fill( int number, int begin, int size) int i, row = begin, col = begin ; if (size = 0) return ; if (size = 1) p
19、beginbegin = number; return; prowcol = number; +number; for (i=0; i<size-1; +i) +row; prowcol = number; +number; for (i=0; i<size-1; +i) +col ; prowcol = number; +number; for (i=0; i<size-1; +i) -row; prowcol = number; +number; for (i=0; i<size-2; +i) -col ; prowcol = number; +number; fi
20、ll(number, begin+1. size 2) ;3下面程序是将终端输入的信息原式原样写入文件data。请填空。#include <iostream>#include <fstream> using namespace std;int main()char ch; ofstream out; out.open(“data”) ; while ( ( ch = cin.get() != eof) out.put(ch); out.close() ; return 0; 4直接选择排序是一种常用的排序方法。它的思想是: l 在所有元素中找到最小的元素放在数组的第0个
21、位置l 在剩余元素中找出最小的放在第一个位置。以此类推,直到所有元素都放在适当的位置请填空。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 ) rh = k ; tmp = arraylh; arraylh = arrayrh ; arrayrh = tmp; for (lh =0; lh<10; +l
22、h) cout << arraylh << ' ' return 0;5下面程序的功能是从字符串"c+ how to program!"中将“to”这个单词提取出来,并显示在屏幕上,其中每个空格部分需要填写一条语句或者表达式,请补充完整。#include<iostream>#include<cstring>using namespace std;bool gettoken(char* str, char *token, int num, char delim);void main()char *str=&quo
23、t;c+ how to program!"char tok20;if(gettoken(str, tok, 2, ' ') cout<<tok<<endl;elsecout<<"error!"<<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=
24、0; i<stringlen; 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;elsestart = end+2;return false;四 编程题(共24分)1 编一程序计算数列:1 2 + 3 4 + 5 6 +的前n项的和。要求不管n的值为多少,计算时间是一个常量。(7分)评判标准:用循环扣2分2编一程序,计算 的值。(7分)评
25、判标准:总体结构2分 用前一项的值计算后一项的值:2分 其他3分3编写一个与计算机猜硬币的小游戏。计算机扔硬币后让用户猜正反面,用户输入0或者1分别表示正面(head)或者反面(tail)。每次用户猜后,计算机给出判断是否正确,游戏每轮采用三局两胜制判断最终胜负。每轮结束后用户可以选择是否再玩下去。每轮的胜负结果最后按如下格式保存在record.dat文件中:(10分)round 0 user winround 1 user loseround 2 user win对应上面记录的游戏运行过程如下所示,你可以从中获得有关程序输入输出的要求。round 0your guess is(0 for h
26、ead, 1 for tail): 0you are wrongyour guess is(0 for head, 1 for tail): 1you are rightyour guess is(0 for head, 1 for tail): 1you are rightyou won this round!play again? yround 1your guess is(0 for head, 1 for tail): 0you are wrongyour guess is(0 for head, 1 for tail): 0you are wrongyou lose this rou
27、ndplay again? yround 2your guess is(0 for head, 1 for tail): 0you are rightyour guess is(0 for head, 1 for tail): 0you are rightyou won this round!play again? n参考答案:#include<iostream>#include<cstdlib>#include<ctime>#include<fstream>using namespace std;bool getyesornoresponse(
28、char* prompt); /变量定义和函数定义1分char getaguess();char throwcoin();main()ofstream outfile("record.dat"); /文件操作2分int coin;int playtimes=0;int hit=0, miss=0;int i, round=0;srand(time(null); /随机数操作2分while(1)cout<<"round "<<round<<endl;hit=miss=0;for(i=0;i<3;i+) /游戏流程控
29、制4分coin = throwcoin();if(coin=getaguess()cout<<"you are right"<<endl;hit+;elsecout<<"you are wrong"<<endl;miss+;if(miss=2|hit=2) break;cout<<(hit=2)?"you won this round!":"you lose this round")<<endl; /输入输出控制1分outfile<<"round "
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中医诊断试题及答案
- 学海大联考 2025年化学高二第二学期期末复习检测模拟试题含解析
- 重庆市两江育才中学2025届高二下数学期末考试模拟试题含解析
- 浙江省丽水地区四校 2108-2025年化学高二下期末联考试题含解析
- 云南省河口县民中2025年高二化学第二学期期末质量检测模拟试题含解析
- 文化艺术展览宣传册定制设计合同
- 海外务工人员权益保障合同
- 车贷贷款逾期罚息及还款条件变更合同
- 儿科护士辞职报告集锦(30篇)
- 供应商安全合同(6篇)
- 公司“三基”工作检查评比细则(抢维修管理)
- 分布式系统复习题与答案
- 甘肃水资源概况
- 压力弹簧力度计算器及计算公式
- (3)-小儿推拿促生长的诊疗思路及手法演示
- 唐宋名家词智慧树知到答案章节测试2023年河南大学
- 超星学习通《汉书》导读(中国人民大学)章节测试答案
- 2023-2024学年浙江省衢州市初中语文七年级下册期末提升试题
- GB/T 7735-2016无缝和焊接(埋弧焊除外)钢管缺欠的自动涡流检测
- GB/T 19879-2005建筑结构用钢板
- GB 6944-2005危险货物分类和品名编号
评论
0/150
提交评论