河北工业大学 C++课后习题实验习题

更新时间:2023-03-16 15:28:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

习题1

1.1 书写一个简单的 C++程序,从而理解 C++程序的结构及特点。该程序只由一个主函数组成,主函数的函数体中只包含一个语句: cout<<\ 【1.1 答】

#include void main() {

cout<<\ } 习题2

2.1 写出 C++标识符的命名规则,写出 5个合法的标识符和 5个不合法的标识符。

2.2 下列符号哪些是 C++标识符?哪些不是,为什么? 2m a+c int i*j mm data n-a m22 x -n a_1 【2.2 答】

mm data m22 x a_1 是C++ 标识符,int是是 C++标识符也是关键字 2.3 指出下列合法的常量,并说明其类型。 65538 1.3 2.1E4 7L 9uL '9' 'a' 123u 【2.3 答】 65538 整型常量 1.3 double型常量 2.1E4 double型常量 7L 长整型常量 9uL 无符号长整型常量

'9' 字符型常量 'a' 字符型常量 123u 无符号整型常量

2.4 写出符合 C++规则的 int、 double、char 和字符串4种类型的常量各 2个。

2.5 说明字符常量和字符串常量有何区别? 2.6 下列变量的定义是否合法?为什么? ⑴ INT i,j,k; ⑵ char c1,c2; ⑶ int a,b;float f,b; ⑷ unsigned int m ⑸ char: a1,a2,a3; ⑹ unsigned float x,y; 【2.6 答】

⑵和⑶合法。⑴的INT要小写;⑷少分号;⑸ char后多了冒号;⑹数据类型错误

2.7 将下列数学表示式写成 C++表达式。 (1 ) (2 ) (3) (4) 【2.7答】

(1 )(2*x*x+3*y*y)/(x-y)

(2 )(-b+sqrt(b*b-4*a*c))/(2*a) 和 (-b-sqrt(b*b-4*a*c))/(2*a) 或 (-b+sqrt(b*b-4*a*c))/2/a 和 (-b-sqrt(b*b-4*a*c))/2/a (3 )1+(1+a/b)/(1-(a/c)

(4 )x/sqrt(fabs(x*x*x+y*y*y+z*z*z)) 2.8 已有如下变量定义,求下列表达式的值。 ⑴

double x=1.2,y=8.5; int a=3;

x+a%3*(int)(x+y) ⑵

int a=2,b=3; double x=3.5,y=2.5;

(double)(a+b)/2+(int)x%(int)y ⑶

int x=4,y=8; (++x)*(--y) ⑷

int e=1,f=4,g=2; double m=10.5,n=4.0,k; k=(e+f)/g+sqrt(n)*1.2/g+m ⑸

double x=2.5,y=4.7; int a=7;

x+a%3*(int)(x+y)%2/4 ⑹ int a,b; int x;

x=(a=2,b=5,a++,b++,a+b) 【2.8 答】 ⑴ 1.2 ⑵ 3.5 ⑶ 35 ⑷ 13.7 ⑸ 2.5 ⑹ 9

⑴ 1.2

#include void main() {

double x=1.2,y=8.5; int a=3;

cout<<(x+a%3*(int)(x+y))<

#include void main() {

int a=2,b=3; double x=3.5,y=2.5;

cout<<((double)(a+b)/2+(int)x%(int)y)<

#include void main() {

int x=4,y=8;

cout<<((++x)*(--y))<

#include

#include void main() {

int e=1,f=4,g=2; double m=10.5,n=4.0,k; k=(e+f)/g+sqrt(n)*1.2/g+m; cout<

#include void main() {

double x=2.5,y=4.7; int a=7;

cout<<(x+a%3*(int)(x+y)%2/4)<

#include void main() { int a,b; int x;

x=(a=2,b=5,a++,b++,a+b); cout<

本文来源:https://www.bwwdw.com/article/oquv.html

Top