//定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数、
//输入坐标的函数,实现矩形加法,以及计算并输出矩形面积的函数。要求使用提示中给出的测试函数并不得改动。
// 两个矩形相加的规则是:决定矩形的对应坐标分别相加,如
// 左下角(1,2),右上角(3,4)的矩形,与
// 左下角(2,3),右上角(4,5)的矩形相加,得到的矩形是
// 左下角(3,5),右上角(7,9)的矩形。
// 这个规则没有几何意义,就这么定义好了。
// 输出面积的功能通过重载"<<"运算完成。
// 本题可以在2383的基础上扩展完成。
//
//Input
//测试函数中第一个矩形直接初始化,第二个矩形通过键盘输入。输入四个数,
//分别表示第二个矩形左下角和右上角顶点的坐标,如输入2.5 1.8 4.3 2.5,
//代表左下角坐标为(2.5, 1.8),右上角坐标为(4.3, 2.5)。
//
//
//Output
//输出两点相加后得到的点的面积。运行测试函数时,p1的顶点是1 1 6 3,
//如果输入的p2是2.5 1.8 4.3 2.5,计算得到的矩形p3的左下角坐标为(3.5, 2.8),
//右上角坐标为(10.3, 5.5),输出为p3的面积18.36。
#include <iostream>
using namespace std;
class Coordinate
{
public:
double x,y;
Coordinate(){}
Coordinate(double xx,double yy):x(xx),y(yy){}
};
class Rectangle:public Coordinate
{
private:
Coordinate left,right;
public:
Rectangle(){}
Rectangle(double lx,double ly,double rx,double ry):left(lx,ly),right(rx,ry){}
void input()
{
cin>>left.x>>left.y>>right.x>>right.y;
}
Rectangle operator+(Rectangle&r1)
{
Rectangle r;
r.left.x=left.x+r1.left.x;
r.left.y=left.y+r1.left.y;
r.right.x=right.x+r1.right.x;
r.right.y=right.y+r1.right.y;
return r;
}
friend ostream & operator<<(ostream&out,Rectangle&r);
};
ostream & operator<<(ostream&out,Rectangle&r)
{
out<<((r.right.x-r.left.x)*(r.right.y-r.left.y))<<endl;
return out;
}
int main()
{
Rectangle p1(1,1,6,3),p2,p3;
p2.input();
p3=p1+p2;
cout<<p3;
return 0;
}
</pre><pre name="code" class="cpp">2
</pre><pre name="code" class="cpp">
<pre name="code" class="cpp">//小华历经12寒窗苦读,又经历4年大学磨砺,终于毕业了,随着毕业季的到来,
//找工作也日益紧张起来。由于要面试不同的公司,因此小华需要准备不同的简历。
//当然最基本的信息是必不可少的,基本信息:姓名、年龄、性别、专业。现有两家公司,
//小华想要投简历试一试。第一家langchao公司需要了解小华毕业学校,第二家lenovo公司需要了解小华的薪金目标(pay)。
//请你用类中继承的方法帮助他完成简历吧*-* *-* *-*
//
//Input
//输入两行,第一行:小华的信息。第二行:小华想要进入的公司名称。
//
//Output
//小华要递交的简历内容
#include <iostream>
#include <string>
using namespace std;
class jianli
{
public:
void getvalue(){
cin>>name>>age>>sex>>major;
}
void display();
private:
string name;
int age;
char sex;
string major;
};class jianli1:public jianli
{
public:
void getvalue1()
{
cin>>school;
}
void display1();
private:
string school;
};
class jianli2:public jianli
{
public:
void getvalue2()
{
cin>>salary;
}
void display2();
private:
string salary;
};
void jianli::display()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"major:"<<major<<endl;
}
void jianli1::display1()
{
cout<<"school:"<<school<<endl;
}
void jianli2::display2()
{
cout<<"pay:"<<salary<<endl;
}
int main()
{
jianli1 xiaohua;
jianli2 xiaohua2;
string a="langchao",b="lenovo";
string c;
xiaohua.getvalue();
xiaohua.getvalue1();
xiaohua2.getvalue2();
cin>>c;
if(c==a){
xiaohua.display();
xiaohua.display1();
}
if(c==b){
xiaohua.display();
xiaohua2.display2();
}
return 0;
}
3
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">//
//已知长度为n的线性表A采用顺序存储结构,请写一时间复杂度为0(n)、空间复杂度为0(1)的算法,
//该算法删除线性表中所有值为item的数据元素。(O(1)表示算法的辅助空间为常量)。
//Input
//输入 n:6
//
//输入数据:1 2 3 4 5 6
//
//输入 item:5
//
//Output
//输出:1 2 3 4 6
#include <iostream>
using namespace std;
int main()
{
int n,item,i,num=0;
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
cin>>item;
for(i=0;i<n;i++)
{
if(a[i]==item)
{
num=i;
}
}
for(i=0;i<n;i++)
{
if(i==num)
{
i++;
}
cout<<a[i]<<" ";
}
return 0;
}
4
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">//(线性表)请写一个算法将顺序存储结构的线性表(a1...an)逆置为(an...a1)。
//Input
//输入长度n:5
//
//输入数据:1 2 3 4 5
//
//Output
//5 4 3 2 1
#include <iostream>
using namespace std;
int main()
{
int n,i;
cin>>n;
int a[n],b[n];
for(i=0;i<n;i++)
{
cin>>a[i];
b[n-i-1]=a[i];
}
for(i=0;i<n;i++)
{
cout<<b[i]<<" ";
}
return 0;
}
5
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">//小慧小时候很喜欢和她的小伙伴们一起玩剪刀(Scissors)、石头(Rock)、布(Cloth)的游戏,
//但现在她上大学了,和小伙伴们不能经常见面,但可以通过网络交流,她现在很想和小伙伴们重温这个小游戏。
//学了c++的你,可以用类帮她完成这个小游戏吗?每局游戏进行n场对战,赢得次数多的是最后的赢家,可以出现平局。
//
//Input
//第1行 n(1<=n<=20) ,表示下面有n组对战信息。
//从第2行到第n+1行,每行两个英文单词,表示对战信息。小慧的信息是第一列
//
//Output
//一个英文字母W或L或D 表示小慧的赢,输或平手。
#include <iostream>
using namespace std;
class RSC
{
private:
char a[21][10];
char b[21][10];
int n;
char result;
int judge(char a,char b);
public:
void input();
void output();
void decide();
};
//以下请给出 成员函数judge
int RSC::judge(char a,char b)
{
switch(a)
{
case 'S':
switch(b)
{
case 'S':return 0;break;
case 'R':return -1;break;
case 'C':return 1;break;
}
break;
case 'R':
switch(b)
{
case 'S':return 1;break;
case 'R':return 0;break;
case 'C':return -1;break;
}
break;
case 'C':
switch(b)
{
case 'S':return -1;break;
case 'R':return 1;break;
case 'C':return 0;break;
}
break;
}
}
//以下代码会自动添加到程序的末尾
void RSC::input()
{
cin>>n;
int i;
for(i=0; i<n; i++)
cin>> a[i]>>b[i];
}
void RSC::output()
{
decide();
cout<<result<<endl;
}
void RSC::decide()
{
int num=0,i;
for(i=0; i<n; i++)
num = num + judge(a[i][0],b[i][0]);
if(num>0)
result ='W';
else if(num==0)
result= 'D';
else
result = 'L';
}
int main()
{
RSC test;
test.input();
test.output();
return 0;
}
6
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">//将输入的学生信息按照要求重新输出。
//
//Input
//第一行是整数t,表明数据组数
//在每组数据中,
//第一行先是整数n(n<100),表示有n个学生。
//接下来有n行,每行表示一个学生。先是一个无空格的字符串,表示姓名,然后是一个非负整数,表示学号。
//姓名长度不超过100字符,学号小于1000。
//
//Output
//按照输入的顺序,输出每个学生的信息。先输出学号,再输出姓名,中间用单个空格隔开。
//一组数据处理完后,要输出一行 "****"。
#include <iostream>
using namespace std;
class Student
{
public:
Student(){}
Student(string na,int n):name(na),num(n){}
void Read()
{
cin>>name>>num;
}
void Print()
{
cout<<num<<" "<<name<<endl;
}
private:
string name;
int num;
};
int main()
{
int t;
cin >> t;
Student s("Tom",12);
while( t-- )
{
int n;
cin >> n;
Student st;
for( int i = 0;i < n; ++i)
{
st.Read();
st.Print();
}
cout << "****" << endl;
}
return 0;
}
7
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">//小慧把手机掉了,着急的她想让你帮她找到,她的手机是很高级的,离开主人一定时间后就会发射固定频率的电磁波。
//现在你有一个仪器可以接受电磁波并能确定手机坐标,请你编程来确定小慧和手机的距离。
//
//Input
//4个double型的数,前两个是手机的坐标,后两个是小慧的坐标。
//
//Output
//小慧与手机的距离。(保留两位小数)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
using namespace std;
class Point
{
public:
Point(double a=0,double b=0,double c=0,double d=0):x1(a),y1(b),x2(c),y2(d) {}
void input();
friend void output(Point &);
private:
double x1;
double y1;
double x2;
double y2;
};
//*************
void Point::input()
{
cin>>x1>>y1>>x2>>y2;
}
void output(Point &p)
{
cout<<fixed<<setprecision(2);
cout<<"There are ";
cout<<sqrt((p.x1-p.x2)*(p.x1-p.x2)+(p.y1-p.y2)*(p.y1-p.y2));
cout<<" meters between xiaohui and her phone."<<endl;
}
//以下代码会自动添加到程序的末尾
int main()
{
Point p1;
p1.input();
output(p1);
return 0;
}
</pre><pre name="code" class="cpp">8
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">//定义一个矩形类,数据成员包括左下角和右上角坐标,
//定义的成员函数包括必要的构造函数、输入坐标的函数,以及计算并输出矩形面积的函数。
//要求使用提示中给出的测试函数并不得改动。
#include <iostream>
using namespace std;
class Coordinate
{
public:
double x,y;
Coordinate(){}
Coordinate(double xx,double yy):x(xx),y(yy){}
};
class Rectangle:public Coordinate
{
private:
Coordinate left,right;
public:
Rectangle(){}
Rectangle(double lx,double ly,double rx,double ry):left(lx,ly),right(rx,ry){}
Rectangle(Rectangle&r)
{
*this=r;
}
void input()
{
cin>>left.x>>left.y>>right.x>>right.y;
}
void output()
{
cout<<((right.x-left.x)*(right.y-left.y))<<endl;
}
};
int main()
{
Rectangle p1;
p1.input();
p1.output();
Rectangle p2(p1);
p2.output();
Rectangle p3(1,1,6,3);
p3.output();
return 0;
}
</pre><pre name="code" class="cpp">9
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">//Student类含有私有数据成员:num,name,sex,公有成员函数:
//输入函数get_value()和输出函数display()。采用私有继承方式实现类Student1,
//增加数据成员:age,addr,成员函数:get_value_1()和display_1()。
//在程序运行时输入num,name,sex,age,addr的值,调用输出函数输出以上5个数据的值。
//
#include <iostream>
using namespace std;
class Student
{
public:
void get_value()
{
cin>>num>>name>>sex;
}
void display( )
{
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
private :
int num;
string name;
char sex;
};
//**************
class Student1 :private Student
{
public:
void get_value_1()
{
get_value();
cin>>age>>addr;
}
void display_1( )
{
display( );
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl;
}
private :
int age;
string addr;
};
/* C++代码 */
int main()
{
Student1 stud1;
stud1.get_value_1();
stud1.display_1();
return 0;
}
10
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">//Student类含有私有数据成员:num,name,sex,公有成员函数:
//输入函数get_value()和输出函数display()。采用公用继承方式实现类Student1,
//增加数据成员:age,addr,成员函数:get_value_1()和display_1()。
//在程序运行时输入num,name,sex,age,addr的值,调用输出函数输出以上5个数据的值。
#include <iostream>
using namespace std;
class Student
{
public:
void get_value()
{
cin>>num>>name>>sex;
}
void display( )
{
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
private :
int num;
char name[10];
char sex;
};
//***************
class Student1 :public Student
{
public:
void get_value_1()
{
cin>>age>>addr;
}
void display_1( )
{
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl;
}
private :
int age;
string addr;
};
/* C++代码 */
int main()
{
Student1 stud1;
stud1.get_value();
stud1.get_value_1();
stud1.display();
stud1.display_1();
return 0;
}