高级语言期末考试试卷_第1页
高级语言期末考试试卷_第2页
高级语言期末考试试卷_第3页
高级语言期末考试试卷_第4页
高级语言期末考试试卷_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

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

文档简介

高级程序设计语言 A 卷答案 1 15 分 试编写一个 C 程序实现求两个数的最大公约数和最小公倍数 要求两个数字必须用随机数生成 答案 include include using namespace std int gcd int x int y int r while x y 0 r x y x y y r return y int lcm int x int y return x y gcd x y void main srand time 0 int a rand b rand cout 输出两个正整数 a t b cout 最大公约数为 gcd a b endl 最小公倍数为 lcm a b y x x y else if y x y y x return y 2 15 分 编写递归函数实现如下函数 答案 include using namespace std double P int n int x if n 0 return 1 else if n 1 return x else if n 1 return 2 n 1 x P n 1 x n 1 P n 2 x n void main int x y cin x y cout P x y endl 3 10 分 编写一函数 int SubStrNum char str char substr 实现统计字 符串 substr 在字符串 str 中出现的次数 答案 include using namespace std int SubStrNum char str char substr int Num 0 h1 h2 p1 0 p2 h1 strlen str h2 strlen substr while p1 h1 p2 0 while str p1 substr p2 if p1 h1 cin substr cout SubStrNum str substr endl 4 15 分 已有一个数组 现在输入一个数 查找数组中是否有这个数 如 果有 把这个元素删除掉 要求 1 查找使用如下函数 5 分 int find int a int n int key int i for i 0 i n i if a i key return i return 1 2 屏幕对话应出现以下格式的画面 5 分 输入 n 5 输入 n 个数 2 6 3 7 9 输入要删除的数 6 删除以后数组变为 2 3 7 9 答案 include using namespace std define N 100 int find int a int n int key int i for i 0 i n i if a i key return i return 1 void main int key n i index a N cout n cout endl cout 输入 n 个数 for i 0 i a i cout endl cout key cout endl index find a n key if index 1 cout 这个数不存在 endl else for int j index j n j a j a j 1 n cout 删除以后数组变为 endl for i 0 i n i cout a i t 5 15 分 有 5 个学生 每个学生的数据包括学号 姓名 3 门课的成绩 从键盘输入 5 个学生数据 并打印出各科的总分和平均分 以及平均成绩 最高的学生的数据 包括学号 姓名 3 门课的成绩 平均分数 要求用 结构体表示学生的基本信息 答案 include using namespace std define M 100 double sum M struct Student char num 11 char name 20 float score1 float score2 float score3 void Input Student s int n for int i 0 i n i cout s i num s i name s i score1 s i score2 s i score3 if s i score1 0 else break cout endl void Output Student s int n int t 0 ss 0 s1 0 s2 0 s3 0 for int i 0 iss ss sum i cout 第一门平均成绩为 s1 n n 第二门平均成绩为 s2 n n 第三门平均成绩为 s3 n endl for i 0 i n i if sum i ss cout 最高分学生是 s i name t 平均分是 ss 3 0 endl void main Student S M Input S 10 Output S 10 6 10 分 设计一个程序 利用友元函数计算平面上两点之间的距离 根据 给定的类定义实现其友元函数 并用主函数进行测试 提示 Point 定义 class Point public Point int xx 0 int yy 0 X xx Y yy void Display cout X t Y endl friend float dis Point private int X Y 答案 include include using namespace std class Point public Point int xx 0 int yy 0 X xx Y yy void Display cout X t Y endl friend float dis Point private int X Y float dis Point void main Point A 1 2 B 3 5 cout 两点之间距离为 dis A B endl 7 20 分 设计一个用于人数管理的 People 人员 类 由于考虑到通用 性 这里只抽象出所有类型人员都具有的属性 number 编号 sex 性别 birthday 出生日期 id 身份证号 其中 出生日期 声明为一个已给 定的 日期类 类型 身份证号为 18 位 要求包括 1 利用给定的条件 将 People 类定义完整 4 分 2 在类外实现对人员信息的录入和显示 8 分 3 在类定义中添加一个析构函数 2 分 4 在类外实现成员函数的功能 6 分 提示 include include using namespace std class Date 日期类 private int year month day public Date 默认构造 Date int y int m int d 带参构造 year y month m day d void set cin year month day void display 显示函数 cout year 年 month 月 day 日 class Person 人员类 private 此部分填写类的数据成员此部分填写类的数据成员 public Person 默认构造 Person int n int y int m int d char id 18 char s m birthday y m d Person Person void input void output 主函数如下 void main Person p1 p1 input p1 output cout endl Person p2 5 2001 3 21m p2 output 答案 include include using namespace std class Date 日期类 private int year month day public Date 默认构造 Date int y int m int d 带参构造 year y month m day d void set cin year month day void display 显示函数 cout year 年 month 月 day 日 class Person 人员类 private int num char sex Date birthday char ID 18 public Person 默认构造 Person int n int y int m int d char id 18 char s m birthday y m d 有默认 值的带参构造 num n sex s strcpy ID id Person Person sex p sex birthday p birthday strcpy ID p ID void input 输入函数 cout 录入数据 endl cout num cout sex cout 生日 birthday set cout ID ID 18 0 cout endl void output 输出函数

温馨提示

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

评论

0/150

提交评论