【C++】实验十五

题目:

1、求一元二次方程ax2+bx+c=0的实根。如果方程没有实根,则利用异常处理处理机制输出有关警告信息

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

参考代码:

1、

#include <iostream>
#include <cmath>
using namespace std;
  double q(double,double,double);
  void main(){
      double a,b,c,p,x1,x2;
      cout<<"请输入abc:";
      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;
  }
 
double q(double a,double b,double c)
 {double t;
  t=b*b-4*a*c;
  if (t<0) throw t;
  return sqrt(t)/(2*a);
 }
  

2、

#include <string>//H1.h
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;
    }
}

#include <string>//H2.h
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;
    }
}

#include <iostream>//Main.cpp
using namespace std;
#include "H1.h"
#include "H2.h"
using namespace std;
using namespace student1;
 
void main()
{
    Student stud1(1001, "zhang", 23, "Beijing,Siheyuan");
    stud1.show_data();
    student2::Student stud2(1102, "Li", 'f', 89.5);
    stud2.show_data();
}
 

  • 17
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值