c++程序設(shè)計與實踐_第1頁
c++程序設(shè)計與實踐_第2頁
c++程序設(shè)計與實踐_第3頁
c++程序設(shè)計與實踐_第4頁
c++程序設(shè)計與實踐_第5頁
已閱讀5頁,還剩63頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

江蘇科技大學(xué)

課程實踐報告

設(shè)計題目:程序設(shè)計(VC++)實踐—

設(shè)計時間:至-------------

學(xué)院:______________________________

專業(yè)班級:_______________________________

學(xué)生姓名:學(xué)號

指導(dǎo)老師:______________________________-

1.試建立一個類PP,求出下列多項式的前n項的值。

具體要求如下:

(1)私有數(shù)據(jù)成員

intn:前若干項的項數(shù)。

doublex:存放x的值。

double*p:根據(jù)n的大小動態(tài)申請存放Pn(x)前n項的數(shù)組空間。

(2)公有成員函數(shù)

PP(intnum,doublexl):構(gòu)造函數(shù),初始化數(shù)據(jù)成員n和x,使p指向動態(tài)申請的數(shù)組空

間。

?PP。:析構(gòu)函數(shù),釋放p指向的動態(tài)內(nèi)存空間。

doublefun(intnl,doublex):遞歸函數(shù),用于求多項式的第nl項。注意:將遞歸公式中

的n用作函數(shù)參數(shù)。本函數(shù)供process函數(shù)調(diào)用。

voidprocess():完成求前n項的工作,并將它們存放到p指向的動態(tài)數(shù)組中。

voidshowO:輸出n和x,并將前n項以每行4個數(shù)的形式輸出到屏幕上。

(3)在主函數(shù)中完成對該類的測試。先輸入num和xl,并定義一個PP類的對象items,

用num和xl初始化items的成員n和x,調(diào)用items的成員函數(shù),求出并輸出多項式前num

項的值。

程序源代碼

#include<iostream.h>

classpp{

intn;

doublex;

double*p;

public:

pp(intnum,doublexl);

?ppO;

doublefun(intnl,doublex);

voidprocess();

voidshow();

);

pp::pp(intnum,doublexl)

(

n=num;

x=xl;

p=newdoublefnum+11;

)

pp"~pp()

(

delete[]p;

)

doublepp::fun(intnl,doublex)

if(nl==O)return1;

if(nl==l)returnx;

if(nl>l)return((2*n1-1)*x*fun(n1-1,x)-(n1-1)*fiin(n1-2,x))/n1;

voidpp::process()

{

inti;

for(i=0;i<=n;i++)

*(p+i)=fun(i,x);

voidpp::show()

(

cout?nn=H?n?,\t,?,,x=,,?x?endl;

for(inti=O,m=l;ivn;i++,m++)

{

cout?*(p+i)?,\t,;

if(m%4==0)cout?,\n,;

voidmain()

{

intnum;

doublexl;

cout?HputinnumandxlH?endl;

cin?num?xl;

ppitems(num,xl);

cess();

items.show();

運行結(jié)果

■*C:\Users\pzq\Desktop\Debug\Cppl.exe"

putinnunandxl

6

5

n=6x=5

1537305

264123525Pressanykeytocontinue

2.試建立一個類SP,求,另有輔助函數(shù)power(m,n)用于求。

具體要求如下:

(1)私有成員數(shù)據(jù)。

intn,k:存放公式中n和k的值;

(2)公有成員函數(shù)。

SP(intnl,intkl):構(gòu)造函數(shù),初始化成員數(shù)據(jù)n和k。

intpower(intm,intn):求mn。

intfun():求公式的累加和。

voidshow():輸出求得的結(jié)果。

(3)在主程序中定義對象s,對該類進(jìn)行測試。

程序源代碼

#include<iostream.h>

classSP{

intn,k;

public:

SP(intnljntkl);

intpower(intm,intn);

intfun();

voidshow();

);

SP::SP(intnl,intml)

(

n=nl;

k=ml;

)

intSP::power(intmjntn)

(

inti;

if(n==O)return1;

else

for(i=l;i<n;i++)

m*=m;

returnm;

)

intSP::fun()

(

inty=0,x;

for(x=l;x<=n;x++)

y+=power(x,k);

returny;

)

voidSP::show()

cout<<”前n項的和為:"<<fun()<<endl;

}

voidmain()

intnl,kl;

cout<v"輸入nl和kl的值"<<endl;

cin?nl?kl;

SPs(nlzkl);

s.fun();

s.show();

)

運行結(jié)果:

1.C:\Users\pzq\Desktop\Debug\CppLexe"

輸入nl和kl的值

5

3

前n項的和為:979

Pressanykeytocontinue

3.建立一個類MOVE,不進(jìn)行排序,將數(shù)組中小于平均值的元素放到數(shù)組的左邊,大于平

均值的元素放到數(shù)組的右邊。

具體要求如下:

(1)私有數(shù)據(jù)成員

floatarray[201:一維整型數(shù)組。

intn:數(shù)組中元素的個數(shù)。

(2)公有成員函數(shù)

MOVE(floatb[],intm):構(gòu)造函數(shù),初始化成員數(shù)據(jù)。

voidaverage():輸出平均值,并將數(shù)組中的元素按要求重新放置。

voidprint():輸出一維數(shù)組。

(3)在主程序中用數(shù)據(jù){1.3,6.2,3,9.1,4.8,7.4,5.6,9.2,2.3}對該類進(jìn)行測試。

程序源代碼

#include<iostream.h>

classMOVE{

floatarray[20];

intn;

public:

MOVE(floatb[],intm);

voidaverage();

voidprint();

);

MOVE::MOVE(floatb[],intm)

inti;

n=m;

for(i=0;i<m;i++)

array[ij=b[ij;

}

voidMOVE::average()

(

inti,x;

floata=0;

for(i=0;i<n;i++)

a+=array[i];

a/=n;

cout?"平均值為"?a?endl;

floatff[20J;

for(i=0,x=0;i<n;i++)

if(array[ij<a)

{

ff[x]=array[i];

x++;

)

for(i=0;i<n;i++)

if(array[i]>a)

{

ff[x]=array[i];

x++;

)

for(i=0;i<n;i++)

array[ij=ff[i];

)

voidMOVE::print()

(

intq,p=l;

for(q=0;q<n;q++)

(

cout?array[q]?'\t';

if(p%5==0)cout?,\n,;

P++;

)

)

voidmain()

(

floatb[]=[136.2,3,9.1,4.8,74,5.6,9.2,2.3};

intm=9;

MOVEaa(b,m);

aa.average();

aa.print();

運行結(jié)果

4,建立一個類MOVE,將數(shù)組中最大元素的值與最小元素的值互換。

具體要求如下:

(1)私有數(shù)據(jù)成員

int*array:一維整型數(shù)組。

intn:數(shù)組中元素的個數(shù)。

(2)公有成員函數(shù)

MOVE(intb[],intm):構(gòu)造函數(shù),初始化成員數(shù)據(jù)。

voidexchange():輸出平均值,并將數(shù)組中的元素按要求重新放置。

voidprint():輸出一維數(shù)組°

?MOVE。:析構(gòu)函數(shù)。

(3)在主程序中用數(shù)據(jù){21,65,43,87,12,84,44,97,32,55}對該類進(jìn)行測試。

程序源代碼

#include<iostream.h>

classMOVE{

int*array;

intn;

public:

MOVE(intb[],intm);

voidexchange();

voidprint();

~MOVE();

);

MOVE::MOVE(intb[]Jntm)

(

n=m;

array=newint[n];

for(intx=0;x<n;x++)

array[x]=b[x];

)

voidMOVE::exchange()

floata=0;

inti,*pl,*p2;

for(i=0;i<n;i++)

a+=array[i];

a/=n;

cout?"Theaverageofthenumberis:"?a?endl;

pl=p2=&array[0];

for(i=0;i<n;i++)

(

if(array[i]<*pl)pl=&array[i];

if(array[i]>*p2)p2=&array[i];

)

i=*pl;

*pl=*p2;

*p2=i;

)

voidMOVE::print()

(

inti,num=l;

for(i=0;i<n;i++)

(

cout?array[i]?'\t';

if(num%5==0)cout?'\n';

num++;

)

)

MOVE::~MOVE()

{delete[]array;}

voidmain()

(

intb[]={21,65,43,87,12,84,44,97z32,55},n=10;

MOVEff(b,n);

ff.exchange();

ff.print();

}

運行結(jié)果

r----

■"C:\Users\pzq\Desktop\Debug\Cppl.exe"

Theaverageofthenunberis:54

2165438797

8444123255

Pressanykeytocontinue

5.定義一個類Palindrome,實現(xiàn)絕對回文數(shù)。設(shè)計一個算法實現(xiàn)對任意整型數(shù)字判斷是否

為絕對回文數(shù)。所謂絕對回文數(shù),是指十進(jìn)制數(shù)和二進(jìn)制數(shù)均對稱的數(shù)。

具體要求如下:

(1)私有數(shù)據(jù)成員

intn:整型數(shù)字。

inty:標(biāo)記是否為回文數(shù)。

(2)公有成員函數(shù)

Palindrome(intx):構(gòu)造函數(shù),根據(jù)x參數(shù)初始化數(shù)據(jù)成員n,y初始化為0。

voidhuiwen():判斷數(shù)n是否為絕對回文數(shù)。

voidshow():若該數(shù)為回文數(shù),則在屏幕顯示。

(3)在主程序中定義inta,由鍵盤輸入數(shù)字。定義一個Palindrome類對象p,用a初始

化P,完成對該類的測試。

程序源代碼

#include<iostream.h>

classpalindrome{

intn;

inty;

public:

palindrome(intx);

voidhuiwen();

voidshow();

};

palindrome::palindrome(intx)

{

n=x;

y=o;

)

voidpalindrome::huiwen()

(

intb[20],c[50],m,i,p=0,tl=l,t2=l;

m=n;

for(i=0;m>0;m/=10)

(

P++;

b[i]=m%10;

i++;

}

for(i=0;i<p;i++)

if(b[i]!=b[p-i-l])

{

tl=0;

break;

)

for(i=0zm=nzp=0;m>0;m/=2)

P++;

c[i]=m%2;

i++;

)

for(i=0;i<p;i++)

if(c[i]!=c[p-i-l])

(

t2=0;

break;

)

if(tl&&t2)y=l;

)

voidpalindrome::show()

(

if(y==0)cout<<”該數(shù)不是回文數(shù)!"?endl;

elsecout<<”該回文數(shù)是:"<<n<<endl;

)

voidmain()

(

inta;

cout<<“輸入a的值“<<endl;

cin?a;

palindromep(a);

p.huiwen();

p.show();

)

運行結(jié)果:

■"C:\Users\pzq\Desktop\Debug\Cppl.exe"

輸入a的值

12345654321

該數(shù)不絕對是回文數(shù)!

Pressanykeytocontinue

6.定義一個字符串類String,實現(xiàn)判斷該字符串是否為回文字符串。所謂回文字符串,是

指該字符串左右對稱。例如字符串“123321”是回文字符串。

具體要求如下:

(1)私有數(shù)據(jù)成員

char*str;

inty:標(biāo)記是否為回文字符串。

(2)公有成員函數(shù)

String(char*s):構(gòu)造函數(shù),用給定的參數(shù)s初始化數(shù)據(jù)成員str。y初始化為0。

voidhuiwen():判斷str所指向的字符串是否為回文字符串。

voidshow():在屏幕上顯小字符串。

(3)在主程序中定義字符串chars[]=nababcedbaba”作為原始字符串。定義一個String

類對象test,用s初始化test,完成對該類的測試。

程序源代碼

#include<iostream.h>

#include<string.h>

classstring{

char*str;

inty;

public:

string(char*s);

voidhuiwen();

voidshow();

);

string::string(char*s)

(

str=newchar[strlen(s)];

strcpy(str,s);

y=o;

)

voidstring::huiwen()

(

char*pl,*p2;

p2=pl=str;

for(inti=O;str[i];i++zp2++);

p2一;

for(;pl!=p2;pl++,p2-)

(

if(*pl!=*p2)

{y=O;break;}

else

y=i;

)

)

voidstring::show()

(

cout<<”字符串為:,/?str?endl;

if(y==0)cout<<"字符串不是回文數(shù)!"?endl;

elsecout?y?endl;

}

voidmain()

(

chars[]="ababcedbaba";

stringtest(s);

test.huiwen();

test.show();

)

運行結(jié)果:

1*C:\Users\pzq\Desktop\Debug\Cppl.exeD回

字符串為:

季特雷京是回ab文ab數(shù)ce!dbaba

Pressanykeytocontinue

7.建立一個類PHALANX,生成并顯示一個折疊方陣。折疊方陣如下圖所示。折疊方陣的

生成過程為:起始數(shù)置于方陣的左上角,然后從起始數(shù)開始遞增,依次折疊構(gòu)成方陣。

具體要求如下:

(1)私有數(shù)據(jù)成員

int(*p)[20]:指向按照折疊規(guī)律存放方陣的二維整型數(shù)組。

intstartnum:折疊方陣的起始數(shù)。

intn:存放方針的層數(shù)。

(2)公有成員函數(shù)

PHALANX(ints,intm):構(gòu)造函數(shù),初始化成員數(shù)據(jù)。

voidprocess():生成起始數(shù)為startnum的n行方陣。

voidprint():輸出折疊方陣。

~PHALANX。:析構(gòu)函數(shù)。

(3)在主程序中對該類進(jìn)行測試。

程序源代碼

#include<iostream.h>

#include<iomanip.h>

classphalanx{

int(*p)[2O];

intstarnum;

intn;

public:

phalanx(ints,intm);

voidprocess();

voidprint();

~phalanx();

};

phalanx::phalanx(ints,intm)

starnum=s;

n=m;

)

voidphalanx::process()

{

intnum=starnum;

inty=n,i,j,x;

p=newint[2O][2O];

for(x=0;x<y;x++)

(

for(i=0,j=x;i<x;i++)

(

p[i][j]=num;

num++;

)

for(;j>=0;j-)

{

p[i][j]=num;

num++;

)

}

)

voidphalanx::print()

(

inti,j,m=0;

for(i=0;i<n;i++)

(

for(j=0;j<n;j++)

(

cout?setw(5)?p[i][j];

)

cout?'\n';

)

)

phalanx::~phalanx()

(

delete[]p;

)

voidmain()

(

ints,m;

coutvc"輸入s和m的值"<<endl;

cin?s?m;

phalanxpp(s,m);

cess();

PP-print();

運行結(jié)果

■*C:\Users\pzq\Desktop\Debug\Cppl.exe"

輸入s和m的值

16

125101726

43611182?

987121928

161514132029

252423222130

363534333231

Pressanykeytocontinue

8.建立一個MATRIX,生成并顯示一個螺旋方陣。螺旋方陣如下圖所示,起始數(shù)置于方陣

的左上角,然后從起始數(shù)開始依次遞增,按順時針方向從外向里旋轉(zhuǎn)填數(shù)而成。

具體要求如下:

(1)私有數(shù)據(jù)成員

inta[20][20]:二維整型數(shù)組存放螺旋方陣。

intstartnum:螺旋方陣的起始數(shù)。

intn:存放方針的層數(shù)。

(2)公有成員函數(shù)

MATRIX(ints,intm):構(gòu)造函數(shù),初始化成員數(shù)據(jù)startnum和n。

voidprocess():生成起始數(shù)為startnum的n行螺旋方陣。

voidprint():輸出螺旋方陣°

(3)在主程序中定義MATRIX類的對象t對該類進(jìn)行測試。

程序源代碼

#include<iostream.h>

#include<iomanip.h>

classmatrix{

inta[20][20];

intstarnum;

intn;

public:

matrix(intsjntm);

voidprocess();

voidprint();

};

matrix::matrix(intsjntm)

starnum=s;

n=m;

)

voidmatrix::process()

{

intst=starnum;

inti,j,x=O,y=n;

for(;x<=(y-l)/2;x++)

(

for(i=x,j=x;j<(y-x-l);j++)

|

a[i][j]=st;

st++;

)

for(;i<(y-x-l);i++)

(

a[i][j]=st;

st++;

)

for(;j>x;j-)

{

a[i][j]=st;

st++;

)

for(;i>x;i--)

(

a[i][j]=st;

st++;

)

if(x==(y-l)/2)a[i][j]=st;

)

}

voidmatrix::print()

(

inti,j;

for(i=0;i<n;i++)

(

for(j=0;j<n;j++)

cout?setw(8)?a[i][j];

cout?'\n';

)

}

voidmain()

(

ints,m;

cout<<"輸入s和m的值"<<endl;

cin?s?m;

matrixpp(s,m);

cess();

PP-printO;

運行結(jié)果

[口回

>"C:\Users\pzq\Desktop\Debug\Cppl,exe"

輸入s和m的值

17

1234567

2425262728298

2340414243309

22394849443110

21384746453211

203?3635343312

19181716151413

Pressanykeytocontinue

9.定義一個字符串類CString,并設(shè)計一個算法對該串中各個不同字符出現(xiàn)的頻率進(jìn)行統(tǒng)計。

具體要求如下:

(1)私有數(shù)據(jù)成員

char*str:指向要統(tǒng)計的字符串。

char(*p)[2]:動態(tài)分配二維空間,用以存放str所指字符串中出現(xiàn)的字符及其出現(xiàn)的

次數(shù)(次數(shù)在存放時,用該數(shù)字對應(yīng)的ASCII值存放:在輸出次數(shù)時,輸出該ASCH字符

對應(yīng)的ASCH值即可)。

intsize:存放字符串中出現(xiàn)的所有不同的字符的個數(shù)。

(2)公有成員函數(shù)

CString(char*s):根據(jù)s參數(shù)初始化數(shù)據(jù)成員str;p和size初始值為0。

voidCount。:p根據(jù)s所指字符串長度分配空間。然后把str所指字符串中的每個字符

放入p數(shù)組中,設(shè)置每個字符的出現(xiàn)次數(shù)為1。根據(jù)p數(shù)組統(tǒng)計不同字符出現(xiàn)的頻率,并求

得size的實際大小。最后根據(jù)size的實際大小,重新分配p所指空間,并把不同字符及其

出現(xiàn)次數(shù)重新放回p數(shù)組(提示:可以借助臨時數(shù)組或指針來實現(xiàn))。

voidShow():屏幕顯示字符串、字符串的每個字符和與之對應(yīng)的次數(shù)。

~CString():釋放動態(tài)分配的空間。

(3)在主程序中定義字符串chars[]="abdabcdesffffd"。定義一個CString類對象test,

用s以初始化test,完成對該類的測試。

程序源代碼

#include<iostream.h>

#include<string.h>

classcstring{

char*str;

char(*p)[2];

intsize;

public:

cstring(char*s);

voidcount();

voidshow();

~cstring();

};

cstring::cstring(char*s)

(

P=O;

size=O;

str=s;

)

voidcstring::count()

(

p=newchar[strlen(str)][2];

charn;

char*pl,*p2;

for(inti=O;str[i];i++)

(

n='\O';

pl=&str[i];

for(intm=0;str[m];m++)

(

p2=&str[m];

if(*pl==*p2)n++;

P2++;

}

p[i][O]=str[i];

P[i][l]=n;

}

)

voidcstring::show()

(

for(inti=O;i<strlen(str);i++)

|

intm,x=l;

for(m=0;m<i;m++)

(

if(p[m][0]==p[i][0])

(

x=0;

break;

}

)

if(x==l)cout?p[i][0]?'\t'<<(int)p[i][l]?endl;

)

)

cstring::~cstring()

(

delete[]p;

)

voidmain()

(

chars[]="abdabcdesffffd";

cstringtest(s);

test.count();

test.show();

)

運行結(jié)果

F

?"C:\Users\pzq\Desktop\Debug\Cppl.exe"

a2

b2

d3

c1

e1

s1

f4

Pressanykeytocontinue

10.定義一個字符串類cstring,并設(shè)計一個算法實現(xiàn),給定關(guān)鍵字strl在字符串str中出現(xiàn)

時用關(guān)鍵字str2進(jìn)行替換的功能。

具體要求如下:

(1)私有數(shù)據(jù)成員

char*str;原始字符串。

char*strl;目標(biāo)關(guān)鍵字。

char*str2;替換關(guān)鍵字。

intflag;標(biāo)記替換是否完成替換。

(2)公有成員函數(shù)

CString(char*s,charsl[],char*s2):用給定的參數(shù)s、si和s2相對應(yīng)的初始化數(shù)據(jù)成

員str、strl和str2。flag設(shè)置缺省0。

voidReplace():判斷str字符串中是否出現(xiàn)strl,若出現(xiàn)就用str2替換,否則什么都不

做。若替換成功了標(biāo)記flag為1,若替換不成功則標(biāo)記flag為0。

voidShowO:若替換成功,則在屏幕上顯示目標(biāo)關(guān)鍵字、替換關(guān)鍵字和替換后的原始

字符串;若不成功則顯示原始字符串。

~CStringO:釋放動態(tài)分配的空間。

(3)在主程序中定義字符串chars[]=wIamstudent,youarestudenttoo,weareall

student."作為原始字符串,定義charsl[]=Mstudent”作為目標(biāo)關(guān)鍵字,定義chars2[]="teacher"

作為替換關(guān)鍵字。定義一個CString類對象test,用s,si和s2初始化test,完成對該類的測

試。

程序源代碼

#include<iostream.h>

#include<string.h>

classcstring{

char*str;

char*strl;

char*str2;

intflag;

public:

cstring(char*s,charsl[],char*s2);

voidreplace();

voidshow();

~cstring();

);

cstring::cstring(char*s,charsl[],char*s2)

(

str=newchar[strlen(s)+l];

strl=newchar[strlen(sl)+l];

str2=newchar[strlen(s2)+l];

strcpy(str,s);

strcpy(strl,sl);

strcpy(str2,s2);

flag=O;

)

voidcstring::replace()

(

inti,nlzn2,y=l;

for(i=0;str[i];i++)

(

if(str[i]==strl[O])

(

for(nl=i,n2=0;strl[n2];nl++,n2++)

if((str[nl]!=strl[n2])||(str[nl]=='\O'))

(

y=o;

break;

)

if(y==i)

(

charpp[100];

for(intx=Ozp=i;x<(strlen(strl));p++,x++)

(

str[p]=str2[x];

)

flag=l;

)

}

)

)

voidcstring二show。

(

if(flag==l)cout<<"改后"<<str?endl;

elsecout<<"未改“<<str<<endl;

}

cstring::~cstring()

(

delete[]str;

delete[]strl;

delete[]str2;

)

voidmain()

(

chars[]="lamstudent,youarestudenttoo,weareallstudent.";

charsl[]="student";

chars2[]="teacher";

cstringtest(s,sl,s2);

test.replace();

test.show();

)

運行結(jié)果

■*C:\Users\pzq\Desktop\Debug\Cppl.exe"

亡(后Ianteacher,youareteachertooj,weareallteacher.

Pressanykeytocontinue

H.建立一個STRING,將一個字符串交叉插入到另一個字符串中(假定兩字符串等長)。

例如將字符串“abcde”交叉插入字符串“ABCDE”的結(jié)果為“aAbBcCdDeE”或

“AaBbCcDdEe

具體要求如下:

(1)私有數(shù)據(jù)成員

charstrl[8O]:存放被插入的字符串。

charstr2[40]:存放待插入的字符串。

(2)公有成員函數(shù)

STRING(char*sl,char*s2):構(gòu)造函數(shù),用si和s2初始化strl和sti2.

voidprocess():將str2中的字符串插入到strl中。

voidprint():輸出插入后的字符串。

(3)在主程序中定義STRING類的對象test對該類進(jìn)行測試。

程序源代碼

#include<iostream.h>

#include<string.h>

classSTRING{

charstrl[80];

charstr2[40];

public:

STRING(char*sl,char*s2);

voidprocess();

voidprint();

);

STRING::STRING(char*sl,char*s2)

{

for(inti=0;i<80;i++)

strl[i]=sl[i];

for(intj=0;j<40;j++)

Str2[j]=s2[j];

)

voidSTRING::process()

(

chars[40];

strcpy(s,strl);

inti=0,j=0;

while(str2[i])

(

strl[j++]=str2[i];

strl[j++]=s[i];

i++;

)

strl[j]=>\O';

)

voidSTRING::print()

{

cout<<"交叉后的字符串為:

cout?strl?endl;

)

voidmain()

(

charstrl[80],str2[40];

cout<<"輸入字符串l"?endl;

cin.getline(strl,39);

cout<<"輸入字符串2"?endl;

cin.getline(str2,39);

STRINGtest(strl,str2);

cess();

test.print();

)

運行結(jié)果

?*C:\Users\pzq\Desktop\Debug\Cppl.exe"

輸入字符串]

abcdefg

輸入字符串2

ABCDEFG

交叉后的字符串為:AaBbCcDdEeFfGg

Pressanykeytocontinue

12.建立一個STRING,將一個字符串交叉插入到另一個字符串中(假定兩字符串不等長)。

例如將字符串“abcde”交叉插入字符串“ABCDEFG”的結(jié)果為“aAbBcCdDeEFG”或

“AaBbCcDdEeFG”。

具體要求如下:

(1)私有數(shù)據(jù)成員

charstrl[60]:存放被插入的字符串。

charstr2[40]:存放待插入的字符串。

charstr3[100]:存放插入后的字符串。

(2)公有成員函數(shù)

STRING(char*sl,char*s2):構(gòu)造函數(shù),用si和s2初始化strl和str2。

voidprocess():將str2中的字符串插入到strl中,存放到str3中。

voidprint():輸出插入后的字符串。

(3)在主程序中定義STRING類的對象test對該類進(jìn)行測試。

程序源代碼

#include<iostream.h>

#include<string.h>

classSTRING{

charstrl[60];

charstr2[40];

charstr3[100];

public:

STRING(char*sl,char*s2);

voidprocess();

voidprint();

);

STRING::STRING(char*slzchar*s2)

(

for(inti=0;i<60;i++)

strl[i]=sl[i];

for(intj=0;j<40;j++)

str2[j]=s2[j];

)

voidSTRING::process()

inti=OJ=O,x;

while(strl[i]&&str2[i])

(

str3[j++]=str2[i];

str3[j++]=strl[i];

i++;

)

x=i;

if(str2[x])while(str2[x])str3[j++]=str2[x++];

if(strl[i])while(strl[i])str3[j++]=strl[i++];

str3[j++]='\O';

)

voidSTRING::print()

{

cout?”合并后的字符串為:

cout?str3?endl;

}

voidmain()

(

charstrl[60],str2[40];

cout<<“輸入字符串lH?endl;

cin.getline(strl,59);

cout<<“輸入字符串2"?endl;

cin.getline(str2,39);

STRINGtest(strl,str2);

cess();

test.print();

)

運行結(jié)果

F

,"C:\Users\pzq\Desktop\Debug\Cppl.exe"

輸入字符串1

abcdefg

輸入字符串2

ABCDEFGHIJKLMN

合并后的字符串為:AaBbCcDdEeFfGgHIJKLMN

Pressanykeytocontinue

13.建立一個類MOVE,對數(shù)組中元素進(jìn)行循環(huán)換位,即每個元素后移三位,最后三個元

素移到最前面。

具體要求如下:

(1)私有數(shù)據(jù)成員

intarray[20]:一維整型數(shù)組。

intn:數(shù)組中元素的個數(shù)。

(2)公有成員函數(shù)

MOVE(intb[],intm):構(gòu)造函數(shù),初始化成員數(shù)據(jù)。

voidchange():進(jìn)行循環(huán)換位。

voidprint():輸出一維數(shù)組。

(3)在主程序中用數(shù)據(jù){21,65,43,87,12,84,44,97,32,55}對該類進(jìn)行測試。

程序源代碼

#include<iostream.h>

classMOVE{

intarray[20];

intn;

public:

MOVE(intb[],intm);

voidchange();

voidprint();

);

MOVE::MOVE(intb[],intm)

(

n=m;

for(inti=0;i<n;i++)

array[i]=b[i];

)

voidMOVE::change()

(

inta[20];

for(inti=0;i<n-3;i++)

a[i+3]=array[i];

a[O]=array[i++];

a[l]=array[i++];

a[2]=array[i];

for(i=0;i<n;i++)

array[i]=a[i];

)

voidMOVE::print()

(

for(inti=0;i<n;i++)

cout?array[i]?'\t';

cout?endl;

)

voidmain()

(

intb[20]={21z65,43,87,12,84,44,97,32,55};

MOVEp(b,10);

p.change();

p.print();

)

運行結(jié)果

14.建立一個類MOVE,實現(xiàn)將數(shù)組中大字字母元素放在小寫字母元素的左邊。

具體要求如下:

(1)私有數(shù)據(jù)成員

char*array:—維字符數(shù)組。

intn:數(shù)組中元素的個數(shù)。

(2)公有成員函數(shù)

MOVE(charb[],intm):構(gòu)造函數(shù),初始化成員數(shù)據(jù)。

voidchange():進(jìn)行排序換位。

voidprint():輸出一維數(shù)組。

?MOVE():析構(gòu)函數(shù)。

(3)在主程序中用數(shù)據(jù)"fdsUFfsTjfsKFEkWC”對該類進(jìn)行測試。

程序源代碼

#include<iostream.h>

#include<string.h>

classmove{

char*array;

intn;

public:

move(charb[]Jntm);

voidchange();

voidprint();

~move();

};

move::move(charb[]Jntm)

array=newchar[strlen(b)+l];

n=m;

strcpy(array,b);

)

voidmove::change()

char*p;

intm=0;

p=newchar[strlen(array)];

strcpy(p,array);

for(inti=0;p[i];i++)

if(p[i]<='Z'&&p[i]>='A')

array[m++]=p[i];

for(i=0;p[i];i++)

if(p[i]<='z'&&p[i]>='a')

array[m++]=p[i];

)

voidmove::print()

cout<<"改后字符串為:"?array?endl;

)

delete[]array;

}

voidmain()

charb[1000];

intm;

cout<<"輸入字符串和字符串個數(shù)"<<endl;

cin?b?m;

movepp(b,m);

pp.change();

PP-print();

)

運行結(jié)果:

?"C:\Users\pzq\Desktop\Debug\Cppl.exe?;貪h

輸入字符串和字符串個數(shù)

fdsUFfsTjfsKFEkWC

17

改后字符串為:UFTKFEWCfdsfsjfsk

Pressanykeytocontinue

15.定義一個一維數(shù)組類Carray,并根據(jù)給定算法實現(xiàn)對原始一維數(shù)組進(jìn)行線性變換。這里

給定的線性變換算法為:T(bx)=bT(x)+i;其中,b為變換常量,x為變量,i為當(dāng)前類中

成員數(shù)組的下標(biāo)值。根據(jù)該算法,原始數(shù)組在變化后,當(dāng)前數(shù)組元素的值是由常量b和i下

標(biāo)來決定的。

具體要求如下:

(1)私有數(shù)據(jù)成員

int*a:指針a指向一個動態(tài)分配的原始數(shù)組。

intn:n表示該數(shù)組的大小。

intb:線性變換的常量。

(2)公有成員函數(shù)

Carray(inta[],intn,intx):用給定的參數(shù)a、n和x初始化數(shù)據(jù)成員a、n和b。缺省都

設(shè)置為0。

voidTransform():根據(jù)上述變化算法,求解數(shù)組變換。

voidShow():在屏幕上顯示數(shù)組元素。

~Carray():釋放動態(tài)分配的空間。

(3)在主程序中定義數(shù)組intarr[]={1,2,3,4,5,6,7,8,9,10}作為原始數(shù)組,intb;由鍵盤輸

入,作為線性變換的常量。定義?個Carray類對象test,用arr初始化test,完成對該類的

測試。

程序源代碼

#include<iostream.h>

classcarray{

int*a;

intn;

intb;

public:

carray(inta[],intn,intx);

voidtransform();

voidshow();

-carrayO;

);

carray::carray(inta[],intn=0,intx=0)

(

this->a=newint[n];

this->n=n;

b=x;

fbr(inti=0;i<n;i++)

(

this->a[i]=a[i];

voidcarray::transfbrm()

(

for(inti=0;i<n;i++)

a[i|=b*a|i]+i;

)

voidcarray::show()

(

cout?”變換后的數(shù)組為:"vvendl;

fbr(inti=0;i<n;i++)

cout?a[i]?,\t,;

cout?endl;

)

carray::-carray()

(

delete[]a;

)

voidmain()

(

intarr[]={1,2,3,4,5,6,7,8,9,10);

intm=10,b;

coutvv”輸入線性變換常量Nvendl;

cin?b;

carraytest(arr,m,b);

test.transform();

test.show();

)

運行結(jié)果:

16.定義一個方陣類CMatrix,并根據(jù)給定算法實現(xiàn)方陣的線性變換。方陣的變換形式為:

F=W*fT

f為原始矩陣,fT為原始矩陣的轉(zhuǎn)置,w為變換矩陣,這里設(shè)定為

1001

0110

0110

1001

具體要求如下:

(1)私有數(shù)據(jù)成員

int(*a)[4]:a指向方陣數(shù)組。

intw[4][4]:w為變換矩陣。

intm:m表示方陣的行和列數(shù)。

(2)公有成員函數(shù)

CMatrix(inta[][4|,intm):用給定的參數(shù)a和m初始化數(shù)據(jù)成員a和m;對變換矩陣w

進(jìn)行初始化,要求必須用循環(huán)實現(xiàn)。

voidTransform():根據(jù)上述變換算法,求出變換后的數(shù)組形式,存放在原始數(shù)組內(nèi)。

voidshow():在屏幕上顯示數(shù)組元素。

~CMatrix():釋放動態(tài)分配的空間。

(3)在主程序中定義數(shù)組intarr|||4|={1,2,3,4,5,6,7,8,9,10,11』2,13』4,15』6}作為原始數(shù)組。

定義一個CMatrix類對象test,用arr初始化test,完成對該類的測試。

程序源代碼:

#include<iostream.h>

#include<iomanip.h>

classcmatrix{

int(*a)[4];

intw[4][41;

intm;

public:

cmatrix(inta[J[4J,intm);

voidtransform();

voidshow();

?cmatrix。;

);

cmatrix::cmatrix(inta[J[4],intm)

(

this->a=newint

for(inti=0;i<4;i++)

for(intj=0;j<4;j++)

this->a[i][j]=a[i][j];

this->m=m;

for(intp=0;p<4;p++)

for(intq=0;qv4;q++)

if((p+q!=3)&&(p-q!=0))w[p][q]=0;

elsew[p][q]=l;

)

voidcmatrix::transform()

(

intb[4][4],i,j;

for(i=0;i<4;i++)

for(j=0;j<4;j++)

brilUl=a[j]ril;

for(i=0;i<4;i++)

for(j=0;j<4;j++)

a[i][j]=w[i][0]*b[0]U]+w[i][l]*b[l]U]+w[i][2]*b[2]U]+w[i][3]*b[3]U];

}

voidcmatrix::show()

(

cout<<"變換后的數(shù)組是:"<<endl;

for(inti=0;i<4;i++)

{

for(intj=0;j<4;j++)

cout?setw(8)?a[i][j];

cout?endl;

cmatrix::-cmatrix()

delete[]a;

)

voidmain()

(

intarr[J[4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);

intm=4;

cmatrixtest(arr,m);

test.transform();

test,show();

)

運行結(jié)果:

17.定義一個類SIN,求具體要求如下:

(1)私有成員數(shù)據(jù)。

intx:輸入公式中x的值,求sin(x)。

intn:輸入公式中n的值。

(2)公有成員函數(shù)。

SIN(intx,intn):構(gòu)造函數(shù),用于初始化x和n的值。

intpower(intq):求q!的值。

intmi(intm,intn):求的值。

intfun():用于求SIN(X)的值。

voidshow():輸出求得的結(jié)果。

(3)在主程序中定義對象test,對該類進(jìn)行測試。

#include<iostream.h>

classsin{

intx;

intn;

public:

sin(intx,intn);

intpower(intq);

intmi(intm,intn);

intfun();

voidshow();

);

sin::sin(intx,intn)

(

this->x=x;

this->n=n;

)

intsin::power(intq)

(

for(inti=l;iv=q;i++)

q*=i;

returnq;

)

intsin::mi(intm,intn)

(

for(inti=l;i<n;i++)

m*=m;

returnm;

)

intsin::fun()

(

floats=0;

for(inti=l;i<=n;i++)

s+=mi(-1,i+1)*mi(x,2*i-1)/power(2*i-1);

returns;

)

voidsin::show()

(

cout?,,sin(,,?x?H)=,,?fun()?endl;

)

voidmain()

{

intx,n;

cout<v”輸入x和n的值”v<endl;

cin?x?n;

sintest(x,n);

test,show();

18.試建立一個類VAR,用于求n()個數(shù)的均方差。均方差的計算公式為,其中平均值

為。

具體要求如下:

(1)私有成員數(shù)據(jù)。

doublea[100]:用于存放輸入的n個數(shù)。

intn:實際輸入數(shù)的個數(shù)n。

(2)公有成員函數(shù)。

VAR(doublex[],intnl):構(gòu)造函數(shù),初始化成員數(shù)據(jù)a和個數(shù)n。

doubleaverage(doublex[],intn):求平均值,數(shù)組x具有n個元素。

voidvariance(doublex[],intn):求均方差,數(shù)組x具有n個元素。

voidshow():輸出求得的均方差。

(3)在主程序中定義一個對象test,對該類進(jìn)行測試。

#include<iostream.h>

classvar{

doublea[100];

intn;

public:

var(doublex[],intnl);

doubleaverage(doublex[],intn);

voidvariance(doublex[],intn);

voidshow();

);

var::var(doublex[],intnl)

(

fbr(inti=0;i<nl;i++)

a[i]=x[i];

n=nl;

)

doublevar::average(doublex口,intn)

(

doubleave=0;

fbr(inti=0;i<n;i++)

ave+=x[i];

ave/=n;

returnave;

)

v

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論