-
第一章 C++语言简介
1.编写一个程序,将从键盘输入n个字符串保存在数组A中。在输入字符串之前,先输入n个值。要求,数组A动态申请空间,程序结束释放。#include <iostream> using namespace std; int main() { int n; int l = 0; char * A; cout << "输入n的值:"; cin >> n; A = new char[n]; cout << "输入" << n << "个字符串:"; for (int i = 0; i < n; i++) { cin >> A[i]; } cout << "输出字符串:"; for (int i = 0; i < n; i++) { cout << A[i]; } delete[]A; return 0; }
2.题目1的基础上,输出n个字符串中最长和最短的串,计算n个串的平均长度并输出结果。
#include <iostream> using namespace std; int main() { int n; int l = 0; string* A; cout << "输入n的值:"; cin >> n; A = new string[n]; cout << "输入" << n << "个字符串:\n"; for (int i = 0; i < n; i++) { cin >> A[i]; l += A[i].size(); } // 取最大值 int heightest = A[0].size(); string hstr; for (int i = 1; i < n; i++) { if (heightest < A[i].size()) { heightest = A[i].size(); hstr = A[i]; } } // 取最小值 int lowest = A[n - 1].size(); string lstr; for (int i = 0; i < n - 1; i++) { if (lowest > A[i].size()) { lowest = A[i].size(); lstr = A[i]; } } cout << "\n"; cout << "最长字符:" << hstr << "\t" << heightest << endl; cout << "最短字符:" << lstr << "\t" << lowest << endl; cout << "总长度:" << l << endl; cout << "平均长度:" << l / n << endl; delete[]A; return 0; }
-
第二章
2.设计并实现二维坐标系下的点类Point,类的每个对象有横纵坐标。为类Point添加必要的计算函数,例如,计算给定点到(0,0)的距离,计算给定两点间的距离,判断给定的3个点是否可以构成一个三角形。
#include <iostream>; #include <string> #include <cmath> using namespace std; class Point { public: Point(); Point(double, double); void setPoint(double, double); double getDistance(); double getDistance(Point); void printPoint(); private: double x, y; }; Point::Point() { x = 0, y = 0; } Point::Point(double x0, double y0) { x = x0, y = y0; } void Point::setPoint(double x0, double y0) { x = y0, y = y0; return; } void Point::printPoint() { cout << "(" << x << "," << y << ")" << endl; return; } double Point::getDistance() { return sqrt(x * x + y * y); } double Point::getDistance(Point p) { return sqrt(pow(abs(x - p.x), 2) + pow(abs(y - p.y), 2)); } bool isTriangle(double a, double b, double c) { //三角形的组成条件为: 任意一边小于其他两边之和 if ((a < b + c) && (b < a + c) && (c < a + b)) { return true; } return false; } int main() { Point p(0, 0), p1(1, 2), p2(3, 4), p3(4, 6); cout << "有4个点:\n"; cout << "p:"; p.printPoint(); cout << "p1:"; p1.printPoint(); cout << "p2:"; p2.printPoint(); cout << "p3:"; p3.printPoint(); // 给定点到原点的距离 cout << "\n"; double d1 = p1.getDistance(); double d2 = p2.getDistance(); double d3 = p3.getDistance(); cout << "点p1到原点p的距离:" << d1 << endl; cout << "点p2到原点p的距离:" << d2 << endl; cout << "点p3到原点p的距离:" << d3 << endl; // 给定两点间的距离 cout << "\n"; double a = p1.getDistance(p2); double b = p1.getDistance(p3); double c = p2.getDistance(p3); cout << "点p1到p2的距离:" << a << endl; cout << "点p1到p3的距离:" << b << endl; cout << "点p2到p3的距离:" << c << endl; // 判断给定3个点能够构成三角形 cout << "\n"; bool it = isTriangle(a, b, c); if (it) { cout << "点p1,p2,p3可以构成三角形"; } else { cout << "点p1,p2,p3不可以构成三角形"; } cout << "\n"; return 0; }
3.设计并实现一个直线类,定义计算函数,求直线的斜率,判别给定点是否在线上,计算给定点到直线的距离
#include<iostream> #include<string> using namespace std; class Line { public: double x1, y1; double x2, y2; double k, b; Line(); Line(double m_x1, double m_y1, double m_x2, double m_y2) :x1(m_x1), y1(m_y1), x2(m_x2), y2(m_y2) {} ~Line() {}; void equation(); //方程 void point(); //交点 void slope(); //斜率 bool LPoint( double, double);// 点是否在直线上 double distance( double, double); // 点到直线的距离 }; Line::Line() { x1 = 0, y1 = 0; x2 = 0, y2 = 0; k = 0, b = 0; } void Line::equation() { if (x2 == x1) cout << "该直线方程是:x=" << x1 << endl; else { b = y1 - x1 * k; //直线方程与y轴的交点 cout << "该直线方程是:y=" << k << "x+" << b << endl; } } void Line::point() { if (x2 == x1) cout << "该直线方程只与x轴有的交点:(" << x1 << ",0)" << endl; else if (y1 == y2) cout << "该直线方程只与y轴有的交点:(0," << b << ")" << endl; else { cout << "该直线方程与x轴的交点:(" << (-b / k) << ",0)" << endl; cout << "该直线方程与y轴的交点:(0," << b << ")" << endl; } } void Line::slope() { if (x2 == x1) cout << "该直线的斜率是无穷大" << endl; else { k = (y2 - y1) / (x2 - x1); //求斜率 cout << "该直线的斜率是:" << k << endl; } } bool Line::LPoint(double x, double y) { if (y == k * x + b) { return 1; } else { return 0; } } double Line::distance( double x,double y) { return abs(x - y + b) / sqrt(pow(k, 2) + 1); } void main() { double x1, x2, y1, y2; cout << "请输入任意两个坐标:"; cin >> x1 >> y1 >> x2 >> y2; if (x1 == y1 == x2 == y2) { cout << "请重新输入任意两个坐标:"; cin >> x1 >> y1 >> x2 >> y2; } Line a(x1, y1, x2, y2); a.slope(); //输出直线斜率 a.equation(); //输出直线方程 a.point(); //输出直线与x、y轴的交点 cout << "\n"; cout << "输入点:"; cin>> x1 >> y1; bool lp = a.LPoint(x1,y1); if (lp) { cout << "在直线上"<<endl; } else { cout << "不在直线上" << endl; } double d = a.distance(x1, y1); cout << "到直线的距离为:"<<d << endl; }
输出结果
请输入任意两个坐标:1 2 3 4 该直线的斜率是:1 该直线方程是:y=1x+1 该直线方程与x轴的交点:(-1,0) 该直线方程与y轴的交点:(0,1) 输入点:5 7 不在直线上 到直线的距离为:0.707107
4.定义圆柱体类Cylinder,有私有成员变量底圆半径r,高h,公有成员函数,底圆周长,底圆面积及圆柱体体积。输入半径和高,计算圆柱体的表面积和体积。
#include<iostream> #include<string> using namespace std; class Cylinder { double r; double h; public: double pi = 3.14; double c; // 圆周长 double s;// 低圆面积 double v;// 圆柱体体积 double sb;// 圆柱体表面积 Cylinder(double r, double h) { this->r = r; this->h = h; c = C(r); s = S(r); v = V(s, h); sb = SB(s, c, h); } double C(double); double S(double); double V(double, double); double SB(double, double, double); }; double Cylinder::C(double r) { return 2 * pi * r; } double Cylinder::S(double r) { return pi * r * r; } double Cylinder::V(double s, double h) { return s * h; } double Cylinder::SB(double s, double c, double h) { return 2 * s + c * h; } int main() { double r, h; cout << "输入圆柱体的半径和高:"; cin >> r >> h; Cylinder cl(r, h); cout << "周长:" << cl.c << endl; cout << "面积:" << cl.s << endl; cout << "体积:" << cl.v << endl; cout << "表面积:" << cl.sb << endl; }
输出结果
输入圆柱体的半径和高:2 3 周长:12.56 面积:12.56 体积:37.68 表面积:62.8
-
第三章
1.为第二章设计的微微坐标系下的类Point添加必要的构造函数、复制构造函数和析构函数。要求能在主函数接收一下形势的对象声明:Point p0; Point p1(2, 3); Point p2(p0);
2.习题1的基础上,为Point添加一个静态变量,统计创建的点的个数。
习题1,2解:
#include <iostream> using namespace std; class Point { private: double x, y; public: Point(); Point(double, double); Point(Point&); void pt(); void pcount(); static int count; }; int Point::count = 0; Point::Point() { x = 0; y = 0; count++; } Point::Point(double x0, double y0) { x = x0; y = y0; count++; } Point::Point(Point& p) { x = p.x; y = p.y; count++; } void Point::pt() { cout << x << " " << y << endl; } void Point::pcount() { cout << "共创建了" << count << "个点" << endl; } int main() { Point p0; p0.pt(); Point p1(2, 3); p1.pt(); Point p2(p0); p2.pt(); p2.pcount(); return 0; }
输出结果
0 0 2 3 0 0 共创建了3个点
3.模仿习题1的形式,为第二章习题中设计类Course,添加构造函数,复制构造函数,析构函数,静态变量,统计课程数。
#include <iostream> using namespace std; class Course { public: char name; double score; int credit; char category; char major; Course(); Course(Course &); ~Course(); static int count; }; int Course::count = 0; Course::Course() { count++; } Course::Course(Course & c) { count++; } Course::~Course() { } int main() { Course c0, c1; cout<<c1.count; return 0; }
输出结果
2
4.重新设计第二章习题中的MyLine类,让其作为点类Point的封闭类。添加必要的构造函数、复制构造函数和析构函数。
#include <iostream>; #include <string> #include <cmath> using namespace std; class Point { public: Point(); Point(double, double); Point(Point&); ~Point(); void setPoint(double, double); void setpp(Point); double getPointX(); double getPointY(); void printPoint(); private: double x, y; }; Point::Point() { x = 0, y = 0; } Point::Point(double x0, double y0) { x = x0, y = y0; } void Point::setPoint(double x0, double y0) { x = y0, y = y0; return; } void Point::setpp(Point p0) { x = p0.x, y = p0.y; return; } double Point::getPointX() { return x; } double Point::getPointY() { return y; } void Point::printPoint() { cout << "(" << x << "," << y << ")" << endl; return; } Point::Point(Point& p) { x = p.getPointX(); y = p.getPointY(); } Point::~Point() { } class Line { Point p; public: double x1, y1; double x2, y2; double k, b; Line(); //Line(double m_x1, double m_y1, double m_x2, double m_y2) :x1(m_x1), y1(m_y1), x2(m_x2), y2(m_y2) {} Line(Point,Point); ~Line() {}; double pt(); }; Line::Line() { x1 = 0, y1 = 0; x2 = 0, y2 = 0; k = 0, b = 0; } Line::Line(Point p1,Point p2) { x1 = p1.getPointX(); y1 = p1.getPointY(); x2 = p2.getPointX(); y2 = p2.getPointY(); } double Line::pt() { cout << "(" << x1 << "," << y1 << ")" << endl; cout << "(" << x2 << "," << y2 << ")" << endl; return 0; } int main() { Point p1(1,2), p2(3,4); p1.printPoint(); p2.printPoint(); Line l(p1,p2); l.pt(); return 0; }
6.定义一个类,在其私有成员变量中保存从键盘输入的10个int类型值,然后按与输入的相反顺序输出它们
#include <iostream>; using namespace std; class T { int A[10]; public: void setA(const int *a) { for (int i = 0; i < 10; i++) { A[i] = a[i]; } return; } void printA() { for (int z = 9; z >= 0; z--) { cout << A[z] << " "; } } }; int main() { T t; int m[10]; for (int i = 0; i < 10; i++) { cin >> m[i]; } t.setA(m); t.printA(); return 0; }
- 第四章
1.重载乘法运算符。设a+bi和c+di是任意两个复数,它们的积(a+bi)*(c+di)=(ac-bd)+(bc+ad)i.
2.重载流插入和流提取运算符.#include <iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0) { real = r; imag = i; } friend ostream& operator << (ostream& out, const Complex& c); friend istream& operator >> (istream& in, Complex& c); friend Complex operator * (const Complex &c1, const Complex& c2); }; ostream& operator << (ostream& out, const Complex& c) { out << c.real << "+" << c.imag << "i" << endl; return out; } istream& operator >> (istream& in, Complex& c) { cout << "Enter Real Part: "; in >> c.real; cout << "Enter Imagenory Part: "; in >> c.imag; return in; } Complex operator * (const Complex& c1, const Complex& c2){ return Complex((c1.real * c2.real - c1.imag*c2.imag),(c1.imag*c2.real + c1.real*c2.imag)); } int main() { // 1.重载乘法运算符 Complex c1(1, 2), c2(3, 4), res; res = c1 * c2; cout << res<<endl; // 2.重载流插入和流提取运算符 Complex c; cin >> c; cout << "The complex object is: "; cout << c<<endl; return 0; }
- 集合Set,重载运算符+(表示集合的并), -(表示集合的差), *(表示集合的交),<(表示集合的真子集),==(判断两个集合是否相等),!=(判断两个集合是否不相等).
#include <iostream> using namespace std; class Set { public: Set(int* p, int n); Set(); friend Set operator+(Set& s, Set& t); friend Set operator-(Set& s, Set& t); friend Set operator*(Set& s, Set& t); friend bool operator<(Set& s, Set& t); friend bool operator==(Set& s, Set& t); friend bool operator!=(Set& s, Set& t); void display(); private: int a[20], num; }; Set::Set(int* p, int n) { int i; num = n; for (i = 0; i < num; i++) { a[i] = p[i]; } } Set::Set() { for (int i = 0; i < 20; i++) { a[i] = '\0'; } } void Set::display() { for (int i = 0; i < num; i++) { if (a[i] != '\0') cout << a[i] << " "; } cout << endl; } Set operator+(Set& s, Set& t) { Set g; int i, j, k = s.num + t.num; g.num = k; for (i = 0; i < s.num; i++) { g.a[i] = s.a[i]; } for (i = 0; i < t.num; i++) { g.a[s.num + i] = t.a[i]; } for (i = 0; i < k; i++) for (j = 0; j < k; j++) { if (i != j) if (g.a[i] == g.a[j]) { for (int q = j; q < k; q++) { g.a[q] = g.a[q + 1]; } } } cout << "并运算:" << endl; return g; } Set operator-(Set& s, Set& t) { Set g; int i, j; for (i = 0; i < s.num; i++) { for (j = 0; j < t.num; j++) { if (s.a[i] == t.a[j]) break; if (j == t.num - 1) g.a[i] = s.a[i]; g.num = i; } } g.num++; cout << "差运算:" << endl; return g; } Set operator*(Set& s, Set& t) { Set g; int i, j; for (i = 0; i < s.num; i++) { for (j = 0; j < t.num; j++) { if (s.a[i] == t.a[j]) { g.a[i] = s.a[i]; g.num = i; break; } } } g.num++; cout << "交运算:" << endl; return g; } bool operator==(Set& x1, Set& x2) { Set g; int i, j; for (int i = 0; i < x1.num; i++) { for (int j = 0; j < x2.num; j++) { if (x1.a[i] == x2.a[i]) { break; } if (j == x2.num - 1) { return 0; } } } return 1; } bool operator!=(Set& x1, Set& x2) { Set g; int i, j; for (int i = 0; i < x1.num; i++) { for (int j = 0; j < x2.num; j++) { if (x1.a[i] == x2.a[i]) { break; } if (j == x2.num - 1) { return 1; } } } return 0; } bool operator<(Set& x1, Set& x2) { Set g; int i, j; if (x1.num >= x2.num) { return 0; } for (int i = 0; i < x1.num; i++) { for (int j = 0; j < x2.num; j++) { if (x1.a[i] == x2.a[i]) { break; } if (j == x2.num - 1) { return 0; } } } cout << 123; return 1; } int main() { int a[5] = { 1,2,3,4,5 }, b[5] = { 1,3,6,7,8 }; int x1[2] = { 1,2 }, x2[5] = { 1,2,3}; bool bl; Set c(a, 5); Set d(b, 5); Set e(x1, 2); Set f(x2, 3); Set g; g = c + d; g.display(); g = c - d; g.display(); g = c * d; g.display(); bl = c == d; cout << "是否相等:"; if (bl) { cout << "是" << endl; } else { cout << "否" << endl; } bl = c != d; cout << "是否不相等:"; if (bl) { cout << "是" << endl; } else { cout << "否" << endl; } bl = e < f; cout << "是否真子集:"; if (bl) { cout << "是" << endl; } else { cout << "否" << endl; } return 0; }
- 第五章
3.设计一个几何图形类,派生类有三角形,正方形,圆形等,定义必要的成员变量,添加构造函数,析构函数和成员函数。成员函数包括计算图形的周长,面积,显示图形信息等。#include <iostream> #include <cmath> using namespace std; class Shape { double C; double S; public: Shape() { } Shape(double c, double s) { C = c; S = s; } double getC() { return C; } double getS() { return S; } void Print() { cout << "周长:" << C << "\t面积" << S << endl; } ~Shape() { } }; class Triangle :public Shape { double a, b, c; double C, S, p; public: Triangle() { } Triangle(double a0, double b0, double c0) :a(a0), b(b0), c(c0) { setCS(); } void setCS() { C = a + b + c; p = C / 2; S = sqrt(p * (p - a) * (p - b) * (p - c)); Shape(C, S); return; } double getC() { return C; } double getS() { return S; } void Print() { cout << "周长:" << C << "\t面积:" << S << endl; } ~Triangle() { } }; class Square :public Shape { double x; double C, S; public: Square() { } Square(double x0) :x(x0) { setCS(); } void setCS() { C = 4 * x; S = x * x; Shape(C, S); return; } double getC() { return C; } double getS() { return S; } void Print() { cout << "周长:" << C << "\t面积:" << S << endl; } ~Square() { } }; class Circle :public Shape { double PI = 3.14, R; double C, S; public: Circle() { } Circle(double r) :R(r) { setCS(); } void setCS() { C = 2 * PI * R; S = PI * R * R; Shape(C, S); return; } double getC() { return C; } double getS() { return S; } void Print() { cout << "周长:" << C << "\t面积:" << S << endl; } ~Circle() { } }; int main() { Shape s; double a, b, c; cout << "输入三角形3边长:"; cin >> a >> b >> c; Triangle tr(a, b, c); tr.Print(); double x; cout << "输入正方角形的边长:"; cin >> x; Square sq(x); sq.Print(); double r; cout << "输入圆形的半径:"; cin >> r; Circle ci(r); ci.Print(); return 0; }
-
第六章
#include <iostream> #include <cmath> using namespace std; class Shape { double C; double S; public: Shape() { } Shape(double c, double s) { C = c; S = s; } double getC() { return C; } double getS() { return S; } virtual void Print() { cout << "shape 周长:" << C << "\t面积" << S << endl; } ~Shape() { } }; class Triangle :public Shape { double a, b, c; double C, S, p; public: Triangle() { } Triangle(double a0, double b0, double c0) :a(a0), b(b0), c(c0) { setCS(); } void setCS() { C = a + b + c; p = C / 2; S = sqrt(p * (p - a) * (p - b) * (p - c)); Shape(C, S); return; } void Print() { cout << "Triangle 周长:" << C << "\t面积:" << S << endl; } ~Triangle() { } }; class Square :public Shape { double x; double C, S; public: Square() { } Square(double x0) :x(x0) { setCS(); } void setCS() { C = 4 * x; S = x * x; Shape(C, S); return; } void Print() { cout << "Square 周长:" << C << "\t面积:" << S << endl; } ~Square() { } }; class Circle :public Shape { double PI = 3.14, R; double C, S; public: Circle() { } Circle(double r) :R(r) { setCS(); } void setCS() { C = 2 * PI * R; S = PI * R * R; Shape(C, S); return; } void Print() { cout << "Circle 周长:" << C << "\t面积:" << S << endl; } ~Circle() { } }; int main() { Shape *s; double a, b, c; cout << "输入三角形3边长:"; cin >> a >> b >> c; Triangle tr(a, b, c); s = &tr; s->Print(); //tr.Print(); cout << "\n"; double x; cout << "输入正方角形的边长:"; cin >> x; Square sq(x); //sq.Print(); s = &sq; s->Print(); cout << "\n"; double r; cout << "输入圆形的半径:"; cin >> r; Circle ci(r); //ci.Print(); s = &ci; s->Print(); return 0; }
输出结果
输入三角形3边长:4 5 6 Triangle 周长:15 面积:9.92157 输入正方角形的边长:7 Square 周长:28 面积:49 输入圆形的半径:8 Circle 周长:50.24 面积:200.96
-
第七章
1.利用流格式控制符,从键盘输入成绩和姓名,然后进行输出,要求名字左对齐,分数右对齐.
#include <iostream> #include<iomanip> using namespace std; int main() { float score; char name[10]; cin >> score >> name; cout << setiosflags(ios::right) << score << " " << setiosflags(ios::left | ios::adjustfield) << name << endl; return 0; }
2.读取一行文字,将此行文字颠倒顺序输出.
#include <iostream> #include<iomanip> using namespace std; int main() { char ch[20]; cin.getline(ch,20); for (int i = strlen(ch)-1; i >= 0; i--) { cout << ch[i]; } return 0; }
#include <iostream> #include<iomanip> using namespace std; int main() { double x[10]; int i = 0; char ch; cout << "输入实数:\n"; while (cin >> x[i]) { if (i >= 10) { break; } i++; } cout << "\n"; cout << "输出:\n"; for (int j = 0; j < i; j++) { cout << x[j] << endl; cout << setiosflags(ios::fixed) << setprecision(5) << x[j] << endl; cout << resetiosflags(ios::fixed) << setiosflags(ios::scientific) << x[j] << endl; cout << setprecision(7) << x[j] << endl; cout << resetiosflags(ios::scientific); cout << "\n"; } return 0; }
输出结果
输入实数: 1.234 3.456 47.89999 ^Z 输出: 1.234 1.23400 1.23400e+00 1.2340000e+00 3.456 3.45600 3.45600e+00 3.4560000e+00 47.89999 47.89999 4.79000e+01 4.7899990e+01
4.输入若干个整数,分别以16进制,8进制输出,然后以10个字符宽度输出,宽度不足左边补0.
#include <iostream> #include<iomanip> using namespace std; int main() { int x[5] = { 0 }; int i = 0; cout << "输入整数:\n"; while (cin >> x[i]) { cout << setbase(16) << x[i] << endl; cout << setbase(8) << x[i] << endl; cout << setw(10) << right << setfill('0') << x[i] << endl; cout << "\n"; i++; if (i >= sizeof(x) / sizeof(int)) { break; } } return 0; }
输入整数: 1 1 1 0000000001 2 2 2 0000000002 33 21 41 0000000041 44 2c 54 0000000054 55 37 67 0000000067
-
设置标准输入重定向,文件data.txt中每一行保存一个整数,求全部整数的个数和平均值.
#include <iostream> #include<iomanip> using namespace std; int main() { int x, count = 0; double sum = 0; FILE* stream1; freopen_s(&stream1, "data.txt", "r", stdin); while (cin >> x) { sum += x; count++; } cout << "个数:" << count << ",平均数:" << sum / count << endl; return 0; }
文件data.txt
1 2 3 4 5 6 7 8 9 10
输出结果
个数:10,平均数:5.5
6.将美国格式的日期 May 28 2019 转换为国际格式的日期 28 May 2019.
#include <iostream> #include<iomanip> using namespace std; int main() { string str[3]; char ch; int i = 0; while ((ch = cin.peek()) != EOF) { cin >> str[i]; i++; } for (int i = 0; i < sizeof(str) / sizeof(string); i++) { if (i == 0) { cout << str[1]<<" "; } else if (i == 1) { cout << str[0] << " "; } else { cout << str[i] << " "; } } return 0; }
输出结果
May 28 2019 ^Z 28 May 2019
-
第八章
1.输入三角形的3边,判断是否合理,不合理给出提示,合理计算面积并将结果存入文件.#include <iostream> #include<fstream> using namespace std; bool isTriangle(double a, double b, double c) { if (a < b + c && b < a + c && c < a + b) { return true; } return false; } double area(double a, double b, double c) { double p = (a + b + c)/2; return sqrt(p * (p - a) * (p - b) * (p - c)); } int main() { double a, b, c; cout << "请输入三角形的三条边:"; cin >> a >> b >> c; bool bo = isTriangle(a, b, c); if (!bo) { cout << "输入的三条边不能构成三角形,请重新输入"; return 0; } double ar = area(a, b, c); cout << "面积为:" << ar << endl; ofstream f1("triangle.txt", ios::out); f1 << ar; f1.close(); return 0; }
输出结果
请输入三角形的三条边:3 4 5 面积为:6
2.设计程序,打开一个指定文本文件,在每行前面加上行号后输出到另一个文本文件中.
#include <iostream> #include<fstream> using namespace std; int main() { char ch, filename[20]; int count = 0; bool newline = true; cout << "请输入文件名:"; cin >> filename; ifstream f1(filename, ios::in); if (!f1) { cout << "打开文件失败" << endl; return 0; } ofstream f2("score2.txt", ios::out); if (!f2) { cout << "打开文件失败" << endl; return 0; } while ((ch = f1.get()) != EOF) { if (newline) { f2 << ++count << ":"; newline = false; } if (ch == '\n') { newline = true; } f2 << ch; } f1.close(); f2.close(); return 0; }
3.读入一个源程序文件,删除全部注释内容,即以“//”开始到行末尾的文本,以及“
/*...*/
”包括的文字,产生新的源程序文件.#include <iostream> #include<fstream> using namespace std; int main() { fstream f1("zs.txt", ios::in); fstream f2("zs2.txt", ios::out); if (!f1 || !f2) { cout << "打开文件失败"; return 0; } const int size = 250; char temp[size]; bool meetFlag = false; // 循环每行250个字符,存到temp里去 while (f1.getline(temp, size)) { for (int i = 0; i < size; i++) { // 删除//后面的文本 if (temp[i] == '/' && temp[i + 1] == '/') { temp[i] = 0;// 此0为ascii值,后面也是 } // 删除/**/的文本,以下判断顺序不能变 // 删除包括*/之前的文本 if (meetFlag && temp[i] == '*' && temp[i + 1] == '/') { meetFlag = !meetFlag; int j, k; for (j = i + 2, k = 0; j < size; j++) { if (temp[j] == '\0') break; // 最后一个文本的后一个字符为\0 temp[k++] = temp[j];// 将本行*/后面的文本从首位开始往前移 } temp[k] = 0; // 最后一个文本后面赋为0 } // 删除/*和*/之间的文本 if (meetFlag) { temp[i] = 0; } // 删除包括/*之后的文本 if (!meetFlag && temp[i] == '/' && temp[i + 1] == '*') { temp[i] = 0; meetFlag = !meetFlag; } } f2 << temp << endl; } return 0; }
4.设有两个按升序排列的文本文件 data1.txt 和 data2.txt,包含若干个整数,要求两个文件合并成一个按升序排列的新文件 data.txt,且包含两个文件的全部数据.
#include <iostream> #include<fstream> #include<cstdlib> using namespace std; int compare(const void* e1, const void* e2) { int* pa = (int*)e1; int* pb = (int*)e2; return (*pa) - (*pb); } int main() { ifstream f1("data1.txt", ios::in); ifstream f2("data2.txt", ios::in); ofstream f3("data3.txt", ios::out); if (!f1 || !f2 || !f3) { cout << "打开文件失败"; return 0; } int n = 0; int temp[50]; while (f1 >> temp[n]) { n++; } while (f2 >> temp[n]) { n++; } qsort(temp, n, sizeof(int), compare); for (int i = 0; i < n; i++) { f3 << temp[i] << endl; } return 0; }
5.文本文件data.txt 包含若干整数,每个整数之间用空格分隔,要求将奇数保存在 file1.txt 中,偶数保存在 file2.txt 中.
#include <iostream> #include<fstream> #include<cstdlib> using namespace std; int compare(const void* e1, const void* e2) { int* pa = (int*)e1; int* pb = (int*)e2; return (*pa) - (*pb); } int main() { ifstream fd("data.txt", ios::in); ofstream f1("file1.txt", ios::out | ios::binary); ofstream f2("file2.txt", ios::out | ios::binary); if (!f1 || !f2 || !fd) { cout << "打开文件失败"; return 0; } int n; while (fd >> n) { if (n % 2 == 0) { f2 << n << " "; } else { f1 << n << " "; } } return 0; }
-
第九章
1.编写一个模板函数,返回两个数中的最大值。使用整形,浮点型,字符型测试模板#include <iostream>; #include <string>; using namespace std; template <typename T> T Max(T x, T y) { return x > y ? x : y; } int main() { int x = 1, y = 2; float f1 = 4.1, f2 = 3.2; char c1 = 'e', c2 = 'd'; cout << Max(x, y)<<endl; cout << Max(f1, f2) << endl; cout << Max(c1, c2) << endl; }
输出结果
2 4.1 e
2.编写函数模板,实现n个数据由小到大排序。使用整形,浮点型,字符型测试模板
#include <iostream>; #include <string>; using namespace std; template<typename T> int Cmp( T l, T r) { if (l < r) { return -1; } else if (l > r) { return 1; } else { return 0; } } template <typename T> void Swap(T& x, T& y) { T temp = x; x = y; y = temp; } template <typename T> T pr(T arr) { int j; for (int i = 0; i < 5; i++) { j = i; while (j > 0 && Cmp<int>(arr[j - 1], arr[j]) > 0) { swap(arr[j], arr[j - 1]); j--; } } for (int i = 0; i < 5; i++) cout << arr[i]<< " "; cout << endl; return 0; } int main() { int arr[5] = { 1, 3, 5, 4, 2 }; float arr1[5] = { 1.1, 3.2, 5.3, 4.3, 2.5 }; char arr2[5] = { 'a', 'c', 'b','e' , 'd'}; pr(arr); pr(arr1); pr(arr2); }
输出结果
1 2 3 4 5 1.1 2.5 3.2 4.3 5.3 a b c d e
3.编写函数模板,求array数组前size个元素之和。使用整形,浮点型,字符型测试模板
#include <iostream>; #include <string>; using namespace std; template <typename T> T iSum(T arr[], int count, int n) { T sum = 0; if (n <= 0) { return 0; } else if (n > count) { n = count; } for (int i = 0; i < n; i++) { sum += arr[i]; } return sum; } int main() { int arr[5] = { 5, 2, 3, 4, 1 }; float arr2[5] = { 5.1, 2.2, 3.4, 4.6, 1.7 }; char arr3[5] = { '1', '2', '3', '4', '5' }; cout << iSum(arr, sizeof(arr) / sizeof(int), 6) << endl; cout << iSum(arr2, sizeof(arr2) / sizeof(float), 2) << endl; //cout << iSum(arr3, sizeof(arr3) / sizeof(char), 5); }