mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-05-11 10:35:32 +08:00
feat: 统一 cpp 文件编码格式为 utf-8
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
//例题ch.cppi
|
||||
//例题ch.cppi
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class X{
|
||||
@@ -9,8 +9,8 @@ public:
|
||||
int main ()
|
||||
{
|
||||
X obj1, obj2, obj3;
|
||||
obj1 = obj2; //调用重载“=”
|
||||
obj1.operator= (obj2); //调用重载“=”
|
||||
obj1 = obj2 = obj3; //调用重载“=”
|
||||
obj1 = obj2; //调用重载“=”
|
||||
obj1.operator= (obj2); //调用重载“=”
|
||||
obj1 = obj2 = obj3; //调用重载“=”
|
||||
system("pause");
|
||||
}
|
||||
|
||||
@@ -2,27 +2,27 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
struct Person{ //职工基本信息的结构
|
||||
struct Person{ //职工基本信息的结构
|
||||
double salary;
|
||||
char *name;
|
||||
};
|
||||
class SalaryManaege{
|
||||
Person *employ; //存放职工信息的数组
|
||||
int max; //数组下标上界
|
||||
int n; //数组中的实际职工人数
|
||||
Person *employ; //存放职工信息的数组
|
||||
int max; //数组下标上界
|
||||
int n; //数组中的实际职工人数
|
||||
public:
|
||||
SalaryManaege(int Max=0){
|
||||
max=Max;
|
||||
n=0;
|
||||
employ=new Person[max];
|
||||
}
|
||||
double &operator[](char *Name) { //重载[],返回引用
|
||||
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);
|
||||
@@ -37,13 +37,13 @@ public:
|
||||
};
|
||||
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;
|
||||
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";
|
||||
cout<<"-------下为display的输出--------\n\n";
|
||||
s.display();
|
||||
system("pause");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//有复数类Complex,利用运算符重载实现复数的加、减、乘、除等复数运算。
|
||||
//有复数类Complex,利用运算符重载实现复数的加、减、乘、除等复数运算。
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
class Complex {
|
||||
@@ -21,7 +21,7 @@ Complex Complex::operator -(Complex b)
|
||||
{
|
||||
return Complex(r-b.r,i-b.i);
|
||||
}
|
||||
//求复数相乘的算法
|
||||
//求复数相乘的算法
|
||||
Complex Complex::operator *(Complex b)
|
||||
{
|
||||
Complex t;
|
||||
@@ -29,7 +29,7 @@ Complex Complex::operator *(Complex b)
|
||||
t.i=r*b.i+i*b.r;
|
||||
return t;
|
||||
}
|
||||
//求复数相除的算法
|
||||
//求复数相除的算法
|
||||
Complex Complex::operator /(Complex b) {
|
||||
Complex t;
|
||||
double x;
|
||||
@@ -48,8 +48,8 @@ void Complex::display(){
|
||||
int main(void) {
|
||||
Complex c1(1,2),c2(3,4),c3,c4,c5,c6;
|
||||
Complex a,b(2,3);
|
||||
a=b+2; //正确
|
||||
// a=2+b; //错误
|
||||
a=b+2; //正确
|
||||
// a=2+b; //错误
|
||||
a.display();
|
||||
c3=c1+c2;
|
||||
c4=c1-c2;
|
||||
|
||||
Reference in New Issue
Block a user