网站建设专业术语,商城网站都有哪 些功能,动漫设计与制作属于计算机类吗,wordpress 开源app适合期末复习c看#xff0c;或者刚入门c的小白看#xff0c;有的题会补充知识点#xff0c;期末复习题的代码一般比较简单#xff0c;所以语法上没那么严谨。本文所有题目要求全在代码块的最上面。
目录
1、设计复数类 2、设计Computer类
3、实现相加的函数模板
4、圆类…
适合期末复习c看或者刚入门c的小白看有的题会补充知识点期末复习题的代码一般比较简单所以语法上没那么严谨。本文所有题目要求全在代码块的最上面。
目录
1、设计复数类 2、设计Computer类
3、实现相加的函数模板
4、圆类的设计
5、学生类设计
6、求圆与直线位置关系
7、家庭账户共享
8、栈类的设计
9、主人召唤宠物
10、点圆圆柱单继承
11、点长方形长方体单继承
12、点圆球体运行时多态
13、学生数据写入文件和读取
14、图形抽象类派生具体类求多个图形面积
15、 除数为0的异常处理 16、学生数据修改【文件操作】
17、分数的加减 18、统计字符数组中某个字符出现次数 1、设计复数类
//设计复数类Complex和友元运算符重载及实现复数类对象的标准输入和输出
#includeiostream
using namespace std;class Complex
{
private:double real, imag;
public:Complex(double r 0, double i 0){real r;imag i;}//下面是友元的声明所以函数参数只写类型是可以的friend istream operator(istream, Complex);friend ostream operator(ostream, Complex);
};
istream operator(istream input, Complex c)
{//输入实部和虚部input c.real c.imag;return input;//返回istream符合operator的实现
}ostream operator(ostream output, Complex c)
{if (c.imag 0)output c.real i c.imag;else if (c.imag 0)output c.real i c.imag;elseoutput c.real i;return output;
}int main()
{Complex c;cin c;cout c;return 0;
}2、设计Computer类
//计算机Computer的属性包括型号、生产厂家和价格用文件流中write()函数将若干
//计算机中的信息写入文件Computer.dat中然后用read()函数将Computer.dat中所有数据读出。
#includeiostream
#includestring
#includefstream
using namespace std;//涉及文件读写操作了肯定要访问成员变量用struct即可
struct Computer
{int id;string manufacter;double price;
};int main()
{Computer c;cout Please input the id, manufacter and price of Computer:;cin c.id c.manufacter c.price;ofstream out(Computer.dat, ios::binary | ios::out);if (!out.is_open())//如果文件打开不成功//也可写为if(!out){cout fail to open the file\n endl;exit(-1);}out.write((const char*)c, sizeof(c));//写入是const char*out.close();ifstream in(Computer.dat, ios::binary | ios::in);if (!in){cout fail to open the file\n endl;exit(-1);}in.read((char*)c, sizeof(c));//读取是char*in.close();cout the data of Computer is: endl;cout c.id c.manufacter c.price endl;return 0;
} 3、实现相加的函数模板
//编写两个相长度数值型一维数组对应元素相加的函数模板
#includeiostream
using namespace std;templateclass T
void add(T a[], T b[], T c[], int n)
{for (int i 0; i n; i)c[i] a[i] b[i];
}int main()
{int a[5] { 1, 2, 3, 4, 5 };int b[5] { 1, 2, 3, 4, 5 };int c[5];add(a, b, c, 5);for (int i 0; i 5; i) cout c[i] ;cout endl;return 0;
} 4、圆类的设计
//1、圆类的设计设计一个圆类计算出它的周长面积
//并有圆心移动和半径移动(就是变化的意思)的操作并打印出数据
#includeiostream
using namespace std;//这里最好不要用float,除非你写为3.14f因为你写一个小数3.14默认为double
//如果你用float这里就会发生截断因为是从double到float
const double pi 3.14;class Circle
{
public://也可采用c11的初始化列表方式/*Circle(int x 0, int y 0, int r 0):_x(x),_y(y),_r(r){}*/Circle(double x 0, double y 0, double r 0){_x x; _y y; _r r;}double circum(){return 2 * pi * _r;}double area(){return pi * _r * _r;}void movep(double x, double y){_x x; _y y;}void mover(double r){_r r;}void show(){cout 圆心( _x , _y ) 半径 _r endl;}private:double _x, _y, _r;
};int main()
{Circle c(1.2, 1.3, 2.1);c.show();cout circum: c.circum() area: c.area() endl;c.movep(1.1, 2.2);c.show();c.mover(1.2);c.show();return 0;
} 5、学生类设计
//学生类设计求学生最高分最低分和平均分,并打印出学生信息
#includeiostream
#includestring
using namespace std;class Student
{
private:int _id;string _name;double _score;public:Student(int id 0, string name , double score 0.0){_id id;_name name;_score score;}void input(int id, string name, double score){_id id;_name name;_score score;}double Getscore(){return _score;}void show(){cout id: _id name: _name score: _score endl;}
};double maxscore(Student stu[], int n)
{double maximum 0.0;for (int i 0; i n; i)if (maximum stu[i].Getscore()) maximum stu[i].Getscore();return maximum;
}double minscore(Student stu[], int n)
{double minimum 100;for (int i 0; i n; i)if (minimum stu[i].Getscore()) minimum stu[i].Getscore();return minimum;
}double averscore(Student stu[], int n)
{double sum 0.0;for (int i 0; i n; i)sum stu[i].Getscore();return sum / n;
}int main()
{Student stu[3];int id; string name; double score;for (int i 0; i 3; i){cin id name score;stu[i].input(id, name, score);}for (int i 0; i 3; i) stu[i].show();cout maxscore: maxscore(stu, 3) endl;cout minscore: minscore(stu, 3) endl;cout averscore: averscore(stu, 3) endl;return 0;
} 6、求圆与直线位置关系
//定义直线类和圆类求两者位置关系相切、相交或相离
#includeiostream
#includecmath
using namespace std;//直线ax by c 0
class Circle;//必须先声明Circle类不然友元函数找不到Circle
class Line
{
private:double _a, _b, _c;
public:Line(double a 0, double b 0, double c 0){_a a;_b b;_c c;}//这里不用友元也可以你可以用一个geta等等来获取私有成员对于圆类同理friend int position_relation(const Line, const Circle);
};class Circle
{
private:double _x, _y, _r;//圆心和半径
public:Circle(double x 0, double y 0, double r 0){_x x;_y y;_r r;}friend int position_relation(const Line, const Circle);
};//圆与直线位置关系公式:daxbyc/根号下a*ab*b【其中abc均为直线的x和y是圆的圆心】
//如果dr相离 dr相切 dr相交
int position_relation(const Line l, const Circle c)
{double d l._a * c._x l._b * c._y l._c / sqrt(l._a * l._a l._b * l._b);//因为d是算出来的浮点数会有精度损失所以比较的时候要靠精度来比if (fabs(d - c._r) 0.000001) return -1; //相离else if (fabs(d - c._r) 0.000001) return 1; //相交else return 0; //相切
}int main()
{Line l(2.1, 2.2, 2.3);Circle c(1.1, 1.2, 1.3);int ret position_relation(l, c);if (ret -1) cout 相离 endl;else if (ret 1) cout 相交 endl;else cout 相切 endl;return 0;
} 回顾友元 在C中友元函数Friend Function是一种特殊的函数它可以访问并操作类的私有成员即使它不是类的成员函数。通过友元函数我们可以实现对类的私有成员的非成员函数访问权限。友元提供了一种突破封装的方式。友元函数提供了一种在需要时访问类的私有成员的机制但应该慎重使用因为过多的友元函数可能破坏类的封装性。 友元函数特性 (1) 友元函数可以访问类的私有和保护成员但不是类的成员函数。(2) 友元函数不能被const修饰。由于友元函数不属于任何类的成员函数它们无法被 const 修饰。(3) 友元函数可以在类定义的任何地方声明不受类访问限定符限制。(4) 一个函数可以是多个类的友元函数。(5) 友元函数的调用与普通函数的调用和原理相同。(6)友元函数/友元类是单向的A在B类中被声明为友元函数/友元类表示A是B的友元函数/友元类但B不是A的友元类函数/友元类(7)友元函数在类中声明时用friend修饰但是在定义时不需要用friend修饰(8)友元函数不能被继承父类的友元函数继承后并不会成为子类的友元函数(9)友元函数不具有传递性A类和B类都是C类的友元类但是A类和B类并不是友元类 7、家庭账户共享
//家庭银行账户共享即一个家庭几个成员共享财产设计一个Familymember类
//实现钱财的支出和存钱并显示剩余钱额【定义静态成员实现】
#includeiostream
#includestring
using namespace std;class Familymember
{
private:string _name;
public:Familymember(string name );static double money;static void save(double m);static void expend(double m);static void show();
};
//类外初始化静态成员变量不要加static
double Familymember::money 0;//类內声明类外定义Familymember::Familymember(string name)
{//类內声明已经有默认参数类外就不能给了否则就重定义默认参数了_name name;
}
//类外定义静态成员函数不能加static
void Familymember:: save(double m)
{money m;
}void Familymember::expend(double m)
{money - m;
}void Familymember::show()
{cout remained money: money endl;
}int main()
{Familymember wife(zhu), husband(jiang);wife.save(10000);husband.expend(5000);husband.save(20000);wife.show();//对象可以访问静态成员函数但静态成员函数没有this指针husband.show();Familymember::show();return 0;
} 8、栈类的设计
//设计一个静态栈类内有构造函数且有压栈和入栈操作并返回栈顶元素输入数据
//以0表示结束
//学校期末要求不高我以前的博客写过动态栈想了解的可以看看
#includeiostream
using namespace std;//这里不要定义为size因为size是一个关键字用于定义类型大小如果你这里
//常量名称定义为size就会冲突但是在devc就能跑过去可能dev不严谨vs下是过不去的
const int stackSize 100;class Stack
{
private:int sta[stackSize];int _top;//栈顶public:Stack();int push(int x);//入栈int pop();//出栈
};Stack::Stack()
{//直接置空即可_top 0;
}int Stack::push(int x)
{if (_top stackSize)return 0;//栈满返回0sta[_top] x;return x;
}int Stack::pop()
{if (_top 0)return 0;//栈空返回0//返回删除之后的栈顶return sta[--_top];
}int main()
{Stack s;int x;cout Please input pushed data: endl;cin x;while (x s.push(x)){cout Please input pushed data: endl;cin x;}while (x s.pop())cout the poped data is : x endl;return 0;
} 9、主人召唤宠物
//主人召唤宠物宠物有名字和应答语主人也有名字他可以召唤宠物看召唤名字与宠物
//的名字是否一致若一致宠物就会回应应答语
#includeiostream
#includestring
using namespace std;
class Pet
{
private:string _name;string _ans;public:Pet(string name , string ans ){_name name;_ans ans;}void answer(string name) const{if (name _name)//调用string的operatorcout _ans endl;elsecout dear host, you are not calling me! endl;}
};class Host
{
private:string _name;public:Host(string name ){_name name;}void call(const Pet p, string name){p.answer(name);}
};int main()
{Host h(MeiZhu);Pet dog(huihui, wangwang);Pet cat(huahua, miaomiao~);h.call(dog, huihui);h.call(cat, huahuaya);return 0;
} 10、点圆圆柱单继承
//点圆圆柱单继承即圆类继承点【圆心】类再由圆柱类继承圆类,求出面积和体积
#includeiostream
using namespace std;const double pi 3.14;class Point
{
private://private即可圆类求面积和体积用不到圆心double _x, _y;
public:Point(double x 0, double y 0){_x x;_y y;}double area(){return 0.0;}double volume(){return 0.0;}
};class Circle : public Point
{
protected://这里若用private的话Cylinder求面积和体积就无法使用了double _r;public:Circle(double r 0.0, double x 0.0, double y 0.0):Point(x, y){_r r;}double area(){return pi * _r * _r;}double volume(){return 0.0;}
};class Cylinder : public Circle
{
private:double _h;public:Cylinder(double x 0.0, double y 0.0, double r 0.0, double h 0.0):Circle(r, x, y){_h h;}double area(){ //上下底圆的面积侧面积长方形面积return 2 * pi * _r * _r 2 * pi * _r * _h;}double volume(){ //圆柱体积为底面积*高return Circle::area() * _h;}
};int main()
{Circle cir(2.1, 1.1, 2.2);Cylinder cy(2.2, 2.3, 2.4, 1.2);cout cir.area() cir.volume() endl;cout cy.area() cy.volume() endl;return 0;
} 11、点长方形长方体单继承
//点长方形长方体单继承长方形继承点类长方体继承长方形类求面积和体积
#includeiostream
using namespace std;class Point
{
private:double _x, _y;public:Point(double x 0.0, double y 0.0){_x x;_y y;}double area(){return 0.0;}double volume(){return 0.0;}
};
//长方形
class Rectangle : public Point
{
protected://不能设为private不然长方体类中用不了了double _length, _width;public:Rectangle(double x 0.0, double y 0.0, double l 0.0, double w 0.0):Point(x, y){_length l;_width w;}double area(){return _length * _width;}double volume(){return 0.0;}
};
//长方体
class Cuboid : public Rectangle
{
private:double _h;public:Cuboid(double x, double y, double l, double w, double h):Rectangle(x, y, l, w){_h h; }double area(){//长方体表面积2长*宽2长*高2宽*高return 2 * _length * _width 2 * _length * _h 2 * _width * _h;}double volume(){//长方体体积底面积*高return _length * _width * _h;}
};int main()
{Rectangle rec(1.1, 2.0, 2.3, 2.4);Cuboid cub(1.2, 2.2, 2.4, 4.5, 1.1);cout area of rectangle is : rec.area() \t volume of rectangle is: rec.volume() endl;cout area of cuboid is : cub.area() \t volume of cuboid is: cub.volume() endl;return 0;
} 12、点圆球体运行时多态
//点圆球体运行时多态由圆类继承点类球类继承圆类实现求面积和体积要求多态实现
#includeiostream
using namespace std;const double pi 3.14;class Point
{
private:double _x, _y;
public:Point(const double x, const double y){//这种加const写法的才是最标准的因为应付期末没有那么高的要求的_x x;_y y;}virtual double area() 0;virtual double volume() 0;
};class Circle : public Point
{
protected:double _r;
public:Circle(double x, double y, double r):Point(x, y){_r r; }virtual double area(){ //圆的面积π*r*rreturn pi * _r * _r;}virtual double volume(){return 0.0;}
};
//球体类
class Sphere : public Circle
{
private:double _z; //因为球体的是三维的所以它的圆心应该再增加一个维度
public:Sphere(double x, double y, double r, double z):Circle(x, y, r){_z z;}double area(){return 4 * pi * _r * _r;}double volume(){ //V(4/3)πr^3return 4 * pi * _r * _r * _r / 3;}
};int main()
{Point* p;Circle c(1.1, 1.2, 1.3);Sphere s(1.2, 1.3, 1.4, 1.5);p c;cout circle: p-area() \t\t p-volume() endl;p s;cout sphere: p-area() \t p-volume() endl;return 0;
} 13、学生数据写入文件和读取
//学生数据写入文件并读取文件数据到程序中【使用read和write实现】
#includeiostream
#includefstream
using namespace std;struct Student
{char name[20];int id;int age;char gender[5];
};int main()
{//创建一个输出流对象并打开文件以二进制的方式写//写法1//ofstream outfile;//outfile.open(test.txt, ios::binary | ios::trunc);//trunc:如果文件存在先删除再创建【一般是不用的】//写法2更简洁ofstream outfile(test.txt, ios::binary | ios::trunc);if (!outfile)//或写为if(!outfile.is_open()){//判断文件是否打开成功cout file open error! endl;exit(1);//exit(非0)都代表异常退出}Student stu[3];cout Please input the data of 3 students endl;for (int i 0; i 3; i){cin stu[i].name stu[i].id stu[i].age stu[i].gender;//写入文件以二进制形式写入outfile.write((const char*)stu[i], sizeof(stu[i]));}outfile.close();//关闭文件以便以下一次的读出数据//再把学生数据读出Student tmp[3];//保存从文件中读出的数据ifstream infile(test.txt, ios::binary);//读取方法一直接读取整个数组的数据中//infile.read((char*)tmp, sizeof(tmp));//或写为infile.read((char*)tmp[0], sizeof(tmp));//因为都是首元素的地址读取的是一整个数组cout The Student data read from file is: endl;for (int i 0; i 3; i){//读取方法二读取数据一个一个数组元素来读取infile.read((char*)tmp[i], sizeof(tmp[i]));cout tmp[i].name tmp[i].id tmp[i].age tmp[i].gender endl;}infile.close();return 0;
}
测试结果如下 此时打开test.txt文件可以看出是二进制的形式我们看不懂但是从文件中读入到程序当中就能看出来了主要是因为这种二进制读写方式很方便 14、图形抽象类派生具体类求多个图形面积
//用图形类派生圆类和三角形类分别求面积并求出面积之和【多态实现】
#includeiostream
using namespace std;const double pi 3.14;class Graph
{
public:virtual double area() 0;
};class Circle : public Graph
{
private:double _x, _y, _r;
public:Circle(const double x, const double y, const double r):_x(x),_y(y),_r(r){}double area(){return pi * _r * _r;}
};class Triangle : public Graph
{
private:double _bottom, _h;
public:Triangle(const double bottom, const double h):_bottom(bottom),_h(h){}double area(){return 0.5 * _bottom * _h;}
};double sumarea(Graph* g[], int n)
{double sum 0.0;for (int i 0; i n; i)sum g[i]-area();return sum;
}int main()
{Circle c1(1.1, 1.2, 1.3), c2(1.2, 1.3, 1.4);Triangle t1(2.1, 3.1), t2(1.1, 3.4);Graph* g[4] { c1, c2, t1, t2 };cout sumarea(g, 4) endl;return 0;
} 15、 除数为0的异常处理
建议先看我写过的异常文章【C】异常【完整版】-CSDN博客
//写一个实现除法的函数主函数利用异常处理除数为0的情况
#includeiostream
#includestring
using namespace std;//division:除法dividend:被除数divisor:除数
double division(double x, double y)
{if (y 0){//我们不建议写为throw 0.0, 因为这种处理方式和之前讲的错误码一样没什么意义//不如直接用错误码了throw string(发生除0错误!);}return x / y;
}int main()
{try{double x, y;cin x y;cout x / y division(x, y) endl;cin x y;cout x / y division(x, y) endl;}catch (const string err){cout err endl;}//下面用基类捕获的方式若除数为0会直接报错//但有时如出现数组越界问题等会直接打印出错误信息//catch (exception e)//{// cout e.what() endl;//}return 0;
} 16、学生数据修改【文件操作】
//建立学生类将学生数据写入文件然后读取回程序中并修改第3个学生的信息然后写入文件
//然后再读取到程序中并打印【要求read和write实现并使用文件指针定位的seekp和seekg】
#includeiostream
#includefstream
#includestring
using namespace std;struct Stu
{int id;char name[20];//不能用string不然会报错读取访问权限冲突除非用c_str原因下面解释double score;
};int main()
{Stu stu[20];//为什么要用fstream呢因为下面有读指针和写指针操作同时进行的这里定义fstream对象//既可以读又可以写很方便省得下面你又得关闭文件然后又打开文件...fstream io(stu.txt, ios::binary | ios::out | ios::in);if (!io){cout Fail to open the file! endl;exit(-1);}cout Please input the datas of students: endl;for (int i 0; i 3; i)cin stu[i].id stu[i].name stu[i].score;io.write((const char*)stu, sizeof(stu));//一次性把整个数组全都写入文件中io.close();io.open(stu.txt, ios::binary | ios::in | ios::out);if (!io){cout Fail to open the file! endl;exit(-1);}Stu tmp;cout Please input the updated datas of the third student: endl;cin tmp.id tmp.name tmp.score;io.seekp(2 * sizeof(tmp), ios::beg);//写文件指针定位从原来第3个学生的位置开始io.write((const char*)tmp, sizeof(tmp));//把修改后的数据放回文件中第3个学生的位置io.close();io.open(stu.txt, ios::binary | ios::out | ios::in);io.read((char*)stu, sizeof(stu));//修改完第3个学生数据后读回数组中io.close();//一定要关闭文件cout The students datas read from file are: endl;for (int i 0; i 3; i)cout stu[i].id stu[i].name stu[i].score endl;return 0;
}问题①、C中文件读写类对象时如果类对象中有string类型的成员变量如何处理 在C中对于类对象的二进制读写涉及到对象的内存布局和成员变量的特性。而对于string这样的对象它的内部实现复杂包括指针指向堆上的动态内存涉及到分配和释放内存等操作。因此直接进行二进制读写可能会导致读取权限冲突。当你尝试将包含string成员变量的对象写入文件时实际上只会将指针的值写入文件而不是字符串的内容。而在读取文件时再次加载这个对象那个指针就指向了一个无效的内存地址。这会导致访问非法内存进而出现读取权限冲突。为了避免这个问题你可以考虑使用序列化和反序列化来实现文件的读写操作。序列化是指将对象转换为序列化的字节流而反序列化则是将字节流重新转换为对象。通过序列化和反序列化操作可以确保对象的完整性和正确性。你可以使用C的一些库如Boost.Serialization、Protocol Buffers来实现对象的序列化和反序列化。这些库提供了易于使用和强大的功能让你可以方便地进行对象的读写操作而不会发生读取权限冲突的问题。 在C中文件读写类对象时是可以包含string类型的成员变量的。C的文件读写操作对于大多数基本类型和标准库类型包括string都有支持。当你将一个自定义的类对象写入文件时包含了string类型成员变量的类实例将被完整地写入文件中。但需要注意的是string对象的存储是动态的所以仅将类对象的二进制数据写入文件是不够的还需要将string的内容一并写入文件并在读取时进行相应处理。你可以使用C标准库中的fstream类进行文件读写操作。例如可以使用ofstream类对象将类对象写入文件再使用ifstream类对象从文件中读取类对象。在写入时可以使用成员函数c_str()将string类型转换为C风格字符串并使用成员函数write()写入文件在读取时可以使用成员函数getline()读取文件中的字符串并重新构造string对象。 注意在进行文件读写操作时需要保证文件的打开和关闭操作正确执行并处理可能出现的异常情况以确保数据的完整性与安全性。同时为了确保跨平台的文件读写兼容性建议在二进制读写时使用文本模式例如使用rb和wb参数。 以下是一个简单的示例代码演示了如何进行文件读写操作
#include fstream
#include iostream
#include stringclass MyClass {
public:std::string name;int age;
};int main() {MyClass obj;obj.name Alice;obj.age 20;std::ofstream outfile(data.txt, std::ios::binary);if (outfile.is_open()) {outfile.write(obj.name.c_str(), obj.name.length() 1);outfile.write(reinterpret_castchar*(obj.age), sizeof(obj.age));outfile.close();std::cout 对象写入文件成功 std::endl;} else {std::cout 打开文件失败 std::endl;return 1;}std::ifstream infile(data.txt, std::ios::binary);if (infile.is_open()) {std::string name;std::getline(infile, name, \0);obj.name name;infile.read(reinterpret_castchar*(obj.age), sizeof(obj.age));infile.close();std::cout 从文件中读取的对象 std::endl;std::cout name: obj.name std::endl;std::cout age: obj.age std::endl;} else {std::cout 打开文件失败 std::endl;return 1;}return 0;
} 该示例中定义了一个包含string类型成员变量的MyClass类通过ofstream对象将类的实例写入文件再通过ifstream对象从文件中读取类对象。在写入时使用了成员函数c_str()获取string对象的C风格字符串在读取时使用了成员函数getline()读取字符串并重新构造了string对象。最后将读取到的类对象的成员变量输出到控制台。 问题②、c中在文件操作中为什么读取完数据后要关闭文件才能写入数据到文件 在C中关闭文件的目的不是为了读取完数据后才能写入数据到文件而是为了确保对文件进行完整的操作并释放相关资源。 文件在被打开后会占用一些系统资源如文件描述符等。当读取完数据后如果不关闭文件文件依然保持打开状态此时其他程序可能无法对该文件进行操作或者文件的状态可能不会及时更新。此外一些操作系统可能会限制同时打开的文件数量如果不关闭文件可能会达到限制而导致其他文件无法正常打开。最重要的是关闭文件还可以确保数据的完整性和正确性。在写入数据时操作系统会将数据缓存到内存中然后根据一定的策略将数据写入到文件中这个过程是异步的。如果在写入数据的过程中突然关闭文件可能会导致数据丢失或不完整。 因此为了保证文件操作的正确性和系统资源的释放我们通常会在读取完数据后关闭文件然后再进行写入操作。这样可以确保文件的状态正确、数据的完整性并释放相关的资源。 17、分数的加减 //定义分数类内有构造函数和显示分数的show函数并利用友元函数实现分数的加减
#includeiostream
using namespace std;class Fraction
{
private:double dividend, divisor;//dividend:被除数即分子 divisor:除数即分母
public:Fraction(const double divid 0.0, const double divir 0.0):dividend(divid),divisor(divir){}void show(){cout dividend / divisor endl;}friend Fraction operator(const Fraction, const Fraction);friend Fraction operator-(const Fraction, const Fraction);
};Fraction operator(const Fraction f1, const Fraction f2)
{return Fraction(f1.dividend * f2.divisor f1.divisor * f2.dividend, f1.divisor * f2.divisor);
}Fraction operator-(const Fraction f1, const Fraction f2)
{return Fraction(f1.dividend * f2.divisor - f1.divisor * f2.dividend, f1.divisor * f2.divisor);
}int main()
{Fraction f1(2, 3), f2(1, 2), f3;f3 f1 f2;f3.show();f3 f1 - f2;f3.show();return 0;
} 18、统计字符数组中某个字符出现次数
//定义求一个字符数组中某字符有多少个的函数sum()用c实现
#includeiostream
using namespace std;int sum(char* arr, char target)
{int cnt 0;int i 0;while (arr[i] ! \0){if (arr[i] target)cnt;i;}return cnt;
}int main()
{char arr[] hello world!;char target o;int ret sum(arr, target);cout target 出现了 ret 次 endl;return 0;
}