當前位置:首頁 » 編程軟體 » 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 05:51:35 瀏覽:686
管理員c語言 發布:2025-03-16 05:40:17 瀏覽:340
安卓軟體上的圖案如何更改 發布:2025-03-16 05:35:57 瀏覽:746
2010編譯c中文亂碼 發布:2025-03-16 05:33:40 瀏覽:548
干一杯密碼箱酒多少錢一箱 發布:2025-03-16 05:31:15 瀏覽:356
我的零錢通密碼是多少 發布:2025-03-16 05:04:36 瀏覽:937
編程貓酷跑 發布:2025-03-16 04:58:35 瀏覽:321
控制演算法規律 發布:2025-03-16 04:54:17 瀏覽:965
tcl門鎖原始設置密碼是多少 發布:2025-03-16 04:52:37 瀏覽:992
如何給wifi加密碼 發布:2025-03-16 04:52:05 瀏覽:367