深圳幻海软件技术有限公司 欢迎您!

C/C++考试必考题目(含答案*仅供参考)

2023-04-02

今天继续来分享几个C++经常考试的几道题目,大家快快拿去,赶紧做一下目录(小事一桩)约瑟夫问题discrebinputoutput效果展示:1、 猜价格游戏2、 计算N以内的所有素数3、 袋中取球4、 乘法口诀表5、 最大公约数和最小公倍数7、&nb

今天继续来分享几个C++经常考试的几道题目,大家快快拿去,赶紧做一下

目录

(小事一桩)约瑟夫问题

discreb

input

output

效果展示:

1、  猜价格游戏

2、  计算 N 以内的所有素数

3、  袋中取球

4、  乘法口诀表

5、  最大公约数和最小公倍数

7、  计算n 阶勒让德多项式

实验二 类与对象

1、  矩形

2、  圆形

3、  友元

4、  分数

1、  矩阵(一)

编写C++程序完成以下功能:

2、  矩阵(二)

3、  矩阵(三)


(小事一桩)约瑟夫问题

discreb

有 m 个人,其编号分别为 1~m。按顺序围成一个圈,现在给定一个数 n,从第一个人开始依次报数,报到 n 的人出圈,然后再从下一个人开始,继续从 1 开始依次报数,报到 n 的人再出圈,……如此循环,直到最后一个人出圈为止。

编程输出所有人出圈的顺序。

input

一行两个正整数 m 和 n,之间用一个空格隔开,1≤m<100,1≤n≤32767。

output

输出 m 行,每行一个正整数,表示依次出圈的人的编号。

  1. #include <iostream>
  2. using namespace std;
  3. int a[50];
  4. int main()
  5. {
  6. int m,n,x,i,count = 0;
  7. cin>>n>>m; //输入总人数n和出局要报的数m
  8. x = n; //把n赋给x,避免使用时n被修改
  9. for (i = 1; i <= n; i++)
  10. {
  11. a[i] = i; //将数组a中每一项按1-n排序
  12. }
  13. do {
  14. for (i = 1; i <= n; i++)
  15. {
  16. //判断是否出局,已经出局的就不用报数了
  17. if (a[i] != 0)
  18. {
  19. count++; //报数
  20. }
  21. if (count == m)
  22. {
  23. a[i] = 0;
  24. count = 0;
  25. x--;
  26. cout<<i<<endl;
  27. }
  28. }
  29. } while (x > 0);//接着循环!!!
  30. return 0;
  31. }

效果展示:

1、  猜价格游戏

编写C++程序完成以下功能:

(1)      假定有一件商品,程序用随机数指定该商品的价格(1-1000的整数);

(2)      提示用户猜价格,并输入:若用户猜的价格比商品价格高或低,对用户作出相应的提示;

(3)      直到猜对为止,并给出提示

  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5. int main() {
  6. srand((int)time(0));
  7. int price=rand()%1000+1;//产生1到1000的随机数
  8. int l=1,r=1000;
  9. while(1) {
  10. cout<<"您可以猜一个价格,当前范围["<<l<<","<<r<<"]的整数。"<<endl;
  11. int guess;
  12. cin>>guess;
  13. if(guess>1000||guess<1||cin.fail()) {
  14. cout<<"输入不合法,请重新输入"<<endl;
  15. cin.clear();
  16. cin.ignore(10000,'\n');
  17. continue;
  18. }
  19. if(guess>price) {
  20. cout<<"猜大了"<<endl;
  21. if(r>guess)
  22. r=guess;//缩小范围
  23. } else if(guess<price) {
  24. cout<<"猜小了"<<endl;
  25. if(l<guess)
  26. l=guess;//缩小范围
  27. } else {
  28. cout<<"您猜对了,价格就是"<<price<<endl;
  29. break;
  30. }
  31. }
  32. return 0;
  33. }

效果展示:

猜小了
您可以猜一个价格,当前范围[550,1000]的整数
600
青小了
您可以猜一个价格,当前范围[600,1000]的整数
700
猜小了
您可以猜一个价格,当前范围[700,1000]的整数
700颢蹦碍
大了
您可以猜一个价格,当前范围[700,800]的整数
750
倩小了
您可以猜一个价格,当前范围[750,800]的整数
760
猜小了
您可以猜一个价格,当前范围[760,800]的整数
780
青小了
您可以猜一个价格,当前范围[780,800]的整数
790
猜大了您可以猜一个价格,当前范围[780,790]的整数。786您猜对了,价格就是786

2、  计算 N 以内的所有素数

编写C++程序完成以下功能:

(1)      提示用户输入N;

(2)      计算出从2到N之间的所有素数;

(3)      将结果保存在一个文本文件中。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. #define N 1000000
  5. using namespace std;
  6. int prime[N],cnt,n;
  7. void getPrime(){
  8. for(int i=2;i<=n;i++){
  9. if(!prime[i])prime[++cnt]=i;
  10. for(int j=1;j<=cnt&&prime[j]<=N/i;j++){
  11. prime[prime[j]*i]=1;
  12. if(i%prime[j]==0)break;
  13. }
  14. }
  15. }
  16. int main(){
  17. ofstream f("prime.txt");
  18. cout<<"请输入n"<<endl;
  19. cin>>n;
  20. getPrime();
  21. for(int i=1;i<=cnt;i++)
  22. f<<prime[i]<<" ";
  23. f.close();
  24. return 0;
  25. }

3、  袋中取球

编写C++程序完成以下功能(使用 enum):

(1)      袋子中有 red, yellow, blue, white, black 五种颜色的球多个;

(2)      一次从袋子里取出3个颜色不同的球,有几种取法;

(3)      将每种方法的所有取法输出到屏幕上。

  1. #include <iostream>
  2. using namespace std;
  3. enum ball{
  4. red,yellow,blue,white,black
  5. };
  6. void output(int i){
  7. switch(i){
  8. case red:cout<<"red ";break;
  9. case yellow:cout<<"yellow ";break;
  10. case blue:cout<<"blue ";break;
  11. case white:cout<<"white ";break;
  12. case black:cout<<"black ";break;
  13. }
  14. }
  15. int main(){
  16. for(int i=red;i<=black;i++)
  17. for(int j=i+1;j<=black;j++)
  18. for(int k=j+1;k<=black;k++){
  19. output(i);
  20. output(j);
  21. output(k);
  22. cout<<endl;
  23. }
  24. return 0;
  25. }

效果展示:

 red yellow bluered

yellow whitered yellow

blackred blue whitered

blue blackred white

blackyellow blue whiteyellow

blue blackvellow white

blackblue white black

 

4、  乘法口诀表

编写C++程序完成以下功能:

(1)      输出乘法口诀表;

(2)      显示格式如下所示。

1*1=1  1*2=2  1*3=3  1*4=4  1*5=5  1*6=6  1*7=7  1*8=8  1*9=9  
       2*2=4  2*3=6  2*4=8  2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 
              3*3=9  3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 
                     4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 
                            5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 
                                   6*6=36 6*7=42 6*8=48 6*9=54 
                                          7*7=49 7*8=56 7*9=63 
                                                 8*8=64 8*9=72 
                                                        9*9=81

(累死了,看到这点个赞呗,感谢少爷~)

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main(){
  5. for(int i=1;i<=9;i++){
  6. for(int j=1;j<=i*7;j++)
  7. cout<<" ";
  8. for(int j=i;j<=9;j++)
  9. cout<<i<<"*"<<j<<"="<<setw(3)<<left<<i*j;
  10. cout<<endl;
  11. }
  12. }

5、  最大公约数和最小公倍数

编写C++程序完成以下功能:

(1)      提示用户输入两个无符号整数;

(2)      计算两者的最大公约数和最小公倍数,并输出

  1. #include <iostream>
  2. #define uint unsigned int
  3. using namespace std;
  4. uint gcd(uint a,uint b){
  5. return b?gcd(b,a%b):a;
  6. }
  7. int main(){
  8. uint a,b;
  9. cout<<"请输入两个无符号整数"<<endl;
  10. cin>>a>>b;
  11. cout<<a<<"和"<<b<<"的最大公约数是"<<gcd(a,b);
  12. cout<<",最小公倍数是"<<b/gcd(a,b)*a<<endl;
  13. return 0;
  14. }

7、  计算n 阶勒让德多项式

编写C++程序完成以下功能:

(1)      提示用户输入整数n和实数x;

(2)      Pn(x),并输出结果

  1. #include <iostream>
  2. using namespace std;
  3. double p(int n,int x){
  4. if(!n)return 1;
  5. if(n==1)return x;
  6. return ((2*n-1)*p(n-1,x)-(n-1)*p(n-2,x))/n;
  7. }
  8. int main(){
  9. int n,x;
  10. cout<<"请输入n、x"<<endl;
  11. cin>>n>>x;
  12. cout<<"Pn("<<x<<")="<<p(n,x)<<endl;
  13. return 0;
  14. }

 

实验二 类与对象

1、  矩形

编写C++程序完成以下功能:

(1)      定义一个Point类,其属性包括点的坐标,提供计算两点之间距离的方法;

(2)      定义一个矩形类,其属性包括左上角和右下角两个点,提供计算面积的方法;

(3)      创建一个矩形对象,提示用户输入矩形左上角和右下角的坐标;

(4)      观察矩形对象以及Point类成员的构造函数与析构函数的调用;

(5)      计算其面积,并输出。

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. class Point {
  5. private:
  6. int x,y;
  7. public:
  8. Point(int _x=0,int _y=0):x(_x),y(_y) {};
  9. Point(Point &p):x(p.x),y(p.y) {};
  10. ~Point() {};
  11. int disX(const Point &b) {
  12. return b.x-x;
  13. };
  14. int disY(const Point &b) {
  15. return b.y-y;
  16. }
  17. };
  18. class Rectangle {
  19. private:
  20. Point a,b;
  21. public:
  22. Rectangle(Point _a,Point _b):a(_a),b(_b) {};
  23. Rectangle(int ax=0,int ay=0,int bx=1,int by=1):a(ax,ay),b(bx,by) {}
  24. Rectangle(Rectangle &r):a(r.a),b(r.b) {};
  25. ~Rectangle() {
  26. cout<<"hh"<<endl;
  27. };
  28. int area() {
  29. return abs(a.disX(b)*a.disY(b));
  30. };
  31. };
  32. int main() {
  33. cout<<"请输入矩形的左上角和右下角坐标(整数)"<<endl;
  34. int ax,ay,bx,by;
  35. while(1) {
  36. cin>>ax>>ay>>bx>>by;
  37. if(ax>bx||ay<by||cin.fail()) {
  38. cout<<"输入的矩形不合法"<<endl;
  39. cin.clear();
  40. cin.ignore(10000,'\n');
  41. } else {
  42. Rectangle myRectangle(ax,ay,bx,by);
  43. cout<<"该矩形的面积为"<<myRectangle.area()<<endl;
  44. break;
  45. }
  46. }
  47. return 0;
  48. }

实验问题

    • 构造函数和析构函数写在哪? 
      写在public里(声明必须在..里,定义里外都可以)
    • 析构函数里面要写什么? 
      留空就可以了吧,如果有动态申请的内存就delete掉
    • 拷贝构造函数怎么写? 
      Point(Point &p){ x=p.x;y=p.y; }; 
      或者 
      Point(Point &p):x(p.x),y(p.y){};
    • 编译错误提示 call of overloaded `Point()' is ambiguous 
      Point()构造时,Point(){};和Point(int _x=0,int _y=0):x(_x),y(_y){};都可以被调用,于是就被overloaded了。
    • 编译错误提示 In member function int Rectangle::area()': 
      int Point::x' is private 
      因为x是Point类的私有成员,不能在Rectangle类里面使用,关于x的计算函数都要写在Point类的public里。
    • 初始化时Rectangle myRectangle=Rectangle(ax,ay,bx,by);报错. 
      应该为Rectangle myRectangle(ax,ay,bx,by);
    • 析构函数怎么调用 
      不用显式调用,需要显式时:p.~Point();即可

2、  圆形

编写C++程序完成以下功能:

(1)      定义一个Point类,其属性包括点的坐标,提供计算两点之间距离的方法;

(2)      定义一个圆形类,其属性包括圆心和半径;

(3)      创建两个圆形对象,提示用户输入圆心坐标和半径,判断两个圆是否相交,并输出结果。

  1. #include <iostream>
  2. #include <cmath>
  3. #define sqr(x) ((x)*(x))
  4. using namespace std;
  5. class Point{
  6. private:
  7. int x,y;
  8. public:
  9. Point(int x=0,int y=0):x(x),y(y){}
  10. double dis(Point &b)const{
  11. return sqrt(sqr(x-b.x)+sqr(y-b.y));
  12. }
  13. };
  14. class Circle{
  15. private:
  16. Point c;
  17. double r;
  18. public:
  19. Circle(int x=0,int y=0,double r=0):c(x,y),r(r){}
  20. double dis(Circle &b)const{
  21. return c.dis(b.c);
  22. }
  23. double getR()const{
  24. return r;
  25. }
  26. };
  27. int main(){
  28. int x,y,r;
  29. cout<<"请输入a 圆心坐标半径"<<endl;
  30. cin>>x>>y>>r;
  31. Circle a(x,y,r);
  32. cout<<"请输入b 圆心坐标半径"<<endl;
  33. cin>>x>>y>>r;
  34. Circle b(x,y,r);
  35. if(a.dis(b)<=a.getR()+b.getR())
  36. cout<<"两圆相交"<<endl;
  37. else
  38. cout<<"两圆相离"<<endl;
  39. return 0;
  40. }

3、  友元

编写C++程序完成以下功能:

(1)      定义一个Boat和Car两个类,他们都具有私用属性——重量;

(2)      编写一个函数,计算两者的重量和。

double TotalWeight(Boat& b, Car& c);

  1. #include <iostream>
  2. using namespace std;
  3. class Car;
  4. class Boat {
  5. private:
  6. double weight;
  7. public:
  8. Boat() {};
  9. Boat(double w=0):weight(w) {};
  10. ~Boat() {};
  11. friend double TotalWeight(Boat&,Car&);
  12. };
  13. class Car {
  14. private:
  15. double weight;
  16. public:
  17. Car() {};
  18. Car(double w=0):weight(w) {};
  19. ~Car() {};
  20. friend double TotalWeight(Boat&,Car&);
  21. };
  22. double TotalWeight(Boat& b,Car& c) {
  23. return b.weight+c.weight;
  24. }
  25. int main() {
  26. cout<<"请输入Boat、Car的重量"<<endl;
  27. while(1) {
  28. double bw,cw;
  29. cin>>bw>>cw;
  30. if(cin.fail()) {
  31. cout<<"输入不合法"<<endl;
  32. cin.clear();
  33. cin.ignore(10000,'\n');
  34. } else {
  35. Boat myBoat(bw);
  36. Car myCar(cw);
  37. cout<<"Boat和Car的总重量为"<<TotalWeight(myBoat,myCar)<<endl;
  38. break;
  39. }
  40. }
  41. return 0;
  42. }

实验问题

    • 什么时候需要用友元? 
      当一个函数要用到这个类时(可能还有其他类)的私有成员,但是它不是这个类独享的函数,调用时不需要通过对象或指针。
    • 友元函数定义在哪? 
      定义在主函数外面,类定义后面,要加上friend然后声明在类的公有属性里。

4、  分数

编写C++程序完成以下功能:

(1)      定义一个分数类,他们都具有私用属性——分子和分母;

(2)      定义分数类的构造函数和析构函数;

(3)      定义方法Set,设置分子和分母;

(4)      定义方法print,打印分数,格式如:2/7;

(5)      定义方法value,返回double型的分数值;

(6)      定义方法invert, 分子和分母交换。

  1. #include <iostream>
  2. using namespace std;
  3. class Fractions{
  4. private:
  5. int num,den;
  6. public:
  7. Fractions(int n=0,int d=0):num(n),den(d){}
  8. ~Fractions(){}
  9. void set(int n,int d){
  10. num=n;den=d;
  11. }
  12. void print(){
  13. cout<<num<<"/"<<den<<endl;
  14. }
  15. double value(){
  16. return num*1.0/den;
  17. }
  18. void invert(){
  19. int t=den;
  20. den=num;
  21. num=t;
  22. }
  23. };
  24. int main(){
  25. return 0;
  26. }

实验三 数组与指针

1、  矩阵(一)

编写C++程序完成以下功能:

(1)      假定矩阵大小为4×5(整型数组表示);

(2)      定义矩阵初始化函数,可以从cin中输入矩阵元素;

(3)      定义矩阵输出函数,将矩阵格式化输出到cout;

(4)      定义矩阵相加的函数,实现两个矩阵相加的功能,结果保存在另一个矩阵中;

(5)      定义矩阵相减的函数,实现两个矩阵相减的功能,结果保存在另一个矩阵中;

(6)      定义三个矩阵:A1、A2、A3;

(7)      初始化A1、A2;

(8)      计算并输出:A3 = A1加A2,A3 = A1减A2。

  1. #include <iostream>
  2. #define ROW 4
  3. #define COL 5
  4. using namespace std;
  5. class Matrix {
  6. private:
  7. int mat[ROW][COL];
  8. public:
  9. Matrix() {};
  10. void init() {
  11. cout<<"please input the Matrix(4 row and 5 col)"<<endl;
  12. for(int i=0; i<ROW; i++)
  13. for(int j=0; j<COL; j++)
  14. cin>>mat[i][j];
  15. }
  16. void output() {
  17. cout<<"The Matrix:"<<endl;
  18. for(int i=0; i<ROW; i++) {
  19. for(int j=0; j<COL; j++)
  20. cout<<mat[i][j]<<"\t";
  21. cout<<endl;
  22. }
  23. }
  24. void add(Matrix &a,Matrix &b) {
  25. for(int i=0; i<ROW; i++)
  26. for(int j=0; j<COL; j++)
  27. mat[i][j]=a.mat[i][j]+b.mat[i][j];
  28. }
  29. void sub(Matrix &a,Matrix &b) {
  30. for(int i=0; i<ROW; i++)
  31. for(int j=0; j<COL; j++)
  32. mat[i][j]=a.mat[i][j]-b.mat[i][j];
  33. }
  34. };
  35. int main() {
  36. Matrix A1,A2,A3;
  37. A1.init();
  38. A2.init();
  39. A3.add(A1,A2);
  40. A3.output();
  41. A3.sub(A1,A2);
  42. A3.output();
  43. return 0;
  44. }

2、  矩阵(二)

编写C++程序完成以下功能:

(1)      假定矩阵大小为4×5(整型);

(2)      矩阵空间采用new动态申请,保存在指针中;

(3)      定义矩阵初始化函数,可以从cin中输入矩阵元素;

(4)      定义矩阵输出函数,将矩阵格式化输出到cout;

(5)      定义矩阵相加的函数,实现两个矩阵相加的功能,结果保存在另一个矩阵中;

(6)      定义矩阵相减的函数,实现两个矩阵相减的功能,结果保存在另一个矩阵中;

(7)      动态申请三个矩阵:A1、A2、A3;

(8)      初始化A1、A2;

(9)      计算并输出A3 = A1加A2,A3 = A1减A2;

(10)   释放矩阵空间。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. const int row = 4;
  5. const int col = 5;
  6. using namespace std;
  7. bool malloc_array(int **&a,int row,int col) {
  8. a=new int *[row];
  9. if(a==NULL) {
  10. cout<<"error:no enough space"<<endl;
  11. return 0;
  12. }
  13. for(int i=0; i<row; i++) {
  14. *(a+i)=new int[col];
  15. if(a+i==NULL) {
  16. cout<<"error:no enough space"<<endl;
  17. return 0;
  18. }
  19. }
  20. return 1;
  21. }
  22. class Matrix
  23. {
  24. private:
  25. int **mat;
  26. public:
  27. Matrix()
  28. {
  29. malloc_array(mat, row, col);
  30. }
  31. Matrix(Matrix &b)
  32. {
  33. if(malloc_array(mat, row, col))
  34. for(int i = 0; i < row; i++)
  35. for(int j = 0; j < col; j++)
  36. mat[i][j] = b.mat[i][j];
  37. }
  38. ~Matrix()
  39. {
  40. for(int i = 0; i < row; i++)
  41. delete [] *(mat + i);
  42. delete [] mat;
  43. mat = NULL;
  44. }
  45. void input()
  46. {
  47. cout << "please input the Matrix:" << endl;
  48. for(int i = 0; i < row; i++)
  49. for(int j = 0; j < col; j++)
  50. cin >> mat[i][j];
  51. }
  52. void output()
  53. {
  54. for(int i = 0; i < row; i++)
  55. {
  56. for(int j = 0; j < col; j++)
  57. cout << mat[i][j] << "\t";
  58. cout << endl;
  59. }
  60. }
  61. void cal(Matrix &a, Matrix &b, int op)
  62. {
  63. for(int i = 0; i < row; i++)
  64. for(int j = 0; j < col; j++)
  65. mat[i][j] = a.mat[i][j] + b.mat[i][j] * op;
  66. }
  67. };
  68. int main()
  69. {
  70. Matrix *A1 = new Matrix();
  71. A1->input();
  72. Matrix *A2 = new Matrix();
  73. A2->input();
  74. Matrix *A3 = new Matrix();
  75. A3->cal(*A1, *A2, 1);
  76. cout << "Matrix A1 + Matrix A2 =" << endl;
  77. A3->output();
  78. A3->cal(*A1, *A2, -1);
  79. cout << "Matrix A1 - Matrix A2 =" << endl;
  80. A3->output();
  81. A1->~Matrix();
  82. A2->~Matrix();
  83. A3->~Matrix();
  84. A1 = NULL;
  85. A2 = NULL;
  86. A3 = NULL;
  87. return 0;
  88. }

实验问题

    • 动态分配内存? 
      定义:int **mat; 
      分配: 
      mat=new int *[ROW]; 
      for(int i=0; i *(mat+i)=new int[COL]; 
      加上判断是否分配成功
    • 释放空间? 
      for(int i=0;i delete [] *(mat+i); 
      delete [] mat;

3、  矩阵(三)

编写C++程序完成以下功能:

(1)      用类来实现矩阵,定义一个矩阵的类,属性包括:

  • 矩阵大小,用 lines, rows(行、列来表示);
  • 存贮矩阵的数组指针,根据矩阵大小动态申请(new)。

(2)      矩阵类的方法包括:

  • 构造函数,参数是矩阵大小,需要动态申请存贮矩阵的数组;
  • 析构函数,需要释放矩阵的数组指针;
  • 拷贝构造函数,需要申请和复制数组;
  • 输入,可以从cin中输入矩阵元素;
  • 输出,将矩阵格式化输出到cout;
  • 矩阵相加的函数,实现两个矩阵相加的功能,结果保存在另一个矩阵类,但必须矩阵大小相同;
  • 矩阵相减的函数,实现两个矩阵相减的功能,结果保存在另一个矩阵类,但必须矩阵大小相同。

(3)      定义三个矩阵:A1、A2、A3;

(4)      初始化A1、A2;

(5)      计算并输出A3 = A1加A2,A3=A1减A2;

(6)      用new动态创建三个矩阵类的对象:pA1、pA1、pA3;

(7)      初始化pA1、pA2;

(8)      计算并输出pA3=pA1加pA2,pA3=pA1减pA2;

(9)      释放pA1、pA1、pA3。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. bool malloc_array(int **&a,int row,int col) {
  6. a=new int *[row];
  7. if(a==NULL) {
  8. cout<<"error:no enough space"<<endl;
  9. return 0;
  10. }
  11. for(int i=0; i<row; i++) {
  12. *(a+i)=new int[col];
  13. if(a+i==NULL) {
  14. cout<<"error:no enough space"<<endl;
  15. return 0;
  16. }
  17. }
  18. return 1;
  19. }
  20. class Matrix {
  21. private:
  22. int **mat;
  23. int row,col;
  24. public:
  25. Matrix(int _row=0,int _col=0):row(_row),col(_col) {
  26. malloc_array(mat,row,col);
  27. }
  28. Matrix(Matrix &b):row(b.getRow()),col(b.getCol()) {
  29. if(malloc_array(mat,row,col))
  30. for(int i=0; i<row; i++)
  31. for(int j=0; j<col; j++)
  32. mat[i][j]=b.mat[i][j];
  33. }
  34. ~Matrix() {
  35. for(int i=0; i<row; i++)
  36. delete [] *(mat+i);
  37. delete [] mat;
  38. mat=NULL;
  39. }
  40. int getRow() {
  41. return row;
  42. }
  43. int getCol() {
  44. return col;
  45. }
  46. void input() {
  47. cout<<"please input the Matrix:"<<endl;
  48. for(int i=0; i<row; i++)
  49. for(int j=0; j<col; j++)
  50. cin>>mat[i][j];
  51. }
  52. void output() {
  53. for(int i=0; i<row; i++) {
  54. for(int j=0; j<col; j++)
  55. cout<<mat[i][j]<<"\t";
  56. cout<<endl;
  57. }
  58. }
  59. int cal(Matrix &a,Matrix &b,int op) {
  60. if(a.getRow()!=b.getRow()||a.getCol()!=b.getCol()) {
  61. cout<<"error:These two Matrix don't have the same size."<<endl;
  62. return 0;
  63. }
  64. for(int i=0; i<row; i++)
  65. for(int j=0; j<col; j++)
  66. mat[i][j]=a.mat[i][j]+b.mat[i][j]*op;
  67. return 1;
  68. }
  69. };
  70. int main() {
  71. int row,col;
  72. cout<<"please input Matrix A1's row and col:"<<endl;
  73. cin>>row>>col;
  74. Matrix *pA1=new Matrix(row,col);
  75. pA1->input();
  76. cout<<"please input Matrix A2's row and col:"<<endl;
  77. cin>>row>>col;
  78. Matrix *pA2=new Matrix(row,col);
  79. pA2->input();
  80. Matrix *pA3=new Matrix(row,col);
  81. if(pA3->cal(*pA1,* pA2,1)) {
  82. cout<<"Matrix A1 + Matrix A2 ="<<endl;
  83. pA3->output();
  84. pA3->cal(*pA1,* pA2,-1);
  85. cout<<"Matrix A1 - Matrix A2 ="<<endl;
  86. pA3->output();
  87. }
  88. pA1->~Matrix();
  89. pA2->~Matrix();
  90. pA3->~Matrix();
  91. pA1=NULL;
  92. pA2=NULL;
  93. pA3=NULL;
  94. return 0;
  95. }

实验问题

    • line和row,不应该是row和col吗? 
      那就用row和col
    • 加减的函数基本一样,我可以写在一起吗? 
      其实用了乘法会降低速度,不过,这种同一级别的计算量,差别微小。
    • 拷贝函数怎么写 
      里面不能用memcpy(mat,b.mat,sizeof mat);
    • 遇到错误error: passing ‘const Matrix’ as ‘this’ argument discards qualifiers [-fpermissive] in call to ‘int Matrix::getRow()’ 
      这是因为const型的Matrix参数要调用getRow,要在getRow的大括号前加上const。不过这个const型的Matrix参数我改回引用了。
    • 怎么释放 
      显式调用析构函数,再将指针指向NULL
文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览42750 人正在系统学习中