第一次实验参考答案.doc_第1页
第一次实验参考答案.doc_第2页
第一次实验参考答案.doc_第3页
第一次实验参考答案.doc_第4页
第一次实验参考答案.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、实验目的1了解在面向过程程序设计中C+对C语言的扩充与增强,并善于在编写程序过程中应用这些新的功能。2熟悉在所用的VC+集成开发环境下编辑、编译、连接和运行C+程序的方法。3熟悉C+程序的结构和编程方法。二、实验内容1请先阅读下面的2个程序,写出程序运行的结果,然后再上机运行程序,验证自己分析的结果是否正确。 程序1:#include /包含头文件命令using namespace std;/使用名字空间stdint main()coutThis is ;couta C+ ;cout program.endl;return 0;程序2:#include /包含头文件命令using namespace std;/使用名字空间stdint main()int a,b,c; /定义三个变量a、b、ccoutPlease input to a and b:ab;/等待用户从键盘输入数据 c=a+b;cout a+b=;coutc;coutendl;return 0;2输入以下程序,进行编译,观察编译结果,如果有错误,请修改程序,再进行编译,直到没有错误,然后进行连接和运行,并分析运行结果。#include /包含头文件命令using namespace std;/使用名字空间stdint main()int a,b; /定义两个变量a和b coutPlease input to a and b: ab;/等待用户从键盘输入数据cout Max= max(a,b) y)?x:y; /求两个数中的大者的函数/参考代码方法一:#include /包含头文件命令using namespace std;/使用名字空间stdint main()int max( int x , int y ) ;/方法一:添加函数声明int a,b; /定义两个变量a和b coutPlease input to a and b: ab;/等待用户从键盘输入数据cout Max= max(a,b) y) ?x:y; /求两个数中的大者的函数方法二:#include /包含头文件命令using namespace std;/使用名字空间stdint max(int x, int y) return (x y) ?x:y; /求两个数中的大者的函数,函数声明放于主函数前定义int main()int a,b; /定义两个变量a和b coutPlease input to a and b: ab;/等待用户从键盘输入数据cout Max= max(a,b)endl;/输出结果信息至屏幕return 0;/主函数返回0至操作系统3编写一个函数,用来求2个或3个正整数中的最大数,并在主函数中调用此函数。(1) 用不带默认参数的函数实现。(2) 用带默认参数的函数实现。并总结函数重载和带默认参数值的函数。参考代码:/函数重载#include using namespace std;int max(int x,int y) int z; if(xy) z=x; else z=y; return(z);int max(int a,int b,int c) if (ba) a=b; if (ca) a=c; return a; int main() int a,b,c,result; cinabc; result=max(a,b); coutmax of (a ,b) is :resultendl;result = max( a, b ,c ) ;coutmax of (a ,b,c) is :resultendl; return 0;/默认参数值#include using namespace std;void max(int a = 8 ,int b = 4 ,int c = 9 ) int result ;if (ba) result=b; if (ca) result=c; coutmax of (a ,b,c) is :resultabc;max(); max(a); max(a,b); max( a, b ,c ) ; return 0;选作题:4.编写程序,用同一个函数名实现n个数据的升序排序,数据类型可以是整型、单精度型、字符串型。用函数模板实现。/冒泡算法#include int main( ) int a5 , i , j , t ; printf( Please input: ) ; for( i = 0 ; i 5 ; i+ ) scanf( %d , &ai ) ; for( j = 0 ; j 4 ; j+ ) for( i = 0 ; i ai+1 ) t = ai ; ai = ai+1 ; ai+1 = t ; printf( The sorted numbers:n ) ; for( i = 0 ; i 5 ; i+ ) printf( %3d , ai ) ; printf( n ) ; return 0 ;/参考代码#include /包含头文件命令#include using namespace std;/使用名字空间stdtemplate void Bubble_Sort( T a , int n )int i , j ;T t; for( j = 0 ; j n - 1 ; j+ ) for( i = 0 ; i ai+1 ) t = ai ; ai = ai+1 ; ai+1 = t ; int main( )int a5 = 5 , 3 , 7 , 4 , 6, i ;float b5 = 5.5 , 6.1, 8.7 , 3.9 , 9.6; string s5 = aaa , b124 , efeg1245 , daojdofa , 232sdads; Bubble_Sort(a , 5 );cout整数排序之后数据是:;for( i = 0 ; i 5 ; i+ ) coutai ;coutendl; Bubble_Sort(b , 5 );cout实数排序之后数据

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论