A. Equation(类与对象+构造)
题目描述建立一个类Equation,表达方程ax2+bx+c=0。类中至少包含以下方法:
1、无参构造(abc默认值为1.0、1.0、0)与有参构造函数,用于初始化a、b、c的值;
2、set方法,用于修改a、b、c的值
3、getRoot方法,求出方程的根。
一元二次方程的求根公式如下:
一元二次方程的求解分三种情况,如下:
输入
输入测试数据的组数t
第一组a、b、c
第二组a、b、c
输出
输出方程的根,结果到小数点后2位
在C++中,输出指定精度的参考代码如下:
#include <iostream>
#include <iomanip> //必须包含这个头文件
using namespace std;
void main( )
{ double a =3.14;
cout<<fixed<<setprecision(3)<<a<<endl; //输出小数点后3位
}
输入样例1
3
2 4 2
2 2 2
2 8 2输出样例1
x1=x2=-1.00
x1=-0.50+0.87i x2=-0.50-0.87i
x1=-0.27 x2=-3.73
#include<iostream> #include<iomanip> #include<cmath> using namespace std; class Equation { private: double a, b, c; public: Equation() { a = 1.0; b = 1.0; c = 1.0; } Equation(double aa, double bb, double cc):a(aa),b(bb),c(cc) { } void set(double aa, double bb, double cc) { a = aa; b = bb; c = cc; } void getroot() { double x1, x2; double real, ima; if ((b * b - 4 * a * c) > 0)//两个不相等实根 { x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a); cout << fixed << setprecision(2) << "x1=" << x1 << " x2=" << x2 << endl; } if ((b * b - 4 * a * c) == 0) //方程两个相等实根 { x1 = -b / (2 * a); cout << "x1=x2=" << fixed << setprecision(2) << x1 << endl; } if ((b * b - 4 * a * c) < 0) //两个共轭复根 { real = -b / (2 * a); ima = sqrt(-(b * b - 4 * a * c)) / (2 * a); cout << fixed << setprecision(2) << "x1=" << real << "+" << ima << "i x2=" << real << "-" << ima << "i" << endl; } } }; int main() { int t; cin >> t; double a, b, c; while (t--) { cin >> a >> b >> c; Equation e1; e1.set(a, b, c); e1.getroot(); } }
B. 对象是怎样构造的(拷贝构造函数)
题目描述
某个类包含一个整型数据成员.程序运行时若输入0表示用缺省方式定义一个类对象;输入1及一个整数表示用带一个参数的构造函数构造一个类对象;输入2及一个整数表示构造2个类对象,一个用输入的参数构造,另一个用前一个对象构造。试完成该类的定义和实现。
输入
测试数据的组数 t
第一组数
第二组数
......
输出
第一个对象构造输出
第二个对象构造输出
......
样例查看模式
正常显示查看格式
输入样例1 <-复制
3
0
1 10
2 20输出样例1
Constructed by default, value = 0
Constructed using one argument constructor, value = 10
Constructed using one argument constructor, value = 20
Constructed using copy constructor, value = 20
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class person
{
private:
int n;
public:
person()
{
n = 0;
cout << "Constructed by default, value = " << n << endl;
}
person(int nn):n(nn)
{
cout << "Constructed using one argument constructor, value = " << n << endl;
}
person(const person& p1)
{
n = p1.n;
cout << "Constructed using copy constructor, value = " << n << endl;
}
};
int main()
{
int t;
cin >> t;
int x,n;
while (t--)
{
cin >> x;
if (x == 0)
{
person p1; //不加括号 无参构造
}
if (x == 1)
{
cin >> n;
person p2(n);
}
if (x == 2)
{
cin >> n;
person p3(n);
person p4(p3);
}
}
}
C. 电话号码升位(拷贝构造函数)
题目描述
定义一个电话号码类CTelNumber,包含1个字符指针数据成员,以及构造、析构、打印及拷贝构造函数。
字符指针是用于动态创建一个字符数组,然后保存外来输入的电话号码
构造函数的功能是为对象设置键盘输入的7位电话号码,
拷贝构造函数的功能是用原来7位号码的对象升位为8位号码对象,也就是说拷贝构造的对象是源对象的升级.电话升位的规则是原2、3、4开头的电话号码前面加8,原5、6、7、8开头的前面加2。
注意:电话号码只能全部是数字字符,且与上述情况不符的输入均为非法)
输入
测试数据的组数 t
第一个7位号码
第二个7位号码
......
输出
第一个号码升位后的号码
第二个号码升位后的号码
......
如果号码升级不成功,则输出报错信息,具体看示例
样例查看模式
正常显示查看格式
输入样例1 <-复制
3
6545889
3335656
565655输出样例1
26545889
83335656
Illegal phone number
#include<iostream> #include<iomanip> #include<cmath> #include<cstring> //strcpy using namespace std; class Ctelnumber { private: char* p = NULL; public: Ctelnumber(char* p_p) { p = new char[8]; //用字符指针 动态创建字符数组 strcpy(p, p_p); //保存输入的电话号码 } void print() { for (int i = 0; i < 8; i++) { cout << p[i]; } cout << endl; } Ctelnumber(const Ctelnumber& c1) //拷贝升位 { p = new char[9]; if (c1.p[0] == '2' || c1.p[0] == '3' || c1.p[0] == '4') { p[0] = '8';//2 3 4开头的 号码首位加个8 } else { p[0] = '2';// 其他+2 } //除了8和2 的后七位 for (int i = 0; i < 7; i++) { p[i + 1] = c1.p[i]; //新号码的第二位 赋值为 原来号码的第一位 后面同理 } } ~Ctelnumber()//析构堆区开辟的数据 { if (p != NULL) { delete p; p = NULL; } } }; int main() { int t; int i, flag; cin >> t; while (t--) { char* p = new char[8]; cin >> p; flag = 1; for (i = 0; i < 7; i++) { if (p[i] > '9' || p[i] < '0') { flag = 0; break; //输入不合法 } } if (flag) //若为1执行 若为0表示输入不合法 { if (strlen(p) == 7) //如果输入长度为7 { if (p[0] >= '2' && p[0] <= '8') //且首位字母为2345678 { Ctelnumber c1(p); Ctelnumber c2(c1); //拷贝升位 c2.print(); } else cout << "Illegal phone number" << endl; //首字母不是2345678 } } else cout << "Illegal phone number" << endl; //长度不为7 } }