C++上機實驗報告材料實驗四_第1頁
C++上機實驗報告材料實驗四_第2頁
C++上機實驗報告材料實驗四_第3頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗四數(shù)組、指針與字符串1. 實驗目的1. 學習使用數(shù)組2. 學習字符串數(shù)據(jù)的組織和處理3學習標準C+庫的使用4. 掌握指針的使用方法5. 練習通過 Debug 觀察指針的容及其所指的對象的容6. 聯(lián)系通過動態(tài)存分配實現(xiàn)動態(tài)數(shù)組,并體會指針在其中的作用7分別使用字符數(shù)組和標準C+庫練習處理字符串的方法2. 實驗要求1編寫并測試3*3矩陣轉置函數(shù),使用數(shù)組保存3*3矩陣。2. 使用動態(tài)存分配生成動態(tài)數(shù)組來重新完成上題,使用指針實現(xiàn)函數(shù)的功能。3編程實現(xiàn)兩字符串的連接。要求使用字符數(shù)組保存字符串,不要使用系統(tǒng)函數(shù)。4使用string類定義字符串對象,重新實現(xiàn)上一小題。5. 定義一個Employee

2、類,其中包括、街道地址、城市和郵編等屬性,以及change_name()和 display。等函數(shù)。Display。顯示、街道地址、城市和郵編等屬性,change_name()改變對象的屬 性。實現(xiàn)并測試這個類。6. 定義包含5個元素的對象數(shù)組,每個元素都是Employee類型的對象。7. (選做)修改實驗4中的選做實驗中的 people (人員)類。具有的屬性如下:char name11、編號 char number7、性別 char sex3、生日 birthday、號 char id16。其中"出生日期”定 義為一個“日期”類嵌對象。用成員函數(shù)實現(xiàn)對人員信息的錄入和顯示。要求包

3、括:構造函 數(shù)和析構函數(shù)、拷貝構造函數(shù)、聯(lián)成員函數(shù)、聚集。在測試程序中定義people 類的對象數(shù)組,錄入數(shù)據(jù)并顯示。3. 實驗容及實驗步驟1. 編寫矩陣轉置函數(shù),輸入?yún)?shù)為3*3整形數(shù)組,使用循環(huán)語句實現(xiàn)矩陣元素的行列對調, 注意在循環(huán)語句中究竟需要對哪些元素進行操作, 編寫 main ()函數(shù)實現(xiàn)輸入、 輸出。 程序 名: lab6_1.cpp。2. 改寫矩陣轉置函數(shù),參數(shù)為整型指針,使用指針對數(shù)組元素進行操作,在main ()函數(shù)中 使用 new 操作符分配存生成動態(tài)數(shù)組。通過 Debug 觀察指針的容及其所指的對象中的容。 程序名: lab6_2.cpp。3. 編程實現(xiàn)兩字符串的連接。

4、定義字符數(shù)組保存字符串,在程序中提示用戶輸入兩個字符串,實現(xiàn)兩個字符串的連接,最后用cout語句顯示輸出。程序名:lab6_3.cpp。用cin實現(xiàn)輸入,注意,字符串的結束標志是ASCII碼0,使用循環(huán)語句進行字符串間的字符拷貝。4. 使用 string 類定義字符串對象,編程實現(xiàn)兩字符串的連接。在 string 類中已重載了運算符“+=”實現(xiàn)字符串的連接,可以使用這個功能。程序名:lab6_4.cpp。5. 在employee.h文件中定義 Employee類。Employee類具有、街道地址、城市和郵編等私有數(shù)據(jù)成員,在成員函數(shù)中,構造函數(shù)用來初始化所有數(shù)據(jù)成員;display()中使用c

5、out顯示、街道地址、城市和郵編等屬性,change_name() 改變類中表示屬性的數(shù)據(jù)成員。在主程序中定義這個類的對象并對其進行操作。程序名: lab6_5.cpp 。6. 使用上一小題中定義的 Employee 類定義對象數(shù)組 emp5 ,使用循環(huán)語句把數(shù)據(jù)顯示出來。 程序名: lab6_6.cpp 。4. 思考題1.如何存儲和處理字符串? (1)可以利用字符數(shù)組存儲和處理字符串;( 2)利用系統(tǒng)提供的 string 類存儲和處理字符串。2頭文件<string.h>和頭文件<string>有何區(qū)別?包含頭文件<string.h>后,可以使用系統(tǒng)的字符串

6、處理函數(shù),如strcat (連接).strcpy (復制).strcmp (比較).strlen (求長度).strlwr (轉換為小寫).strupr (轉換為大寫)等等; 而包含頭文件<string>后,則可以定義string類,并且使用系統(tǒng)提供的string類操作符對string類型的對象進行處理。3. 有幾種方法來表示和處理數(shù)組元素?( 1)數(shù)組下標方法,如ai(2)指針的方法,如 int* p=&a05. 源程序#include<iostream>using namespace std;int a33;void showTrans()int i,j;c

7、out<<"The transposition matrix is:"<<endl;for(j=0;j<3;j+)for(i=0;i<3;i+)cout<<aij;cout<<" "cout<<endl;void input()int i,j; cout<<"Please input your 3*3 matrix:"<<endl;for(i=0;i<3;i+)for(j=0;j<3;j+)cin>>aij;int

8、main() input(); showTrans();return 0;#include<iostream> using namespace std;int* p9;void showTrans()int i,j;cout<<"The transposition matrix is:"<<endl; for(i=0;i<3;i+)for(j=i;j<9;j=j+3) cout<<*pj; cout<<" " cout<<endl;void input()int i,n;

9、cout<<"Please input your 3*3 matrix:"<<endl; for(i=0;i<9;i+)cin>>n;pi=new int(n);int main() input(); showTrans();int i; for(i=0;i<9;i+) delete pi;return 0;#include<iostream>/ 不使用系統(tǒng)自帶函數(shù) strcpy using namespace std;char a20="/0",b20="/0",c45=&

10、quot;/0"int main()int i,j,k; cout<<"Input the first string:"<<endl; cin.getline(a,20,'n');cout<<"Input the second string:"<<endl; cin.getline(b,20,'n');int m=0,n=0;/ 將數(shù)組 a 中的字符串拷貝到數(shù)組 c 中 for(k=0;k<45,am!='0'k+,m+) ck=am;/將數(shù)組

11、b中的字符串接著a,拷貝到數(shù)組c中 for(;k<45,bn!='0'k+,n+) ck=bn; cout<<c;return 0;4. lab6_4.cpp#include<iostream> #include<cstring> using namespace std; char a20="/0",b20="/0"int main()cout<<"Input the first string:"<<endl; cin.getline(a,20,'

12、;n');cout<<"Input the second string:"<<endl; cin.getline(b,20,'n');string s1=a;string s2=b;string s3=s1+s2; cout<<s3;return 0;5. Employee.h#ifndef Employee_H_INCLUDED #define Employee_H_INCLUDEDclass Employee private:char name15;char address25;char city10;int

13、postcode;public:Employee();Employee(char n,char a,char c,int p); Employee();void change_name();void change_address();void change_city();void change_postcode();void display();#endif / Employee_H_INCLUDEDEmployee.cpp#include<iostream> #include"Employee.h" using namespace std;Employee:E

14、mployee()Employee:Employee(char n,char a,char c,int p) name0=n;name1='0'address0=a;address1='0'city0=c;city1='0'postcode=p;Employee:Employee()void Employee:change_name()cout<<"Please input your changed name:"<<endl; cin.getline(name,15,'n');void

15、Employee:change_address()cout<<"Please input your changed address:"<<endl; cin.getline(address,25,'n');void Employee:change_city()cout<<"Please input your changed city:"<<endl; cin.getline(city,10,'n');void Employee:change_postcode()cout<

16、;<"Please input your changed postcode:"<<endl; cin>>postcode;void Employee:display()cout<<"Your information shows as follow:"<<endl; cout<<"name:"<<name<<endl; cout<<"address:"<<address<<endl; cout

17、<<"city:"<<city<<endl;cout<<"postcode:"<<postcode<<endl;int main()Employee person(1,1,1,1);person.display();person.change_name();person.change_address();person.change_city();person.change_postcode(); person.display();return 0;6. Employee.h#ifnd

18、ef Employee_H_INCLUDED #define Employee_H_INCLUDEDclass Employee private:char name15;char address25;char city10;int postcode;public:Employee();Employee(char n,char a,char c,int p); Employee();void change_name();void change_address();void change_city();void change_postcode(); void display();#endif /

19、Employee_H_INCLUDEDEmployee.cpp#include<iostream> #include"Employee.h" using namespace std;Employee:Employee()Employee:Employee(char n,char a,char c,int p) name0=n; name1='0'address0=a; address1='0'city0=c;city1='0'postcode=p;Employee:Employee()void Employee:c

20、hange_name()cout<<"Please input your changed name:"<<endl; cin.getline(name,15,'n');void Employee:change_address()cout<<"Please input your changed address:"<<endl; cin.getline(address,25,'n');void Employee:change_city()cout<<"Ple

21、ase input your changed city:"<<endl;cin.getline(city,10,'n');void Employee:change_postcode()cout<<"Please input your changed postcode:"<<endl; cin>>postcode;void Employee:display()cout<<"Your information shows as follow:"<<endl; co

22、ut<<"name:"<<name<<endl; cout<<"address:"<<address<<endl; cout<<"city:"<<city<<endl;cout<<"postcode:"<<postcode<<endl;int main()Employeeemp5=Employee(1,1,1,1),Employee(2,2,2,2),Employee(3,

23、3,3,3),Employee(4,4,4,4),Employee(5,5,5,5);int i;for(i=0;i<5;i+)empi.display();empi.change_name();empi.change_address();empi.change_city();empi.change_postcode(); cin.get(); for(i=0;i<5;i+)cout<<"The emp "<<i<<" " empi.display();return 0;7.#include<iost

24、ream> #include<cstring> using namespace std;/Date 類class Dateprivate:int year;int month;int day;public:Date();Date(int y,int m,int d);Date(Date &p);Date();void setDate();void showDate();/People 類,其中含 Date 類型的數(shù)據(jù) class Peopleprivate:char name11;char number7;char sex3;Date birthday; char i

25、d16;public: People();People(char* n,char* nu,char* s,Date b,char* i); People(People &p);People();void setName();void setNumber();void setSex();void setId();void showPeople();/Date 構造函數(shù)Date:Date()Date:Date(int y,int m,int d) year=y; month=m; day=d;Date:Date(Date &p)year=p.year;month=p.month;d

26、ay=p.day;/ 析構inline Date:Date()/Date 成員函數(shù),設置出生年月日void Date:setDate()int y,m,d; cout<<"Input the year:" cin>>y;cout<<"Input the month:" cin>>m;cout<<"Input the day:" cin>>d; year=y;month=m;day=d;/Date 聯(lián)成員函數(shù),輸出年月日 inline void Date:showD

27、ate() cout<<"Birthday is "<<year<<"年"<<month<<"月"<<day<<"日"<<endl;/People 構造函數(shù)People:People();People:People(char* n,char* nu,char* s,Date b,char* i) strcpy(name,n); strcpy(number,nu); strcpy(sex,s); birthday=b; s

28、trcpy(id,i);People:People(People &p)strcpy(name,); strcpy(number,p.number); birthday=p.birthday; strcpy(id,p.id);/People 析構 inline People:People()/People 成員函數(shù),設置各類數(shù)據(jù)void People:setName()cout<<"Please input the person's name:" cin.getline(name,11,'n');void People

29、:setNumber()cout<<"Input number:" cin.getline(number,7,'n');void People:setSex()cout<<"Input sex:" cin.getline(sex,3,'n');void People:setId()cout<<"Input id:" cin.getline(id,16,'n');/People 聯(lián)成員函數(shù),輸出人員信息 inline void People:showPe

30、ople() cout<<"Name:"<<name<<endl;cout<<"Number:"<<number<<endl;cout<<"Sex:"<<sex<<endl;cout<<"ID:"<<id<<endl;int main()int i;char spaceA;/ 生成 3 個 Date 類型的對象Date date3=Date(0,0,0),Date(0,0,

31、0),Date(0,0,0);/ 生成 3 個 People 類型的對象People person3=People("0","0","0",date0,"0"),People("0","0","0",date1,"0"),People("0","0","0",dat e2,"0");/設置這 3 個對象的各類信息for(i=0;i<3;i+)perso

32、ni.setName();personi.setNumber();personi.setSex();personi.setId();datei.setDate(); spaceA=getchar();/輸出這 3 個對象的各類信息for(i=0;i<3;i+)personi.showPeople(); datei.showDate();return 0;6. 運行結果2.3.4.Input the first string:abcl23Input the second string-:deg45Ga)jcl23deg456Process returned 0 (0x0) executio

33、n time : 6 _55Press anv kep to continue .Your informat ion shows as fo>11owbnanecity :postcade-1Please Input youir changed nane lizhihoPlease input oui*shisMC175Flcase input 胃uir shenvaingrPlease input yourchanged address =GhaRnyGdchancfed postcode»6.1234567Vouf infoi*mat ioin showsfollow:name lizhibo address - shisheC175 city-shenyangr postcode =1234567Ppoce? i*e|i4ifen|d 0 C0x0)9x9cut xon <r line 二 23 * 930 &Press anv key to continue.He em)i)0 ?ourinformationsiloesasfollou:njiine =01bifid. 1c ity:2 li

溫馨提示

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

評論

0/150

提交評論