c++final exam test 3//20191222//待续中

Dec.22,2019

menu

1.二维方阵主、次对角线的形成
2.求二维方阵主、次对角线之和
3.形成二维数组的上三角或下三角
4.一维数组的左移n位,或右移n位
5.求长方形的面积和周长
6.求正方形的面积和周长
7.求圆的面积和周长
8.求n!
9.比较两个数的大小
10.比较两个字符串的大小
11.求若干学生一门课的成绩总和、平均成绩、最高成绩、最低成绩等。
12.求若干数的最大值或最小值
13.实现复数相加、复数相减的运算符重载
14.用重载运算符+,实现两个一维数组相加
15.用重载运算符-,实现两个一维数组相减
16.公有继承、私有继承方式下,基类和派生类的数据成员或成员函数的访问方式和访问的合法性,
构造函数和析构函数的调用顺序等等

1

#include<iostream>
using namespace std;
#define n 10//自己改
void matrix_main_diagonal()
{
	int a[n][n];
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (i == j)
				a[i][j] = 1;
			else a[i][j] = 0;
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}
/*for(i=0;i<n;i++)//n*n方阵次对角线元素置1
	{
		a[i][n-i-1]=1;
	}*/
void matrix_counter_diagonal()
{
	int a[n][n];
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (i +j+1==n )
				a[i][j] = 1;
			else a[i][j] = 0;
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}
int main()
{
	matrix_main_diagonal();
	cout << "-------------------" << endl;
	matrix_counter_diagonal();

}

2

#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;
#define n 3//维数自己改
void matrix_main_diagonal()
{
	srand((int)time(0));
	int s = 0;
	int a[n][n];//int a[n][n]=rand() % 10;❌
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			a[i][j] = rand() % 10;
			cout << a[i][j] << " ";
			if (i == j)s += a[i][j];
		}
		cout << endl;
	}
	cout << "主对角线之和="<<s<<endl;
}
void matrix_counter_diagonal()
{
	srand((int)time(0));
	int s = 0;
	int a[n][n];
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			a[i][j] = rand() % 10;
			cout << a[i][j] << " ";
			if (i+j ==n-1 )s += a[i][j];
		}
		cout << endl;
	}
	cout << "副对角线之和="<<s << endl;
}
int main()
{
	matrix_main_diagonal();
	cout << "-------------------" << endl;
	matrix_counter_diagonal();
}

参考:
https://blog.csdn.net/dagedeshu/article/details/80851322
https://blog.csdn.net/weixin_44811068/article/details/103015881

3

#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;
#define n 5//维数自己改
void matrix_main_diagonal()
{
	srand((int)time(0));
	int a[n][n];//int a[n][n]=rand() % 10;❌
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (i <= j)
			{
				a[i][j] = rand() % 10;
			}
			else a[i][j] = 0;
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	cout << "上三角"<<endl;
}
void matrix_counter_diagonal()
{
	srand((int)time(0));
	int a[n][n];
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (i >= j)
			{
				a[i][j] = rand() % 10;
			}
			else a[i][j] = 0;
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	cout << "下三角"<< endl;
}
int main()
{
	matrix_main_diagonal();
	cout << "-------------------" << endl;
	matrix_counter_diagonal();
}

4

#include <iostream>   //左移
using namespace std;
void move(int arr[], int num , int digit)
	{
		int x;
		for(int i=1;i<=digit ; i++)
		{
			x=arr[0];
			for(int j=1;j<num;j++)
				  arr [j-1] = arr[j];
			arr[num-1]=x;
		}
	}
void output(int arr[] , int num)
{
	for(int i=0;i<num;i++)
		cout<<arr[i]<<" ";
	cout<<endl;
}
int main()   
{	int a[50];
		int n , w;//n表示数据个数,w要移动的位数
		cout<<"请输入数据个数:";
		cin>>n;
		cout<<"请输入"<<n<<"个数据:";
		for(int i=0;i<n;i++)
			cin>>a[i];
		cout<<"请输入要移动的位数:";
		cin>>w;
		move( a , n , w );
		cout<<"左移后:";
		output( a , n);
		return 0;
}

5-9略

5-9这么简单还用敲?

10

//注意点
C++strcmp() 的参数可以是string类型吗?
函数原型:int strcmp(const char *s1,const char *s2);
参数是const char*,或者char*
不能是string类型,但是可以通过string的成员函数c_str()把string转换为const char *
>>
怎么强转的?
string str1 = "asdf";
string str2 = "qwer";
strcmp(str1.c_str(), str2.c_str());要用c_str()
链接 https://zhidao.baidu.com/question/937791824946081492.html
#include<iostream> 
#include <string.h> 
using namespace std;
void main(void)

{

	char buf1[] = "aaa";

	char buf2[] = "bbb";

	char buf3[] = "ccc";

	int ptr;

	ptr = strcmp(buf2, buf1);

	if (ptr > 0)

		cout << "Buffer 2 is greater than buffer 1" << endl;

	else

		cout << "Buffer 2 is less than buffer 1" << endl;

	ptr = strcmp(buf2, buf3);

	if (ptr > 0)

		cout << "Buffer 2 is greater than buffer 3" << endl;

	else

		cout << "Buffer 2 is less than buffer 3" << endl;

}

11

12

13

成员函数

#include <iostream>//运算符重载,复数相加
 
using namespace std;
 
class Complex//复数类   
{
public:
    //定义默认构造函数初始化数据成员
    Complex()
    {
        real = 0;
        imag = 0;
    }
 
    //定义初始化表初始化Complex类
    Complex(double r, double i):real(r),imag(i){}
    
    //声明重载运算符函数(实现两个复数相加)
    Complex operator+(Complex &c1);   
    
    void display( );//显示复数
 
private:
      double real;//复数的实部
      double imag;//复数的虚部
};
 
//定义重载运算符函数实现复数相加
Complex Complex::operator+(Complex &c1)  
{  
    Complex c2;  
    
    c2.real = c1.real + real;  
    c2.imag = c1.imag + imag;  
    
    return c2;  
}  
 
void Complex::display( )//显示复数
{
    cout<<real<<'+'<<imag<<'i'<<endl;
}
 
int main( )
{
    Complex  c1(8,2), c2(7,10), c3;
 
    cout<<"c1 = ";
    c1.display();
    
    cout<<"c2 = ";
    c2.display();
 
    //使用运算符重载实现两个复数相加
    c3 = c1 + c2;
    
    cout<<"c1 + c2 = ";
    c3.display();
 
    system("pause");
}
 
#include<iostream>//运算符重载,复数相减
using namespace std;
class Complex
{
public:
	Complex()
	{real = 0;imag = 0;}
	Complex(double r, double i) :real(r), imag(i) {}//{}后面加不加";"随意>OoO<
	Complex operator-(Complex& c2);
	void display();
private:
	double real;
	double imag;
};
Complex Complex::operator-(Complex& c2)
{
	return Complex(real - c2.real, imag - c2.imag);
}
void Complex::display()
{
	cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
	Complex c1(3, 4), c2(5, -10), c3;
	c3 = c1 - c2;
	cout << "c1=";
	c1.display();
	cout << "c2=";
	c2.display();
	cout << "c1-c2=";
	c3.display();
	return 0;
}

友元函数

#include<iostream>
using namespace std;
class Complex
{
private:
	int real;
	int imag;
public:
	Complex()
	{ 
		real = 0;
		imag = 0;
	}
	Complex(int r, int i) :real(r), imag(r) {};
	friend Complex operator+(Complex &c1, Complex &c2);
	void display();


};



void Complex::display()
{
	cout << "real=" << real << endl;
	cout << "imag=" << imag << endl;
}
Complex operator+(Complex &c1, Complex &c2)
{
	return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main()
{
	Complex c1(3, 4);
	Complex c2(5, 8);
	Complex c3;
	c3 = c1 + c2;
	c3.display();
	return 0;


}
区分成员函数、友元函数、普通函数
参考:https://www.cnblogs.com/Mayfly-nymph/p/9034936.html
推荐:对二元运算符,重载为成员函数时,仅一个参数,另一个被隐含;重载为友元函数时,
有两个参数,没有隐含参数。
一般来说,一元运算符最好被重载为成员函数,对二元运算符最好被重载为友元函数。
//私人笔记,忽视
典型单目运算符为“++” 或(- -),它们分别有前置与后置两种,如果没有特殊说明,
它们的前置用法与后置用法均使用同一个重载函数:
operator ++();
为了能够区分++- -)的前置与后置用法,可为它们再写一个重载函数用于实现它们的后置用法,
该重载函数应有一个形式上的int型参数:
operator ++(int);
//私人笔记,忽视
单目运算是指:只有一个操作数的运算,所以也称一元运算。如, !a , ++i ,j- - 等等。
单目运算符既可以重载为成员函数,也可以重载为类的友元函数。

14

15

16

发布了38 篇原创文章 · 获赞 2 · 访问量 1185

猜你喜欢

转载自blog.csdn.net/weixin_44811068/article/details/103652113