实验五模板与异常处理Word文档下载推荐.docx
- 文档编号:18353849
- 上传时间:2022-12-15
- 格式:DOCX
- 页数:32
- 大小:401.26KB
实验五模板与异常处理Word文档下载推荐.docx
《实验五模板与异常处理Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《实验五模板与异常处理Word文档下载推荐.docx(32页珍藏版)》请在冰豆网上搜索。
y;
}
intmax(inta,intb)
{returna>
b?
a:
b;
doublemax(doublea,doubleb)
intmain()
{cout<
<
”max(‘3’,’7’)is“<
max(‘3’,’7’)<
endl;
return0;
(2)
//test6_1_2.cpp
usingnamespacestd;
代码:
(1)
#include<
template<
Tmax(Tx,Ty)
{returnx>
x:
}
intmax(inta,intb)
{returna>
a:
doublemax(doublea,doubleb)
intmain()
{
cout<
"
max('
3'
'
7'
)is"
)<
return0;
实验结果:
(1)
定义了一个求最大值的函数模板,定义了一个求整型最大值的函数以及一个求双精度型最大值的函数,这两个同名非模板函数与函数模板重载.’3’和’7’均是字符型,找不到与之匹配的函数,所以调用了函数模板,使其实例化,由’T’型转变为’char’型。
定义了一个求整型最大值的函数以及一个求双精度型最大值的函数,这两个同名函数重载.’3’和’7’均是字符型,找不到与之匹配的函数,所以得不出正确结果。
2.编写一个求任意类型数组中最大元素和最小元素的程序,要求将求最大元素和最小元素的函数设计成函数模板。
#include<
template<
Tmax(T*x,intn)
Tmax=x[0];
for(inti=1;
i<
n;
i++)
max=max>
x[i]?
max:
x[i];
returnmax;
Tmin(T*x,intn)
{Tmin=x[0];
min=min<
min:
returnmin;
{inta[5]={1,4,2,3,5};
doubleb[5]={1.1,4.4,2.2,3.3,5.5};
数组a[5]={1,4,2,3,5}的最大值is:
"
max(a,5)<
数组a[5]={1,4,2,3,5}的最小值is:
min(a,5)<
数组b[5]={1.1,4.4,2.2,3.3,5.5}的最大值is:
max(b,5)<
数组b[5]={1.1,4.4,2.2,3.3,5.5}的最小值is:
min(b,5)<
3.编写一个程序,使用类模板对数组元素进行排序、倒置、查找和求和。
【提示】
设计一个类模板
classType>
classArray{
...
};
具有对数组元素进行排序、倒置、查找和求和功能,然后产生类型实参分别为int型和double型的两个模板类,分别对整型数组与双精度数组完成所要求的操作。
classArray1{
public:
Array1(Type*a,intlength)
{len=length;
for(inti=0;
len;
Array[i]=a[i];
}
//template<
voidsort()//排序
{
Typea[10],temp;
for(intn=0;
n<
n++)
a[n]=Array[n];
for(intj=0;
j<
j++)
for(inti=0;
len-1-j;
if(a[i]>
a[i+1])
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
cout<
数组排序"
for(intm=0;
m<
m++)
a[m]<
//template<
voidinvert()//倒置
Typeinvert[10];
invert[j]=Array[len-1-j];
数组倒置"
{cout<
invert[m]<
voidseek(Typey)//查找
inti=0;
if(Array[j]==y)
查找成功!
在第"
位置"
i=j;
//break;
if(i==0)
查找不成功!
voidsum()//求和
Typesum=Array[0];
for(intj=1;
sum=sum+Array[j];
数组之和:
sum<
private:
intlen;
TypeArray[10];
};
intc[5]={1,8,2,7,9};
doubled[4]={3.1,8.9,56.9,2.9};
Array1<
int>
a(c,5);
double>
b(d,4);
***int型数组***"
原函数为:
for(inti=0;
5;
c[i]<
;
a.sort();
a.invert();
a.seek(3);
a.sum();
***double型数组***"
for(intj=0;
4;
d[j]<
b.sort();
b.invert();
b.seek(8.9);
b.sum();
4.编写一个程序,求输入数的平方根。
设置异常处理,对输入负数的情况给出提示。
cmath>
voidmain()
doublenumber;
doubleresult;
cout<
请输入一个数:
cin>
>
number;
try
if(number<
0)
throwexception("
输入的数是负数!
);
result=sqrt(number);
平方根是:
<
result<
endl;
catch(exceptione)
e.what()<
3、实验总结:
1.在使用非模板函数时要注意输入的语句应符合函数的类型,或者定义一个函数模板使其实例化,否则无法得出正确结论。
2.对异常处理有了一些理解,但不知道异常处理(如第4题)可以循环吗?
3.对用类模板实现线性表不是很懂。
任课教师签名:
实验六C++的流类库与输入输出(2学时)
C++的流类库与输入输出
(1)掌握C++格式化的输入输出方法。
(2)掌握重载运算符“<
”和“>
”的方法。
(3)掌握磁盘文件的输入输出方法。
1.下面给出的test7_1_1.cpp程序用于打印九九乘法表,但程序中存在错误。
请上机调试,使得此程序运行后,能够输出如下所示的九九乘法表。
*123456789
11
224
3369
4481216
5510152025
661218243036
77142128354249
8816243240485664
991827364554637281
//test7_1_1.cpp
iomanip>
{
inti,j;
”*”;
for(i=1;
=9;
i++)
””;
for(i=1;
i;
for(j=1;
=i;
j++)
Cout<
i*j;
setiosflags(ios:
:
right)<
setw(4)<
*"
cout<
for(j=1;
cout<
}
2.下面的程序用于统计文件xyz.txt中的字符个数,请填空完成程序。
//test7_2_1.cpp
#include<
fstream>
intmain()
{charch;
inti=0;
ifstreamfile;
file.open(“xyz.txt”,ios:
in);
if(
)
”xyz.txtcannotopen”<
abort();
While(!
file.eof())
i++;
”文件字符个数:
”<
charch;
ifstreamfile;
file.open("
xyz.txt"
ios:
if(!
file)
xyz.txtcannotopen"
abort();
while(!
{
file.get(ch);
if((ch<
=122&
&
ch>
=97)||(ch<
=90&
=65))
i++;
文件字符个数:
file.close();
3.重载运算符“<
”,使其能够输入一件商品的信息和输出这件商品的信息。
商品的信息由编号、商品名和价格。
假如商品类Merchandise的框架如下:
classmerchandise{
public:
Merchandiss();
~Merchandiss();
friendistream&
operator>
(istream&
in,Merchandiss&
s);
friendostream&
operator<
(ostream&
out,Merchandiss&
intno;
char*name;
doubleprice;
要求实现该类,并编写以下的main函数对该类进行操作。
{Merchandisemer;
cin>
mer;
iostream.h>
classMerchandise
Merchandise();
~Merchandise();
operator>
in,Merchandise&
out,Merchandise&
Merchandise:
Merchandise()
no=0;
name=newchar[20];
price=0;
~Merchandise()
deletename;
istream&
s)
inputtheMerchandise^s:
no:
in>
s.no;
name:
s.name;
price:
s.price;
returnin;
ostream&
out<
s.no<
s.name<
s.price<
returnout;
Merchandisemer;
4.编写一个程序,将两个文本文件连接成一个文件,然后将此文件中所有小写字母转换成大写字母,并打印出来。
ifstreamfile1;
ifstreamfile2;
ofstreamfile3;
file1.open("
file2.open("
qwe.txt"
file3.open("
asd.txt"
out);
file1||!
file2)
file1.seekg(0);
file2.seekg(0);
while(!
file1.eof())
file1.get(ch);
if(ch<
=97)
ch=ch-32;
file3.put(ch);
ch;
file2.eof())
file2.get(ch);
file1.close();
file2.close();
file3.close();
通过这次试验,我学习掌握了C++格式化的输入输出方法,掌握了重载运算符“<
”的方法,掌握了磁盘文件的输入输出方法,能够更加熟悉地运用VisualC++6.0的系统。
实验七Windows程序框架及MFC编程(2学时)
Windows程序框架及MFC编程
1.学习windows基于消息事件驱动方式的程序设计
2.学习一个完整的窗口编制的四个操作步骤
3.了解Windows提供的窗口类
4.了解回调函数
5.学习MFC对话框和单文档编程的一般方法
5.学习使用MSDN帮助解决问题
1.写一个Win32Application应用程序,要求当点击关闭时,弹出对话框确定关闭,否则不关闭。
运行初始时,在文档窗口显示“Hello”。
//-----初始化
BOOLCTestDlg:
OnInitDialog()
CDialog:
OnInitDialog();
//Add"
About..."
menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&
0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<
0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->
AppendMenu(MF_SEPARATOR);
AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'
smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);
//Setbigicon
SetIcon(m_hIcon,FALSE);
//Setsmallicon
//TODO:
Addextr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 模板 异常 处理