当前位置:首页 » 编程软件 » rhs编程

rhs编程

发布时间: 2022-04-14 18:08:16

A. C++中,时间排序怎么编程

如果是数据库的数据,取数据时 order by 日期就可以了

B. C++编程:用面向对象的方法求圆面积. 要求编写一个圆Circle类

我来写一个简单的吧:
#include<iostream>
using namespace std;

#define pi 3.14 // 宏定义 π
class Circle {
private:
double Radius; // 半径

public:
Circle () : Radius( 0 ) { } // 默认构造函数
explicit Circle ( double r ) : Radius( r ) { }
Circle ( Circle & rhs ) { Radius = rhs.Radius; } // 复制构造函数
const Circle & operator= ( Circle & rhs ) { Radius = rhs.Radius; } // 重载 =

void setRadius( double r ) { Radius = r; } // 给半径赋值
double Area() { return pi * Radius * Radius; } // 返回面积
};

int main()
{
Circle test;
double r;
cout << "请输入圆的半径:" << endl;
cin >> r;
test.setRadius( r );
cout << endl << "圆的面积是: " << test.Area() << endl;
return 0;
}

PS:析构函数没有给出,因为此 类 中只有一个 double 类型数据,所以可以不显示定义析构函数,使用编译器默认的析构函数即可

C. C++编程题:定义一个字符串类型并重载一个运算符

C++中定义一个字符串类string,并实现加号运算符重载operator+

程序如下:

<文件名:String.h>

#include <string.h>
#include <stdio.h>

/*创建一个字符串类:String*/

class String{
public:
String(const char* str = NULL); //构造函数之一
String(const String& another); //构造函数之二
void input_str(); //字符串输入
String& operator +(const String& rhs); //重载加法运算符
void print(); //字符串输出
private:
char *m_data;
char *addstr;
};

<文件名:String.cpp>

#include <iostream.h>
#include "String.h"

/*构造函数之一*/

String::String(const char* str)
{
if(str == NULL) //如果输入为空字符串,则添加“\0”表示空串。
{
m_data = new char[1];
m_data[0] = '\0';
}
else //如果输入非空串,复制该字符串。
{
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}
}

/*构造函数之二*/

String::String(const String& another)
{
m_data = new char[strlen(another.m_data) + 1];
strcpy(m_data,another.m_data);
}

/*输入字符串*/

void String::input_str()
{
char str[256];
printf("请输入字符串:");
cin>>str;
printf("\n");
m_data = new char[strlen(str) + 1];
strcpy(m_data,str);
}

/*连接后的字符串输出*/

void String::print()
{
if(addstr == NULL) printf("No Content!\n");
else
{
for(char *c = addstr; *c != '\0'; c++)
printf("%c", *c);
printf("\n");
}
}

//"+"运算符重载

String& String::operator+(const String& rhs)
{

addstr=new char[strlen(m_data)+strlen(rhs.m_data)+1];
strcpy(addstr,m_data);
strcat(addstr,rhs.m_data);
return *this;
}

<文件名:Str_main.cpp>

#include "String.h"

/*开始演示字符串类*/

void main()
{
String S1;
String S2;

S1.input_str();
S2.input_str();

S1+S2;

S1.print();

}

D. 求助各位c语言编程高手~帮我做3道题~

#include<stdio.h>
#include<string.h>

#defineN4

/*-----------------------------------------
第一题
-------------------------------------------*/
voidDiamond(constchar*s,intn,intlen)
{
printf("%*s%-s\n",len,s+n-1,s+n);
if(n>1)
Diamond(s,n-1,len);
printf("%*s%-s\n",len,s+n,n==len?s+n:s+n+1);
}

/*-----------------------------------------
第二题
-------------------------------------------*/
typedefstruct
{
intgcd;
intlcm;
}pair;

voidGCD_LCM(inta,intb,pair*p)
{
inttmp;
intproct=a*b;
while(b%a)
{
tmp=a;
a=b%a;
b=tmp;
}
p->gcd=a;
p->lcm=proct/a;
}

/*-----------------------------------------
第三题
-------------------------------------------*/
voidSwap(int*lhs,int*rhs)
{
inttmp=*lhs;
*lhs=*rhs;
*rhs=tmp;
}

voidBubbleSort(int*beg,int*end)
{
for(;beg!=end;++beg)
for(int*p=end-1;p!=beg;--p)
if(*p<*(p-1))
Swap(p,p-1);
}

voidSelectSort(int*beg,int*end)
{
for(;beg!=end;++beg)
{
int*max=beg;
for(int*p=beg+1;p!=end;++p)
if(*max<*p)
max=p;
Swap(beg,max);
}
}

voidPrint(int*beg,int*end)
{
while(beg!=end)
printf("%d",*beg++);
putchar('\n');
}

intmain()
{
/*一*/
charpt[N+1]={0};
memset(pt,'*',N);
Diamond(pt,N,N);

/*二*/
pairp;
GCD_LCM(3,6,&p);
printf("%d%d\n",p.gcd,p.lcm);

/*三*/
inta[]={32,9,45,22,15,48,47,8,55,1};
Print(a,a+10);
BubbleSort(a,a+10);
Print(a,a+10);
SelectSort(a,a+10);
Print(a,a+10);
}

E. C++编程,从一个文件中统计所有出现过的单词,并按次数从大到小输出

问题可以分为2个部分:

  1. 统计出现过的所有单词

  2. 按次数从大到小输出

#include<iostream>
#include<fstream>
#include<unordered_map>
#include<algorithm>
#include<string>
usingnamespacestd;

boolmysort(pair<int,string>a,pair<int,string>b){
if(a.first!=b.first)
returna.first>b.first;
else
returna.second<b.second;
}

intmain(){
//统计出现过的所有单词:
ifstreamifs("input.in",ifstream::in);
unordered_map<string,int>um;
strings;
while(ifs>>s){
if(um.find(s)==um.end())um[s]=1;
else++um[s];
}
ifs.close();
//排序(以出现次数大到小为最优先排位方式,如果出现次数一致,则以辞典编纂从小到大的顺序排位:
vector<pair<int,string>>v;
for(unordered_map<string,int>::iteratorit=um.begin();it!=um.end();++it)
v.push_back(make_pair(it->second,it->first));
sort(v.begin(),v.end(),mysort);

//输出:
for(inti=0;i<v.size();++i)
puts(v[i].second.c_str());
}


几点小贴士:

  1. 如果只输出字符串的话puts是最快的内部函数(比printf快大概10倍,而printf又比cout要快),不过要记得puts只能输出c字符串,所以要输出string的时候记得用 .c_str() 函数。

  2. unordered_map 比 map要快上很多,因为它使用哈希表(调用的时间是O(1),map调用时间是O(nlogn)),但是代价就是它不是按顺序储存的。

F. 求解C语言编程:题目,给定程序的功能是将在字符串S中出现、而未在字符串T中出现的字符形成一个新的字符串

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int compare(const void *lhs,const void*rhs){
return *(char*)lhs-*(char*)rhs;
};
char* diff(char*lsz,char*rsz){
char *tmp,*p,*pl;
pl=lsz;
p=tmp=(char*)malloc(strlen(lsz)+1);
memset(tmp,0,strlen(lsz)+1);
qsort(lsz,strlen(lsz),sizeof(char),compare);
qsort(rsz,strlen(rsz),sizeof(char),compare);
while(*pl){
if(*p!=*pl && !bsearch(pl,rsz,strlen(rsz),sizeof(char),compare) ){
*++p=*pl++;
}
else
pl++;
}
strcpy(lsz,tmp+1);
free(tmp);
return lsz;
};
int main(){
char sz1[128],sz2[128];
scanf("%s %s",sz1,sz2);
printf("%s\n%s\n",sz1,sz2);
printf("%s\n",diff(sz1,sz2));
return 0;
};

G. c语言编程 组数游戏

这个我以前写过,不过不是用你老师提供的思路:

C:

#include<stdio.h>
#include<stdlib.h>

intMyStrCmp(constvoid*lhs,constvoid*rhs)
{
constchar*a=(constchar*)lhs;
constchar*b=(constchar*)rhs;

while(1)
{
if(*a!=*b)
return*a-*b;

while(*a&&*b&&*a==*b)
++a,++b;

if(!(*a)&&!(*b))
return0;

if(!*b)
b=(constchar*)rhs;
elseif(!*a)
a=(constchar*)lhs;
}
}

intLexicalCmp(constvoid*lhs,constvoid*rhs)
{
staticcharBufLhs[16],BufRhs[16];

sprintf(BufLhs,"%d",*(constint*)lhs);
sprintf(BufRhs,"%d",*(constint*)rhs);
returnMyStrCmp(BufRhs,BufLhs);
}

intmain()
{
intn,*seq;
scanf("%d",&n);
seq=(int*)malloc(n*sizeof(int));

for(inti=0;i<n;++i)
scanf("%d",&seq[i]);

qsort(seq,n,sizeof(int),LexicalCmp);

for(inti=0;i<n;++i)
printf("%d",seq[i]);

free(seq);
return0;
}

C++:

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iterator>
usingnamespacestd;

boolMyStrCmp(conststring&s1,conststring&s2)
{
returns1+s2>s2+s1;
}

intmain()
{
vector<string>v;

stringstr;
while(cin.peek()!='\n'&&cin>>str)
v.push_back(str);

sort(v.begin(),v.end(),MyStrCmp);

(v.begin(),v.end(),ostream_iterator<string>(cout));
}

H. c语言编程求任意对称正定矩阵的逆。

#ifndef __MATRIX_DOT_H__
#define __MATRIX_DOT_H__

template <class T>
void Swap(T& a, T& b)
{
T temp;

temp = a;
a = b;
b = temp;
}

class CMatrix
{
static double ZERO;//极小值
public:
CMatrix(int row = 3, int col = 3); //
CMatrix(const CMatrix& right); //拷贝构造函数
~CMatrix();
void Show(const char* pre = NULL)const; //输出矩阵
void Free();
int Resize(int row, int col); //重新定义矩阵大小
int GetRow()const{ return m_iRow; } //返回矩阵行数
int GetCol()const{ return m_iCol; } //返回矩阵列数
int RowSwap(int x, int y); //行交换,成功返回1,否则0
int ColSwap(int x, int y); //列交换
static void SetZero(double z); //设定ZERO的值,所有CMatrix实例精度都将改变

const CMatrix Transpose()const; //返回转置矩阵
const CMatrix Adjoint()const; //伴随矩阵
const CMatrix Resie(int row, int col)const;//求对应元素的余子式
const CMatrix Contrary()const;//逆矩阵
const CMatrix Gauss_Jordan(double* pDet = NULL)const;//逆矩阵(高斯-约旦法),pDet为行列式,
//此法精度较低,但效率较高
double Resie_a(int row, int col)const;//求对应元素的代数余子式
double Determinant()const; //返回方阵的行列式
double Det_Recursion()const; //返回方阵的行列式(递归)

int IsZero()const; //判断元素是否全为0(零矩阵)
int IsPhalanx()const; //判断是否为方阵
int IsReverse()const; //判断矩阵是否可逆
int IsNonfunnyPhalanx()const; //判断是否非奇异方阵

double* operator[](int i)const; //操作单个元素
CMatrix& operator=(const CMatrix& right);
CMatrix& operator=(const double* pRight);
CMatrix& operator=(const double** ppRight);
const CMatrix& operator+()const; //一元操作符
const CMatrix operator-()const; //一元操作符
const CMatrix operator+(const CMatrix& right)const;
const CMatrix operator-(const CMatrix& right)const;
const CMatrix operator*(const CMatrix& right)const;
const CMatrix operator*(const double& right)const;
const CMatrix operator/(const double& right)const;
CMatrix& operator+=(const CMatrix& right);
CMatrix& operator-=(const CMatrix& right);
CMatrix& operator*=(const CMatrix& right);
CMatrix& operator*=(const double& right);
CMatrix& operator/=(const double& right);
int operator==(const CMatrix& right)const;
int operator!=(const CMatrix& right)const;

private:
int m_iRow; //行数
int m_iCol; //列数
double** m_ppData; //数据
};

#endif //__MATRIX_DOT_H__

//matrix.cpp
//

#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include "matrix.h"

double CMatrix::ZERO = 1e-10;

CMatrix::CMatrix(int row/*=3*/, int col/*=3*/)
{
m_ppData = NULL;
Resize(row, col);
}

//拷贝构造函数
CMatrix::CMatrix(const CMatrix& right)
{
m_ppData = NULL;//一定要加这句初始化(一个对象不会同时调用构造函数和拷贝构造函数)

Resize(right.GetRow(), right.GetCol());
for(int i = 0; i < right.GetRow(); i++)
{
for(int j = 0; j < right.GetCol(); j++)
m_ppData[i][j] = right[i][j];
}
}

CMatrix::~CMatrix()
{
Free();
}

void CMatrix::Free()
{
if(m_ppData != NULL){
for(int i = 0; i < m_iRow; i++)
{
if(m_ppData[i] != NULL)
delete[] m_ppData[i];
m_ppData[i] = NULL;
}
m_ppData = NULL;
}
}

int CMatrix::Resize(int row, int col)
{
assert(row > 0 && col > 0);

//释放空间
Free();

//申请空间
m_iRow = row;
m_iCol = col;
m_ppData = new double*[m_iRow];
assert(m_ppData != NULL);
for(int i = 0; i < m_iRow; i++)
{
m_ppData[i] = new double[m_iCol];
assert(m_ppData[i] != NULL);
}

//初始化
for(i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
m_ppData[i][j] = 0;
}
return 1;
}

//zero
void CMatrix::SetZero(double z)
{
double zero = fabs(z);
if(zero > 1.0f) return;

ZERO = zero;
}

//show
void CMatrix::Show(const char* pre/*=NULL*/)const
{
int i, j;
#ifdef _WINDOWS
if(m_iRow > 10 || m_iCol > 10)
MessageBox(NULL, "矩阵数据量太大,不能输出", "警告", MB_OK);

char buf[4096];
char temp[256];

strcpy(buf, "");
if(pre != NULL)
{
strcpy(buf, pre);
strcat(buf, "\n");
}

for(i = 0; i < m_iRow; i++)
{
for(j = 0; j < m_iCol; j++)
{
sprintf(temp, "%.3f\t", m_ppData[i][j]);
strcat(buf, temp);
}
strcat(buf, "\n");
}
MessageBox(NULL, buf, "提示信息", MB_OK);
#else
if(pre != NULL)
puts(pre);
for(i = 0; i < m_iRow; i++)
{
for(j = 0; j < m_iCol; j++)
printf("%f\t", m_ppData[i][j]);
printf("\n");
}
#endif //_WINDOWS
}

///////////////////////////////////////计算//////////////////////////////////////

//行交换
int CMatrix::RowSwap(int x, int y)
{
if(x < 0 || x >= m_iRow || y < 0 || y >= m_iRow)
return 0;
if(x == y)
return 1;
for(int i = 0; i < m_iCol; i++)
{
Swap(m_ppData[x][i], m_ppData[y][i]);
}
return 1;
}

//列交换
int CMatrix::ColSwap(int x, int y)
{
if(x < 0 || x >= m_iCol || y < 0 || y >= m_iCol)
return 0;
if(x == y)
return 1;
for(int i = 0; i < m_iRow; i++)
{
Swap(m_ppData[i][x], m_ppData[i][y]);
}
return 1;
}

//转置
const CMatrix CMatrix::Transpose()const
{
CMatrix tr(m_iCol, m_iRow);

for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
tr[j][i] = m_ppData[i][j];
}
return tr;
}

//计算方阵的行列式(精度不高)
double CMatrix::Determinant()const
{
assert(m_iRow == m_iCol);

CMatrix temp = *this;
int i,j,m,n,s,t,k=1;
double f=1,c,x,sn;

for (i=0,j=0; i<m_iRow&&j<m_iRow; i++,j++)
{
if (temp[i][j]==0)
{
for (m = i;m < m_iRow; m++)
{
if(fabs(temp[m][j]) > ZERO)//0
break;
}

if (m == m_iRow)
{
return 0;
}
else
for (n = j; n < m_iRow; n++)
{
c = temp[i][n];
temp[i][n] = temp[m][n];
temp[m][n] = c;
}
k *= (-1);
}
for (s = m_iRow-1; s>i; s--)
{
x = temp[s][j];
for (t = j; t < m_iRow; t++)
temp[s][t] -= temp[i][t] * (x/temp[i][j]);
}
}
for (i = 0; i < m_iRow; i++)
f *= temp[i][i];
sn = k * f;

return sn;
}

//行列式(递归,精度较高)
double CMatrix::Det_Recursion()const
{
assert(m_iRow == m_iCol);

CMatrix temp;
double ans = 0;

if(m_iRow == 1)
{
return m_ppData[0][0];
}
else if(m_iRow == 2)
{
return m_ppData[0][0]*m_ppData[1][1] - m_ppData[1][0]*m_ppData[0][1];
}
else
{
for(int i = 0; i < m_iRow; i++)
{
temp = Resie(i, 0);//this->Resie(i, 0)
ans += temp.Det_Recursion()*m_ppData[i][0]*pow(-1, i);
}
}

return ans;
}

//计算方阵的余子式
const CMatrix CMatrix::Resie(int row, int col)const
{
CMatrix re;
int index = 0;

assert(m_iRow == m_iCol);
assert(m_iRow >= 2);
assert(row < m_iRow && col < m_iCol);
assert(row >= 0 && col >= 0);
double* pData = NULL;
pData = new double[(m_iRow-1)*(m_iCol-1)];
assert(pData != NULL);
re.Resize(m_iRow-1, m_iCol-1);

for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
if(i != row && j != col)
pData[index++] = m_ppData[i][j];
}
re = pData;

delete[] pData;
pData = NULL;
return re;
}

//计算方阵的代数余子式
double CMatrix::Resie_a(int row, int col)const
{
return (Resie(row, col)).Det_Recursion()*pow(-1, row+col);
}

//伴随矩阵
const CMatrix CMatrix::Adjoint()const
{
CMatrix ad(m_iRow, m_iCol);

for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
ad[j][i] = Resie_a(i, j);
}
return ad;
}

//逆矩阵
const CMatrix CMatrix::Contrary()const
{
assert(IsReverse());

CMatrix co(m_iRow, m_iCol);
co = Adjoint();//this
co /= Det_Recursion();//this

return co;
}

//高斯-约旦法求逆矩阵(全选主元), pDet为原方阵的行列式
const CMatrix CMatrix::Gauss_Jordan(double* pDet/*=NULL*/)const
{
assert(IsReverse());

double fDet = 1.0f;
int flag = 1;
int k = 0, i = 0, j = 0;
CMatrix out(m_iRow, m_iCol);//逆
CMatrix m = *this;//原
CMatrix rhs(2, m_iRow);//保存主元素位置,0 i, 1 j;

for(k = 0; k < m_iRow; k++)
{
//第一步:全选主元
double fMax = 0.0f;
for(i = 0; i < m_iRow; i++)
{
for(j = 0; j < m_iCol; j++)
{
const double f = fabs(m[i][j]);
if(f > fMax)
{
fMax = f;
rhs[0][k] = i;
rhs[1][k] = j;
}
}
}
//if(fMax < 0.00001)//元素全为0
//{
// fDet = 0.0f;
// return out;
//}
if((int)rhs[0][k] != k)
{
flag = -flag;
m.RowSwap((int)rhs[0][k], k);
}
if((int)rhs[1][k] != k)
{
flag = -flag;
m.ColSwap((int)rhs[1][k], k);
}

//计算行列值
fDet *= m[k][k];

//计算逆矩阵
//第二步
m[k][k] = 1.0f/m[k][k];

//第三步
for(j = 0; j < m_iCol; j++)
{
if(j != k)
m[k][j] *= m[k][k];
}

//第四步
for(i = 0; i < m_iRow; i++)
{
if(i != k)
{
for(j = 0; j < m_iCol; j++)
{
if(j != k)
m[i][j] = m[i][j] - m[i][k]*m[k][j];
}
}
}

//第五步
for(i = 0; i < m_iRow; i++)
{
if(i != k)
{
m[i][k] *= -m[k][k];
}
}
}//end for(k);

for(k = m_iRow-1; k >= 0; k--)
{
if((int)rhs[1][k] != k)
{
m.RowSwap((int)rhs[1][k], k);
}
if((int)rhs[0][k] != k)
{
m.ColSwap((int)rhs[0][k], k);
}
}

fDet *= flag;
if(pDet != NULL)
*pDet = fDet;

return m;
}

///////////////////////////////////////判断//////////////////////////////////////

//零矩阵
int CMatrix::IsZero()const
{
for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
if(fabs(m_ppData[i][j]) > ZERO)
return 0;
}
return 1;
}

//方阵
int CMatrix::IsPhalanx()const
{
return (m_iRow == m_iCol);
}

//非奇异方阵
int CMatrix::IsNonfunnyPhalanx()const
{
return (IsPhalanx() && fabs(Det_Recursion()) > ZERO);
}

//可逆矩阵
int CMatrix::IsReverse()const
{
return IsNonfunnyPhalanx();
}

///////////////////////////////////////操作符重载//////////////////////////////////////

// []
double* CMatrix::operator [](int i)const
{
assert(i >= 0 && i < m_iRow);

return m_ppData[i];
}

// =
CMatrix& CMatrix::operator =(const CMatrix& right)
{
if(this == &right) return *this;

if((m_iRow != right.GetRow())
|| (m_iCol != right.GetCol()))// 添加于 2005-11-09
{
Resize(right.GetRow(), right.GetCol());
}
for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
m_ppData[i][j] = right[i][j];
}

return *this;
}

// =
CMatrix& CMatrix::operator =(const double* pRight)
{
assert(pRight != NULL);
for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
m_ppData[i][j] = pRight[m_iCol*i + j];
}
return *this;
}

// =
CMatrix& CMatrix::operator =(const double** ppRight)
{
assert(ppRight != NULL);
for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
m_ppData[i][j] = ppRight[i][j];
}
return *this;
}

// 一元操作符+
const CMatrix& CMatrix::operator +()const
{
return *this;
}

// 一元操作符-
const CMatrix CMatrix::operator -()const
{
CMatrix temp(m_iRow, m_iCol);

for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
temp[i][j] = -m_ppData[i][j];
}
return temp;
}

// +
const CMatrix CMatrix::operator +(const CMatrix& right)const
{
CMatrix temp(m_iRow, m_iCol);

if(m_iRow == right.GetRow()
&& m_iCol == right.GetCol())
{
for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
temp[i][j] = m_ppData[i][j] + right[i][j];
}
}
return temp;
}

// -
const CMatrix CMatrix::operator -(const CMatrix& right)const
{
CMatrix m_temp(m_iRow, m_iCol);

if(m_iRow == right.GetRow()
&& m_iCol == right.GetCol())
{
for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
m_temp[i][j] = m_ppData[i][j] - right[i][j];
}
}
return m_temp;
}

// *
const CMatrix CMatrix::operator *(const CMatrix& right)const
{
double temp = 0;

CMatrix m_temp(m_iRow, right.GetCol());

if(m_iCol != right.GetRow())
return m_temp;

for(int i = 0; i < m_temp.GetRow(); i++)
{
for(int j = 0; j < m_temp.GetCol(); j++)
{
temp = 0;
for(int k = 0; k < right.GetRow(); k++)
temp += m_ppData[i][k] * right[k][j];
m_temp[i][j] = temp;
}
}
return m_temp;
}

// *
const CMatrix CMatrix::operator *(const double& right)const
{
CMatrix m_temp(m_iRow, m_iCol);

for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
m_temp[i][j] = m_ppData[i][j] * right;
}
return m_temp;
}

// /
const CMatrix CMatrix::operator /(const double& right)const
{
CMatrix m_temp(m_iRow, m_iCol);

for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
m_temp[i][j] = m_ppData[i][j] / right;
}
return m_temp;
}

// +=
CMatrix& CMatrix::operator +=(const CMatrix& right)
{
*this = (*this) + right;
return *this;
}

// -=
CMatrix& CMatrix::operator -=(const CMatrix& right)
{
*this = (*this) - right;
return *this;
}

// *=
CMatrix& CMatrix::operator *=(const CMatrix& right)
{
*this = (*this) * right;
return *this;
}

// *=
CMatrix& CMatrix::operator *=(const double& right)
{
*this = (*this) * right;
return *this;
}

// /=
CMatrix& CMatrix::operator /=(const double& right)
{
*this = (*this) / right;
return *this;
}

// ==
int CMatrix::operator ==(const CMatrix& right)const
{
if(this == &right) return 1;

if((m_iRow != right.GetRow())
|| (m_iCol != right.GetCol()))
{
return 0;
}

for(int i = 0; i < m_iRow; i++)
{
for(int j = 0; j < m_iCol; j++)
if(fabs(m_ppData[i][j] - right[i][j]) > ZERO)//0
return 0;
}

return 1;
}

// !=
int CMatrix::operator !=(const CMatrix& right)const
{
return !(*this == right);
}

I. 复数四则运算 编程

#include <iostream.h>

class Complex{
private:
float _real;
float _image;
public:
Complex(float real=0,float image=0);
Complex operator +(const Complex &rhs);
Complex operator -(const Complex &rhs);
Complex operator *(const Complex &rhs);
float GetReal()const;
float GetImage()const;
};

Complex::Complex(float real,float image)
{
_real=real;
_image=image;
}

Complex Complex::operator +(const Complex &rhs)
{
_real+=rhs.GetReal();
_image+=rhs.GetImage();
return *this;
}

Complex Complex::operator -(const Complex &rhs)
{
_real-=rhs.GetReal();
_image-=rhs.GetImage();
return *this;
}

Complex Complex::operator *(const Complex &rhs)
{
_real=_real*rhs.GetReal()-_image*rhs.GetImage();
_image=_real*rhs.GetImage()+_image*rhs.GetReal();
return *this;
}

float Complex::GetReal()const
{
return _real;
}

float Complex::GetImage()const
{
return _image;
}

void main()
{
cout<<"依次输入两个复数的实部和虚部"<<endl;
float realA,imageA,realB,imageB;
cin>>realA>>imageA>>realB>>imageB;
Complex A(realA,imageA);
Complex B(realB,imageB);
Complex temp;
//减法和乘法类似
temp=A+B;
cout<<"两者之和为:"<<temp.GetReal()<<"+"<<temp.GetImage()<<"i"<<endl;
cout<<"其实部为"<<temp.GetReal()<<"虚部为"<<temp.GetImage()<<endl;
}

J. 《C++编程规范》上有这么一段代码,表示不能理解

那个叫做“new的放置语法”(placement new )

正常的new包括两个步骤:

  1. 分配足够大的内存;

  2. 调用对象的构造函数;


使用放置new,我们可以自己实现上面的第一步(即手动分配内存),具体的做法是:首先包含头文件 #include <new>, 然后在new的后面添加一对括号,并在括号中提供已分配好的内存的地址,这样当程序执行到new的时候,系统不会分配内存,而是直接在你提供的内存块上调用构造函数(代码是否安全得看你怎么写了)。这个语法主要用于实现自定义的内存分配。


下面有一段代码,可以看一下:

#include<string>
#include<new>
enumclassSex{
Male,
Female,
Unknown,
};
classStudent{
public:
Student(conststd::string&name,intage,Sexsex)
:m_Name(name),m_Age(age),m_Sex(sex){
}
Student(){
//使用放置new再次调用构造函数
new(this)Student("Unknown",0,Sex::Unknown);
}
private:
std::stringm_Name;
intm_Age;
Sexm_Sex;
};


如果一个类有多个构造函数,并且每个构造函数都执行类似得初始化,这会导致初始化代码的冗余,借助放置new,我们可以将初始化代码集中放到一个构造函数中,其他的构造函数调用即可,这样消除了代码冗余。

热点内容
安卓高级开发考什么 发布:2025-03-16 11:20:35 浏览:716
揽胜哪个配置带二代地形 发布:2025-03-16 11:16:52 浏览:969
c语言数组存储文件 发布:2025-03-16 11:16:48 浏览:788
sqlserver2016r 发布:2025-03-16 11:15:58 浏览:25
网页登录找不到该服务器什么意思 发布:2025-03-16 11:14:19 浏览:831
网站搭建服务器搭建 发布:2025-03-16 10:33:27 浏览:795
游戏目录在哪里安卓 发布:2025-03-16 10:33:19 浏览:467
婉儿脚本 发布:2025-03-16 10:19:33 浏览:580
c语言ftp下载文件 发布:2025-03-16 10:05:02 浏览:307
手机帐户密码怎么找回密码 发布:2025-03-16 10:02:10 浏览:706