第1章 C++语言简介
一、简答题
1. 请介绍C++语言的发展过程
C++语言源于C语言,1972年,AT&T公司设计了C语言并得到广泛应用。为了使得C语言能够支持面向对象技术,在20世纪80年代,Bell实验室设计了一门新的语言C++。1985年起,C++语言开始进入商业应用阶段,并由此进入了高速发展时期,各种面向对象程序设计思想与技术逐步融入到C++语言之中。在C++语言标准中,先后提供了对虚函数、重载、继承、模板和命名空间技术的支持。现在的C++语言已经是一门完全独立的语言,而不再是对C语言的简单扩充。
2.试比较C++语言与Java语言的优缺点。
(1)安全性。用Java语言编写的程序,其安全性要高于用C++语言编写的程序。Java语言对语法做了更多的限制,添加了更多的数据检测内容,从而减少了程序出错的概率。更重要的是,Java语句取消了指针数据类型,并且添加了内存自动回收功能,从而避免了系统内容出错的情况。而这一切都是C++程序设计中极其容易出现错误的地方。
(2)效率。虽然C++程序的安全性相对低于Java程序,但是,C++程序的运行效率确是Java程序难以企及的。一方面,Java程序需要Java虚拟机对中间代码进行解释执行,而C++程序则是一次编译后可以立即执行;另一方面,在C++程序中,指针数据类型的使用可
以大幅度提高程序运行效率和程序的灵活性,而这都是Java程序所不具备的。
二、程序设计题
创建一个简单C++程序,在屏幕上显示This is a C++ program。【步骤】
用户利用VC6.0开发工具,创建一个空的Win32控制台项目,在项目中添加一个空的cpp文件,添加如下代码。
#include
using namespace std;
int main(int argc, char** argv)
{
cout << "This is a C++ program." << endl;
system("PAUSE");
return 0;
}
第2章数据描述与基本运算
简答题
1. 请指出下面标识符中,哪些是合法的变量名称。
abc, _3x, _b为合法变量名称,其他为非法。
2.请指出下面哪些是合法的常量取值。
23,3e-5,'\n',12.56E,"abc"为合法常量取值。
3. 请给出下面表达式的取值。
(1)-3 + 4 * 5 – 6 =11
(2)-3 + 4 % 5 – 6 =-5
(3)-3 * 4 % -6 / 5 =0
(4)(7 + 6)% 5 / 2 =1
4.请完成下列表达式。
(1)sin30°- tan20°:sin(30 * 3.14 / 180) – tan(20 * 3.14 / 180)
(2)232- :pow(232, -sqrt( a *a + b * b))
5.求表达式值。
(1)1 < 4 && 4 > 10 = 0;
(2)! (5 >= 15) || (2 < 4 - 3) = true;
第3章 C++语句
一、简答题
1.答:语句是程序设计语言中的最小可执行单位,是构成程序的基础。语句由表达式构成,与表达式相比较,语句需要有分号表示语句结束。
2.答:C++语言与C语言有显著不同,首先,在语法格式上,C++除了兼容C语言语法外,还增加了很多新的表达方式,更重要的是,C++语言支持面向对象程序设计,涵盖了继承、封装与多态等重要内容,这是C语言中所不具备的。
3. (需要在程序中加入cmath头函数:#include
(1)pow(1 + x * y , 6)
(2)a * sin(x + 5) * sin(x + 5) + b * cos(6 * y - c);
(3)4 * y * (1 - pow(x, y))
(4)(4 / y) / ((5 *x) / (7 * y) - 6 / (5 * z - 8 * x))
4.Continue与break都表示终止执行当前循环过程,continue语句要求重新回到循环开始处,重新判断循环条件,决定是否执行循环体;而break则要求退出循环,执行循环体后的语句。
5.typedef struct _tmp
{
int age;
char sex;
}TMP;
TMP tmpInf[10];
二、程序设计题
1.参考代码
#include
#include
using namespace std;
int main(int argc, char** argv)
{
double y = 0.0;
int x = 0;
cout << "Please input x:";
cin >> x;
if (x < -1)
y = -x + 9;
else if (x < 0)
y = 2 * x + 4.5;
else if (x < 1)
y = x * x + 4 * x - 7;
else
y = 3 * sin(x) + 12;
cout << "y = " << y << endl; }
2.参考代码
#include
#include
using namespace std;
int main(int argc, char** argv) {
double a = 0.0;
double b = 0.0;
double c = 0.0;
double x = 0.0;
double delta = 0.0;
cout << "Please input a:";
cin >> a;
cout << "Please input b:";
cin >> b;
cout << "Please input c:";
cin >> c;
delta = sqrt(b * b - 4 * a * c);
if (delta < 0)
cout << "x is no real number!" << endl;
else if(delta == 0)
cout << "x = " << -b / (2 * a) << endl;
else
cout << "x = " << (-b + delta) / (2 * a) << "; " << (-b - delta) / (2 * a) << endl;
return 0;
}
3.参考代码
#include
#include
using namespace std;
int fact(int a)
{
if (a == 0 || a == 1)
return 1;
else
return fact(a - 1) * a;
}
int main(int argc, char** argv) {
int x = 0;
double y = 0.0;
double tmp = 0.0;
int i = 0;
cout << "Please input x:";
cin >> x;
do
{
tmp = pow(x, i) / fact(i);
y = y + tmp;
i++;
} while (tmp >= pow(10, -8));
cout << "e^" << x << " = " << y << endl;
return 0;
}
4.参考代码
#include
using namespace std;
int leapyear(int a)
{
if((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0)) return a;
else
return -1;
}
int main(int argc, char** argv)
{
int year;
cout << "Please input year:";
cin >> year;
if (leapyear(year) > 0)
cout << year << " is leapyear." << endl;
else
cout << year << " is not leapyear." << endl;
return 0;
}
第4章数组与结构体
一、简答题
1.字符数组初始化通常两种形式,一是在定义时将其初始为空串,即赋值为{0};另外是使用memset函数,将字符数组设定为空或其他指定形式。
2.结构体与共用体有很多相似地方,但也有重要区别,主要是共用体在存储时使用覆盖技术,可以共用同一个存储空间,而结构体没有这个功能。
二、程序设计题
1. 参考代码
#include
using namespace std;
int main(int argc, char** argv)
{
int scores[8] = { 0 };
int maxscore = 0;
int minscore = 0;
int totalsocre = 0;
double avgscore = 0.0;
int i = 0;
for(i = 0; i < 8; i++)
{
cout << "Please input NO." << i + 1 << " score:";
cin >> scores[i];
}
maxscore = scores[0];
minscore = scores[0];
totalsocre = scores[0];
for (i = 1; i < 8; i++)
{
if(scores[i] > maxscore)
maxscore = scores[i];
if (scores[i] < minscore)
minscore = scores[i];
totalsocre = totalsocre + scores[i];
}
avgscore = (totalsocre - maxscore - minscore) / 6.0;
cout << "MaxScore:" << maxscore << endl;
cout << "MinScore:" << minscore << endl;
cout << "AvgScore:" << avgscore << endl;
return 0;
}
2. 参考代码
#include
using namespace std;
typedef struct _stuinf{
char name[16];
int sex; //0-man, 1-woman
int age;
}STUINF;
int main(int argc, char** argv)
{
STUINF stuInf[10] = {0};
int i = 0;
int ages = 0;
int mages = 0;
int wages = 0;
int mcount = 0;
int wcount = 0;
double avgages = 0.0;
for (i = 0; i < 10; i ++)
{
cout << "Please input NO." << i + 1 << " Student information:" << endl;
cout << "NAME:";
cin >> stuInf[i].name;
cout << "SEX(0-MAN;1-WOMAN):";
cin >> stuInf[i].sex;
cout << "AGE:";
cin >> stuInf[i].age;
}
for(i = 0; i < 10; i++)
{
if(stuInf[i].sex == 0)
{
mages += stuInf[i].age;
mcount++;
}
else
{
wages += stuInf[i].age;
wcount++;
}
}
ages = mages + wages;
cout << "Man Avg Ages:" << mages / double(mcount) << endl;
cout << "Woman Avg Ages:" << wages / double(wcount) << endl;
cout << "Avg Ages:" << ages / 10.0;
return 0;
}
第5章指针
一、简答题
1.运行结果:
3
23
123
2.运行结果:
a = 5;
b = -2;
c = 7
二、程序设计题
参考代码
#include
#include
using namespace std;
void getStr(char* inStr, char* outStr, int inPos) {
int i = 0;
char* tmp = inStr;
while (*tmp != '\0' && i < inPos)
{
tmp++;
i++;
}
i = 0;
while(*tmp != '\0')
{
*outStr = *tmp;
outStr++;
tmp++;
}
*outStr = '\0';
}
int main(int argc, char** argv)
{
char srcStr[64] = {0};
char dstStr[64] = {0};
char* p = NULL;
int pos = 0;
int startpos = 0;
int i = 0;
fflush(stdin);
cout << "Please input Source String:";
cin.getline(srcStr, sizeof(srcStr));
cout << "Please input Start Position:";
cin >> startpos;
getStr(srcStr, dstStr, startpos);
cout << dstStr << endl;
return 0;
}
第6章函数
一、选择题
C C A C
二、程序设计题
参考代码
#include
#define ARRCOUNT 4
using namespace std;
void test(int (&inArr)[ARRCOUNT])
{
int i = 0;
int j = 0;
int tmp = 0;
for(i = 0; i < ARRCOUNT; i++)
{
for(j = 0; j < ARRCOUNT - i - 1; j++)
{
if(inArr[j] < inArr[j + 1])
{
tmp = inArr[j];
inArr[j] = inArr[j + 1];
inArr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[ARRCOUNT] = { 23, 15, 56, 0 };
test(arr);
int i = 0;
for(i = 0; i < ARRCOUNT; i++)
cout << arr[i] << endl;
}
第7章类与对象
一、选择题
D C D D C
二、程序设计题
参考代码
#include
using namespace std;
class employee
{
public:
employee();
~employee();
void changename(char* inStr);
void display();
protected:
char name[16];
char addr[64];
char postno[8];
};
employee::employee()
{
memset(name, 0, sizeof(name));
memset(addr, 0, sizeof(addr));
memset(postno, 0, sizeof(postno)); }
employee::~employee()
{
memset(name, 0, sizeof(name));
memset(addr, 0, sizeof(addr));
memset(postno, 0, sizeof(postno));
}
void employee::changename(char* inStr) {
memset(name, 0, sizeof(name));
strcpy(name, inStr);
}
void employee::display()
{
cout << "Name:" << name << endl;
cout << "Addr:" << addr << endl;
cout << "PostNO:" << postno << endl; }
int main(int argc, char** argv)
{
employee worker;
worker.display();
worker.changename("Tom");
worker.display();
return 0;
}
第8章类的继承与派生一、选择题
1~4 C A D B;5~8 C D B A
二、程序设计题
1.参考代码
#include
using namespace std;
class wehicle
{
public:
wehicle(int x, double y);
~wehicle();
void listinf();
protected:
int wheels;
int weight;
};
实验一C语言的运行环境的使用 一、目的与要求 1. 了解Windows系统下C语言的运行环境,熟悉C程序调试、运行的基本操作方法。 2. 熟练掌握编辑、编译、连接和运行C程序的方法。 3. 通过运行简单的C程序,初步了解C源程序的特点。 4. 初步理解C语言的数据类型,了解变量定义、变量赋值以及简单运算方法,了解程序运 行结果的基本输出方法。 二、实验例题 在C语言运行环境下,按以下例题要求完成程序的编辑、编译、连接和运行,直至取得正确的运行结果。 【例1】编程实现在屏幕上显示如下三行文字 Hello, world ! Wolcome to the C language world! Everyone has been waiting for. (1)输入如下程序: #include
《C语言程序设计》大作业报告 1.目的 掌握所学C语言程序设计的方法,熟悉所学语言的开发环境及调试过程,熟悉所学C语言中的数据类型,数据结构、语句结构、运算方法,巩固和加深对理论课中知识的理解,提高学生对所学知识的综合运用能力。通过综合设计要求达到下列基本技能: 1.培养查阅参考资料、手册的自学能力,通过独立思考深入钻研问题,学会自己分析、解决问题。 2.通过对所选题目方案分析比较,确立方案,编制与调试程序,初步掌握程序设计的方法,能熟练调试程序。 2.作业内容
熟练掌握所学语言的基本知识:数据类型(整形、实型、字符型、指针、数组、结构等);运算类型(算术运算、逻辑运算、自增自减运算、赋值运算等);程序结构(顺序结构、判断选择结构、循环结构);大程序的功能分解方法(即函数的使用)等。进一步掌握各种函数的应用等。 3.要求: 1.要求每个同学都要认真对待,积极参与。 2.独立完成,不能抄袭。 3.课程设计结束时每位同学必须完成《大作业报告册》,其中包含设计源 代码和设计思路。 4.不符合要求的程序、设计报告、抄袭的设计报告或源程序代码、在设 计中完全未参与的将作不及格处理。 5.统一格式,A4打印,按时提交。 4.题目:设计要求:编写一个程序,求3x4数组的转置矩阵。要求在main函数里面读数,在change函数里面把矩阵转置。 5.程序设计 设计思路:1是先定义两个数组,一个是a[3][4],另一个是b[4][3]。2是将随便输入的12个数输入到a[3][4]。3是在change函数中将a[3][4]中值通过for循环的镶嵌将数组a[3][4]的值赋值给数组b[4][3]。4在主函数中将数组b[4][3]通过for循环的嵌套输出。 代码
谭浩强c++程序设计课后答案 娄警卫