C++程序设计实验报告_3.docVIP

  • 6
  • 0
  • 约2.12万字
  • 约 32页
  • 2023-10-11 发布于未知
  • 举报
C++程序设计实验报告 一、实验目的和要求 1、熟悉Visual C++ 6.0的集成开发环境 2、学会使用 Visual C++ 6.0编辑、编译、连接和运行C++单文件程序的方法。 3、初步勒戒C++源程序的基本结构,学会使用简单的输入输出操作; 4、了解C++在非面向对象方面对C语言功能的扩充与增强。 实验内容和步骤 : 1、编辑、编译、连接和运行C++单文件程序 //test2_1.cpp #includeiostream using namespace std; int main(){ coutHello!\n; coutthis is a program.endl; return 0; } 2、输入以下程序,进行编译,如果有错误,进行改正,直到没有错误,然后进行连接和运行。 (1)//test2_2_1_1.cpp #includeiostream using namespace std; int main(){ coutHello!\n; coutWelcome to C++!; }1 (2) //test2_2_2_1.cpp #includeiostream using namespace std; int main(){ int x,y; x=5; y=6; int z=x*y; coutx*y=zendl; return 0; } 程序没有错误,运行截图如下: (3) //test2_2_3_1.cpp #includeiostream using namespace std; int main(){ void fun(int,int); int x,y; fun(3,x); fun(4,y); coutx+y=x+yendl; return 0; } void fun(int m,int n){ n=m*5; } 程序没有错误,运行截图如下: 3、编写一个程序,用来分别求2个整数,3个整数,2个双精度数和3个双精度数的最大值。要求使用冲在函数来完成。 //test2_3_1.cpp #includeiostream using namespace std; void max(int x,int y){ if(xy) coutmax is xendl; else coutmax is yendl; } void max(int x,int y,int z){ int max; max=xy?x:y; max=maxz?max:z; coutmax is maxendl; } void max(double x,double y){ if(xy) coutmax is xendl; else coutmax is yendl; } void max(double x,double y,double z){ double max; max=xy?x:y; max=maxz?max:z; coutmax is maxendl; } int main(){ max(3,4); max(7,5,9); max(2.7,8.9); max(9.1,8.77,6.7); return 0; } 4、编写一个程序,任意从减半输入两个字符,能将他们按由大到小的顺序输出。要求程序中有一个交换两个字符的函数,其形参是变量的引用。 //test2_4_1.cpp #includeiostream using namespace std; void change(char a,char b){ char t; t=a; a=b; b=t; } void max(char a,char b){ char c,d; c=ab?a:b; d=ab?a:b; cout按由大到小的顺序输出是cdendl; } int main(){ char a,b; cout输入两个字符,按空格隔开endl; cina; cinb; cout你输入的字符是abendl; change(a,b); cout交换后的字符是abendl; max(a,b); return 0; } 5.编写一个程序,声明一个双精度型指针变量,使用运算符new动态分配一个double型存储区,将首地址赋给该指针变量,并输入一个数到该存储区中。计算以该数为半径的圆的面积,并在屏幕上显示出来,最后使用元算符delete释放该空间。 //test2_5_1.cpp #includeiostream using namespace std; void area(double x){ double s; s=x*x*3.14; cout以该半径为圆的面积是:sendl; } int ma

文档评论(0)

1亿VIP精品文档

相关文档