实验二 c程序设计基础练习new.doc

上传人:bao****ty 文档编号:144915336 上传时间:2020-09-14 格式:DOC 页数:7 大小:60KB
返回 下载 相关 举报
实验二 c程序设计基础练习new.doc_第1页
第1页 / 共7页
实验二 c程序设计基础练习new.doc_第2页
第2页 / 共7页
实验二 c程序设计基础练习new.doc_第3页
第3页 / 共7页
实验二 c程序设计基础练习new.doc_第4页
第4页 / 共7页
实验二 c程序设计基础练习new.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《实验二 c程序设计基础练习new.doc》由会员分享,可在线阅读,更多相关《实验二 c程序设计基础练习new.doc(7页珍藏版)》请在金锄头文库上搜索。

1、实验二 C+程序设计基础练习1实验目的(1)学习编写简单的C+程序,并掌握C+程序的基本格式与规范。(2)理解C+程序结构的特点。(3)学习C+程序基本的输入输出操作。(4)学习数据类型常量和变量以及修饰符的使用。(5)学习基本的函数定义与调用方法。(6)学习作用域运算符的功能与基本使用方法。(7)学习内存的动态分配与释放方法。(8)学习引用的概念,掌握引用的基本使用方法。2实验基本要求 (1)输入并运行所给的参考程序1,并将程序中的注释部分也输入计算机,体会和理解程序的基本格式规范。(2)编制一个完整的包含输入和输出的简单C+程序,如参考程序2和参考程序3。掌握输入输出及其格式控制的基本使用

2、方法。(3)输入并调试参考程序4和参考程序5,体会和理解内置函数与函数重载的概念与基本使用方法。 (4)输入并调试参考程序6和参考程序7。体会和理解作用域运算符的概念与基本使用方法。 (5)输入并调试参考程序8和参考程序9,体会和理解内存动态分配的概念与new、delete运算符的基本使用方法。3实验基本步骤 (1)建立一个控制台应用程序项目baseform1,向其中添加个源程序文件sum .cpp。按照所给的程序代码输入到计算机中,检查和调试程序,在确认没有发现错误之后,编译、链接、运行程序并观察输出结果。若有问题,则需要重新检查程序。说明:在C+程序中如果使用了系统提供的些功能(如cin和

3、cout),就必须在程序的首部首先声明相关的包含这些功能的头文件(如iostream.h),否则,系统无法找到实现这些功能的代码。在C+语言中个头文件就是一个类库,类与类的预定义对象就包含在其中。所以,在使用这些类库中所包含的预定义对象时,必须在程序首部加以声明。/参考程序1/sum.cpp#include int add(int a ,int b);int main() int x,y,sum; coutEnter two numbers:x; ciny; sum=add(x,y); coutThe sum is:sumn; return 0;int add(int a,int b) int

4、 c; c=a+b; return c; 运行结果:Enter two numbers:1 2The sum is:3Press any key to continue(2)按照参考程序l的输入与调试方法,输入、检查、调试和运行参考程序2和参考程序3,掌握输入输出及其格式控制的基本使用方法。/参考程序2#include int main() char name20; coutname; coutname;return 0;运行结果:Hello,your name: JacksonJacksonPress any key to continue/参考程序3#include int main()

5、int x=25; couthexx decx octxn;运行结果:19 25 31Press any key to continue(3)按照参考程序1的输入与调试方法,输入、检查、调试和运行参考程序4和参考程序5。理解内置函数与函数重载的概念与基本使用方法。/参考程序4#include inline int doub(int x)return x*2;int main() for(int i=1;i3;i+) couti”doubled is ”doub(i)endl; cout”1+2 doubled is ”doub(1+2)endl;运行结果:1doubled is 22doubl

6、ed is 41+2 doubled is 6Press any key to continue/参考程序5#include int mul(int x,int y)return x*y;int mul(int x,int y,int z)return x*y*z;void main()int a=3,b=4,c=5;couta*b=mul(a,b)endl;couta*b*c=mul(a,b,c)endl;运行结果:3*4=123*4*5=60Press any key to continue(4)按照参考程序1的输入与调试方法,输入、检查、调试和运行参考程序6和参考程序7,并观察输出结果,

7、体会和理解作用域运算符的概念与基本使用方法。/参考程序6#include int avar=10;main() int avar; avar=25; cout”avar is ”avarendl; return 0;运行结果:avar is 25Press any to continue/参考程序7#include int avar;main()int avar;avar=25; /局部变量avar:avar=10; /全局变量avar cout”local avar=”avarendl;cout”global avar=”:avarendl;return 0;运行结果:local avar=

8、25global avar=10Press any key to continue(5)按照参考程序1的输入与调试方法,输入、检查、调试和运行参考程序8和参考程序9,并观察输出结果,体会和理解内存动态分配的概念与new、delete运算符的基本使用方法。/参考程序8#include main()int *p; /声明一个整型指针变量p p=new int; /动态分配一个int型存储区,并将首地址赋给p*p=10;cout*p;delete p; /撤销指针,释放p指向的存储空间return 0;运行结果:10Press any key to continue/参考程序9#include ma

9、in() int *p; p=new int; if(!p) cout”allocation failuren”; return 1;*p=20;cout*p;delete p;return 0;运行结果:20Press any key to continuep46 习题 : 2.182.20、2.222.252.18运行结果101Press any key to continue2.19运行结果1010Press any key to continue2.20运行结果10Press any key to continue2.22程序#include main()int *p80,i;p0=n

10、ew int;*p0=1;p1=new int;*p1=1;for(i=2;i20;i+)pi=new int; *pi=*pi-1+*pi-2;for(i=0;i20;i+)cout*piendl;return 0;2.23程序#include #includeint sroot(int a)return sqrt(a);int sroot(long int a)return sqrt(a);double sroot(double a)return sqrt(a);main()int a=4;long int b=12345;double c=0.25;coutsroot(a)endl;co

11、utsroot(b)endl;coutsroot(c)endl;return 0;2.24 程序#include using namespace std;int main()int i,j,k;int sum=0;for(i=0;i=100;i+)for(j=0;j=50;j+)for(k=0;k=20;k+)if(i*1+2*j+5*k=100)sum+;cout总数为:sumendl;return 0;225程序#include int fun(int &a,int &b)int c,temp; c=a-b; if(c=0) temp=a; a=b; b=temp;return 0;main()int a,b;cinab;fun (a,b);coutabendl;return 0;4. 实验结论:(1)学习了编写简单的C+程序,并掌握了C+程序的基本格式与规范。(2)理解了C+程序结构的特点。(3)学习了C+程序基本的输入输出操作。(4)学习了数据类型常量和变量以及修饰符的使用。(5)学习了基本的函数定义与调用方法。(6)学习了作用域运算符的功能与基本使用方法。(7)学习了内存的动态分配与释放方法。(8)学习了引用的概念,掌握了引用的基本使用方法。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号