程序填空
1
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() { cout<<"默认构造:"<<number<<endl; }
MyClass(int n) {
number=n;
cout<<"构造:"<<number<<endl;
}
MyClass(const MyClass &other) {
number=other.number ;
cout<<"复制:"<<number<<endl;
}
~MyClass() { cout<<"析构"<<endl; }
private:
int number=4 ;
};
void fun(MyClass &p) {
MyClass temp(p);
}
int main() {
MyClass a, b(2);
fun(a);
return 0;
}
2
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int n) {
number=n;
cout<<"构造:"<<number<<endl;
}
MyClass(const MyClass &other) {
number=other.number;
cout<<"复制:"<<number<<endl;
}
~MyClass() { cout<<"析构"<<endl; }
private:
int number;
};
void fun(MyClass p) {
MyClass temp(p);
return;
}
int main() {
MyClass a(2);
fun(a);
return 0;
}
程序改错
1
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(double r=0, double i=0 )
{ real=r; imag=i; }
Complex add(Complex &);
void display() {
cout<<"("<<real<<", "<<imag<<"i)"<<endl;
}
private:
double real, imag;
};
Complex Complex::add(Complex &c)
{
return Complex(real+c.real,imag+c.imag);
}
int main() {
Complex c1(1.2,2.4),c2(3.5,-1),c3;
cout<<"c3="; c3.display();
c3=c1.add(c2);
cout<<"c3=c1+c2="; c3.display();
return 0;
}
2
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass()
{ cout<<"默认构造:"<<number<<endl; }
MyClass(int n) {
number=n;
cout<<"构造:"<<number<<endl;
}
MyClass(const MyClass &other) {
number=other.number;
cout<<"复制:"<<number<<endl;
}
~MyClass()
{ cout<<"析构"<<endl; }
private:
int number=6;
};
void fun(MyClass &p) {
MyClass temp(p);
cout<<"调用fun函数"<<endl;
}
int main() {
MyClass a, b(2);
fun(b);
return 0;
}
程序设计
1
#include<iostream>
#include<fstream>
using namespace std;
class Square {
public:
Square(float xx, float yy, float len):x(xx),y(yy),length(len){};
void resetSquare(float newX, float newY, float newLen){
x=newX;y=newY;length=newLen;
}
float getLen(){
return length;
}
double getArea() {
return length*length;
}
bool isEqual(Square &s) {
if(length==s.getLen())return true;
else return false;
}
private:
float x, y ;
float length ;
};
int main() {
float x,y,len;
cin>>x>>y>>len;
Square s1(x,y,len),s2(s1);
cout<<"s1 边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
cout<<"s2 边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
cout<<"是否相等: "<<s2.isEqual(s1)<<endl;
cin>>x>>y>>len;
s2.resetSquare(x,y,len);
cout<<"s1 边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
cout<<"s2 边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
cout<<"是否相等: "<<s2.isEqual(s1)<<endl;
ifstream in1("4.1.1.4_1-2_in.dat");
ofstream out1("4.1.1.4_1-2_out.dat");
while(in1>>x>>y>>len)
{
Square s1(x,y,len),s2(s1);
out1<<"s1 边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
out1<<"s2 边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
out1<<"是否相等: "<<s2.isEqual(s1)<<endl;
in1>>x>>y>>len;
s2.resetSquare(x,y,len);
out1<<"s1 边长: "<<s1.getLen()<<", 面积: "<<s1.getArea()<<endl;
out1<<"s2 边长: "<<s2.getLen()<<", 面积: "<<s2.getArea()<<endl;
out1<<"是否相等: "<<s2.isEqual(s1)<<endl<<endl;
}
in1.close();
out1.close();
return 0;
}
2
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
class Clock {
public:
Clock(int hh=0,int mm=0,int ss=0):hour(hh),minute(mm),second(ss){};
void setTime(int newH,int newM,int newS) {
hour=newH;minute=newM;second=newS;
}
void add(int n);
void dec();
int dist(Clock &c);
void showTime();
private:
int hour, minute, second ;
};
void Clock::add(int n) {
second+=n;
minute+=second/60;
second%=60;
hour+=minute/60;
minute%=60;
hour%24;
}
void Clock::dec() {
second--;
if(second<0){
second+=60;
minute--;
if(minute<0){
minute+=60;
hour=(hour+23)%24;
}
}
}
int Clock::dist(Clock &c) {
return abs((hour*3600+minute*60+second)-(c.hour*3600+c.minute*60+c.second));
}
void Clock::showTime() {
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main() {
int h,m,s,n;
cin>>h>>m>>s;
Clock c1(h,m,s),c2;
cout<<"时钟c1: "; c1.showTime();
cout<<"时钟c2: "; c2.showTime();
cout<<"时钟c1与c2相差"<<c1.dist(c2)<<"秒"<<endl;
cin>>n;
c1.add(n); c2.dec();
cout<<"时钟c1: "; c1.showTime();
cout<<"时钟c2: "; c2.showTime();
cout<<"时钟c1与c2相差"<<c1.dist(c2)<<"秒"<<endl;
cin>>h>>m>>s;
c1.setTime(h,m,s);
cout<<"重置后时钟c1: "; c1.showTime();
ifstream in1("4.1.5_1-5.in");
ofstream out1("4.1.5_1-5.out");
streambuf *cinbackup;
streambuf *coutbackup;
cinbackup=cin.rdbuf(in1.rdbuf());
coutbackup=cout.rdbuf(out1.rdbuf());
while(cin>>h>>m>>s) {
Clock c1(h,m,s),c2;
cout<<"时钟c1: "; c1.showTime();
cout<<"时钟c2: "; c2.showTime();
cout<<"时钟c1与c2相差"<<c1.dist(c2)<<"秒"<<endl;
cin>>n;
c1.add(n); c2.dec();
cout<<"时钟c1: "; c1.showTime();
cout<<"时钟c2: "; c2.showTime();
cout<<"时钟c1与c2相差"<<c1.dist(c2)<<"秒"<<endl;
cin>>h>>m>>s;
c1.setTime(h,m,s);
cout<<"重置后时钟c1: "; c1.showTime();
cout<<endl;
}
cin.rdbuf(cinbackup);
cout.rdbuf(coutbackup);
in1.close();
out1.close();
return 0;
}
3
#include<iostream>
#include<fstream>
using namespace std;
const double PI=3.14;
class Point{
public:
Point(float xx, float yy):x(xx),y(yy){}
void moveTo(float newX, float newY) {
x=newX;y=newY;
}
private:
float x, y ;
};
class Circle{
public:
Circle(float x=0.0,float y=0.0,float r=1.0):p(x,y){
radius=r;
circumference=2*PI*radius;
}
void resetCircle(float newX, float newY, float newR) {
p.moveTo(newX,newY);
radius=newR;
circumference=2*PI*radius;
}
double getRadius() {
return radius;
}
double getCircumference(){
return circumference;
}
bool isEqual(Circle &c) {
if(radius==c.radius) return true;
return false;
}
private:
Point p ;
float radius ;
double circumference ;
};
int main() {
float x,y,r;
cin>>x>>y>>r;
Circle c1(x,y,r),c2;
cout<<"c1 半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<endl;
cout<<"c2 半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<endl;
cout<<"是否相等: "<<c2.isEqual(c1)<<endl;
cin>>x>>y>>r;
c2.resetCircle(x,y,r);
cout<<"重置后:\nc1 半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<endl;
cout<<"c2 半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<endl;
cout<<"是否相等: "<<c2.isEqual(c1)<<endl;
ifstream in1("4.2.2_2-2.in");
ofstream out1("4.2.2_2-2.out");
while(in1>>x>>y>>r) {
Circle c1(x,y,r),c2;
out1<<"c1 半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<endl;
out1<<"c2 半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<endl;
out1<<"是否相等: "<<c2.isEqual(c1)<<endl;
in1>>x>>y>>r;
c2.resetCircle(x,y,r);
out1<<"重置后:\nc1 半径: "<<c1.getRadius()<<", 周长: "<<c1.getCircumference()<<endl;
out1<<"c2 半径: "<<c2.getRadius()<<", 周长: "<<c2.getCircumference()<<endl;
out1<<"是否相等: "<<c2.isEqual(c1)<<endl<<endl;
}
return 0;
}
4
#include<iostream>
#include<fstream>
using namespace std;
class Point {
public:
Point(float xx, float yy):x(xx),y(yy){}
void moveTo(float newX, float newY){
x=newX;y=newY;
}
private:
float x, y;
};
class Rectangle {
public:
Rectangle(float xx=0.0,float yy=0.0,float ww=1.0,float hh=1.0):p(xx,yy),w(ww),h(hh){}
void resetRect(float newX, float newY, float newW, float newH) {
p.moveTo(newX,newY);
w=newW;h=newH;
}
double getArea() {
return w*h;
}
double getCircumference(){
return 2*w+2*h;
}
bool isSquare(){
if(w==h)return true;
return false;
}
private:
Point p;
float w, h;
};
int main() {
float x,y,w,h;
cin>>x>>y>>w>>h;
Rectangle rect1(x,y,w,h),rect2;
cout<<"rect1 面积: "<<rect1.getArea()<<" 周长: "<<rect1.getCircumference()<<endl;
cout<<"是否正方形: "<<rect1.isSquare()<<endl;
cout<<"rect2 面积: "<<rect2.getArea()<<" 周长: "<<rect2.getCircumference()<<endl;
cout<<"是否正方形: "<<rect2.isSquare()<<endl;
cin>>x>>y>>w>>h;
rect2.resetRect(x,y,w,h);
cout<<"重置后:\nrect2 面积: "<<rect2.getArea()<<" 周长: "<<rect2.getCircumference()<<endl;
cout<<"是否正方形: "<<rect2.isSquare()<<endl;
ifstream in1("4.2.1_1-2_in.dat");
ofstream out1("4.2.1_1-2_out.dat");
while(in1>>x>>y>>w>>h) {
Rectangle rect1(x,y,w,h),rect2;
out1<<"rect1 面积: "<<rect1.getArea()<<" 周长: "<<rect1.getCircumference()<<endl;
out1<<"是否正方形: "<<rect1.isSquare()<<endl;
out1<<"rect2 面积: "<<rect2.getArea()<<" 周长: "<<rect2.getCircumference()<<endl;
out1<<"是否正方形: "<<rect2.isSquare()<<endl;
in1>>x>>y>>w>>h;
rect2.resetRect(x,y,w,h);
out1<<"重置后:\nrect2 面积: "<<rect2.getArea()<<" 周长: "<<rect2.getCircumference()<<endl;
out1<<"是否正方形: "<<rect2.isSquare()<<endl<<endl;
}
in1.close();
out1.close();
return 0;
}