自学考试C++书上例题.docx

上传人:b****1 文档编号:672197 上传时间:2023-04-29 格式:DOCX 页数:36 大小:331.01KB
下载 相关 举报
自学考试C++书上例题.docx_第1页
第1页 / 共36页
自学考试C++书上例题.docx_第2页
第2页 / 共36页
自学考试C++书上例题.docx_第3页
第3页 / 共36页
自学考试C++书上例题.docx_第4页
第4页 / 共36页
自学考试C++书上例题.docx_第5页
第5页 / 共36页
自学考试C++书上例题.docx_第6页
第6页 / 共36页
自学考试C++书上例题.docx_第7页
第7页 / 共36页
自学考试C++书上例题.docx_第8页
第8页 / 共36页
自学考试C++书上例题.docx_第9页
第9页 / 共36页
自学考试C++书上例题.docx_第10页
第10页 / 共36页
自学考试C++书上例题.docx_第11页
第11页 / 共36页
自学考试C++书上例题.docx_第12页
第12页 / 共36页
自学考试C++书上例题.docx_第13页
第13页 / 共36页
自学考试C++书上例题.docx_第14页
第14页 / 共36页
自学考试C++书上例题.docx_第15页
第15页 / 共36页
自学考试C++书上例题.docx_第16页
第16页 / 共36页
自学考试C++书上例题.docx_第17页
第17页 / 共36页
自学考试C++书上例题.docx_第18页
第18页 / 共36页
自学考试C++书上例题.docx_第19页
第19页 / 共36页
自学考试C++书上例题.docx_第20页
第20页 / 共36页
亲,该文档总共36页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

自学考试C++书上例题.docx

《自学考试C++书上例题.docx》由会员分享,可在线阅读,更多相关《自学考试C++书上例题.docx(36页珍藏版)》请在冰点文库上搜索。

自学考试C++书上例题.docx

自学考试C++书上例题

例;1.5间接引用数组

#include

usingnamespacestd;

typedefdoublearray[10];

voidmain(){

arraya={12,34,56,78,90,98,76,85,64,43};

array&b=a;

a[2]=100;

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

cout<

}

例:

1.6数组升幂排序,复制,逆向和输出

#include

#include

usingnamespacestd;

voidmain(){

doublea[]={1.1,4.4,3.3,2.2},b[4];

copy(a,a+4,ostream_iterator(cout,""));

cout<

copy(a,a+4,b);

copy(b,b+4,ostream_iterator(cout,""));

cout<

sort(a,a+4);

copy(a,a+4,ostream_iterator(cout,""));

cout<

reverse_copy(a,a+4,b);

copy(b,b+4,ostream_iterator(cout,""));

cout<

}

例:

1.7数组降幂排序,查找和输出

#include

#include

#include

usingnamespacestd;

voidmain(){

doublea[]={1.1,4.4,3.3,2.2};

sort(a,a+4);

copy(a,a+4,ostream_iterator(cout,""));

cout<

sort(a,a+4,greater());

copy(a,a+4,ostream_iterator(cout,""));

cout<

double*x=find(a,a+4,4.4);

if(x==a+4)cout<<"没有值为4.4的数组元素";

elsecout<<"有值为"<<*x<<"的数组元素";

cout<

x=find(a,a+4,8);

if(x==a+4)cout<<"没有值为8的数组元素";

elsecout<<"有值为"<<*x<<"的数组元素";

}

例:

1.8对一维数组进行局部操作

#include

#include

#include

usingnamespacestd;

voidmain(){

doublea[]={1.1,4.4,3.3,2.2},b[8]={8};

copy(a+2,a+4,ostream_iterator(cout,""));//输出a[2]和a[3],copy(a,a+Len...

cout<

reverse_copy(a+1,a+4,ostream_iterator(cout,""));//输出a[3]~a[1]

cout<

copy(a,a+4,&b[4]);//将数组赋值到数组b的尾部

copy(b,b+8,ostream_iterator(cout,""));//输出数组b

cout<

sort(a+1,a+3);//对部分数组元素升幂排序

copy(a,a+4,ostream_iterator(cout,""));//输出数组a

cout<

sort(b,b+6,greater());//对部分数组元素降幂排序

copy(b,b+8,ostream_iterator(cout,""));//输出数组b

cout<

}

例:

1.9对字符数组进行操作

#include

#include

#include

usingnamespacestd;

voidmain(){

chara[]="wearehere!

",b[11];

reverse(a,a+10);//数组元素逆向

copy(a,a+10,ostream_iterator(cout));//输出逆向后的数组内容

cout<

copy(a,a+11,b);//原样赋值到数组b

sort(a,a+10);//默认升幂排序

cout<

cout<

reverse_copy(a,a+10,b);//逆向赋值到数组b

cout<

reverse(b+2,b+8);//数组b部分逆向

copy(b+2,b+8,ostream_iterator(cout));//输出数组b逆向后的部分内容

cout<

sort(a,a+10,greater());//降幂排序

cout<

cout<<(*find(a,a+10,'e')=='e')<<""

<<(*find(a,a+10,'o')=='o')<

}

例:

1.10使用setw设置输出宽度

#include

#include

usingnamespacestd;

voidmain(){

inta=29,b=1001;

cout<

291001

cout<

291001

}

例:

1.11在下面的程序中,当要求输入时,均输入100,给出程序的输出结果。

#include

#include

usingnamespacestd;

constdoublePI=3.141592;

voidmain(){

cout<

<

<

(1)<

<

(2)<

<

<

intb=100;

cout<<"Dec:

"<

<<"Hex:

"<

<<"Oct:

"<

cout<

<<100<

<<"Inputb=";

cin>>b;

cout<

:

showpos)<

cout<<"Inputb=";

cin>>b;

cout<

cout<

:

showpos);

cout<

}

例:

1.12分析下面程序的输出

#include

#include

usingnamespacestd;

voidmain(){

cout<

<

<

(1)<<15<

<

(2)<<15<

<

<

cout<

cout<

:

right)

<

<

<

cout<

:

right);

cout<

:

left)

<

<

<

}

习题1p27

1.分别用字符和ASCII码形式输出整数值65和66.

#include

usingnamespacestd;

voidmain(){

chara='A',b='B';

intascii_1=53,ascii_2=54;//ASCII码中的5和6

cout<<"字符输出:

"<<(int)a<<","<

cout<<"ASCII码输出:

"<<(char)ascii_2<<(char)ascii_1<<"";

cout<<(char)ascii_1<<(char)ascii_1<

}

2.编写一个为int型变量分配100个整型量空间的程序。

#include

usingnamespacestd;

voidmain(){

int*p;

p=newint[100];

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

{

*(p+i)=i;

}

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

{

cout<<*(p+i)<<"";

}

deletep;

}

3.编完完整的程序,它读入15个float值,用指针把它们存放在一个存数块里,然后输出这些值的和以及最小值。

#include

#include

usingnamespacestd;

voidmain(){

float*p;

p=newfloat[15];

cout<<"输入15个float类型的值:

"<

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

{

cin>>*(p+1);

}

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

{

cout<<*(p+i)<<",";

}

sort(p,p+15);

cout<<"\n最小的是:

"<<*(p)<

deletep;

}

4.声明如下数组:

inta[]={1,2,3,4,5,6,7,8};先查找4的位置,将数组a复制给数组b,然后将数组a的内容反转,再查找4的位置,最后分别输出数组a和b的内容。

#include

#include

#include

usingnamespacestd;

voidmain()

{

inta[]={1,2,3,4,5,6,7,8},b[8];

cout<<"数组a中'4'的位置是:

"<

copy(a,a+8,b);//将数组a制造给数组b

reverse_copy(b,b+8,a);//把数组b逆向复制给a,完成a的逆转

cout<<"数组a反转后,'4'的位置是:

"<

cout<<"数组a的内容:

"<

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

cout<

cout<<"\n数组b中的内容:

"<

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

cout<

}

例:

2.1使用成员函数的实例

#include

usingnamespacestd;

structPoint{

voidSetxy(doublea,doubleb)//成员函数,用来重新设置数据成员

{x=a;y=b;}

voidDisplay()//成员函数,按指定格式输出数据成员的值

{cout<

doublex,y;//数据成员

}

;

voidmain(){

Pointa;//定义对象a

a.Setxy(10.6,18.5);//设置对象a的数据成员

a.Display();//显示对象a的数据成员

cout<

}

例2.2使结构具有封装性的实例

#include

usingnamespacestd;

structPoint{

private:

doublex,y;//数据成员

public:

voidSetxy(doublea,doubleb)//成员函数,用来重新设置数据成员

{x=a;y=b;}

voidDisplay()//成员函数,按指定格式输出数据成员的值

{cout<

};

例2.3使用构造函数初始化对象的实例

#include

usingnamespacestd;

structPoint{

private:

doublex,y;//数据成员

public:

Point(){};

Point(doublea,doubleb)

{x=a;y=b;}

voidSetxy(doublea,doubleb)

{x=a;y=b;}

voidDisplay()

{cout<

};

voidmain(){

Pointa;

Pointb(18.5,10.6);

a.Setxy(10.6,18.5);

a.Display();

b.Display();

}

例:

2.4定义类的实例

#include

usingnamespacestd;

classpoint{

private:

doublex,y;//类point的数据成员

public:

point(){};//类point的无参数构造函数

point(doublea,doubleb)//具有两个参数的构造函数

{x=a;y=b;}

voidsetxy(doublea,doubleb)//成员函数,用来重新设置数据成员

{x=a;y=b;}

voiddiaplay()//成员函数,按指定格式输出数据成员的值

{cout<

};

例2.8演示使用string对象及初始化的例子

#include

#include

usingnamespacestd;

voidmain(){

stringstr1("wearehere!

");

stringstr2="whereareyou?

";

cout<

cout<

cout<<"pleaseinputaword:

";

cin>>str1;

cout<<"lengthofthe"<

}

例:

2.10演示使用complex和string对象及初始化的例子

#include

#include

#include

usingnamespacestd;

voidmain(){

complexnum1(2,3);

complexnum2(3.5,4.5);

stringstr1("realis");

stringstr2="iamgeis";

cout<

cout<

}

例2.11演示string对象的例子

#include

#include

#include

usingnamespacestd;

voidmain(){

stringstr1="wearehere!

",str2=str1;

reverse(&str1[0],&str1[0]+12);//str字符串的元素逆向

cout<

copy(&str1[0],&str1[0]+12,&str2[0]);//原样复制到str2

cout<

reverse_copy(&str2[0],&str2[0]+12,ostream_iterator(cout));//逆向输出str2

cout<

}

例:

演示string对象使用成员函数表示存储区间的例子

#include

#include

#include

#include

usingnamespacestd;

voidmain(){

stringstr1="wearehere!

",str2(str1);//使用str1初始化str2

reverse(str1.begin(),str1.end());//字符串的元素逆向

cout<

copy(str1.begin(),str1.end(),str2.begin());//原样复制到str2,str2应能容纳下str1

sort(str1.begin(),str1.end());//默认升幂排序

cout<

cout<

reverse_copy(str1.begin(),str1.end(),str2.begin());//逆向复制到str2

cout<

reverse(str2.begin()+2,str2.begin()+8);

copy(str2.begin()+2,str2.begin()+8,ostream_iterator(cout));//输出逆向后的部分内容

cout<

sort(str1.begin(),str1.end(),greater());//降幂排序

cout<

str1.swap(str2);//互相交换内容

cout<

cout<<(*find(str1.begin(),str1.end(),'e')=='e')<<""//注意不是成员函数find

<<(*find(str1.begin(),str1.end(),'0')=='0')<

}

例2.13演示string对象数组的例子

#include

#include

#include

usingnamespacestd;

voidmain(){

stringstr[]={"wearehere!

","whereareyou?

","welcome!

"};

for(inti=0;i<3;i++){

copy(str[i].begin(),str[i].end(),ostream_iterator(cout));

cout<

}

str[0].swap(str[2]);

str[0].swap(str[1]);

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

cout<

}

例:

3.1传对象不会改变原来对象数据成员值的例子

#include

#include

usingnamespacestd;

voidswap(string,string);//使用string类的对象作为函数参数

voidmain(){

stringstr1("现在"),str2("过去");//定义对象str1和str2

swap(str1,str2);//使用传值方式传递str1和str2的数据成员值

cout<<"返回后:

str1="<

}

voidswap(strings1,strings2)///string类的对象s1,s2作为函数参数

{

stringtemp=s1;s1=s2;s2=temp;

cout<<"交换为:

str1="<

}

例3.2使用对象指针作为函数参数的例子

#include

#include

usingnamespacestd;

voidswap(string*,string*);//使用string类的指针作为函数参数

voidmain(){

stringstr1("现在"),str2("过去");//定义对象str1和str2

swap(&str1,&str2);//使用传地址值方式传递str1和str2的地址值

cout<<"返回后:

str1="<

}

voidswap(string*s1,string*s2)//string类的对象指针s1和s2作为函数参数

{

stringtemp=*s1;*s1=*s2;*s2=temp;

cout<<"交换为:

str1="<<*s1<<"*str2="<<*s2<

}

例3.3传递数组名实例

#

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2