//1.定义一个学生类,包含姓名、年龄、绩点,要求能够初始化学生信息(定义常规构造函数和拷贝构造函数)、显示学生信息、设置学生信息;
//
//2.在主函数中定义一个学生数组(普通数组或者动态数组都可以),大小为5,然后利用设置学生信息的成员函数输入每个学生信息;
//
//3.定义student类的友元函数Sort实现按绩点排序,以对象指针(或对象数组)和数组大小为参数,对学生按绩点由高到低排序。
// 在主函数中调用函数sort,输出排序后的学生信息。
//
//在此基础上,为student类增加:
//
//(1)静态数据成员 count, 负责统计学生总数,并实现其值的更新;
//
//(2)Date 类的成员对象 birthday, 并为student类定义相应的构造函数;
#include
#include<string>
using namespace std;
class Date
{
int year;
int month;
int day;
public:
Date(int year = 0, int month = 0, int day = 0);
int Getyear() { return year; }
int Getmonth() { return month; }
int Getday() { return day; }
};
//************************
Date::Date(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
//*************************
class Student
{
static int Count;
string Name;
int Age;
double Point;
Date Birthday;
public:
Student();
Student(string name, int age, double point, Date birthday);
Student(const Student& a);
~Student();
int Getcount();
void Show();
void Set(string name, int age, double point, Date birthday);
friend void Sort(Student a[], int b);
};
//****************************
int Student::Count = 0;
Student::Student()
{
this->Name = "0";
this->Age = 0;
this->Point = 0;
this->Birthday = 0;
}
Student::Student(string name, int age, double point, Date birthday)
{
this->Name = name;
this->Age = age;
this->Point = point;
this->Birthday = birthday;
}
Student::Student(const Student& a)
{
this->Name = a.Name;
this->Age = a.Age;
this->Point = a.Point;
this->Birthday = a.Birthday;
}
Student::~Student()
{
}
int Student::Getcount()
{
Count = 5;
return Count;
}
void Student::Show()
{
cout << this->Name << " " << this->Age << " "
<< " " << this->Point << endl;
}
void Student::Set(string name, int age, double point, Date birthday)
{
this->Name = name;
this->Age = age;
this->Point = point;
this->Birthday = birthday;
}
void Sort(Student a[], int b)
{
for (int i = 0; i < b; i++)
{
for (int j = i + i; j < b; j++)
{
if (a[i].Point < a[j].Point)
{
Student temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
//**********************
int main()
{
cout << "请输入学生信息:" << endl;
string name; int age; double point;
int year, month, day;
Student a[5];
for (int i = 0; i < 5; i++)
{
cin >> name >> age >> point >> year >> month >> day;
Date birthday(year, month, day);
a[i].Set(name, age, point, birthday);
}
Sort(a, 5);
cout << "共有学生" << a[0].Getcount() << "名。" << endl;
cout << "分别为:" << endl;
for (int i = 0; i < 5; i++)
{
a[i].Show();
}
return 0;
}
以下是实现该功能的代码示例(包含对Date类的定义):
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// Date 类定义,用于保存学生的出生日期
class Date {
public:
Date(int y, int m, int d) : year(y), month(m), day(d) {}
friend bool operator< (const Date& lhs, const Date& rhs);
friend bool operator== (const Date& lhs, const Date& rhs);
friend ostream& operator<< (ostream& os, const Date& date);
private:
int year;
int month;
int day;
};
bool operator< (const Date& lhs, const Date& rhs) {
if (lhs.year != rhs.year) {
return lhs.year < rhs.year;
}
else if (lhs.month != rhs.month) {
return lhs.month < rhs.month;
}
else {
return lhs.day < rhs.day;
}
}
bool operator== (const Date& lhs, const Date& rhs) {
return lhs.year == rhs.year && lhs.month == rhs.month && lhs.day == rhs.day;
}
ostream& operator<< (ostream& os, const Date& date) {
os << date.year << "/" << date.month << "/" << date.day;
return os;
}
// Student 类定义,用于保存学生的信息
class Student {
public:
Student() : name(""), age(0), score(0.0), birthday(2000, 1, 1) {
++count;
}
Student(const string& name_, int age_, double score_, const Date& birthday_)
: name(name_), age(age_), score(score_), birthday(birthday_) {
++count;
}
Student(const Student& other) {
name = other.name;
age = other.age;
score = other.score;
birthday = other.birthday;
++count;
}
~Student() {
--count;
}
// 访问器和修改器
string getName() const { return name; }
void setName(const string& name_) { name = name_; }
int getAge() const { return age; }
void setAge(int age_) { age = age_; }
double getScore() const { return score; }
void setScore(double score_) { score = score_; }
// 友元函数
friend void Sort(Student* arr, int size);
friend ostream& operator<< (ostream& os, const Student& student);
private:
string name;
int age;
double score;
Date birthday;
static int count;
};
int Student::count = 0;
// 比较函数,用于排序
bool cmp(const Student& lhs, const Student& rhs) {
return lhs.score > rhs.score;
}
// Sort 函数,用于按照绩点排序
void Sort(Student* arr, int size) {
sort(arr, arr + size, cmp);
}
ostream& operator<< (ostream& os, const Student& student) {
os << "姓名:" << student.name << ",年龄:" << student.age
<< ",绩点:" << student.score << ",出生日期:" << student.birthday;
return os;
}
int main() {
// 定义学生数组
Student students[5];
// 输入学生信息
for (int i = 0; i < 5; ++i) {
string name;
int age;
double score;
int year, month, day;
cout << "请输入第" << i + 1 << "个学生的信息:" << endl;
cout << "姓名:";
cin >> name;
cout << "年龄:";
cin >> age;
cout << "绩点:";
cin >> score;
cout << "出生年份:";
cin >> year;
cout << "出生月份:";
cin >> month;
cout << "出生日期:";
cin >> day;
students[i] = Student(name, age, score, Date(year, month, day));
}
// 按照绩点排序
Sort(students, 5);
// 输出学生信息
cout << endl;
for (int i = 0; i < 5; ++i) {
cout << students[i] << endl;
}
// 输出学生总数
cout << endl << "学生总数为:" << Student::count << endl;
return 0;
}
#include <iostream>
#include <iomanip>
#include <fstream>
#include <process.h>
#include <string.h>
using namespace std;
const int LIST_INIT_SIZE=10;
const int LISTINCREMENT=5;
//student,
typedef struct Contacts
{
char id[10]; //学号
char name[10]; //姓名
char sex[10]; //性别
char class_number[10]; //班级
char class_name[30]; //科目
char score[30]; //成绩
}Student;
//scorebook表
typedef struct scorebook
{
Student *elem;
int length;
int listsize;
int incrementsize;
}scorebook;
//错误运行
void ErrorMessage(char *s)
{
cout<<s<<endl;
exit(1); //非正常运行导致退出程序
}
//getchar()吸收换行来模拟暂停
void Pause()
{
cout<<endl<<"按任意键继续......";
getchar();getchar();
}
//初始化 length=0,获取 maxsize, incresize,new 长度为maxsize的student数组
void InitList_Sq(scorebook &L, int maxsize=LIST_INIT_SIZE, int incresize=LISTINCREMENT)
{
L.elem=new Student [maxsize];
L.length=0;
L.listsize=maxsize;
L.incrementsize=incresize;
}
//查找元素(按id学号查询)
int LocateElem_Sq(scorebook &L, Student e)
{
for(int i=0;i<L.length;i++)
if(strcmp(L.elem[i].id,e.id)==0) return i+1;
return 0;
}
//获取元素
void GetElem_Sq(scorebook &L, int i, Student &e)
{
e=L.elem[i-1];
}
//增加存储空间和长度
void increment(scorebook &L)
{
Student *p=new Student[L.listsize+L.incrementsize];
for(int i=0;i<L.length;i++)
p[i]=L.elem[i];
delete [] L.elem;
L.elem=p;
L.listsize=L.listsize+L.incrementsize;
}
//增加元素
void ListInsert_Sq(scorebook &L, Student e)
{
if(L.length>=L.listsize)
increment(L);
if(L.length ==0)
{
L.elem[0]=e;
L.length++;
}
else
{
int i=L.length-1;
while((strcmp(L.elem[i].id,e.id)>0)&&i>=0)
{
L.elem[i+1]=L.elem[i];
i--;
}
L.elem[i+1]=e;
++L.length;
}
}
void ListInsert_Sq(scorebook &L, int i, Student e )
{
if (i<1||i>L.length+1) ErrorMessage(" i 值不合法");
if (L.length>=L.listsize) increment(L);
Student *q = &(L.elem[i-1]);
for (Student *p=&(L.elem[L.length-1]); p>=q;--p)
*(p+1) = *p;
*q = e;
++L.length;
}
//删除信息
void ListDelete_Sq(scorebook &L, int i,Student e)
{
if ((i<1) || (i>L.length)) ErrorMessage("i值不合法");
Student *p=&(L.elem[i-1]);
e=*p;
Student *q=L.elem+L.length-1;
for(++p; p<=q; ++p )
*(p-1)=*p;
--L.length;
}
//销毁表
void DestroyList_Sq(scorebook &L)
{
delete [] L.elem;
L.listsize=0;
L.length=0;
}
//清空表
void ClearList_Sq(scorebook &L)
{
L.length=0;
}
bool ListEmpty_Sq(scorebook &L)
{
if(L.length==0)
return true;
else return false;
}
//输出信息
void ListTraverse_Sq(scorebook &L)
{
cout<<"\n======================== 学生成绩信息表 ========================"<<endl<<endl;
cout<<"学号、姓名、性别、班级、科目、成绩:\n";
for(int i=0;i<L.length;i++)
cout<<setiosflags(ios_base::left)<<setw(12)<<L.elem[i].id
<<setw(12)<<L.elem[i].name<<setw(15)<<L.elem[i].sex<<setw(10)<<L.elem[i].class_number
<<setw(12)<<L.elem[i].class_name<<setw(16)<<L.elem[i].score<<resetiosflags(ios_base::left)<<endl;
Pause();
}
//输入
void input(char *tid,scorebook &L)
{
int n;
Student e;
ofstream wfile;
wfile.open(tid,ios_base::binary);
cout<<"请输入信息表中的联系人人数:";
cin>>n;
cout<<"请输入学生的学号、姓名、性别、班级、科目、成绩:\n";
for(int i=1 ;i<=n;i++)
{
cin>>e.id>>e.name>>e.sex>>e.class_number>>e.class_name>>e.score;
ListInsert_Sq(L,e);
wfile.write((char *)(&e),sizeof(Student));
}
wfile.close();
}
void load(char *tid,scorebook &L)
{
Student e;
ifstream rfile;
rfile.open(tid,ios_base::binary);
while(rfile.read((char *)(&e),sizeof(e)))
{
ListInsert_Sq(L,e);
}
rfile.close();
}
void save(char *tid,scorebook &L)
{
ofstream save;
save.open(tid,ios_base::binary);
for(int i=0;i<L.length;i++)
save.write((char *)(&L.elem[i]),sizeof(Student));
save.close();
}
//单个输出信息
void SingleOut(Student e)
{
cout<<"学号、姓名、性别、班级、科目、成绩: ";
cout<<setiosflags(ios_base::left)<<setw(12)<<e.id
<<setw(12)<<e.name<<setw(15)<<e.sex<<setw(10)<<e.class_number
<<setw(12)<<e.class_name<<setw(16)<<e.score<<resetiosflags(ios_base::left)<<endl;
}
int main()
{
Student e;
int i,n;
bool flag=true;
char yn;
char addlist[30];
scorebook L;
InitList_Sq(L,LIST_INIT_SIZE,LISTINCREMENT);
cout<<"请输入成绩表的存盘文件名:";
cin>>addlist;
fstream file;
file.open(addlist,ios::in);
if(!file)
{
cout<<"成绩表不存在,建立一个新的成绩表(Y/N)?";
cin>>yn;
if(yn=='Y'||yn=='y')
{ file.close();
input(addlist,L);
}
}
else
{
file.close();
load(addlist,L);
ListTraverse_Sq(L);
}
while(flag)
{
system("cls");
cout<<"\n 学生成绩管理系统主菜单 \n";
cout<<"======================================\n";
cout<<" 1 输入学生信息 \n"<<endl;
cout<<" 2 删除学生信息 \n"<<endl;;
cout<<" 3 查询学生信息\n"<<endl;
cout<<" 4 输出学生信息\n"<<endl;
cout<<" 5 修改学生信息\n"<<endl;
cout<<" 6 退出\n"<<endl;
cout<<"======================================\n";
cout<<" \n";
cout<<" Please select 1、 2、 3、 4、 5、 6: ";
cin>>n;
switch (n)
{
case 1:
cout<<"请输入该学生新的学号、姓名、性别、班级、科目、成绩:\n";
cin>>e.id>>e.name>>e.sex>>e.class_number>>e.class_name>>e.score;
ListInsert_Sq(L,e);
break;
case 2 :
cout<<"请输入要删除的学生的学号:";
cin>>e.id;
char yn;
i=LocateElem_Sq(L,e);
if(i==0)
{
cout<<"学号为:"<<e.id<<" 的学生不存在,不能被删除!!\n";
break ;
}
cout<<"你确实要删除么?";
cin>>yn;
if(yn=='Y'||yn=='y')
ListDelete_Sq(L,i,e);
else
{ cout <<"!!不删除!!"<<endl;}
break;
case 3:
{ cout<<"请输入要查找的学生学号:";
cin>>e.id;
i=LocateElem_Sq(L,e);
if(i!=0)
{ GetElem_Sq(L,i,e);
SingleOut(e);
}
else cout<<endl<<"!!学号为:"<<e.id<<" 的学生不存在!!\n";
}
Pause();
break;
case 4:
ListTraverse_Sq(L);
break;
case 5:
cout<<"请输入要修改的学生的学号:";
cin>>e.id;
i=LocateElem_Sq(L, e);
if(i!=0)
{ GetElem_Sq(L,i,e);
SingleOut(e);
cout<<"确实要修改么Y/N?";
cin>>yn;
if(yn=='y'||yn=='Y')
{
ListDelete_Sq(L,i,e);
cout<<"请输入该学生新的学号、姓名、性别、班级、科目、成绩:\n";
cin>>e.id>>e.name>>e.sex>>e.class_number>>e.class_name>>e.score;
ListInsert_Sq(L,e);
}
}
else cout<<endl<<"!!学号为 "<<e.id<<" 的学生不存在!!\n";
Pause();
break;
case 6:
flag=false;
break;
default:
cout<<endl<<"!! 选择错误,请重新选择 !!"<<endl;
Pause();
}
}
return 0;
}
以下是使用 Dijkstra 算法实现的 C++ 代码:
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#define MAX_N 100005
#define INF 0x3f3f3f3f
using namespace std;
struct Edge {
int to;
int w;
Edge (int t, int _w): to(t), w(_w) {}
bool operator<(const Edge& e) const {
return w > e.w;
}
};
int N, M;
vector<Edge> G[MAX_N];
priority_queue<Edge> q;
int dist[MAX_N];
void dijkstra(int s) {
memset(dist, INF, sizeof(dist));
dist[s] = 0;
q.push(Edge(s, 0));
while (!q.empty()) {
Edge u = q.top(); q.pop();
if (u.w > dist[u.to]) continue;
for (int i = 0; i < G[u.to].size(); i++) {
Edge& e = G[u.to][i];
if (dist[e.to] > dist[u.to] + e.w) {
dist[e.to] = dist[u.to] + e.w;
q.push(Edge(e.to, dist[e.to]));
}
}
}
}
int main() {
cin >> N >> M;
for (int i = 1; i <= N-1; i++) {
int u, v, w;
cin >> u >> v >> w;
G[u].push_back(Edge(v, w));
G[v].push_back(Edge(u, w));
}
for (int i = 1; i <= M; i++) {
int s, t;
cin >> s >> t;
dijkstra(s);
cout << max(dist[t], 2000) << endl;
}
return 0;
}