C++程序设计 上机实验(第14章——C++工具)

本文通过两个C++程序示例,分别展示了如何处理一元二次方程无实根的情况以及异常处理在学生数据管理中的应用。在第一个程序中,通过try-catch结构捕获方程无解时的异常,并输出警告信息。第二个程序展示了不同命名空间中同名类的使用,避免了名称冲突,并利用异常处理显示错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 求一元二次方程式ax2+bx+c=0ax^2+bx+c=0ax2+bx+c=0的实根,如果方程没有实根,则输出有关警告信息。

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	double q(double, double, double);
	double a, b, c, p, x1, x2;
	cout << "please enter a,b,c:";
	cin >> a >> b >> c;
	p = -b / (2 * a);
	try
	{
		x1 = p + q(a, b, c);
		x2 = p - q(a, b, c);
		cout << "x1=" << x1 << endl << "x2=" << x2 << endl;
	}
	catch(double d)
	{
		cout << "a=" << a << ",b=" << b << ",c=" << c << ",disc=" << d << ",error!" << endl;
	}
	cout << "end" << endl;
	return 0;	
}

double q(double a, double b, double c)
{
	double disc;
	disc = b * b - 4 * a * c;
	if (disc < 0) throw disc;
	return sqrt(disc) / (2 * a);
}

运行结果:

2. 分析程序执行过程,写出允许结果。并指出由于异常处理而调用了哪些析构函数

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
	Student(int n, string nam)
	{
		cout << "constructor - " << n << endl;
		num = n;
		name = nam;
	}
	~Student()
	{
		cout << "destructor - " << num << endl;
	}
	void get_data();
private:
	int num;
	string name;
};

void Student::get_data()
{
	if (num == 0) throw num;
	else cout << num << " " << name << endl;
	cout << "in get_data()" << endl;
}

void fun()
{
	Student stud1(1101, "tan");
	stud1.get_data();
	try
	{
		Student stud2(0, "Li");
		stud2.get_data();
	}
	catch(int n)
	{ cout << "num=" << n << ",error!" << endl; }
}

int main()
{
	cout << "main begin" << endl;
	cout << "call fun()" << endl;
	fun();
	cout << "main end" << endl;
	return 0;
}

程序分析:

  • 由于在fun函数中有catch处理器,catch处理器捕获异常信息num,并将num的值赋给了变量n。此时流程脱离try块,系统开始进行析构工作,对于从相应的try块开始到throw语句抛出异常信息这段过程中已构造而未析构的局部对象(在本程序中为stud2)进行析构,输出"destructor - 0",然后执行catch处理块中的语句,输出"num=0,error!"。fun函数已执行完毕,在流程转回main函数之前先调用对象stud1的析构函数,输出"destructor - 1101",最后执行main函数中最后一行cout语句。

运行结果:

3. 学校的人事部门保存了有关学生的部分数据(学号、姓名、年龄、住址),教务部门也保存了学生的另外一些数据(学号、姓名、性别、成绩),两个部门分别编写了本部门的学生数据管理程序,其中都用了Student作为类名。现在要求在全校的学生数据管理程序中调用这两个部门的学生数据,分别输出两种内容的学生数据。要求用C++编程,使用命名空间。

//header1.h(头文件1,文件名为xt14-3-h1.h)
#include<string>
namespace student1
{
	class Student
	{
	public:
		Student(int n, string nam, int a, string addr)
		{
			num = n;
			name = nam;
			age = a;
			address = addr;
		}
		void show_data();
	private:
		int num;
		string name;
		int age;
		string address;
	};

	void Student::show_data()
	{
		cout << "num:" << num << "name:" << name << "age:" << age << "address:" << address << endl;
	}
}

//header2(头文件2,文件名为xt14-3-h2.h)
#include<string>
namespace student2
{
	class Student
	{
	public:
		Student(int n, string nam, char s, float sco)
		{
			num = n;
			name = nam;
			sex = s;
			score = sco;
		}
		void show_data();
	private:
		int num;
		string name;
		char sex;
		float score;
	};
	void Student::show_data()
	{
		cout << "num:" << num << "name:" << name << "sex:" << sex << "score:" << score << endl;
	}
}

//main file(主函数)
#include<iostream>
#include"xt14-3-h1.h" //头文件1
#include"xt14-3-h2.h" //头文件2
using namespace std;
int main()
{
	Student stud1(1001, "Wang", 18, "123 Beijing Road,Shanghai");
	stud1.show_data();
	student2::Student stud2(1102, "Li", 'f', 89.5);
	stud2.show_data();
	return 0;
}

程序分析:

  • stud1是用命名空间student1中的Student类定义的,stud2是用命名空间student2中的Student类定义的。由于在主文件的开头已用了using namespace student1作了声明,因此对命名空间student1的成员不必再用命名空间名作显式限定(不必写成student1::Student),程序中的Student就是student1中的Student。
  • 由于两个命名空间中有同名的类student,因此程序中只能用一个using namespace语句对一个命名空间进行声明,不能写成
using namespace student1;
using namespace student2;
  • 如果这样写,表示这两个命名空间中所用的标识符在本文件中都是全局量(不必加命名空间限定),这时两个命名空间中的类名Student就会发生同名冲突,系统无法判别它们是哪个命名空间的Student。所以不用using namespace student2,而对student2中的成员分别用命名空间加以限定(如student::Student)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值