update
This commit is contained in:
26
practical_exercises/key_exercises/README.md
Normal file
26
practical_exercises/key_exercises/README.md
Normal file
@@ -0,0 +1,26 @@
|
||||
## 重点实战练习
|
||||
|
||||
├── [中括号重载.cpp](中括号重载.cpp)
|
||||
├── [时钟++运算符重载.cpp](时钟++运算符重载.cpp)
|
||||
├── [运算符重载之强制转换.cpp](运算符重载之强制转换.cpp)
|
||||
└── [重载圆括号的时钟.cpp](重载圆括号的时钟.cpp)
|
||||
|
||||
├── [函数模板.cpp](函数模板.cpp)
|
||||
|
||||
├── [动态数组.cpp](动态数组.cpp)
|
||||
|
||||
├── [字典插入与查找.cpp](字典插入与查找.cpp)
|
||||
|
||||
├── [异常捕获.cpp](异常捕获.cpp)
|
||||
|
||||
├── [类模板之栈.cpp](类模板之栈.cpp)
|
||||
|
||||
├── [类模板特化之数组.cpp](类模板特化之数组.cpp)
|
||||
|
||||
├── [继承与封装.cpp](继承与封装.cpp)
|
||||
|
||||
├── [读写文件综合题.cpp](读写文件综合题.cpp)
|
||||
├── [输入输出运算符重载.cpp](输入输出运算符重载.cpp)
|
||||
├── [输入输出重载.cpp](输入输出重载.cpp)
|
||||
├── [输出格式.cpp](输出格式.cpp)
|
||||
|
||||
51
practical_exercises/key_exercises/中括号重载.cpp
Normal file
51
practical_exercises/key_exercises/中括号重载.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//Eg8-9.cpp
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
struct Person{ //职工基本信息的结构
|
||||
double salary;
|
||||
char *name;
|
||||
};
|
||||
class SalaryManaege{
|
||||
Person *employ; //存放职工信息的数组
|
||||
int max; //数组下标上界
|
||||
int n; //数组中的实际职工人数
|
||||
public:
|
||||
SalaryManaege(int Max=0){
|
||||
max=Max;
|
||||
n=0;
|
||||
employ=new Person[max];
|
||||
}
|
||||
//返回引用特性是可以直接在放在左值,直接使用
|
||||
double &operator[](char *Name) { //重载[],返回引用
|
||||
Person *p;
|
||||
for(p=employ;p<employ+n;p++)
|
||||
//如果存在处理
|
||||
if(strcmp(p->name,Name)==0)
|
||||
return p->salary;
|
||||
//不存在情况处理
|
||||
p=employ + n++;
|
||||
p->name=new char[strlen(Name)+1];
|
||||
strcpy(p->name,Name);
|
||||
p->salary=0;
|
||||
return p->salary;
|
||||
}
|
||||
|
||||
void display(){
|
||||
for(int i=0;i<n;i++)
|
||||
cout<<employ[i].name<<" "<<employ[i].salary<<endl;
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
SalaryManaege s(3);
|
||||
s["张三"]=2188.88;
|
||||
s["里斯"]=1230.07;
|
||||
s["王无"]=3200.97;
|
||||
cout<<"张三\t"<<s["张三"]<<endl; cout<<"里斯\t"<<s["里斯"]<<endl;
|
||||
cout<<"王无\t"<<s["王无"]<<endl;
|
||||
|
||||
cout<<"-------下为display的输出--------\n\n";
|
||||
s.display();
|
||||
system("pause");
|
||||
}
|
||||
|
||||
19
practical_exercises/key_exercises/函数模板.cpp
Normal file
19
practical_exercises/key_exercises/函数模板.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
|
||||
|
||||
template<class T> T compareMax(T t1,T t2){
|
||||
return t1>t2?t1:t2;
|
||||
}
|
||||
|
||||
template<> char* compareMax<char *>(char *s1,char *s2){
|
||||
return strcmp(s1,s2)>=0?s1:s2;
|
||||
}
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
cout<<compareMax(1,2)<<endl;
|
||||
cout<<compareMax("asda","qweq")<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
5
practical_exercises/key_exercises/动态数组.cpp
Normal file
5
practical_exercises/key_exercises/动态数组.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include<iostream>
|
||||
char *sPtr; //存放字符串的指针
|
||||
s="哈哈"
|
||||
sPtr = new char[strlen(s)+1];
|
||||
strncpy(sPtr,s;
|
||||
78
practical_exercises/key_exercises/字典插入与查找.cpp
Normal file
78
practical_exercises/key_exercises/字典插入与查找.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include<iostream>
|
||||
#include<map>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
map<const char*,const char*> mp;
|
||||
map<const char*,const char*>::iterator iter;
|
||||
const char key[3][20]={"img","system","ip"};
|
||||
const char value[3][20]={"d:/a.img","win7","193.68.6.3"};
|
||||
|
||||
//make_pair插入
|
||||
for(int i=0;i<2;i++){
|
||||
mp.insert(make_pair(key[i],value[i]));
|
||||
}
|
||||
//pair<const char*,const char*>插入
|
||||
mp.insert(pair<const char*,const char*>(key[2],value[2]));
|
||||
|
||||
//数组插入方式
|
||||
mp["addr"]="中国";
|
||||
//迭代器取出元素
|
||||
//循环取出元素
|
||||
for(iter=mp.begin();iter!=mp.end();iter++){
|
||||
cout<<iter->first<<"\t"<<iter->second<<endl;
|
||||
}
|
||||
char key1[20];
|
||||
cout<<"请输入按key查找:";
|
||||
cin.getline(key1,20);
|
||||
//查找元素
|
||||
for(iter=mp.begin();iter!=mp.end();iter++){
|
||||
if(strcmp(iter->first,key1)==0){
|
||||
cout<<iter->first<<"查找出来了!"<<"对应的值为:"<<iter->second<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
//第一种删除方式
|
||||
//find只能用于查找数组建立的形式!并且不支持输入用cin等
|
||||
iter=mp.find("addr");
|
||||
if(iter!=mp.end()){
|
||||
cout<<iter->first<<"按照key查找出来了!"<<"对应的value为:"<<iter->second<<endl;
|
||||
cout<<"开始删除元素!"<<endl;
|
||||
mp.erase(iter);
|
||||
// break;
|
||||
}
|
||||
//第二种方式删除
|
||||
//按照key删除元素
|
||||
char drop_key[20];
|
||||
//按照value删除元素
|
||||
char drop_value[20];
|
||||
cout<<"请输入按key删除:";
|
||||
cin.getline(drop_key,20);
|
||||
cout<<"请输入按value删除:";
|
||||
cin.getline(drop_value,20);
|
||||
for(iter=mp.begin();iter!=mp.end();iter++){
|
||||
if(strcmp(iter->first,drop_key)==0){
|
||||
cout<<iter->first<<"按照key查找出来了!"<<"对应的value为:"<<iter->second<<endl;
|
||||
cout<<"开始删除元素!"<<endl;
|
||||
mp.erase(iter);
|
||||
break;
|
||||
}
|
||||
if(strcmp(iter->second,drop_value)==0){
|
||||
cout<<iter->second<<"value查找出来了!"<<"对应的key为:"<<iter->first<<endl;
|
||||
cout<<"开始删除元素!"<<endl;
|
||||
mp.erase(iter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
cout<<"------删除元素以后--------\n";
|
||||
//循环取出元素
|
||||
for(iter=mp.begin();iter!=mp.end();iter++){
|
||||
cout<<iter->first<<"\t"<<iter->second<<endl;
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
25
practical_exercises/key_exercises/异常捕获.cpp
Normal file
25
practical_exercises/key_exercises/异常捕获.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
//函数异常可以抛出去由主函数来处理
|
||||
void fun(int x){
|
||||
try{
|
||||
if (x==0)
|
||||
throw "异常";
|
||||
}catch(...){
|
||||
cout<<"in fun"<<endl;
|
||||
throw 1;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
try{
|
||||
fun(0);
|
||||
}catch(int n){
|
||||
cout<<"in main"<<endl;
|
||||
}
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
93
practical_exercises/key_exercises/时钟++运算符重载.cpp
Normal file
93
practical_exercises/key_exercises/时钟++运算符重载.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include<iostream>
|
||||
#include<cmath>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* 时钟类
|
||||
*/
|
||||
class Clock
|
||||
{
|
||||
private:
|
||||
int Hour, Minute, Second;
|
||||
public:
|
||||
Clock(int h=0, int m=0, int s=0);
|
||||
void ShowTime();
|
||||
Clock& operator ++();
|
||||
Clock operator ++(int);
|
||||
};
|
||||
|
||||
/*
|
||||
* 时钟类构造函数
|
||||
*/
|
||||
Clock::Clock(int h,int m, int s)
|
||||
{
|
||||
if(h>=0 && h<24 && m>=0 && m<60 && s>=0 && s<60)
|
||||
{
|
||||
Hour = h;
|
||||
Minute =m;
|
||||
Second= s;
|
||||
}
|
||||
else
|
||||
cout<<"输入的时间格式错误!"<<endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 显示时间
|
||||
*/
|
||||
void Clock::ShowTime()
|
||||
{
|
||||
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* 时间递增一秒(重载前缀++运算符)
|
||||
*/
|
||||
Clock& Clock::operator ++()
|
||||
{
|
||||
Second++;
|
||||
if (Second >= 60)
|
||||
{
|
||||
Second = Second - 60;
|
||||
Minute++;
|
||||
if (Minute >= 60)
|
||||
{
|
||||
Minute = Minute - 60;
|
||||
Hour++;
|
||||
Hour = Hour % 24;
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*
|
||||
* 时间递增一秒(重载后缀++运算符)
|
||||
*/
|
||||
Clock Clock::operator ++(int)
|
||||
{
|
||||
Clock old = *this;
|
||||
++(*this);
|
||||
return old;
|
||||
}
|
||||
|
||||
/*
|
||||
* 主函数
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
Clock myClock(23,59,59);
|
||||
cout<<"初始化显示时间为:\t\t";
|
||||
myClock.ShowTime();
|
||||
|
||||
cout<<"执行myClock++后的时间为:\t";
|
||||
|
||||
//先执行ShowTime(),输出myClock=23:59:59,
|
||||
//再执行myClock++,此时myClock=00:00:00
|
||||
(myClock++).ShowTime();
|
||||
|
||||
cout<<"执行++myClock后的时间为:\t";
|
||||
|
||||
//先执行++myClock,此时myClock=00:00:01
|
||||
//再执行ShowTime(),输出myClock=00:00:01
|
||||
(++myClock).ShowTime();
|
||||
system("pause");
|
||||
}
|
||||
63
practical_exercises/key_exercises/类模板之栈.cpp
Normal file
63
practical_exercises/key_exercises/类模板之栈.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include<iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<typename T,int MAXSIZE>
|
||||
|
||||
class Stack{
|
||||
public:
|
||||
Stack(){}
|
||||
void init(){
|
||||
top=-1;
|
||||
}
|
||||
bool isFull(){
|
||||
if(top>=MAXSIZE-1)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
bool isEmpty(){
|
||||
if(top==-1)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
void push(T e);
|
||||
T pop();
|
||||
|
||||
private:
|
||||
T elems[MAXSIZE];
|
||||
int top;
|
||||
};
|
||||
|
||||
template<typename T,int MAXSIZE> void Stack<T,MAXSIZE>::push(T e){
|
||||
if(!isFull()){
|
||||
elems[++top]=e;
|
||||
}
|
||||
else{
|
||||
cout<<"栈已满,请不要再加入元素!";
|
||||
return;
|
||||
}
|
||||
}
|
||||
template<typename T,int MAXSIZE> T Stack<T,MAXSIZE>::pop(){
|
||||
if(!isEmpty()){
|
||||
return elems[top--];
|
||||
}
|
||||
else{
|
||||
cout<<"栈已空,请不要再弹出元素!";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
Stack<int,10> s1;
|
||||
s1.init();
|
||||
int i;
|
||||
for(i=1;i<11;i++)
|
||||
s1.push(i);
|
||||
for(i=1;i<11;i++) cout<<s1.pop()<<"\t";
|
||||
cout<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
76
practical_exercises/key_exercises/类模板特化之数组.cpp
Normal file
76
practical_exercises/key_exercises/类模板特化之数组.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
#define MAXSIZE 5
|
||||
template<class T>
|
||||
class Array{
|
||||
public:
|
||||
Array(){
|
||||
for(int i=0;i<MAXSIZE;i++){
|
||||
array[i]=0;
|
||||
}
|
||||
}
|
||||
|
||||
T &operator[](int i);
|
||||
void Sort();
|
||||
|
||||
|
||||
private:
|
||||
T array[MAXSIZE];
|
||||
};
|
||||
|
||||
template<class T> T& Array<T>::operator[](int i){
|
||||
if(i<0||i>MAXSIZE-1){
|
||||
cout<<"Êý×éϱêÔ½½ç£¡"<<endl;
|
||||
exit(0);
|
||||
}
|
||||
return array[i];
|
||||
}
|
||||
|
||||
template<class T> void Array<T>::Sort(){
|
||||
int p,j;
|
||||
for(int i=0;i<MAXSIZE-1;i++){
|
||||
p=i;
|
||||
for(j=i+1;j<MAXSIZE;j++){
|
||||
if(array[p]<array[j])
|
||||
p=j;
|
||||
}
|
||||
T t;
|
||||
t=array[i];
|
||||
array[i]=array[p];
|
||||
array[p]=t;
|
||||
}
|
||||
}
|
||||
template<> void Array<char *>::Sort(){
|
||||
int p,j;
|
||||
for(int i=0;i<MAXSIZE-1;i++){
|
||||
p=i;
|
||||
for(j=i+1;j<MAXSIZE;j++){
|
||||
if(strcmp(array[p],array[j])<0)
|
||||
p=j;
|
||||
}
|
||||
char* t=array[i];
|
||||
array[i]=array[p];
|
||||
array[p]=t;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
Array<int> a1;
|
||||
Array<char*>b1;
|
||||
a1[0]=1;a1[1]=23;a1[2]=6;
|
||||
a1[3]=3; a1[4]=9;
|
||||
a1.Sort();
|
||||
for(int i=0;i<5;i++)
|
||||
cout<<a1[i]<<"\t";
|
||||
cout<<endl;
|
||||
b1[0]="x1"; b1[1]="ya"; b1[2]="ad";
|
||||
b1[3]="be"; b1[4]="bc";
|
||||
b1.Sort();
|
||||
for(int i=0;i<5;i++)
|
||||
cout<<b1[i]<<"\t";
|
||||
cout<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
90
practical_exercises/key_exercises/继承与封装.cpp
Normal file
90
practical_exercises/key_exercises/继承与封装.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
|
||||
using namespace std;
|
||||
class Employee{
|
||||
public:
|
||||
Employee(const char *name,const char *id){
|
||||
strcpy(Name,name);
|
||||
strcpy(Id,id);
|
||||
}
|
||||
char* getName(){
|
||||
return Name;
|
||||
}
|
||||
char* getId(){
|
||||
return Id;
|
||||
}
|
||||
void display(){
|
||||
cout<<Name<<"\t"<<Id<<endl;
|
||||
}
|
||||
|
||||
private:
|
||||
char Name[20];
|
||||
char Id[20];
|
||||
};
|
||||
|
||||
|
||||
class Manager:public Employee{
|
||||
public:
|
||||
//直接调用构造方法传递,基类构造方法有参数,派生类必须通过构造方法,在初始化列表中传递参数
|
||||
Manager(const char *name,const char *id,int week):Employee(name,id){
|
||||
WeeklySalary=week*1000;
|
||||
}
|
||||
void display(){
|
||||
cout<<"经理:"<<getName()<<"\t"<<getId()<<"\t"<<WeeklySalary<<endl;
|
||||
}
|
||||
private:
|
||||
int WeeklySalary;
|
||||
};
|
||||
|
||||
class SaleWorker:public Employee{
|
||||
public:
|
||||
SaleWorker(const char*name,const char *id,int profit,int x):Employee(name,id){
|
||||
workerMoney=baseMoney+x*0.05*profit;
|
||||
}
|
||||
void display(){
|
||||
cout<<"销售员:"<<getName()<<"\t"<<getId()<<"\t"<<workerMoney<<endl;
|
||||
}
|
||||
|
||||
private:
|
||||
float baseMoney=800.0;
|
||||
float workerMoney;
|
||||
};
|
||||
class HourWorker:public Employee{
|
||||
public:
|
||||
HourWorker(const char*name,const char *id,int h):Employee(name,id){
|
||||
TotalMoney=h*hourMoney;
|
||||
}
|
||||
void display(){
|
||||
cout<<"小时工:"<<getName()<<"\t"<<getId()<<"\t"<<TotalMoney<<endl;
|
||||
}
|
||||
private:
|
||||
float hourMoney=100.0;
|
||||
float TotalMoney;
|
||||
};
|
||||
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
cout<<"请输入工作周:";
|
||||
int week;
|
||||
cin>>week;
|
||||
Manager m("小王","11111111",week);
|
||||
m.display();
|
||||
cout<<"请输入销售利润:";
|
||||
int profit;
|
||||
cin>>profit;
|
||||
cout<<"请输入销售件数:";
|
||||
int x;
|
||||
cin>>x;
|
||||
SaleWorker s("小李","222222",profit,x);
|
||||
s.display();
|
||||
cout<<"请输入工作小时:";
|
||||
int hour;
|
||||
cin>>hour;
|
||||
HourWorker h("小何","333333",hour);
|
||||
h.display();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
73
practical_exercises/key_exercises/读写文件综合题.cpp
Normal file
73
practical_exercises/key_exercises/读写文件综合题.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
|
||||
#include<iostream>
|
||||
#include<fstream>
|
||||
#include<cstring>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
class Person{
|
||||
public:
|
||||
Person(){}
|
||||
Person(char *name,char *id,int math,int chinese,int english){
|
||||
strcpy(Name,name);
|
||||
strcpy(Id,id);
|
||||
Math=math;
|
||||
Chinese=chinese;
|
||||
English=english;
|
||||
Sum=Math+Chinese+English;
|
||||
}
|
||||
|
||||
void display(){
|
||||
cout<<Name<<"\t"<<Id<<"\t"<<Math<<"\t"<<Chinese<<"\t"<<English<<"\t"<<Sum<<endl;
|
||||
}
|
||||
private:
|
||||
char Name[20];
|
||||
char Id[20];
|
||||
int Math;
|
||||
int Chinese;
|
||||
int English;
|
||||
int Sum;
|
||||
};
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char ch;
|
||||
char Name[20],Id[20];
|
||||
int Math,Chinese,English;
|
||||
fstream ioFile;
|
||||
ioFile.open("d:/per.dat",ios::out|ios::app);
|
||||
cout<<"---------建立学生档案信息----------\n";
|
||||
do{
|
||||
cout<<"请输入姓名:";
|
||||
cin>>Name;
|
||||
cout<<"请输入身份证号:";
|
||||
cin>>Id;
|
||||
cout<<"请输入数学成绩:";
|
||||
cin>>Math;
|
||||
cout<<"请输入汉语成绩:";
|
||||
cin>>Chinese;
|
||||
cout<<"请输入英语成绩:";
|
||||
cin>>English;
|
||||
Person per(Name,Id,Math,Chinese,English);
|
||||
ioFile.write((char *)&per,sizeof(per));
|
||||
cout<<"您是否继续建档?(Y/y) ";
|
||||
cin>>ch;
|
||||
}while(ch=='y'||ch=='Y');
|
||||
ioFile.close();
|
||||
ioFile.open("d://per.dat",ios::in);
|
||||
Person p;
|
||||
ioFile.read((char*)&p,sizeof(p));
|
||||
vector<Person> v;
|
||||
vector<Person>::iterator vt;
|
||||
while(!ioFile.eof()){
|
||||
v.push_back(p);
|
||||
ioFile.read((char*)&p,sizeof(p));
|
||||
}
|
||||
ioFile.close();
|
||||
cout<<"---------输出档案信息-----------"<<endl;
|
||||
for(vt=v.begin();vt!=v.end();vt++){
|
||||
(*vt).display();
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
39
practical_exercises/key_exercises/输入输出运算符重载.cpp
Normal file
39
practical_exercises/key_exercises/输入输出运算符重载.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
有一销售人员类Sales,其数据成员有姓名name,身份证号id,年龄age。
|
||||
重载输入/输出运算符实现对Sales类数据成员的输入和输出。
|
||||
*/
|
||||
#include<iostream.h>
|
||||
#include<string.h>
|
||||
class Sales{
|
||||
private:
|
||||
char name[10];
|
||||
char id[18];
|
||||
int age;
|
||||
public:
|
||||
Sales(char *Name,char *ID,int Age);
|
||||
|
||||
friend Sales &operator<<(ostream &os,Sales &s); //重载输出运算符
|
||||
friend Sales &operator>>(istream &is,Sales &s); //重载输入运算符
|
||||
};
|
||||
Sales::Sales(char *Name,char *ID,int Age) {
|
||||
strcpy(name,Name);
|
||||
strcpy(id,ID);
|
||||
age=Age;
|
||||
}
|
||||
Sales& operator<<(ostream &os,Sales &s) {
|
||||
os<<s.name<<"\t"; //输出姓名
|
||||
os<<s.id<<"\t"; //输出身份证号
|
||||
os<<s.age<<endl; //输出年龄
|
||||
return s;
|
||||
}
|
||||
Sales &operator>>(istream &is,Sales &s) {
|
||||
cout<<"输入雇员的姓名,身份证号,年龄"<<endl; is>>s.name>>s.id>>s.age;
|
||||
return s;
|
||||
}
|
||||
void main(){
|
||||
Sales s1("杜康","214198012111711",40); //L1
|
||||
cout<<s1; //L2
|
||||
cout<<endl; //L3
|
||||
cin>>s1; //L4
|
||||
cout<<s1; //L5
|
||||
}
|
||||
41
practical_exercises/key_exercises/输入输出重载.cpp
Normal file
41
practical_exercises/key_exercises/输入输出重载.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
#include<cstring>
|
||||
class Sales{
|
||||
private:
|
||||
char name[10];
|
||||
char id[18];
|
||||
int age;
|
||||
public:
|
||||
Sales(char *Name,char *ID,int Age);
|
||||
friend Sales &operator<<(ostream &os,Sales &s);
|
||||
friend Sales &operator>>(istream &is,Sales &s);
|
||||
|
||||
};
|
||||
|
||||
|
||||
Sales::Sales(char *Name,char *ID,int Age){
|
||||
strcpy(name,Name);
|
||||
strcpy(id,ID);
|
||||
age=Age;
|
||||
}
|
||||
|
||||
Sales &operator<<(ostream&os,Sales &s){
|
||||
os<<s.name<<"\t"<<s.id<<"\t"<<s.age<<endl;
|
||||
return s;
|
||||
}
|
||||
|
||||
Sales &operator>>(istream&is,Sales &s){
|
||||
cout<<"输入雇员的姓名,身份证,年龄:\n";
|
||||
is>>s.name>>s.id>>s.age;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
Sales s("张三","15611",26);
|
||||
cout<<s;
|
||||
cin>>s;
|
||||
cout<<s;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
19
practical_exercises/key_exercises/输出格式.cpp
Normal file
19
practical_exercises/key_exercises/输出格式.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include<iostream>
|
||||
#include<iomanip>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char s[20]="this is a string";
|
||||
double digit=-36.96656;
|
||||
cout<<setw(30)<<left<<setfill('*')<<s<<endl;
|
||||
cout<<dec<<setprecision(4)<<digit<<endl;
|
||||
cout<<dec<<15<<endl;
|
||||
//setbase(int x)设置进制后,后面所有操作都是按照这个进制来计算!
|
||||
cout<<setbase(10)<<15<<endl;
|
||||
//四舍五入,并保留2位有效数组
|
||||
float x=6.6937;
|
||||
cout<<float(int(x*1000+0.5)/1000.0)<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
35
practical_exercises/key_exercises/运算符重载之强制转换.cpp
Normal file
35
practical_exercises/key_exercises/运算符重载之强制转换.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
有一个类Circle,设计该类的类型转换函数,当将Circle对象转换成int型时,返回圆的半径;当将它转换成double型时,就返回圆的周长;当将它转换成float型时,就返回圆的面积。
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
类型转换函数没有参数。
|
||||
类型转换函数没有返回类型。
|
||||
类型转换函数必须返回将要转换成的type类型数据。
|
||||
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Circle{
|
||||
private:
|
||||
double x,y,r;
|
||||
public:
|
||||
Circle(double x1,double y1,double r1){x=x1;y=y1;r=r1; }
|
||||
operator int(){return int(r);}
|
||||
operator double(){return 2*3.14*r;}
|
||||
operator float(){return (float)3.14*r*r;}
|
||||
};
|
||||
int main(){
|
||||
Circle c(2.3,3.4,2.5);
|
||||
int r=c; //调用operator int(),将Circle类型转换成int
|
||||
double length=c; //调用operator double(),转换成double
|
||||
float area=c; //调用operator float(),将Circle类型转换成float
|
||||
double len=(double) c; //将Cirlce类型对象强制转换成double
|
||||
cout<<r<<endl;
|
||||
cout<<length<<endl;
|
||||
cout<<len<<endl;
|
||||
cout<<area<<endl;
|
||||
system("pause");
|
||||
}
|
||||
25
practical_exercises/key_exercises/重载圆括号的时钟.cpp
Normal file
25
practical_exercises/key_exercises/重载圆括号的时钟.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Time{
|
||||
private:
|
||||
int hh,mm,ss;
|
||||
public:
|
||||
Time(int h=0,int m=0,int s=0):hh(h),mm(m),ss(s){}
|
||||
void operator()(int h,int m,int s) {
|
||||
hh=h;
|
||||
mm=m;
|
||||
ss=s;
|
||||
}
|
||||
void ShowTime(){
|
||||
cout<<hh<<":"<<mm<<":"<<ss<<endl;
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
Time t1(12,10,11);
|
||||
t1.ShowTime();
|
||||
t1.operator()(23,20,34);
|
||||
t1.ShowTime();
|
||||
t1(10,10,10);
|
||||
t1.ShowTime();
|
||||
system("pause");
|
||||
}
|
||||
Reference in New Issue
Block a user