1. | 函数模版及函数重载 【问题描述】 编写一个函数模版,能够处理整数、实数、串类对象的大小比较,返回两个值中的最小者。为了能够处理字符数组存储的字符串的大小比较,则需要使用函数重载的形式。为了能够处理串类对象的大小比较,则需要对串类实现关系运算符'>'或'<'的重载。 主函数如下,请勿修改: int main() { int x,y; double a,b; char c1[20],c2[20],c3[20],c4[20]; cin>>x>>y; cout<<Min(x,y)<<endl; cin>>a>>b; cout<<Min(a,b)<<endl; cin>>c1>>c2; cout<<Min(c1,c2)<<endl; cin>>c3>>c4; String s1(c3),s2(c4); cout<<Min(s1,s2)<<endl; return 0; } 5 9 13.2 7.8 Zhang Helen Wang Jack 5 7.8 Helen Jack |
---|
#include<bits/stdc++.h>
using namespace std;class String
{
private:char *s;
public:String(){}String(const char *a){s=new char[strlen(a)];strcpy(s,a);}friend bool operator<(const String &s1, const String &s2){if(strcmp(s1.s, s2.s)<0) return true;else return false;}friend ostream& operator<<(ostream & out, String a){out << a.s << endl;return out;}
};double Min(double a,double b)
{if(a<b) return a;else return b;
}char *Min(char *a,char *b)
{return strcmp(a,b)<0? a:b;
}template<typename T>
T Min(T a, T b)
{return a<b? a:b;
}int main()
{int x,y;double a,b;char c1[20],c2[20],c3[20],c4[20];cin>>x>>y;cout<<Min(x,y)<<endl;cin>>a>>b;cout<<Min(a,b)<<endl;cin>>c1>>c2;cout<<Min(c1,c2)<<endl;cin>>c3>>c4;String s1(c3),s2(c4);cout<<Min(s1,s2)<<endl;return 0;
}
2. | 学生类模板 【问题描述】 定义一个模板类Student,为了增强类的适用性,将学号设计为参数化类型,它可以实例化成字符串、长整形等;将成绩设计为参数化类型,它可以实例化成整形、浮点型、字符型(用来表示等级分)等。 template<class TNo, class TScore, int num>//TNo和TScore为参数化类型 class Student{ private: TNo StudentID; //参数化类型,存储姓名 TScore score[num]; //参数化类型数组,存储num门课程的分数 public: void Input();//数据的录入 TScore MaxScore(); //查找score的最大值并返回该值 void Update(TScore sscore,int i);//更新学生的第i门课程成绩为sscore void SelectSort(); //采用选择排序法对学生成绩进行升序排列 void Print(); //输出所有学生的信息 }; 请注意String是自定义的class类型,参考如下: class String{ public: char Str[20]; friend istream &operator>>(istream &in, String &s); friend ostream &operator<<(ostream &out, String &s); }; 请自行设计主函数,并做如下声明。从键盘依次录入数据,然后分别调用四个成员函数执行得到所需的结果。 Student<String,float,3>;
zhao 82.5 96.3 73.5 2 74.5 【样例输出1】 96.3 zhao 74.5 82.5 96.3 【样例输入2】 ma 10 20 30 1 50 【样例输出2】 50 ma 10 30 50 【样例输入3】 zhang 82.3 50.5 92.1 1 62.3 【样例输出3】 92.1 zhang 62.3 82.3 92.1 【样例说明】 【评分标准】 |
---|
#include <iostream>
using namespace std;class String {
public:char Str[20];friend istream& operator>>(istream& in, String& s) {in >> s.Str;return in;}friend ostream& operator<<(ostream& out, const String& s) {out << s.Str;return out;}
};template<class TNo, class TScore, int num>
class Student {
private:TNo StudentID;TScore score[num];public:void Input() {cin >> StudentID;for (int i = 0; i < num; i++) {cin >> score[i];}}TScore MaxScore() {TScore max = score[0];for (int i = 1; i < num; i++) {if (score[i] > max) {max = score[i];}}return max;}void Update(TScore sscore, int i) {if (i >= 0 && i < num) {score[i] = sscore;}}void SelectSort() {for (int i = 0; i < num - 1; i++) {int minIndex = i;for (int j = i + 1; j < num; j++) {if (score[j] < score[minIndex]) {minIndex = j;}}if (minIndex != i) {TScore temp = score[i];score[i] = score[minIndex];score[minIndex] = temp;}}}void Print() {cout << StudentID;for (int i = 0; i < num; i++) {cout << " " << score[i];}cout << endl;}
};int main() {Student<String, float, 3> student;student.Input();float newScore;int courseIndex;cin >> courseIndex >> newScore;student.Update(newScore, courseIndex);cout << student.MaxScore() << endl;student.SelectSort();student.Print();return 0;
}
3. | 二进制类(运算符号的重载) 【问题描述】将一个16位二进制数表示成0和1的字符序列,即用一个字符数组来存放这个二进制数。在这个类中设置两个构造函数,一个是传递整数参数的,另一个是传递字符串参数的。因为用户在创建对象时传递的二进制数,可能是以整数形式给出,也可能是以数字串形式给出,系统应该都能接受。另外有一个类型转换函数int(),用来将类类型向整型转换,即将二进制形式的类对象转换为整形数。两个重载运算符“+”,“-”,用来完成两个二进制数之间的加减运算。 class binary { //定义二进制类 char bits[16]; //二进制字模数组 public: binary(char *); //字符串参数构造函数 binary(int); //整型参数构造函数 friend binary operator +(binary,binary); //重载“+”,友元函数 friend binary operator -(binary,binary); //重载“-”,友元函数 operator int(); //类类型转换函数,成员函数 friend ostream & operator <<(ostream &out, binary &b);//重载“<<”,以二进制形式输出 void print();//以整型形式输出 }; 主函数设计如下,请勿修改: int main(){ binary n1="1011"; binary n2=int(n1)+15; binary n3=n1-binary(7); cout<<n1<<endl; cout<<n2<<endl; cout<<n3<<endl; cout<<int(n2)+5<<endl; n2=n2-binary(5); n2.print(); n3=n3+binary(5); n3.print(); cout<<int(n3)-5<<endl; return 0; } 【样例输出】 0000000000001011 0000000000011010 0000000000000100 31 21 9 4 |
---|
#include<iostream>
#include<string.h>
#include<cmath>
using namespace std;
class binary {char bits[16];
public:binary(char *s);binary(int n);friend binary operator+(binary b1, binary b2);friend binary operator-(binary b1, binary b2);operator int();friend ostream & operator<<(ostream &out, binary &b);void print();
};
binary::binary(char *s)
{int i, size = strlen(s);for (i = 0; i < 16 - size; i++){bits[i] = '0';}for (int j = 0; j < size; j++,i++){bits[i] = s[j];}
}binary::binary(int n)
{int re, i = 0;while (n != 0){re = n % 2;bits[i++] = re + 48;n /= 2;}while (i<16){bits[i++] = '0';}for (int k = 0, j = 15; k < j; k++, j--){re = bits[k];bits[k] = bits[j];bits[j] = re;}
}binary::operator int()
{int j = 0, ans = 0;for (int i = 15; i >= 0; i--,j++){ans += (bits[i] - 48)*pow(2, j);}return ans;
}void binary::print()
{cout << int(*this) << endl;
}binary operator+(binary b1, binary b2)
{binary temp(int(b1)+int(b2));return temp;
}binary operator-(binary b1, binary b2)
{binary temp(int(b1)-int(b2));return temp;
}ostream & operator<<(ostream & out, binary & b)
{for (int i = 0; i < 16; i++)out << b.bits[i];return out;
}int main()
{binary n1 = const_cast<char*>("1011");binary n2 = int(n1) + 15;binary n3 = n1 - binary(7);cout << n1 << endl;cout << n2 << endl;cout << n3 << endl;cout << int(n2) + 5 << endl;n2 = n2 - binary(5);n2.print();n3 = n3 + binary(5);n3.print();cout << int(n3) - 5 << endl;return 0;
}
4. | 时间类的改进(运算符重载) 【问题描述】对前面实验写过的Time类进行修改,删去Add和Sub成员函数,通过重载“+”、“-”运算符直接进行时间的加减运算。 提示: (1)可以用友元函数来实现“+”“-”运算符的重载。 (2)加法运算符可以是两个Time对象进行相加,也可以是一个表示秒数的int型数据加上一个Time对象,还可以是Time对象加上int型数据,得到的结果都是Time类型的对象。 (3)减法运算符可以是两个Time对象进行相减,也可以是Time对象减去一个表示秒数的int型数据,得到的结果都是Time类型的对象。 主函数设计如下,请勿修改: int main(){ Time t1(2,34),t2,t3; t2.SetTime(13,23,34); cout<<"t1+t2:"; t3=t1+t2;//两个Time类对象相加 t3.print_24(); cout<<"\nt1+65:"; t3=t1+65;//Time类对象加上65秒 t3.print_24(); cout<<"\n65+t1:"; t3=65+t1;//65秒加上Time类对象 t3.print_24(); cout<<"\nt2-t1:"; t3=t2-t1;//两个Time类对象相减 t3.print_24(); cout<<"\nt1-70:"; t3=t1-70;//Time类对象减去70秒 t3.print_24(); return 0; } 【样例输出】 t1+t2:15:57:34 t1+65:02:35:05 65+t1:02:35:05 t2-t1:10:49:34 t1-70:02:32:50 |
---|
#include<iostream>
using namespace std;
class Time
{int hour, minute, second;
public:int SecCalc() { return(hour * 60 + minute) * 60 + second; }Time(int h, int m, int s = 0);Time(int s = 0);void SetTime(int h = 0, int m = 0, int s = 0);void print_12();void print_24();Time operator+(Time &t);Time operator-(Time &t);Time operator+(int s);Time operator-(int s);friend Time operator+(int s,Time &t);friend Time operator-(int s, Time &t);
};
Time::Time(int h, int m, int s)
{hour = h;minute = m;second = s;
}
Time::Time(int s)
{hour = minute = second = 0;for (int i = 0; i < s; i++){second++;if (second == 60){second = 0;minute++;if (minute == 60){hour++;minute = 0;}}}
}
void Time::SetTime(int h, int m, int s)
{hour = h;minute = m;second = s;
}
void Time::print_12()
{bool afternoon = false;if (hour > 12){if (hour - 12 < 10)cout << "0";cout << hour - 12 << ":";afternoon = true;}else{if (hour < 10)cout << "0";cout << hour << ":";}if (minute < 10)cout << "0";cout << minute << ":";if (second < 10)cout << "0";cout << second;if (afternoon)cout << " PM" << endl;elsecout << " AM" << endl;
}
void Time::print_24()
{if (hour < 10)cout << "0";cout << hour << ":";if (minute < 10)cout << "0";cout << minute << ":";if (second < 10)cout << "0";cout << second << endl;
}
Time Time::operator+(Time & t)
{Time temp = *this;for (int i = 0; i < t.second; i++){temp.second += 1;if (temp.second == 60){temp.minute += 1;if (temp.minute == 60){temp.hour += 1;temp.minute = 0;}temp.second = 0;}}for (int i = 0; i < t.minute; i++){temp.minute += 1;if (temp.minute == 60){temp.hour += 1;temp.minute = 0;}}temp.hour += t.hour;return temp;
}
Time Time::operator-(Time & t)
{Time temp = *this;temp.hour -= t.hour;for (int i = 0; i < t.minute; i++){temp.minute -= 1;if (temp.minute < 0){temp.hour -= 1;temp.minute = 59;}}for (int i = 0; i < t.second; i++){temp.second -= 1;if (temp.second < 0){temp.minute -= 1;if (temp.minute < 0){temp.hour -= 1;temp.minute = 59;}temp.second = 59;}}return temp;
}
Time Time::operator+(int s)
{Time temp = *this;for (int i = 0; i < s; i++){temp.second += 1;if (temp.second == 60){temp.minute += 1;if (temp.minute == 60){temp.hour += 1;temp.minute = 0;}temp.second = 0;}}return temp;
}
Time Time::operator-(int s)
{Time temp = *this;for (int i = 0; i < s; i++){temp.second -= 1;if (temp.second < 0){temp.minute -= 1;if (temp.minute < 0){temp.hour -= 1;temp.minute = 59;}temp.second = 59;}}return temp;
}
Time operator+(int s, Time & t)
{return t + s;
}
Time operator-(int s, Time & t)
{return t - s;
}int main()
{//主函数设计如下,请勿修改.Time t1(2, 34), t2, t3;t2.SetTime(13, 23, 34);cout << "t1+t2:";t3 = t1 + t2;//两个Time类对象相加t3.print_24();cout << "\nt1+65:";t3 = t1 + 65;//Time类对象加上65秒#t3.print_24();cout << "\n65+t1:";t3 = 65 + t1;//65秒加上Time类对象#t3.print_24();cout << "\nt2-t1:";t3 = t2 - t1;//两个Time类对象相减t3.print_24();cout << "\nt1-70:";t3 = t1 - 70;//Time类对象减去70秒t3.print_24();return 0;
}
5. | 运算符的重载 【问题描述】不允许修改主函数,请将以下代码补充完整。 int main( ) 【样例输入】 |
---|
#include<bits/stdc++.h>
using namespace std;class String
{ char *ptr;
public:String(){ptr=NULL;}String(char *s) { ptr=new char[strlen(s)+1];strcpy(ptr, s); }~String() { delete []ptr; }void operator +=(const String &s);String& operator =(const String &s);friend ostream & operator <<(ostream &out, String &s);void print() { cout<<ptr<<endl; }
};void String::operator +=(const String &s)
{char a[100];strcpy(a, ptr);delete []ptr;ptr=new char[strlen(a)+strlen(s.ptr)+1];strcpy(ptr,strcat(a,s.ptr));
}String& String::operator =(const String &s)
{if(this==&s) return *this;delete []ptr;ptr=new char[strlen(s.ptr)+1];strcpy(ptr,s.ptr);return *this;
}ostream & operator <<(ostream &out, String &s)
{out<<s.ptr<<endl;return out;
}int main( )
{String p1("book"), p2("pen"), p3("good"), p4;p4 = p4 = p1; p3 = p1 = p2;cout<<"p2:";p2.print();cout<<"p1:"<<p1;cout<<"p3:"<<p3;p4 += p3;cout<<"p4:"<<p4;return 0;
}