第6章? ?數(shù)組、指針與字符串?
6-20 實現(xiàn)一個名為 SimpleCircle 的簡單圓類。其數(shù)據(jù)成員 int* itsRadius 為一個指向其半徑值的指針,存放其半徑值。設(shè)計對數(shù)據(jù)成員的各種操作,給出這個類的完整實現(xiàn)并測試這個類。
#include<iostream>
using namespace std;
const float PI=3.14;
class SimpleCircle{
private:
int* itsRadius;
public:
SimpleCircle(){
itsRadius = new int(5);
}
SimpleCircle(int R){
itsRadius = new int(R);
}
SimpleCircle(const SimpleCircle &rhs){
int val = rhs.getRadius();
itsRadius = new int(val);
}
~SimpleCircle() {}
int getRadius() const{
return *itsRadius;
}
float area(){
return PI*(*itsRadius)*(*itsRadius);
}
};
int main()
{
int R;
cout<<"請輸入s2圓的半徑:";
cin>>R;
SimpleCircle s1,s2(R),s3(s2);
cout<<"該圓s1的面積是:"<<s1.area()<<endl;
cout<<"該圓s2的面積是:"<<s2.area()<<endl;
cout<<"該圓s3的面積是:"<<s3.area()<<endl;
return 0;
}
6-21 編寫一個函數(shù),統(tǒng)計一條英文句子中字母的個數(shù),在主程序中實現(xiàn)輸入輸出。
#include<iostream>
#include<string>
using namespace std;
int count(char *s)
{
int n;
for(int i=0;s[i]!='\0';i++){
if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z'))
n++;
}
return n;
}
int main()
{
char s[100];
cout<<"請輸入一個英文句子:";
gets(s); //輸入可以帶空格的字符串
cout<<"這個句子中字母有"<<count(s)<<"個"<<endl;
return 0;
}
6-22 編寫函數(shù) void reverse(string & s),用遞歸算法使字符串 s倒序。
#include <iostream>
#include <string>
using namespace std;
string reverse(string& str)
{
if(str.length()>1){
string sub = str.substr(1,str.length()-2);
return str.substr(str.length()-1,1) + reverse(sub) + str.substr(0,1);
}
else
return str;
}
int main()
{
string str;
cout<<"請輸入一個字符串:";
cin>>str;
cout<<"倒序反轉(zhuǎn)后的字符串為:"<<reverse(str)<<endl;
return 0;
}
6-23 設(shè)學(xué)生人數(shù) N = 8,提示用戶輸入 N 個人的考試成績,然后計算出他們的平均成績并顯示出來。
#include <iostream>
using namespace std;
int main()
{
float num[8],score = 0,ave;
cout<<"請輸入8個人的考試成績:"<<endl;
for(int i=0;i<8;i++){
cin>>num[i];
score+=num[i];
}
ave=score/8;
cout<<"它們的平均成績?yōu)椋?<<ave<<endl;
return 0;
}
6-24 基于 char*設(shè)計一個字符串類 M yString,并且具有構(gòu)造函數(shù)、析構(gòu)函數(shù)、復(fù)制構(gòu)造函數(shù),重載運算符“+”,“=”,“+=”,“[]”,盡可能完善它,使之能滿足各種需要(運算符重載功能為選做,?參見第 8 章)。
#include <iostream>
#include <cstring>
using namespace std;
class myString{
private:
char *s;
int Size;
public:
myString(){ //無參數(shù)構(gòu)造函數(shù)
s=new char[1];
s[0]='\0';
Size=0;
}
myString(char str[]){ //有參數(shù)構(gòu)造函數(shù)
Size=strlen(str);
s=new char[Size+1];
strcpy(s,str);
}
myString( const myString &b ){ //復(fù)制構(gòu)造函數(shù)
Size=strlen(b.s);
s=new char[Size+1];
strcpy(s,b.s);
}
~myString(){ //析構(gòu)函數(shù)
delete []s;
Size=0;
}
int size(){ //返回字符串長度
return Size;
}
int set(char str[]){ //拷貝字符串str
Size=strlen(str);
s=new char[Size+1];
strcpy(s,str);
return Size;
}
myString& operator= ( const myString &b ){ //重載運算符"="
Size=b.Size;
s=new char[Size+1];
strcpy(s,b.s);
return *this; //賦值運算,返回給自身this
}
myString operator+ ( const myString &b ){ //重載運算符"+"
myString m;
int l=Size+b.Size;
m.s=new char[l+1];
m.Size=l;
strcat(s,b.s);
strcpy(m.s,s);
return m; //返回一個新對象的字符串
}
myString& operator+= ( const myString &b ){ //重載運算符"+="
myString m;
int l=Size+b.Size;
m.s=new char[l+1];
m.Size=l;
strcat(s,b.s);
strcpy(m.s,s);
*this=m;
return *this; //相加完的字符串返回給自身this
}
char &operator[] (int l){ //重載運算符"[]"
return s[l]; //返回字符串
}
myString sub( int start, int l ){
//返回一個 myString 對象m,其數(shù)據(jù) s 為當(dāng)前對象的 s[start, start + l] 范圍內(nèi)的字符
myString m;
m.s=new char[l+1];
m.Size=l;
for(int i=0;i<l;i++)
m.s[i]='\0';
int j=0;
for(int i=start-1;(i<start+l-1)&&(i<Size);i++,j++){
m.s[j]=s[i];
}
return m;
}
void output(){ //輸出字符串
cout<<s<<endl;
}
};
int main()
{
myString tmp;
cout << tmp.size() << endl; // 預(yù)期輸出:0
char t[20]; //重載"[]"
strcpy(t,"Hello");
tmp += t;
cout << t << endl; // 預(yù)期輸出:Hello
char s1[] = "abcd";
myString a( s1 );
a.output(); // 預(yù)期輸出:abcd
cout << a.size() << endl; // 預(yù)期輸出:4
char s2[] = "1234";
myString b( s2 );
b.output(); // 預(yù)期輸出:1234
myString c;
c = a; // 運算符"="重載
c.output(); // 預(yù)期輸出:abcd
cout << c.size() << endl; // 預(yù)期輸出:4
c = a + b; //運算符“+ ”重載
c.output(); // 預(yù)期輸出:abcd1234
cout << c.size() << endl; // 預(yù)期輸出:8
myString d(c); //復(fù)制構(gòu)造函數(shù)
d.output(); // 預(yù)期輸出:abcd1234
d += c; //運算符“+= ”重載
d.output(); //預(yù)期輸出:abcd1234abcd1234
b = c.sub( 3, 4 );
b.output(); // 預(yù)期輸出:cd12
b = c.sub( 5, 10 ); // 從第5個位置至末尾,最多只有4個字符,故取完這4個字符即結(jié)束
b.output(); // 預(yù)期輸出:1234
char s3[] = "6666666666";
c.set( s3 );
c.output(); // 預(yù)期輸出10個6
return 0;
}
6-25 編寫一個 3×3 矩陣轉(zhuǎn)置的函數(shù),在 main()函數(shù)中輸入數(shù)據(jù)。
#include <iostream>
#include <string>
using namespace std;
int a[3][3];
void zhuanzhi(int a[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<i;j++){
int k=a[i][j];
a[i][j]=a[j][i];
a[j][i]=k;
}
}
int main()
{
cout<<"請輸入一個3*3的矩陣:"<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>a[i][j];
zhuanzhi(a);
cout<<"轉(zhuǎn)置后矩陣為:"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
6-26 編寫一個矩陣轉(zhuǎn)置的函數(shù),矩陣的行數(shù)和列數(shù)在程序中由用戶輸入。
#include<iostream>
using namespace std;
void exchange(int *p1,int *p2,int r,int c)
{
int i,j;
cout<<"輸入的矩陣為:"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++)
cin>>p1[i*c+j];
}
for(i=0;i<c;i++)
for(j=0;j<r;j++)
p2[i*r+j]=p1[i+c*j]; //矩陣轉(zhuǎn)置;這里的i j分別代表轉(zhuǎn)置后的行列
cout<<"轉(zhuǎn)置后的矩陣為:"<<endl;
for(i=0;i<c;i++){ //輸出矩陣
for(j=0;j<r;j++)
cout<<p2[i*r+j]<<' ';
cout<<endl;
}
}
int main(){
int r,c;
cout<<"請輸入矩陣行數(shù)、列數(shù):"<<endl;
cin>>r>>c;
int *p3=new int[r*c];
int *p4=new int[r*c];
exchange(p3,p4,r,c);
delete p3;
delete p4;
return 0;
}
6-27 定義一個 Employee 類,其中包括表示姓名、地址、城市和郵編等屬性,包括setName()和display()等函數(shù)。display()使用cout語句顯示姓名、地址、城市和郵編等屬性,函數(shù) setName()改變對象的姓名屬性,實現(xiàn)并測試這個類。
#include <iostream>
#include <cstring>
using namespace std;
class Employee
{
private:
char name[50];
char street[60];
char city[60];
char zip[80];
public:
Employee(const char *n, const char *str, const char *ct, const char *z);
void change_name(const char *n);
void display();
};
Employee::Employee(const char *n, const char *str, const char *ct, const char *z)
{
strcpy(name,n);
strcpy(street,str);
strcpy(city,ct);
strcpy(zip,z);
}
void Employee::change_name(const char *n)
{
strcpy(name,n);
}
void Employee::display()
{
cout << name << " " << street << " "<< city << " " << zip;
}
int main()
{
Employee x("美國隊長", "碧桂園2號樓", "地球", "34534534");
x.display();
cout << endl;
x.change_name("鋼鐵俠");
x.display();
cout << endl;
return 0;
}
其中,構(gòu)造函數(shù)Employee和成員函數(shù)change_name()的形參都是使用指針來進行傳遞,類型const char *表示傳入的形參為字符串常量,是無法改變的。對字符型的私有成員進行初始化賦值時,使用的是<cstring>頭文件中的strcpy()函數(shù)。
此外,構(gòu)造函數(shù)Employee的形參可以使用指針,也可以不使用。但使用指針可以提高程序的編譯效率和執(zhí)行速度,使程序更加簡潔,也實現(xiàn)動態(tài)內(nèi)存分配,減少內(nèi)存的消耗??梢灾苯硬倏v內(nèi)存地址,從而可以完成和匯編語言類似的工作。
6-28 分別將例6-10程序和例6-16程序中對指針的所有使用都改寫為之等價的引用形式,比較修改前后的程序,體會在哪些情況下使用指針更好,哪些情況下使用引用更好。
//例6-10程序改寫
#include<iostream>
using namespace std;
void splitFloat(float x,int &intPart,float &fracPart)
{
intPart = static_cast<int>(x);
fracPart = x-intPart;
}
int main()
{
cout<<"Enter 3 float point numbers:"<<endl;
for(int i=0;i<3;i++){
float x,f;
int n;
cin>>x;
splitFloat(x,n,f);
cout<<"Integer Part="<<n<<" Fraction Part="<<f<<endl;
}
return 0;
}
//例6-16程序改寫
#include<iostream>
using namespace std;
class Point{
public:
Point():x(0),y(0){
cout<<"Default Constructor called."<<endl;
}
Point(int x,int y):x(x),y(y){
cout<<"Constructor called."<<endl;
}
~Point(){ cout<<"Destructor called."<<endl;}
int getX() const{ return x; }
int getY() const{ return y; }
void move(int newX,int newY){
x=newX;
y=newY;
}
private:
int x,y;
};
int main()
{
cout<<"Step one:"<<endl;
Point &ptr1 = *new Point;
delete &ptr1;
cout<<"Step two:"<<endl;
ptr1 = *new Point(1,2);
delete &ptr1;
return 0;
}
指針和引用的對比與分析:
- 引用是一個別名,不能為NULL值,初始化指定被引用的對象后不能被重新分配了。引用本身(而非被引用的對象)的地址是不可以獲得的,是被隱藏的。引用一經(jīng)定義初始化后,對于它的全部行為,全是針對被引用對象的;
- 指針是一個存放地址的變量。它可以被多次賦值,當(dāng)需要的對變量重新賦以另外的地址或賦值為NULL時只能使用指針。
- 指針能進行動態(tài)內(nèi)存分配和管理,減少內(nèi)存的消耗。地址是變量的門牌號,指針傳遞大型數(shù)據(jù)的地址而不是傳值,就像快捷方式一樣可以省空間,省時間,提高了效率。
- 通過指針被調(diào)用函數(shù)可以向調(diào)用函數(shù)處返回除正常的返回值之外的其他數(shù)據(jù),從而實現(xiàn)兩者間的雙向通信。
- 更容易實現(xiàn)函數(shù)的編寫和調(diào)用。
- 如果想要創(chuàng)建一些表示和實現(xiàn)各種復(fù)雜、大型的數(shù)據(jù)結(jié)構(gòu)或者動態(tài)分配空間時,那么使用指針可以編寫出更加高質(zhì)量的程序,更靈活地分配好空間,改善某些子程序的效率。
- 但對于簡單小型的數(shù)據(jù)雙向傳遞、減少大對象的參數(shù)傳遞開銷這兩個用途來說,引用可以很好地代替指針,它不需要像指針那樣需要在主調(diào)函數(shù)中用“&”操作符傳遞函數(shù),在被調(diào)函數(shù)中用“*”操作符使來訪問數(shù)據(jù),用引用比指針更加簡潔、安全。
?哦,對了,別忘記……
本專欄為本人大二C++課程的習(xí)題作業(yè)和一些學(xué)習(xí)經(jīng)驗的分享,供大家參考學(xué)習(xí)。如有侵權(quán)請立即與我聯(lián)系,我將及時處理。
參考書籍為:C++語言程序設(shè)計 第五版 -清華大學(xué)出版社- 鄭莉,董淵、C++語言程序設(shè)計 第五版 -清華大學(xué)出版社- 鄭莉,董淵(學(xué)生用書)
編譯環(huán)境:Visual Studio 2019、Dev-C++文章來源:http://www.zghlxwxcb.cn/news/detail-425079.html
歡迎關(guān)注我的微信公眾號,分享一些有趣的知識:程序猿的茶水間文章來源地址http://www.zghlxwxcb.cn/news/detail-425079.html
到了這里,關(guān)于C++語言程序設(shè)計第五版 - 鄭莉(第六章課后習(xí)題)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!