0
点赞
收藏
分享

微信扫一扫

C++面向对象实验(二)


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
using namespace std;
class Student {
char name[15];
int english;
int computer;
int total;
public:
void input();
void display();
friend void Sort(Student *T, int n);
Student& operator=(const Student &T);
};

void Student::input()
{
cout << "输入名字:";
cin >> name;
cout << "输入英文成绩:";
cin >> english;
cout << "输入计算机成绩:";
cin >> computer;
total = english + computer;
}

void Student::display()
{

cout << "名字:" << name << endl;
cout << "英语成绩:" << english << endl;
cout << "计算机成绩:" << computer << endl;
cout << "总成绩:" << total << endl;
}

Student& Student::operator=(const Student &T)
{
if (this == &T)
return *this;

strcpy(name, T.name);
english = T.english;
computer = T.computer;
total = T.total;
return *this;
}

void Sort(Student *T, int n)
{
int biao = 0;

for (int i = 0; i < n - 1; i++)
{
biao = 0;
for (int j = n - 2 ; j >= 0; j--)
{
if (T[j+1].total > T[j].total)
{
swap(T[j],T[j+1]);
biao = 1;
}
if (!biao)
break;
}
}

}
int main()
{
int n;
cout << "输入学生人数:";
cin >> n;
Student *p = new Student[n];
for (int i = 0; i < n; i++)
p[i].input();
Sort(p,n);
for (int i = 0; i < n; i++)
p[i].display();

return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
using namespace std;

class Point {
int x;
int y;
public:
Point(int x1, int y1) : x(x1), y(y1) { }
friend double getDistance(const Point &p1, const Point &p2);
};

double getDistance(const Point &p1, const Point &p2)
{
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return sqrt(pow(x, 2) + pow(y, 2));
}

int main()
{
int x, y;
cin >> x >> y;
Point p1(x,y);
x = y = 0;
cin >> x >> y;
Point p2(x,y);
cout << "距离:" << getDistance(p1, p2);
return 0;
}


#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
using namespace std;

class Time {
int hour;
int minute;
int second;
public:
Time(int hour1, int minute1, int second1) : hour(hour1), minute(minute1), second(second1) { }
friend int relSecond(Time &t1, Time &t2);
};

int relSecond(Time &t1, Time &t2)
{
int sec1 = t1.hour * 3600 + t1.minute * 60 + t1.second;
int sec2= t2.hour * 3600 + t2.minute * 60 + t2.second;
if (sec1 >= sec2)
return sec1 - sec2;
else
return sec2 - sec1;
}
int main()
{
int hour, minute, second;
cin >> hour >> minute >> second;
Time t1(hour, minute, second);
hour = minute = second = 0;
cin >> hour >> minute >> second;
Time t2(hour, minute, second);
cout << "相差的秒数为:" << relSecond(t1, t2) << endl;
return 0;
}



举报

相关推荐

0 条评论