尔雅C++程序设计基础答案(学习通2023课后作业答案)

尔雅C++程序设计基础答案(学习通2023课后作业答案)

第1章——初识C++语言:从认识变量和常量开始,尔雅数据的程础答表示

第一章单元测试

1、以下关于C++语言描述错误的序设习通是()
A、一个C++程序总是计基从main函数开始执行
B、每个语句和数据声明的案学最后必须有一个分号
C、C++语言的课后注释符是以“/*”开始并以“*/”结束的
D、一个C++程序可以包含多个main函数

2、作业C++ 语言源程序文件名后缀为( )。答案
A、尔雅.EXE
B、程础答.OBJ
C、序设习通.cpp
D、计基.ASM

3、案学C++语言是课后( )
A、机器语言
B、作业汇编语言
C、仅具有面向对象特征的语言
D、既表现面向对象的特征,又表现面向过程的特征

4、C++语言中普通整型变量int在内存中占( )字节。
A、1
B、2
C、3
D、4

5、下列不是C++语言基本数据类型的是 ( )。
A、字符型
B、整型
C、浮点型
D、结构体

6、各种基本数据类型的存储空间正确的长度排列为( )。
A、int < char <double
B、double <int<char
C、char<int <double
D、int <=char<double

7、下面的变量说明中( )是正确的。
A、char:a, b, c;
B、char a; b; c;
C、char a, b, c;
D、char a, b, c,

8、转义字符“反斜杠线”的表示方法正确的是( ) 。
A、\
B、\\
C、‘\’
D、“\”

9、在C++语言中,自定义的标识符( )。
A、能使用关键字并且不区分大小写
B、不能使用关键字并且不区分大小写
C、能使用关键字并且区分大小写
D、不能使用关键字并且区分大小写

10、存储字符串“a+b=12\n\t”所占存储空间的字节数为( )  
A、8
B、9
C、10
D、11

第一章编程题

1、输出由输入的字符组成的如下形状的图形。

2、信息的输出

第2章——计算:从数据运算开始,数据简单运算

第二章单元测试

1、执行下面程序段的输出结果是( )。 int x=23, y=5,z; z=2+(y++,x+8,x++); cout<<x<<" "<<z<<endl;
A、24 25
B、24 26
C、25 26
D、25 25

2、若int型变量x=29,double型变量y=6.0,则表达式x/y的值为( ) 。
A、4.83333
B、4
C、5
D、5.0

3、判断字符型变量ch是否为大写英文字母,应使用表达式( )。
A、ch>='A' & ch<='Z'
B、ch<='A' ||ch>='Z'
C、'A'<=ch<='Z'
D、ch>='A' && ch<='Z'

4、int x=5, y=6;下列表达式结果为真的是( ) 。
A、x==y--
B、x==++y
C、x++==y
D、x==--y

5、设有a、b、c、d、m、n均为int型变量,且a=5、b=6、c=7、d=8、m=2、n=2, 则逻辑表达式(m=a>b)&&(n=c>d)运算后,n的值为( )
A、0
B、1
C、2
D、3

6、执行下列语句后,输出的结果是( ). int a=3,b=5; double c=b/a; cout<<c<<endl;
A、1.66666
B、1
C、1.0
D、1.7

7、已知int x=5,y=5,z=5;执行语句x%=y+z;后,x的值是( )。
A、5
B、6
C、0
D、1

8、逗号表达式a=3,a++,a+=6的值是( )。
A、10
B、9
C、11
D、12

9、已知下列语句中的x和y都是int型变量,其中错误的语句( )。
A、x=y++;
B、x=++y;
C、(x+y)++;
D、++x==y;

10、执行以下代码后,变量x、y、z的值分别为() int x=1,z=1,y=1,k; k=x++||++y&&++z
A、2 1 1
B、2 2 2
C、1 2 2
D、1 1 2

第二章编程题

1、计算表达式y=2^n+1 的值

2、2.根据输入的字符输出对应字符的Ascii 值。

3、将一个正整数逆序输出。

第3章——分支结构:无处不在的抉择

第三章单元测试

1、执行语句序列 int n; cin >> n; switch(n++) { case 1: case 2: cout << '1'; case 3: case 4: cout << '2'; break; default: cout << '3'; } 时,若键盘输入1,则屏幕显示( )。
A、1
B、2
C、23
D、12

2、下面程序的输出结果是( ). #include<iostream> using namespace std; int main() { int a=2,b=-1,c=2; if (a<b) if (b<0) c=0; else c+=1; cout <<c<<endl; return 0; }
A、0
B、1
C、2
D、3

3、为了避免嵌套的if-else语句的二义性,C++语言规定else总是与( )组成配对关系。
A、缩排位置相同的if
B、在其之前未配对的if
C、其之前未配对的最近的if
D、同一行上的if

4、与表达式if(!k)等价的是( )。
A、if(k==0)
B、if(k!=1)
C、if(-k)
D、if(k!=0)

5、以下代码执行后,a、b、c的值分别为( )。 int a=8,b=10,c=3; if (a>b) c=a; a=b; b=c;
A、10 3 3
B、8 10 3
C、10 8 8
D、10 10 8

6、下列描述正确的是( ) 。
A、表示m>n1或m<n2 的表达式为m>n1&&m<n2
B、switch语句结构中必须有default语句
C、if语句结构中必须有else语句
D、如果至少有一个操作数为true,则包含"||"运算符的表达式为true

7、下面程序的运行结果是( ). #include<iostream> using namespace std; int main( ) { int i=10; switch (i) { case 9:i++; case 10:++i; case 11:i++; default:i=i+1; } cout<<i<<endl; return 0; }
A、9
B、10
C、11
D、13

8、若int a=6,则执行完下列语句后,a的值为( ). if (a>10) a-=5; a+=5;
A、1
B、6
C、10
D、11

9、#include<iostream> using namespace std; int main() { int a=3; if(a=5) cout<<a+1<<endl; else cout<<a<<endl; return 0; } 程序的输出是( )。
A、3
B、4
C、5
D、6

10、若x和y是程序中的两个整型变量,则下列if语句中正确的是( )。
A、if(x==0) y=1; else y=2;
B、if(x==0) then y=1 else y=2;
C、if(x==0) y=1 else y=2;
D、if x==0 y=1; else y=2;

第三章编程题

1、请编写程序根据快递商品的重量计算应付运费用。

2、编写程序,输入一个整数,判断该数是否是3或7的倍数,可分为4种情况输出。

3、编写程序,输入一门课程的成绩输出对应的等级。

第三章 练一练-----经典提高题---不计入MOOC总成绩---

1、计算天数

2、编写一个简单的计算器程序

3、求方程ax^2+bx+c=0的根。

第4章——循环结构:周而复始,求同存异

第四章单元测试

1、下面关于break语句的描述中,不正确的是( ).
A、break可以用于循环体内
B、break语句可以在for循环语句中出现多次
C、break语句可以在switch语句中出现多次
D、一个break语句就可以跳出多重循环

2、以下语句不正确的是( )。
A、语句for(i=0;;)表示无限循环
B、for(;;)表示无限循环
C、for( )也表示无限循环
D、while(1)表示无限循环

3、有如下程序:   #include<iostream>   using namespace std;  int main( ) {  int i ,f,f1=0,f2=1;   for( i=3;i<=6;i++) { f=f1+f2;   f1=f2; f2=f;  }  cout<<f<<endl; return 0; }  运行时的输出结果是( )
A、2
B、3
C、5
D、8

4、有如下程序段: int i=5; while (int i=0){ cout<<'*'; i--; } 运行时输出"*"的个数是( )。
A、0
B、1
C、5
D、无穷

5、执行完以下语句 int i=0; do i++; while(i*i<10); 时,do后面的循环体语句i++被执行的次数为 ( )
A、2
B、3
C、4
D、5

6、有如下循环语句: for(int i=50; i>20; i-=2) cout<<i<<','; 运行时循环体的执行次数是( )
A、14
B、15
C、30
D、27

7、有如下程序段: int i=1; while (1) { i++; if(i == 10) break; if(i%2 == 0) cout << '*'; } 执行这个程序段输出字符*的个数是( )
A、10
B、3
C、4
D、5

8、有如下程序: #include<iostream> using namespace std; int main( ){ int sum; for(int i=0; i<6; i+=3){ sum=i; for(int j = i; j<6; j++) sum+=j; } cout<<sum<<end1; return 0; } 运行时的输出结果是( )。
A、3
B、10
C、12
D、15

9、下面程序的运行结果是( )。 #include<iostream> using namespace std; void main() { int i,j,a=0; for(i=0;i<2;i++) { for (j=0; j<4; j++) { if (j%2) break; a++; } a++; } cout<<a<<endl; }
A、3
B、4
C、5
D、6

10、有如下程序段: for (int i=1;i<=50;i++){ if (i%3!=0) continue; else if(i%5!=0) continue; cout<<i<<","; } 执行这个程序段的输出是( )
A、15,30,45,
B、15,45,
C、15,30,
D、30,45,

第四章 编程题

1、求函数s(n)的值

2、求整数的数根

3、求和

第四章 练一练-----经典基础题---不计入MOOC总成绩---

1、求最大值、平均值

2、分解质因数

3、球队比赛

4、关于素数的问题

5、各位数之积大于各位数之和

第四章 练一练-----经典提高题---不计入MOOC总成绩---

1、计算1分钱、2分钱、5分钱组成1元钱的组合。 提示:用穷举法解

2、计算步法

3、4块砝码称重

4、用泰勒展开式求近似值

5、求其最大公约数和最小公倍数。

第5章——数组:实现算法的利器

第五章单元测试

1、要定义整型数组x,使之包括初值为0的三个元素,下列语句中错误的是( )
A、int x[3]={ 0,0,0};
B、int x[ ]=;
C、int x[3]=;
D、int x[ ]={ 0,0,0};

2、在C++语言中,二维数组元素在内存中的存放顺序是( )。
A、按行优先存放
B、按列优先存放
C、由用户自己定义
D、由编译器决定

3、在下述对C++语言字符数组的描述中,有错误的是( )。
A、字符数组可以存放字符串。
B、字符数组中的字符串可以进行整体输入输出。
C、可以在赋值语句中通过赋值运算符"="对字符数组整体赋值。
D、字符数组的下标从0开始。

4、以下数组的初始化,正确的是( )。
A、int a[]={ 1,2,3,4,5};
B、int a[4]={ 1,2,3,4,5};
C、int a[2]={ 1,2,3} ;
D、int a[][]={ 1,2,3,4};

5、下面关于数组的描述错误的是( )。
A、在C++语言中数组的名字就是该数组第一个元素的首地址。
B、长度为n的数组,下标的范围是0~n-1 。
C、数组的大小必须在编译时确定。
D、数组元素的个数在定义时可以缺省。

6、下面程序的运行结果是( )。 char c[5] = { 'a','b',' ','c', ' '}; cout<<c;
A、1012ACF0(数组c的首地址值)
B、ab
C、ab c
D、以上都不是

7、有如下程序段: char c[20]="examination"; c[4]=0; cout<<c<<endl; 执行这个程序段的输出是( )。
A、examination
B、exam
C、exa
D、exami

8、以下程序运行后的输出结果是( )。 int main( ) { char a[]="abbcabbcabbc"; int i =0,j=0,k=0; while(a[i]) { if(a[i] =='a') j++; if(a[i]=='b') k++; i++; } cout<<j<<" "<<k<<endl; return 0; }
A、2 6
B、3 5
C、4 7
D、3 6

9、已知: int i,x[3][3] = { 1,2,3,4,5,6,7,8,9}; 则下面语句的输出结果是( ). for(i = 0;i < 3;i ++) cout<<x[i][2-i]<<" ";
A、1 5 9
B、1 4 7
C、3 5 7
D、3 6 9

10、下面程序的运行结果是( ). #include <iostream> #include <cstring> using namespace std; int main( ) { char s1[10]="abc"; char s2[20]="inter"; int k=0,j=0; while (s2[k]) k++; while(s1[j]) s2[k--]=s1[j++]; cout<<s2<<endl; return 0; }
A、inter
B、abc
C、interabc
D、intcba

第五章 编程题

1、字符转换

2、在一堆数据中,查找特殊数据及其所处位置信息

3、学校教学管理要求,每个学期末教学管理老师必须按要求整理学生成绩并输出学生成绩汇总表。

第五章 练一练----经典基础题-----不计入MOOC总成绩---

1、字符串加密算法

2、冒泡排序算法

3、二分查找算法,折半查找算法

4、求回文数

5、矩阵的最大值及其下标

第五章 练一练----经典提高题-----不计入MOOC总成绩---

1、狼追兔子问题

2、矩阵转置

3、杨辉三角

4、多项式加法

5、谁家孩子跑得最慢

第6章——指针:所向披靡的“金箍棒” 魂

第6章单元测试

1、下面程序的运行结果是( ). #include<iostream> using namespace std; int main ( ) { float a=1,b=2,c; float *p1=&a,*p2; p2=&b; c= * p1 + *p2; cout<<*p1<<'\n'; }
A、1
B、2
C、a
D、b

2、设有定义一维数组如下:int a[5],*p=a;,则下列描述中错误的是( ).
A、表达式 p=p+1是合法的
B、表达式a=a+1是合法的
C、表达式p-a是合法的
D、表达式a+2是合法的

3、已知数组arr的定义如下: int arr[5] = { 1,2,3,4,5}; 下列语句中输出结果不是2的是( )
A、cout << *arr+1 <<endl;
B、cout << *(arr+1)<<endl;
C、cout << arr[1] <<endl;
D、cout << *arr <<endl;

4、下列语句中,正确的是( )。
A、char*myString="Hello-World!";
B、char myString="Hello-World!";
C、char myString[11]="Hello-World!";
D、char myString[12]="Hello-World!";

5、已知:int m=10;在下列语句中错误的是( )
A、int *p=new int(m);
B、int *p=new int[m]=;
C、float *p=new float(m);
D、float *p=new float[m];

6、下面程序的运行结果是( ). #include<iostream> using namespace std; void main(void) { int a[5]={ 10,20,30,40,50}; int *p=&a[0]; p++; cout<<++*p<<endl; }
A、10
B、21
C、31
D、41

7、下列程序的输出结果是 #include <iostream> using namespace std; int main() { char a[] = "Hello, World"; char *ptr = a; while (*ptr) { if (*ptr >= 'a' && *ptr <= 'z') cout << char(*ptr + 'A' -'a'); else cout << *ptr; ptr++; } return 0; }
A、HELLO, WORLD
B、Hello, World
C、hELLO, wORLD
D、hello, world

8、已知有数组定义?char?a[3][4];? 下列表达式中错误的是( )。?
A、a[2]="WIN"?;
B、strcpy(a[2],"WIN")?;
C、a[2][3]='W'?;
D、a[0][1]=a[0][1]?;

9、下列程序运行时的输出结果是( )。 #include <iostream> using namespace std; int main() { int a[7]={ 23,15,64,33,40,58}; int s1,s2,*p; s1=s2=a[0]; for(p=a+1;*p;p++) { if(s1>*p) s1=*p; if(s2<*p) s2=*p;} cout<<s2; return 0; }
A、23
B、58
C、64
D、79

10、下面程序的运行结果是( ). #include<iostream> using namespace std; int main( ) { int aa[3][3]={ { 1},{ 2},{ 3}},i,*p=&aa[0][0]; for(i=0;i<2;i++) { if(i==0) aa[i][i+1]=*p+1; else ++p; cout<<*p<<" ";} cout<<endl; return 0; }
A、1 1
B、2 1
C、1 2
D、3 1

第六章 练一练 经典基础题

1、阅读程序,请输出程序的结果。 #include<iostream> using namespace std; int main() { int a[]={ 11,16,13,14,15}; int *ptr = a; cout<<*ptr<<endl; cout<<*(++ptr)<<endl; return 0; }
A、11 11
B、11 16
C、11 12
D、16 17

2、阅读程序,请输出程序的结果。 #include<iostream> using namespace std; int main() { int a[]={ 11,16,13,14,15}; int *ptr = a; cout<<*ptr<<endl; cout<<*(ptr++)<<endl; return 0; }
A、11 11
B、11 16
C、11 12
D、16 16

3、阅读程序,输出结果。 #include<iostream> using namespace std; int main() { int a[]={ 11,16,13,14,15}; int *ptr = a; cout<<*ptr<<endl; cout<<*ptr++<<endl; cout<<a[0]<<endl; cout<<*ptr<<endl; return 0; }
A、11 11 11 16
B、11 11 12 11
C、11 12 11 12
D、11 16 11 17

4、#include<iostream> using namespace std; int main() { int a[]={ 11,16,13,14,15}; int *ptr = a; cout<<*ptr<<endl; cout<<(*ptr)++<<endl; cout<<a[0]<<endl; cout<<*ptr<<endl; return 0; }
A、11 11 11 16
B、11 11 12 12
C、11 12 11 16
D、11 16 11 17

5、阅读程序,输出结果。 #include<iostream> using namespace std; int main() { int a[]={ 11,16,13,14,15}; int *ptr = a; cout<<*ptr<<endl; cout<<*(ptr++)<<endl; cout<<a[0]<<endl; cout<<*ptr<<endl; return 0; }
A、11 11 11 16
B、11 12 11 12
C、11 16 11 16
D、11 16 11 17

6、阅读程序,输出结果。 #include<iostream> using namespace std; int main() { int a[]={ 11,16,13,14,15}; int *ptr = a; cout<<*ptr<<endl; cout<<*++ptr<<endl; cout<<a[0]<<endl; cout<<*ptr<<endl; return 0; }
A、11 11 11 16
B、11 16 11 16
C、11 11 11 12
D、11 11 12 12

7、阅读程序,输出结果。 #include<iostream> using namespace std; int main() { int a[]={ 11,16,13,14,15}; int *ptr = a; cout<<*ptr<<endl; cout<<++*ptr<<endl; cout<<a[0]<<endl; cout<<*ptr<<endl; return 0; }
A、11 11 11 12
B、11 16 11 16
C、11 12 12 12
D、11 12 11 12

8、阅读程序,输出结果。 #include<iostream> using namespace std; int main() { int a[]={ 11,16,13,14,15}; int *ptr = a; cout<<*ptr<<endl; cout<<*(++ptr)<<endl; cout<<a[0]<<endl; cout<<*ptr<<endl; return 0; }
A、11 11 11 12
B、11 16 11 16
C、11 12 11 12
D、11 11 11 16

9、阅读程序,输出结果 #include<iostream> using namespace std; int main() { int a[]={ 11,16,13,14,15}; int *ptr = a; cout<<*ptr<<" "<<*(++ptr)<<endl; return 0; }
A、16 16
B、11 11
C、11 16
D、11 12

10、以下程序的功能是:将无符号八进制数字构成的字符串转换为十进制整数。例如,输入的字符串为:556,则输出十进制整数366。请在黄色处选择合适的语句。 #include<iostream> using namespace std; int main() { char *p,s[6]; int n; p=s; cin>>p; n=*p-'0'; while( !='') n=n*8+*p-'0'; cout<<n<<endl; return 0;}
A、*++p
B、*p++
C、(*p)++
D、*p

第六章 练一练 经典提高题

1、阅读程序,回答问题: #include <iostream> #include<iomanip> using namespace std; int main() { int a[3][4]={ 1,2,3,4,5,6,7,8,9,10,11,12}; int (*p)[4]; //该语句是定义一个数组指针,指向含4个元素的一维数组。 p=a; cout<<*p<<endl; p++; cout<<*p<<endl; return 0; } 请问这两个输出的地址差是多少字节?
A、16
B、4
C、1
D、编译错误

2、阅读程序,回答下面问题: #include <iostream> #include<iomanip> using namespace std; int main() { int a[3][4]={ 1,2,3,4,5,6,7,8,9,10,11,12}; int *p[3]; //定义一个指针数组,表示一个一维数组内存放着三个指针变量,分别是p[0]、p[1]、p[2] p=a; cout<<*p<<endl; p++; cout<<*p<<endl; return 0; } 请问这两个输出的数据相差多少?
A、16
B、4
C、1
D、编译错误

3、若定义 char s[2][3]={ “ab”, “cd”}, *p=(char *)s; //字符型指针p存放的是字符串s的首地址,而字符串s表示的是这样一个字符串: 那么下列表达式语法正确,并且其值与 s[1][1]相等的表达式(并非一定与其等价)是() //根据字符串的表示,s[1][1]是字符‘d’
A、*(s+3)
B、p[1][1]
C、*(p+3)
D、char(*++p+2)

4、#include<iostream> using namespace std; int main() { int a[5] = { 1, 2, 3, 4, 5 }; int *ptr = (int*)(&a + 1); cout<< *(a + 1)<<" "<< *(ptr - 1)<<endl; return 0; } 程序运行结果是什么?
A、2 5
B、编译错误
C、1 2
D、2 不确定

5、阅读程序,输出结果。 #include<iostream> using namespace std; int main() { int a[3][2] = { 1, 2,4, 5 }; int *p; p = a[0]; cout<<p[0]<<" "<<p[1]<<" "<<p[2]<<" "<<p[3]<<endl; p=a[1]; cout<<p[0]<<" "<<p[1]<<" "<<p[2]<<" "<<p[3]<<endl; p=a[2]; cout<<p[0]<<" "<<p[1]<<" "<<p[2]<<" "<<p[3]<<endl; return 0; }
A、1 2 4 5 4 5 0 0 0 0 不确定 不确定
B、编译错误
C、1 2 4 5 0 0 0 0 0 0 不确定 不确定
D、1 2 4 5 4 5 0 0 不确定 不确定 不确定 不确定

6、阅读程序,输出结果。 #include<iostream> using namespace std; int main() { int aa[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int *ptr1 = (int *)(&aa + 1); int *ptr2 = (int *)(*(aa + 1)); cout<<*(ptr1 - 1)<<" "<< *(ptr2 - 1); return 0; }
A、10 5
B、编译错误
C、地址 5
D、10 地址

7、阅读程序,输出结果 #include<iostream> using namespace std; int main() { char *a[] = { "work", "at", "alibaba" }; char **pa = a; pa++; cout<<*pa<<endl; return 0; }
A、at
B、编译错误
C、work
D、alibaba

8、有定义语句:char s[3][10],(*k)[3],*p;则对于下列赋值语句,哪个是正确的?
A、p = s[0];
B、p = s;
C、p = k;
D、k = s;

9、如定义 int a[3][4]; 下面哪个不能表示 a[1][1]?
A、*(&a[0][0]+5)
B、*(*(a+1)+1)
C、*(&a[1]+1)
D、*(a[1]+1)

10、阅读程序,输出结果 #include<iostream> using namespace std; int main() { int a[3][4]={ { 0,1,2,10},{ 3,4,5,11},{ 6,7,8,12}}; cout<<*(&a[0][0])<<endl; cout<<**a<<endl; cout<<*(&a[1][0])<<endl; cout<<*(a[0]+1)<<endl; cout<<*(a+1)<<endl; return 0; }
A、0 0 3 1 第一行的首地址
B、第一个元素地址 0 3 1 3
C、0 0 3 1 3
D、0 0 3 1 3 1

第六章 编程题

1、运用指针技术实现,将输入的两个字符串中第二个字符串插入到第一个字符串中指定输入数字位置字符的后面。

2、运用指针技术实现,将输入的十六进制数的字符串转换为对应的整数数。

3、运用指针技术实现,统计输入字符串的各个字母各出现了多少次。区分大小写。

第7章 函数:面向过程的基础

第7章单元测试

1、有如下程序段: int i=4;int j=1; int main( ) { int i=8,j=i; cout<<i<<j<<endl; } 运行时的输出结果是______。
A、44
B、41
C、88
D、81

2、有以下程序 #include<iostream> int i = 0 ; void fun() { { static int i = 1; cout<<i++<<','; } cout<<::i<<','; } int main() { fun(); fun(); return 0; } 程序执行后的输出结果是( )
A、0,1,1,2,
B、1,2,2,3,
C、2,0,3,0,
D、1,0,2,0,

3、已知函数fun的原型为 int fun (int,int,int); 下列重载函数原型中错误的是______。
A、char fun(int,int);
B、double fun(int,int,double);
C、int fun(int,clar*);
D、float fun(int,int,int);

4、下列函数原型声明中错误的是( )
A、void Fun(int x=0, int y=0);
B、void Fun(int x, int y);
C、void Fun(int x, int y=0);
D、void Fun(int x=0, int y);

5、已知函数FA调用FB,若要把这两个函数定义在同一个文件中,则
A、FA必须定义在FB之前
B、FB必须定义在FA之前
C、若FA定义在FB之后,则FA的原型必须出现在FB的定义之前
D、若FB定义在FA之后,则FB的原型必须出现在FA的定义之前

6、为了提高函数调用的实际运行速度,可以将较简单的函数定义为
A、内联函数
B、重载函数
C、递归函数
D、函数模板

7、下面的函数利用递归实现了求1+2+3…..+n的功能: int sum(int n){ if( n==0 ) return 0; else return n+sum(n-1); } 在执行sum(10)的过程中,递归调用sum函数的次数是( )。
A、9
B、10
C、11
D、8

8、已知函数f的原型是:void f(int *a, long &b); 变量v1、v2的定义是: int v1; long v2;,正确的调用语句是( )。
A、f(v1, &v2);
B、f(v1, v2);
C、f(&v1, v2);
D、f(&v1, &v2);

9、有如下程序: #include<iostream> using namespace std; void f1(int& x, int& y) { int z=x; x=y; y=z;} void f2(int x; int y) { int z=x; x=y; y=z;} int main() { int x=10,y=26; f1(x,y); f2(x,y); cout<<x<<endl; return 0; } 运行时的输出结果是
A、10
B、16
C、26
D、36

10、以下程序的执行结果是( )。 int fun(int b[],int n) { int i,r=1; for(i=0;i<n;i++) r=r*b[i]; return r; } void main() { int x,a[]={ 1,2,3,4,5,6,7,8}; x=fun(a,3); cout<<x<<endl; }
A、5
B、6
C、7
D、8

第七章 编程题

1、机器人排序

2、手机营运商的识别

3、卡特兰数

第七章 练一练---经典基础题--不计入总分---

1、在字符串的指定位置插入字符

2、删除星号

3、求梅森素数

4、分鱼问题

5、24点问题

第七章 练一练---经典提高题---不计入总分---

1、验证哥德巴赫猜想

2、矩阵转置

3、奶牛生子的问题

4、任意进制相互转化

5、麻将清一色和牌问题

第8章 文件:让数据流动起来

第8章 单元测试

1、要利用C++流实现输入输出的各种格式控制,必须在程序中包含的头文件是(  )
A、fstream
B、istream
C、ostream
D、iomanip

2、以下不能作为输出流对象的是(  )。
A、文件
B、内存
C、键盘
D、显示器

3、C++系统预定义了4个用于标准数据流的对象,下列选项中不属于此类对象的是( )  
A、cout
B、cin
C、cerr
D、cset 

4、下列关于文件流的描述中,正确的是( )   
A、文件流只能完成针对磁盘文件的输入输出  
B、建立一个文件流对象时,必须同时打开一个文件  
C、若输入流要打开的文件不存在,将建立一个新文件  
D、若输出流要打开的文件不存在,将建立一个新文件

5、以下叙述中不正确的是( )
A、C++语言中的文本文件以ASCII码形式存储数据
B、C++语言中对二进制文件的访问速度比文本文件快
C、使用函数open()打开的文件使用完毕后,必须使用close()函数将其关闭
D、C++语言中,随机读写方式不适用于文本文件

6、以下不能正确创建输出文件对象并使其与磁盘文件相关联的语句是(  )。
A、ofstream myfile; myfile.open("d:ofiltxt");
B、ofstream*myfile=new ofstream; myfile->open("d:ofiltxt");
C、ofstream myfile("d:ofiltxt");
D、ofstream*myfile=new("d:ofiletxt");

7、如果调用C++流进行输入输出,下面的叙述中正确的是( )
A、只能借助于流对象进行输入输出
B、只能进行格式化输入输出
C、只能借助于cin和cout进行输入输出
D、只能使用运算符 》和《 进行输入输出

8、打开文件时可单独或组合使用下列文件打开模式   ①ios_base::app ②ios_base::binary   ③ios_base::in ④ios_base::out   若要以二进制读方式打开一个文件,需使用的文件打开模式为( ).
A、①③ 
B、①④ 
C、②③ 
D、②④

9、当使用ifstream流类定义一个流对象并打开一个磁盘文件时,文件的默认打开方式为( )。
A、ios_base::in
B、ios_base::in│ios_base::out
C、ios_base::out
D、ios_base::in & ios_base::out

10、执行语句序列 ofstream outfile("DATA.DAT"); if(...) cout << "OK"; else cout << "FAIL"; 后,如果文件打开成功显示“OK”,否则就显示“Fail。”。由此可知,上面if语句的... 处的表达式应是( ) .
A、outfile.fail() 或 outfile
B、outfile.good() 或 !outfile
C、outfile.good() 或 outfile
D、outfile.fail() 或 !outfile

第8章 练一练经典提高题 程序填空题

1、下列程序的功能是:将M行N列的二维数组中的字符数据,按列的顺序依此放到一个字符串并将该字符显示在屏幕上,然后将此字符串的内容存入e:\out.txt文件中。请在程序( )位置填入适当的一条语使程序完整。 请注意本题中的3个空分别是接下来的3个填空题,本题只需填入第一空位置//1 处的答案。 #include <fstream> #include<iostream> using namespace std; const int M=3; const int N=4; int main() { char b[100],w[M][N]={ { 'W','W','W','W'},{ 'S','S','S','S'},{ 'H','H','H','H'}}; ofstream wf; ( ) // 1 int i,j,k=0; cout<<"The matrix:\n"; for(i=0;i<M;i++) { for(j=0;j<N;j++) cout<<w[i][j]; cout<<"\n"; } for(i=0;i<N;i++) for(j=0;j<M;j++) ( ) //2 b[k]='\0'; cout<<"The A string:\n"; cout<<b; cout<<"\n"; ( ) //3 wf.close(); return 0; }

2、题目题干见第1题。将第1题中第二空位置//2 处 的答案填入此题。

3、题目题干见第1题。将第1题中第三空位置//3 处的答案填入此题。

4、给定程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到e:\file1.txt文本文件中,再用字符方式从此文本文件中逐个读入并显示在终端屏幕上。程序的运行结果是:Hello!1234598.76 请在程序( )位置填入适当的一条语句使程序完整。 请注意本题中的4个空//4 ,//5,//6, //7 分别对应问题4,问题5,问题6和问题7. 本题只需填入//4位置第一空的答案。 #include <iostream> #include <fstream> using namespace std; void fun(char *s, int a, double f) { fstream fp; char ch; ( ) //4 fp<<s; fp<<a; fp<<f; fp.close(); ( ) //5 cout<<"The result :\n"; fp.get(ch); while ( ) //6 { cout<<ch; ( ) //7 } cout<<endl; fp.close(); } int main() { char a[10]="Hello!"; int b=12345; double c= 98.76; fun(a,b,c); return 0; }

5、题目题干见第4题。将第4题中第二空位置//5 处的答案填入此题。

6、题目题干见第4题。将第4题中第三空位置//6 处的答案填入此题。

7、题目题干见第4题。将第4题中第四空位置//7 处的答案填入此题。

8、编写一个函数 intcharnum(char fn[ ]),该函数打开文件fn,,通过统计,返回文件中字符的个数。 请在程序( )位置填入适当的一条语句使程序完整。 请注意本题中的3个空//8, //9, //10 分别对应问题8,问题9和问题10 ,本题只需填入第一空位置//8 处的答案。 #include <iostream> #include <fstream> using namespace std; int charnum(char fn[]); int main() { int num; num=charnum("abc.txt"); cout<<"num="<<num<<endl; return 0;} int charnum(char fn[]) { fstream file; ( ) //8 if(!file) { cout<<"abc.txt can't open"<<endl; abort();} char ch; int i=0; file.get(ch); while( ) //9 { ( ) //10 i++;} file.close(); return i; }

9、题目题干见第8题。将第8题中第二空位置//9 处的答案填入此题。

10、题目题干见第8题。将第8题中第三空位置//10 处的答案填入此题。

第9章 以人为本:用类与对象诠释现实世界

第9章 单元测试

1、下列关于构造函数的描述中,错误的是( )
A、构造函数名与类名相同
B、构造函数可以有返回值
C、构造函数可以重载
D、每个类都有构造函数

2、在 C++中,编译系统自动为一个类生成缺省构造函数的条件是( )。
A、该类没有定义任何有参构造函数
B、该类没有定义任何无参构造函数
C、该类没有定义任何构造函数
D、该类没有定义任何成员函数

3、若AA为一个类,a为该类的非静态数据成员,在该类的一个成员函数定义中访问a时,其书写格式为( )
A、a
B、AA.a
C、a( )
D、AA::a( )

4、Sample是一个类,执行下面语句后,调用Sample类的构造函数的次数是   Sample a[2], *q, *p = new Sample;
A、0
B、4
C、2
D、3

5、若MyClass是一个类名,且有如下语句序列 MyClass c1,*c2; MyClass *c3=new MyClass; MyClass &c4=c1; 上面的语句序列所定义的类对象的个数是
A、1
B、2
C、3
D、4

6、下列运算符函数中肯定不属于类FunNumber的成员函数的是
A、int operator-(FunNumber)
B、FunNumber operator-( )
C、FunNumber operator-(int)
D、int operator – (FunNumber,FunNumber)

7、关于友元,下列说法错误的是
A、如果类A是类B的友元,那么类B也是类A的友元
B、如果函数fun()被说明为类A的友元,那么在fun()中可以访问类A的私有成员
C、友元关系不能被继承
D、如果类A是类B的友元,那么类A的所有成员函数都是类B的友元

8、有以下类定义 class Point { public: Point(int x = 0, int y = 0) { _.x = x; _.y = y; } void Move(int xOff, int yOff) { _x += xOff; _.y += yOff; } void Print() const { cout << '(' << _x << ',' << _y << ')' << endl; } private: int _x, _y; }; 下列语句中会发生编译错误的是
A、Point pt; pt.Print( );
B、const Point pt; pt.Print( );
C、Point pt; pt.Move(l, 2);
D、const Point pt; pt.Move(l, 2);

9、有如下两个类定义 class AA{ }; class BB{ AA v1,*v2; BB v3; int *v4; }; 其中有一个成员变量的定义是错误的,这个变量是
A、v1
B、v2
C、v3
D、v4

10、有如下程序: #include<iostream> using namespace std;   class XA { int a; public: static int b; XA(int aa):a(aa){ b++;} ~XA(){ } int get(){ return a;} }; int XA::b=0; int main() { XA d1(2),d2(3); cout<<d1.get()+d2.get()+XA::b<<endl; return 0; } 运行时的输出结果是( ) 
A、5
B、6
C、7
D、8

第九章 练一练--经典基础题-钟类专题--不计入总分---

1、简单钟(类与对象的定义):设计一个钟类,其特征数据为:时、分、秒;其行为特征为:显示时间和设置时间,说明一个钟类的对象myclock,并设置时间和显示时间。其程序为: #include<iostream> using namespace std; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int NewH, int NewM, int NewS); void ShowTime(); private: //私有数据成员 int hp,mp,sp; }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime() { cout<<hp<<":"<<mp<<":"<<sp<<endl; } int main() { Clock myClock; //定义对象myClock cout<<"First time set and output:"<<endl; ① //设置时间 ② //显示时间 cout<<"Second time set and output:"<<endl; ③ //设置时间 ④ //显示时间 return 0; } 程序运行的结果为: 根据输出结果,完成程序中 ① ② ③ ④位置所对应的语句。
A、① myClock.SetTime(8,30,30); //设置时间 ② myClock.ShowTime(); //显示时间 ③myClock.SetTime(10,20,20); //设置时间 ④myClock.ShowTime(); //显示时间
B、① SetTime(8,30,30); //设置时间 ② ShowTime(); //显示时间 ③SetTime(10,20,20); //设置时间 ④ShowTime(); //显示时间
C、① Clock::SetTime(8,30,30); //设置时间 ② Clock::ShowTime(); //显示时间 ③Clock::SetTime(10,20,20); //设置时间 ④Clock::ShowTime(); //显示时间
D、都不对

2、简单钟(类与对象的定义):设计一个钟类,其特征数据为:时、分、秒;其行为特征为:获取时间和设置时间,说明一个钟类的对象myclock,并设置时间和显示时间。其程序运行结果为:
A、#include<iostream> using namespace std; class Clock //时钟类的声明 { void SetTime(int NewH, int NewM, int NewS); void ShowTime(int &NewH, int &NewM, int &NewS); int hp,mp,sp; }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime(int &NewH, int &NewM, int &NewS) { NewH=hp; NewM=mp; NewS=sp; } int main() { int h,m,s; Clock myClock; //定义对象myClock cout<<"First time set and output:"<<endl; myClock.SetTime(8,30,30); //设置时间 myClock.ShowTime(h,m,s); //获取时间 cout<<h<<":"<<m<<":"<<s<<endl; cout<<"Second time set and output:"<<endl; myClock.SetTime(10,20,20); //设置时间 myClock.ShowTime(h,m,s); //获取时间 cout<<h<<":"<<m<<":"<<s<<endl; return 0; }
B、#include<iostream> using namespace std; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int NewH, int NewM, int NewS); void ShowTime(int &NewH, int &NewM, int &NewS); private: //私有数据成员 int hp,mp,sp; }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime(int &NewH, int &NewM, int &NewS) { NewH=hp; NewM=mp; NewS=sp; } int main() { int h,m,s; Clock myClock; //定义对象myClock cout<<"First time set and output:"<<endl; myClock.SetTime(8,30,30); //设置时间 myClock.ShowTime(h,m,s); //获取时间 cout<<h<<":"<<m<<":"<<s<<endl; cout<<"Second time set and output:"<<endl; myClock.SetTime(10,20,20); //设置时间 myClock.ShowTime(h,m,s); //获取时间 cout<<h<<":"<<m<<":"<<s<<endl; return 0; }
C、#include<iostream> using namespace std; class Clock //时钟类的声明 { int hp,mp,sp; public: void SetTime(int NewH, int NewM, int NewS); void ShowTime(int NewH, int NewM, int NewS); }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime(int &NewH, int &NewM, int &NewS) { NewH=hp; NewM=mp; NewS=sp; } void main() { int h,m,s; Clock myClock; //定义对象myClock cout<<"First time set and output:"<<endl; myClock.SetTime(8,30,30); //设置时间 myClock.ShowTime(h,m,s); //获取时间 cout<<h<<":"<<m<<":"<<s<<endl; cout<<"Second time set and output:"<<endl; myClock.SetTime(10,20,20); //设置时间 myClock.ShowTime(h,m,s); //获取时间 cout<<h<<":"<<m<<":"<<s<<endl; return 0; }
D、#include<iostream> using namespace std; class Clock //时钟类的声明 { int hp,mp,sp; public: void SetTime(int NewH, int NewM, int NewS); }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } int main() { Clock myClock; //定义对象myClock cout<<"First time set and output:"<<endl; myClock.SetTime(8,30,30); //设置时间 cout<<myClock.hp<<":"<<myClock.mp<<":"<<myClock.sp<<endl; cout<<"Second time set and output:"<<endl; myClock.SetTime(10,20,20); //设置时间 cout<<myClock.hp<<":"<<myClock.mp<<":"<<myClock.sp<<endl; return 0; }

3、简单钟(构造函数):设计一个钟类,其特征数据为:时、分、秒;其行为特征为:获取时间和设置时间,说明一个钟类的对象myclock,并设置时间和显示时间。其主函数为: int main() { int h,m,s; Clock myClock,yourClock(8,30,30); //定义对象myClock cout<<"my time set and output:"<<endl; myClock.ShowTime(h,m,s); //获取时间 cout<<h<<":"<<m<<":"<<s<<endl; cout<<"your time set and output:"<<endl; yourClock.ShowTime(h,m,s); //获取时间 cout<<h<<":"<<m<<":"<<s<<endl; return 0; } 根据主函数,完成类的定义。
A、#include<iostream> using namespace std; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int NewH, int NewM, int NewS); void ShowTime(int &NewH, int &NewM, int &NewS); private: //私有数据成员 int hp,mp,sp; }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime(int &NewH, int &NewM, int &NewS) { NewH=hp; NewM=mp; NewS=sp; }
B、#include<iostream> using namespace std; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 Clock(int NewH, int NewM, int NewS); void ShowTime(int &NewH, int &NewM, int &NewS); private: //私有数据成员 int hp,mp,sp; }; //时钟类成员函数的具体实现 Clock::Clock(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime(int &NewH, int &NewM, int &NewS) { NewH=hp; NewM=mp; NewS=sp; }
C、#include<iostream> using namespace std; class Clock //时钟类的声明 { int hp,mp,sp; public: //外部接口,公有成员函数 void SetTime(int NewH=0, int NewM=0, int NewS=0); void ShowTime(int &NewH, int &NewM, int &NewS) { NewH=hp; NewM=mp; NewS=sp; } }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; }
D、#include<iostream> using namespace std; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 Clock(int NewH, int NewM, int NewS); Clock(){ } void ShowTime(int &NewH, int &NewM, int &NewS); private: //私有数据成员 int hp,mp,sp; }; //时钟类成员函数的具体实现 Clock::Clock(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime(int &NewH, int &NewM, int &NewS) { NewH=hp; NewM=mp; NewS=sp; }

4、走动的钟(类与对象):设计一个钟类,其特征数据为:时、分、秒;其行为特征为:时钟的走动、显示时间和设置时间,说明一个钟类的对象myclock,并设置时间和显示时间。其主函数为: int main() { Clock myClock; //定义对象myClock cout<<"First time set and output:"<<endl; myClock.SetTime(23,59,54); //根据参数设置时间 myClock.ShowTime(); //显示时间 system("pause");//屏幕输出暂停一下,敲任意键盘继续运行程序 for(int i=0;;i++) { system("CLS");//清理屏幕 cout<<"作业答案查询

*****走动的钟作业答案查询

**\n"; myClock.ShowTime() ; myClock.AddOneS();//按24小时计算走动的时钟 cout<<"作业答案查询

*****走动的钟作业答案查询

**\n"; Sleep(1000);//休眠1000毫秒再运行CPU中的程序 } }
A、#include<iostream> #include<windows.h> using namespace std; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int NewH=0, int NewM=0, int NewS=0); void ShowTime(); void DispTime(); AddOneS(); private: //私有数据成员 int hp,mp,sp; }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime() { cout<<" "<<hp<<":"<<mp<<":"<<sp<<endl; } void Clock::AddOneS()//按12小时走动时钟 { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } }
B、#include<iostream> #include<windows.h> using namespace std; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int NewH=0, int NewM=0, int NewS=0); void ShowTime(); void AddOneS()//以24小时计算走动时钟 { if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=24) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp; }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime() { cout<<" "<<hp<<":"<<mp<<":"<<sp<<endl; }
C、#include<iostream> using namespace std; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int NewH=0, int NewM=0, int NewS=0); void ShowTime(); void AddOneS()//以24小时计算走动时钟 { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=24) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp; }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime() { cout<<" "<<hp<<":"<<mp<<":"<<sp<<endl; }
D、#include<iostream> #include<windows.h> using namespace std; class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int NewH=0, int NewM=0, int NewS=0); void ShowTime(); private: //私有数据成员 void AddOneS()//以24小时计算走动时钟 { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=24) { hp=0; } } } } int hp,mp,sp; }; //时钟类成员函数的具体实现 void Clock::SetTime(int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; } void Clock::ShowTime() { cout<<" "<<hp<<":"<<mp<<":"<<sp<<endl; }

5、带日期的时钟(组合类):设计一个钟类,其特征数据为:时、分、秒,日期;其行为特征为:时钟的走动、显示时间(包括日期)和设置时间(包括日期),其中,日期也作为一个独立的日期类,其特征数据为:年、月、日;其行为特征为:获得年、月和日的数据,并能设置日期,说明一个钟类的对象myclock,并设置时间和显示时间。其主函数为: #include<iostream> #include<windows.h> using namespace std; class Date //定义日期类 { int year; int month; int day; public: void SetDay(int NewY, int NewM, int NewD)//设置日期 { year= NewY; month=NewM; day=NewD; } int Get_Y() { return year;} //获得时间的年份 int Get_M() { return month;} //获得时间的月份 int Get_D() { return day;} //获得时间的日期 void ShowDay(int &NewY,int &NewM,int &NewD) //获得时间的年月日 { NewY= year; NewM=month; NewD=day; } }; /作业答案查询

请完成定义相关的时钟类,日期类作为时钟类的日期的类型作业答案查询

*****/ int main() { Clock myClock; //定义对象myClock myClock.SetTime(); //设置时间为默认值 myClock.DispTime(); //显示时间 system("pause"); system("CLS"); while(1) { cout<<"*****欢迎进入各时区计时系统*****"<<endl; cout<<endl<<endl<<endl; myClock.DispTime(); //显示时间 myClock.AddOneS(); //走动时间 Sleep(1000); system("CLS"); } }
A、class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int z1,int NewH, int NewM, int NewS,Date d1); int Get_H() { return hp;} int Get_M() { return mp;} int Get_S() { return sp;} Date Get_Day() { return d;} void SetZone(int z); int GetZone(); void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 void Clock::SetTime(int z1,int NewH, int NewM, int NewS,Date d1) { hp=NewH; mp=NewM; sp=NewS; z=z1; d.SetDay(d1.Get_Y(),d1.Get_M(),d1.Get_D() ); } //设置时区 void Clock::SetZone(int z) { z=this->z; } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { cout<<"作业答案查询

****北京时间作业答案查询

***\n"; cout<<"日期: "<<d.Get_Y()<<"年"<<d.Get_M()<<"月"<<d.Get_D()<<" 日 "<<endl; cout<<"时间: "<<hp<<":"<<mp<<":"<<sp<<endl; cout<<"作业答案查询

作业答案查询

作业答案查询

作业答案查询

作业答案查询

****\n"; cout<<endl<<endl; }
B、class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int z1,int NewH, int NewM, int NewS,Date d1); int Get_H() { return hp;} int Get_M() { return mp;} int Get_S() { return sp;} Date Get_Day() { return d;} void SetTime() { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; d.SetDay(sys.wYear,sys.wMonth,sys.wDay); } void SetZone(int z); int GetZone(); void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 //设置时区 void Clock::SetZone(int z) { this->z=z; } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { cout<<"作业答案查询

****北京时间作业答案查询

***\n"; cout<<"日期: "<<d.year<<"年"<<d.month<<"月"<<d.day<<" 日 "<<endl; cout<<"时间: "<<hp<<":"<<mp<<":"<<sp<<endl; cout<<"作业答案查询

作业答案查询

作业答案查询

作业答案查询

作业答案查询

****\n"; cout<<endl<<endl; }
C、class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int z1,int NewH, int NewM, int NewS,Date d1); int Get_H() { return hp;} int Get_M() { return mp;} int Get_S() { return sp;} Date Get_Day() { return d;} void SetTime() { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; d.SetDay(sys.wYear,sys.wMonth,sys.wDay); } void SetZone(int z); int GetZone(); void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //设置时区 void Clock::SetZone(int z) { z=z; } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { cout<<"作业答案查询

****北京时间作业答案查询

***\n"; cout<<"日期: "<<d.Get_Y()<<"年"<<d.Get_M()<<"月"<<d.Get_D()<<" 日 "<<endl; cout<<"时间: "<<hp<<":"<<mp<<":"<<sp<<endl; cout<<"作业答案查询

作业答案查询

作业答案查询

作业答案查询

作业答案查询

****\n"; cout<<endl<<endl; }
D、class Clock //时钟类的声明 { public: //外部接口,公有成员函数 void SetTime(int z1,int NewH, int NewM, int NewS,Date d1);//设置时间 int Get_H() { return hp;} //获得时间的时数 int Get_M() { return mp;} //获得时间的分钟 int Get_S() { return sp;} //获得时间的秒数 Date Get_Day() { return d;} //获得时间的日期 void SetTime() //设置默认时间 { SYSTEMTIME sys; //系统时间 GetLocalTime( &sys ); //获得本机器的当前时间 hp=sys.wHour; //把本机器的当前时间时数赋给相应的成员变量 mp=sys.wMinute; //把本机器的当前时间分钟赋给相应的成员变量 sp=sys.wSecond; //把本机器的当前时间秒数赋给相应的成员变量 z=8; //把本机器的当前时间时区赋给相应的成员变量 d.SetDay(sys.wYear,sys.wMonth,sys.wDay); //把本机器的当前时间日期赋给相应的成员变量 } void SetZone(int z);//设置时间的时区 int GetZone(); //获得时间的时区 void DispTime(); //显示时间 void AddOneS() //时钟走动的函数 { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 void Clock::SetTime(int z1,int NewH, int NewM, int NewS,Date d1) { hp=NewH; mp=NewM; sp=NewS; z=z1; d.SetDay(d1.Get_Y(),d1.Get_M(),d1.Get_D() ); } //设置时区 void Clock::SetZone(int z) { this->z=z; } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { cout<<"作业答案查询

****北京时间作业答案查询

***\n"; cout<<"日期: "<<d.Get_Y()<<"年"<<d.Get_M()<<"月"<<d.Get_D()<<" 日 "<<endl; cout<<"时间: "<<hp<<":"<<mp<<":"<<sp<<endl; cout<<"作业答案查询

作业答案查询

作业答案查询

作业答案查询

作业答案查询

****\n"; cout<<endl<<endl; }

6、带日期的时钟(组合类构造函数):设计一个钟类,其特征数据为:时、分、秒,日期;其行为特征为:时钟的走动、显示时间(包括日期)和构造函数(包括有参构造函数和无参构造函数),其中,日期也作为一个独立的日期类,其特征数据为:年、月、日;其行为特征为:获得年、月和日的数据,并定义了构造函数,说明两个钟类的对象myclock和myclock1,显示相应的时间,时间显示北京时间和伦敦时间。其主函数为: #include<iostream> #include<windows.h> using namespace std; class Date //定义日期类 { int year; int month; int day; public: Date() //无参构造函数,用系统时间设置 { SYSTEMTIME sys; GetLocalTime( &sys ); year=sys.wYear,month=sys.wMonth,day=sys.wDay; } int Get_Y() { return year;} //获得时间的年份 int Get_M() { return month;} //获得时间的月份 int Get_D() { return day;} //获得时间的日期 void ShowDay(int &NewY,int &NewM,int &NewD) //获得时间的日期 { NewY= year; NewM=month; NewD=day; } }; /作业答案查询

请完成定义相关的时钟类,日期类作为时钟类的日期的类型,时钟类赋值通过构造函数赋值作业答案查询

*****/ int main() { Date l(2020,1,1); //定义一个指定日期 Clock myClock,myClock1(0,10,45,50,l); //定义对象myClock(不须初始化)对象myClock1须初始化 myClock.DispTime(); //显示对象myClock时间 myClock1.DispTime(); //显示对象myClock1时间 system("pause"); system("CLS"); while(1) { cout<<"*****欢迎进入各时区计时系统*****"<<endl; cout<<endl<<endl<<endl; myClock.DispTime(); //显示对象myClock时间 myClock.AddOneS(); //对象myClock时间走动 cout<<endl<<endl<<endl; myClock1.DispTime(); //显示对象myClock1时间 myClock1.AddOneS();//对象myClock1时间走动 Sleep(1000);//隔1秒钟再运行 system("CLS"); //清屏 } } 程序运行的初始结果如下图所示:
A、/作业答案查询

请完成定义相关的时钟类,日期类作为时钟类的日期的类型,时钟类赋值通过构造函数赋值作业答案查询

*****/ class Clock //时钟类的声明 { public: //外部接口,公有成员函数 Clock(int z1,int NewH, int NewM, int NewS,Date d1); int Get_H() { return hp;} int Get_M() { return mp;} int Get_S() { return sp;} Date Get_Day() { return d;} Clock():d(0,0,0) { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; } void SetZone(int z); int GetZone(); void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 Clock::Clock(int z1,int NewH, int NewM, int NewS,Date d1):d(d1) { hp=NewH; mp=NewM; sp=NewS; z=z1; } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { if(z==8) cout<<"作业答案查询

****北京时间作业答案查询

***\n"; if(z==0) cout<<"作业答案查询

****伦敦时间作业答案查询

***\n"; cout<<"日期: "<<d.Get_Y()<<"年"<<d.Get_M()<<"月"<<d.Get_D()<<" 日 "<<endl; cout<<"时间: "<<hp<<":"<<mp<<":"<<sp<<endl; cout<<"作业答案查询

作业答案查询

作业答案查询

作业答案查询

作业答案查询

****\n"; cout<<endl<<endl; }
B、/作业答案查询

请完成定义相关的时钟类,日期类作为时钟类的日期的类型,时钟类赋值通过构造函数赋值作业答案查询

*****/ class Clock //时钟类的声明 { public: //外部接口,公有成员函数 Clock(int z1,int NewH, int NewM, int NewS,Date d1); int Get_H() { return hp;} int Get_M() { return mp;} int Get_S() { return sp;} Date Get_Day() { return d;} Clock():d() { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; } void SetZone(int z); int GetZone(); void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 Clock::Clock(int z1,int NewH, int NewM, int NewS,Date d1):d1(d) { hp=NewH; mp=NewM; sp=NewS; z=z1; } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { if(z==8) cout<<"作业答案查询

****北京时间作业答案查询

***\n"; if(z==0) cout<<"作业答案查询

****伦敦时间作业答案查询

***\n"; cout<<"日期: "<<d.Get_Y()<<"年"<<d.Get_M()<<"月"<<d.Get_D()<<" 日 "<<endl; cout<<"时间: "<<hp<<":"<<mp<<":"<<sp<<endl; cout<<"作业答案查询

作业答案查询

作业答案查询

作业答案查询

作业答案查询

****\n"; cout<<endl<<endl; }
C、/作业答案查询

请完成定义相关的时钟类,日期类作为时钟类的日期的类型,时钟类赋值通过构造函数赋值作业答案查询

*****/ class Clock //时钟类的声明 { public: //外部接口,公有成员函数 Clock(int z1,int NewH, int NewM, int NewS,Date d1);//有参的构造函数 int Get_H() { return hp;} int Get_M() { return mp;} int Get_S() { return sp;} Date Get_Day() { return d;} Clock():d() //无参的构造函数 { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; } void SetZone(int z); //设置时区 int GetZone(); //获得时区 void DispTime(); //显示时间 void AddOneS() //时钟走动函数 { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 Clock::Clock(int z1,int NewH, int NewM, int NewS,Date d1):d(d1) //组合构造函数 { hp=NewH; mp=NewM; sp=NewS; z=z1; } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() //显示时间 { if(z==8) cout<<"作业答案查询

****北京时间作业答案查询

***\n"; if(z==0) cout<<"作业答案查询

****伦敦时间作业答案查询

***\n"; cout<<"日期: "<<d.Get_Y()<<"年"<<d.Get_M()<<"月"<<d.Get_D()<<" 日 "<<endl; cout<<"时间: "<<hp<<":"<<mp<<":"<<sp<<endl; cout<<"作业答案查询

作业答案查询

作业答案查询

作业答案查询

作业答案查询

****\n"; cout<<endl<<endl; }
D、/作业答案查询

请完成定义相关的时钟类,日期类作为时钟类的日期的类型,时钟类赋值通过构造函数赋值作业答案查询

*****/ class Clock //时钟类的声明 { public: //外部接口,公有成员函数 Clock(int z1,int NewH, int NewM, int NewS,Date d1); int Get_H() { return hp;} int Get_M() { return mp;} int Get_S() { return sp;} Date Get_Day() { return d;} Clock() { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; } void SetZone(int z); int GetZone(); void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } private: //私有数据成员 int hp,mp,sp,z; Date d; }; //时钟类成员函数的具体实现 Clock::Clock(int z1,int NewH, int NewM, int NewS,Date d1) { hp=NewH; mp=NewM; sp=NewS; z=z1; } //获取时区 int Clock::GetZone() { return z; } void Clock::DispTime() { if(z==8) cout<<"作业答案查询

****北京时间作业答案查询

***\n"; if(z==0) cout<<"作业答案查询

****伦敦时间作业答案查询

***\n"; cout<<"日期: "<<d.Get_Y()<<"年"<<d.Get_M()<<"月"<<d.Get_D()<<" 日 "<<endl; cout<<"时间: "<<hp<<":"<<mp<<":"<<sp<<endl; cout<<"作业答案查询

作业答案查询

作业答案查询

作业答案查询

作业答案查询

****\n"; cout<<endl<<endl; }

7、带时区的时钟(静态成员):一个小作坊,生产时钟能力有限,最多只能生产10个时钟,请统计该小作坊生产时钟的个数,并给每个时钟进行编号,编号从1开始;下面是完成该功能的主函数,请完成相应时钟类的相关信息; int main() { int i,j; Clock s[10]; //定义最多十个时钟 for(i=0;i<10;i++){ //给十个时钟计数、赋值和编号 s[i].init(); //count++;Number=count; for(j=0;j<=i;j++) s[j].DispTime(); cout<<"--------------"<<endl; system("pause"); system("CLS"); } }
A、#include<iostream> #include<windows.h> using namespace std; class Clock //时钟类的声明 { //私有数据成员 static int count=0; int Number; int hp,mp,sp,z; public: //外部接口,公有成员函数 Clock(int z1,int NewH, int NewM, int NewS); int Get_H() { return hp;} int Get_M() { return mp;} int Get_S() { return sp;} Clock() { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; } void init(){ count++; Number=count; } void DispTime(); void AddOneS() { sp++; if(sp>=60) { mp++; sp=0; if(mp>=60) { mp=0; hp++; if(hp>=12) { hp=0; } } } } }; //时钟类成员函数的具体实现 Clock::Clock(int z1,int NewH, int NewM, int NewS) { hp=NewH; mp=NewM; sp=NewS; z=z1; } //获取时区 void Clock::DispTime() { cout<<"No."<<Number<<" "; cout<<"count="<<count<<endl; cout<<"作业答案查询

****北京时间作业答案查询

***\n"; cout<<"时间: "<<hp<<":"<<mp<<":"<<sp<<endl; cout<<"作业答案查询

作业答案查询

作业答案查询

作业答案查询

作业答案查询

****\n"; }
B、#include<iostream> #include<windows.h> using namespace std; class Clock //时钟类的声明 { //私有数据成员 static int count; int Number; int hp,mp,sp,z; public: //外部接口,公有成员函数 Clock(int z1,int NewH, int NewM, int NewS); int Get_H() { return hp;} int Get_M() { return mp;} int Get_S() { return sp;} Clock() { SYSTEMTIME sys; GetLocalTime( &sys ); hp=sys.wHour; mp=sys.wMinute; sp=sys.wSecond; z=8; } void init(){ count++;

学习通C++程序设计基础

随着计算机技术的不断发展,C++编程语言越来越成为程序员们的首选。学习通C++程序设计基础课程,对于想要从事软件开发行业的人来说,是非常重要的。本文将对学习通C++程序设计基础课程进行详细的介绍。

C++是什么?

C++是一种面向对象的编程语言,它是由Bjarne Stroustrup所开发的。C++是C语言的一种升级版,它拥有C语言的所有特性,同时还具有面向对象编程的特性。C++语言可以用于开发各种类型的软件,包括Windows应用程序、游戏、数据库、浏览器和操作系统等。

学习通C++程序设计基础课程内容

学习通C++程序设计基础课程包含以下内容:

  • C++语言基础知识
  • 数据类型和变量
  • 运算符和表达式
  • 控制结构
  • 函数
  • 数组和指针
  • 字符串和结构体
  • 类和对象
  • 继承和多态
  • 文件I/O操作
  • 异常处理
  • 标准模板库

学习路线

学习通C++程序设计基础课程,需要按照以下路线进行学习:

  • 学习C++基础语法,了解C++的数据类型、运算符和表达式、控制结构以及函数等基本知识。
  • 学习C++的高级特性,包括数组和指针、字符串和结构体、类和对象、继承和多态等。
  • 学习C++的文件I/O操作和异常处理,掌握如何读取和写入文件,以及如何处理程序中出现的异常。
  • 学习C++的标准模板库,了解STL的容器和算法,以及如何使用迭代器。
  • 在学习过程中,需要进行实践编程,通过编写实际的程序案例来加深对C++语言的理解和掌握程度。

学习方式

学习通C++程序设计基础课程有以下几种方式:

  • 在网上进行自学,可以通过学习通平台上提供的视频、课件和练习题来学习。
  • 参加线下C++培训班,可以通过老师的讲解和互动来学习。
  • 参加C++编程社群,通过与编程爱好者交流、分享和学习,来提高自身的编程技能。

学习成果

通过学习通C++程序设计基础课程,可以获得以下成果:

  • 掌握C++语言的基础知识,包括数据类型、运算符、控制结构、函数等。
  • 掌握C++语言的高级特性,包括数组和指针、字符串和结构体、类和对象、继承和多态等。
  • 掌握C++语言的文件I/O操作和异常处理。
  • 掌握C++语言的标准模板库,了解STL的容器和算法,以及如何使用迭代器。
  • 能够使用C++语言编写实际的程序,从而提高自身的编程技能。

总结

学习通C++程序设计基础课程是学习C++编程语言的必经之路。通过系统学习C++语言的基础知识和高级特性,以及掌握C++语言的文件I/O操作和异常处理,可以提高自身的编程技能,从而在软件开发行业中获得更多的机会。