首页 > 技术文章 > 实践教学小程序(2022-5-29)

duanqibo 2022-05-31 08:36 原文

一、

#include <iostream>
#include <string>
using namespace std;

int main()
{
int n, i;
cout << "请输入几个字符串:";
cin >> n;
string *p;
string *A = new string[n];
p = A; //指针p指向A所指向的存储单元
cout << endl; //输入一个回车键
cout << "请输入:";
for (i = 0; i < n; i++)
cin >> A[i];
p = A; //指针的复位
cout << endl;
cout << "输入的字符串为:";
for (i = 0; i < n; i++)
cout << p[i] << endl;

delete[]p; //释放指针p的空间()存储单位
system("pause");
return 1;
}

 

二、

#include <iostream>
#include<math.h>
#include <string>
using namespace std;

class Test //Test类
{
private:
int x, y;//私有变量,不能初始化
public:
Test(int a, int b);//构造函数,用来初始化私有变量,同时初始化对象
int get_x()
{
return x;
}
int get_y()
{
return y;
}
int get_dis()
{
return sqrt(x*x + y*y);
}
void show();//成员函数
};//类定义结束
Test::Test(int a, int b){
x = a;
y = b; //构造函数的参数初始化私有变量
}
void Test::show(){
cout << "x=" << x << ",y=" << y << endl;
}
int main()
{
Test t(3, 4);//用Tset类定义一个对象他t,并给对象初始化,对象t由构造函数初始化
t.show();//用对象调用成员函数
cout << "横坐标:" << t.get_x() << endl;
cout << "纵坐标:" << t.get_y() << endl;
cout << "该点到原点的距离为:" << t.get_dis() << endl;
system("pause");
return 1;
}

 

三、

#include <iostream>
#include <string>
using namespace std;

int main()
{
int n, i, length=0;
cout << "请输入几个字符串:";
cin >> n;
string *p;
string *A = new string[n];
p = A; //指针p指向A所指向的存储单元
cout << endl; //输入一个回车键
cout << "请输入:";
for (i = 0; i < n; i++)
{
cin >> A[i];
length = length + A[i].length();
}
p = A; //指针的复位
cout << endl;
string max = A[0]; //定义第一个字符串最长
string min = A[0];
for (i = 1; i < n; i++)
{
if (p[i].length()>max.length())
max = p[i];
if (p[i].length() < min.length())
min = p[i];
}
cout << "最长的字符串是:" << max << "\n" << "最短的字符串是:" << min << endl;

//cout << "输入的字符串为:";
//for (i = 0; i < n; i++)
cout << p[i] << endl;

delete []p; //释放指针p的空间()存储单位
system("pause");
return 1;
}

 

四、

#include <iostream>
#include<math.h>
#include <string>
using namespace std;

class Point{
private:
int X, Y;
public:
Point(int a = 0,int b = 0) //构造函数,默认参数
{
X = a;
Y = b;
}
Point(const Point &p); //复制的构造函数

int Get_X()
{
return X;
}
int Get_Y(){
return Y;
}

void show(){
cout << "X=" << X << ",Y=" << Y << endl;
}
~Point()
{
cout << "析构函数,用来释放空间!" << endl;
}
};//类定义结束

Point::Point(const Point &p) //定义复制构造函数
{
X = p.X;
Y = p.Y;
}
int main()
{
Point A(42, 35);
Point B = A;//调用复制的构造函数
A.show();
B.show();
Point();
system("pause");
return 1;
}
8.
#include <iostream>
#include<math.h>
#include <string>
using namespace std;

class myComplex
{
private:
double real, imag;
public:
myComplex();
myComplex(double x, double y);
void outCom();
myComplex operator-(const myComplex &c);
friend myComplex operator+(const myComplex &c1, const myComplex &c2); //友元函数
};//类定义结束

myComplex::myComplex()
{
real = 0;
imag = 0;
}
myComplex::myComplex(double r, double i){
real = r;
imag = i;
}
void myComplex::outCom(){
cout << "(" << real << "," << imag << ")";
}
myComplex myComplex::operator-(const myComplex &c)
{
return myComplex(this->real - c.real, this->imag - c.imag);
}
myComplex operator+(const myComplex &c1, const myComplex &c2){
return myComplex(c1.real + c2.real, c1.imag + c2.imag);
}
int main()
{
myComplex c1(1, 2), c2(3, 4), res;
c1.outCom();
cout << "operator+";
c2.outCom();
cout << "=";
res = c1 + c2;
res.outCom();
cout << endl;

c1.outCom();
cout << "operator-";
c2.outCom();
cout << "=";
res = c1 - c2;
res.outCom();
cout << endl;
system("pause");
return 1;
}

 

五、

#include <iostream>
using namespace std;

class myComplex
{
private:
double real, imag;
public:
myComplex(); //缺省的构造函数
myComplex(double x, double y);
void outCom();
myComplex operator-(const myComplex &c); //成员函数
friend myComplex operator+(const myComplex &c1,const myComplex &c2); //友元函数
}; //类定义结束

myComplex::myComplex()
{
real = 0;
imag = 0;
}
myComplex::myComplex(double r, double i)
{
real = r;
imag = i;
}

void myComplex::outCom()
{
cout << "(" << real << "," << imag << ")";
}

myComplex myComplex::operator-(const myComplex &c)
{
return myComplex(this->real - c.real, this->imag - c.imag);
}

myComplex operator+(const myComplex &c1,const myComplex &c2)
{
return myComplex(c1.real+c2.real,c1.imag+c2.imag);
}

int main()
{
myComplex c1(1, 2), c2(3, 4), res;
c1.outCom();
cout<<"operator+";
c2.outCom();
cout<<"=";
res=c1+c2;
res.outCom();
cout<<endl;

c1.outCom();
cout << "oprator-";
c2.outCom();
cout << "=";
res = c1 - c2;

res.outCom();
cout << endl;
system("pause");
return 1;
}

 

推荐阅读