一..实验目的
???? 复习巩固VC编程环境的使用,以及C++模板设计。
1.回顾并掌握VC单文件结构程序设计过程。
2.回顾并掌握VC多文件工程设计过程
3.掌握VC程序调试过程。
4.回顾C++模板和模板的程序设计。
二.实验内容
1. 设计一个单文件结构程序完成从键盘输入两个数,输出二者的“和”和“积”的结果。要求如下:
1)设计函数来计算“和”和“积”,在主函数中调用,并能考虑重载函数,使整数和小数均能计算。
2)分别使用单步调试和断点调试来调试程序。并多次运行力求熟练调试方法。
2.使用函数的模板来实现上述功能。
3.使用一个类来实现上述功能。要求:
? 1)使用类模板
? 2)使用多文件:类的声明有头文件中;类的函数定义一个源文件中,在主程序文件中设计主函数程序,在实例化输出结果。
三.源代码
(1)
 #include<iostream>
 using namespace std;
 int sum(int a,int b)
 {
int c;
c=a+b;
return c;
 }//实现整数下a和b相加的函数
 int sum(int i1,int m,int n)
 {
int o;
o=m*n;
return o;
 }//实现整数下m与n相乘的函数
 float sum(int j,int l,float a1,float b1)
 {
float c;
c=a1+b1;
return c;
 }//实现小数下a和b相加的函数
 float sum(int k,int k1,int l1,float m1,float n1)
 {
float o;
o=m1*n1;
return o;
 }//实现小数下m与n相乘的函数
 void main()
 { 
char sel;
cout<<"尊敬的用户:"<<endl;
cout<<" ? ? ? ? ?您即将进行求两个数字的“和”与“积”的操作,请问输入的数字中是否包括小数?(Y/N):";
cin>>sel;
if(sel=='Y' || sel=='y' )?
{
float x,y,h,h1; 
? ?cout<<"请输入两个数字:";
? ?cin>>x>>y;
sum(0,x,y);
h=sum(0,y);
cout<<"这两个数的和是:"<<h<<endl;
 ? ? ? ? sum(0,y);
h1=sum(0,y);
cout<<"这两个数的积是:"<<h1<<endl;
}
else
{
int x,h1;
? ?cout<<"请输入两个数字:";
? ?cin>>x>>y;
 ? ? sum(x,y);
h=sum(x,y);
cout<<"这两个数的和是:"<<h<<endl;
sum(0,y);
cout<<"这两个数的积是:"<<h1<<endl;
}
 }
 
(2)
 #include<iostream>
 using namespace std;
 template<class A>
 void sum(A a,A b,A m,A n)
 {
cout<<"请输入两个数字:";
cin>>a>>b;
m=a+b;
cout<<"这两个数的和是:"<<m<<endl;
n=a*b;
 ? ? cout<<"这两个数的积是:"<<n<<endl;
 }
 void main()
 {
 ? ? int a1,b1,m1,n1;
double a2,b2,m2,n2;
char sel;
cout<<"尊敬的用户:"<<endl;
cout<<" ? ? ? ? ?您即将进行求两个数字的“和”与“积”的操作,请问输入的数字中是否包括小数?(Y/N):";
cin>>sel;
if(sel=='Y' || sel=='y' )?
sum(a2,n2);
else
sum(a1,n1);
 }
 
(3)
 #include<iostream>
 using namespace std;
 class sum
 {
int a,b;
float c,d;
 public:
?void zhengshu(int i);
 ? ? ? void xiaoshu();
 };
 void sum::zhengshu(int i)
 {
cout<<"请输入两个数字:";
cin>>a>>b;
cout<<"这两个数的和是:"<<a+b<<endl;
 ? ? cout<<"这两个数的积是:"<<a*b<<endl;
 }
 void sum::xiaoshu()
 {
cout<<"请输入两个数字:";
cin>>c>>d;
cout<<"这两个数的和是:"<<c+d<<endl;
 ? ? cout<<"这两个数的积是:"<<c*d<<endl;
 }
 void main()
 {
sum q,w;
char sel;
cout<<"尊敬的用户:"<<endl;
cout<<" ? ? ? ? ?您即将进行求两个数字的“和”与“积”的操作,请问输入的数字中是否包括小数?(Y/N):";
cin>>sel;
if(sel=='Y' || sel=='y' )?
w.xiaoshu();
else
q.zhengshu(0);
 }
?
四.实验心得
? ? ? ?通过这次实验,我了解到还有很多东西不够熟练,必须好好学习,天天向上。