C++实验1 简单程序开发.docx

上传人:b****1 文档编号:12518514 上传时间:2023-04-19 格式:DOCX 页数:25 大小:123.92KB
下载 相关 举报
C++实验1 简单程序开发.docx_第1页
第1页 / 共25页
C++实验1 简单程序开发.docx_第2页
第2页 / 共25页
C++实验1 简单程序开发.docx_第3页
第3页 / 共25页
C++实验1 简单程序开发.docx_第4页
第4页 / 共25页
C++实验1 简单程序开发.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

C++实验1 简单程序开发.docx

《C++实验1 简单程序开发.docx》由会员分享,可在线阅读,更多相关《C++实验1 简单程序开发.docx(25页珍藏版)》请在冰豆网上搜索。

C++实验1 简单程序开发.docx

C++实验1简单程序开发

实验1简单程序开发

班级学号姓名成绩

一、实验目的

1.掌握C++的基本语法及程序基本结构,了解C++中的数据类型,常量与变量。

2.了解基本数据输入与输出方法。

3.初步掌握变量的引用。

4.掌握C++中函数的定义、调用以及主调函数和被调函数之间的数据传递规则。

5.掌握结构成员的访问、结构赋值的含义以及结构与指针、函数的关系。

6.掌握程序设计方法和应用。

二、实验内容

1.编写函数将字符串按逆序存放。

2.编写函数实现字符串拷贝功能。

3.编写函数将用户输入的一个十进制数转换成二、八、十六进制数。

4.利用重载编写求整数绝对值和求实数绝对值两个函数。

5.用条件编译方法实现下列功能:

输入一行电报文字,可以任意选择两种输出,按原文输出或将字母变成其下一个字母(例’a’变成’b’……’z’变成’a’,其他字符不变)。

用#define命令来控制是否要译成密码。

例如,#defineREAD1则输出密码。

#defineREAD0则不输出密码,按原字符输出。

6.定义学生成绩结构体,含有学号,英语,数学,物理,总分。

要求:

1)编写input函数,输入全班学号,英语,数学,物理成绩,并计算总分。

2)编写sort函数,按全班同学总分从高到低排序。

3)编写out函数,输出全班同学学号,总分。

主函数中声明学生成绩数组,调用上述三个函数。

7.编写将两个已知的有序链表合并为一个有序链表的函数。

8.编写从无序的整数链表中找出最大元素,并将此最大元素从链表中删除的函数。

三、实验源程序、测试与结论

1.

#include

#include

usingnamespacestd;

voidnixu(chara[]);

voidmain()

{

charstr[50];

cout<<"pleaseinputastring:

"<

cin>>str;

nixu(str);

cout<

}

voidnixu(chara[])

{

chartemp;

inti;

intlen=strlen(a);

for(i=0;i

{

temp=a[i];

a[i]=a[len-1-i];

a[len-1-i]=temp;

}

}

2.

#include

#include

usingnamespacestd;

voidcopy(chara[],charb[]);

voidmain()

{

charstr1[100],str2[50];

cout<<"pleaseinputastring:

"<

cin>>str1;

cout<<"pleaseinputanotherstring:

"<

cin>>str2;

copy(str1,str2);

}

voidcopy(chara[],charb[])

{

intlen1,len2,i;

len2=strlen(b);

len1=strlen(a);

for(i=0;i

{

a[i+len1]=b[i];

}

a[len1+len2]='\0';

cout<

}

3.

#include

usingnamespacestd;

voidtransB(intnum)

{

charB[30],D[30];

intn,i;

for(i=0;num!

=0;i++)

{D[i]=num%2+48;

num=num/2;

}

B[i]='\0';

n=i;

for(i=0;i

B[n-1-i]=D[i];

cout<<"\t二进制为:

"<

}

voidtransO(intnum)

{charO[10],D[10];

intn,i;

for(i=0;num!

=0;i++)

{D[i]=num%8+48;

num=num/8;

}

O[i]='\0';

n=i;

for(i=0;i

O[n-1-i]=D[i];

cout<<"\t八进制为:

"<

}

voidtransH(intnum)

{charH[10],D[10];

intn,i;

for(i=0;num!

=0;i++)

{if(num%16==10)

D[i]='A';

elseif(num%16==11)

D[i]='B';

elseif(num%16==12)

D[i]='C';

elseif(num%16==13)

D[i]='D';

elseif(num%16==14)

D[i]='E';

elseif(num%16==15)

D[i]='F';

else

D[i]=num%16+48;

num=num/16;

}

H[i]='\0';

n=i;

for(i=0;i

H[n-1-i]=D[i];

cout<<"\t十六进制为:

"<

}

voidmain()

{

intn;

cout<<"\t请输入十进制数:

";

cin>>n;

transB(n);

transO(n);

transH(n);

}

4.#include

usingnamespacestd;

intabs(inta)

{

intx;

x=a;

if(x<0)

return((-1)*(x));

elsereturn(x);

}

floatabs(floata)

{

floatx;

x=a;

if(x<0)

return((-1.0)*(x));

elsereturn(x);

}

voidmain()

{

intx;

floaty;

cout<<"请输入一个整数:

";

cin>>x;

cout<<"您输入的数值"<

"<

cout<<"请输入一个实数:

";

cin>>y;

cout<<"您输入的数值"<

"<

}

5.

#include

#include

#defineREAD1

main()

{

chara[80];

inti=0;

cout<<"请输入一行电报文字(只改变字母):

"<

gets(a);

while(a[i]!

='\0')

{

#ifREAD

if(a[i]=='z'||a[i]=='Z')

a[i]=a[i]-25;

elseif(a[i]>='a'&&a[i]<'z'||a[i]>='A'&&a[i]<'Z')

a[i]=a[i]+1;

#endif

i++;

}

cout<<"输出改变后的密码:

"<

}

6.

#include

#defineMAX80

usingnamespacestd;

structstudent

{

intnum;

doubleeng;

doublemath;

doublephy;

doubletotal;

};

studentstu[MAX],cstu;

voidinput(intj)

{

inti;

for(i=0;i

{

cout<<"请输入第"<

";

cin>>stu[i].num;

cout<<"请输入第"<

";

cin>>stu[i].eng;

cout<<"请输入第"<

";

cin>>stu[i].math;

cout<<"请输入第"<

";

cin>>stu[i].phy;

stu[i].total=stu[i].eng+stu[i].math+stu[i].phy;

}

}

intsort(intj)

{

inti,m;

for(i=j-1;i>0;i--)

for(intm=0;m

{

if(stu[m].total

{

cstu=stu[m];

stu[m]=stu[i];

stu[i]=cstu;

}

}

return1;

}

voidout(intj)

{

inti;

cout<<"学号\t总分"<

for(i=0;i

cout<

}

intmain()

{

inta;

cout<<"请输入学生人数:

";

cin>>a;

input(a);

sort(a);

out(a);

}

7.

#include

usingnamespacestd;

structLNode

{

intdata;

LNode*next;

};

LNode*LNodeCreate()

{

LNode*a,*b,*c;

inti,m,n;

cout<<"请输入链式线性表元素的个数:

";

cin>>n;

a=c=newLNode;

cout<<"请按从小到大输入线性表的"<

";

for(i=0;i

{

b=newLNode;

cin>>m;

b->data=m;

c->next=b;

c=b;

}

c->next=NULL;

c=a;

returnc;

}

voidLNodeSort(LNode*L1,LNode*L2)

{

LNode*L3;

LNode*pa,*pb,*pc;

pa=L1->next;

pb=L2->next;

L3=pc=L1;

while(pa&&pb)

{

if(pa->data<=pb->data)

{

pc->next=pa;

pc=pa;

pa=pa->next;

}

else

{

pc->next=pb;

pc=pb;

pb=pb->next;

}

}

if(pc->next=pa)

pc->next=pa;

else

pc->next=pb;

delete(L2);

cout<<"排列后的线性表为:

";

for(pc=L3->next;pc!

=NULL;pc=pc->next)

cout<data<<"";

cout<

}

intmain()

{

LNode*L1,*L2;

L1=LNodeCreate();

L2=LNodeCreate();

LNodeSort(L1,L2);

}

8.

#include

usingnamespacestd;

typedefstructLNode

{

intdata;

structLNode*next;

}LNode;

LNode*head;

voidMaxDel(LNode*head)

{

LNode*p,*a[2];

intb;

p=head;

b=p->next->data;

a[0]=p;

a[1]=p->next;

for(p;p->next!

=NULL;p=p->next)

{

if(p->next->data>b)

{

b=p->next->data;

a[0]=p;

a[1]=p->next;

}

}

cout<<"该链表中最大值为:

"<

a[0]->next=a[1]->next;

delete(a[1]);

p=head;

cout<<"删除该链表中最大值后,该链表为:

"<

while(p->next!

=NULL)

{

cout<next->data<<'';

p=p->next;

}

cout<

}

intmain()

{

LNode*s,*p;

inti,n;

head=newLNode;

head->next=NULL;

p=head;

cout<<"请输入链式线性表元素的个数:

";

cin>>n;

cout<<"请随意输入线性表的"<

";

for(i=0;i

{

s=newLNode;

cin>>s->data;

p->next=s;

s->next=NULL;

p=s;

}

cout<<"该链表为:

"<

p=head;

while(p->next!

=NULL)

{

cout<next->data<<'';

p=p->next;

}

cout<

MaxDel(head);

}

四、实验小结

本次试验让我了解到了c++与基本c语言不同的地方,以及初步掌握了变量的引用!

还有熟悉了函数的定义和调用,以及赋值和被调函数之间的数据传递规则!

学习了成员的访问,结构赋值的含义以及结构与指针,函数的关系!

c++更多偏向的是面向对象的设计!

而c则更多偏向面向程序的设计!

两者共通,但是也有很多的不同之处!

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

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1