《数据结构》实验一: VC编程工具的灵活使用
一、实验目的 ???? 复习巩固VC编程环境的使用,以及C++模板设计。 1.回顾并掌握VC单文件结构程序设计过程。 2.回顾并掌握VC多文件工程设计过程 3.掌握VC程序调试过程。 4.回顾C++模板和模板的程序设计。
二、实验内容 1. 设计一个单文件结构程序完成从键盘输入两个数,输出二者的“和”和“积”的结果。要求如下: 1)设计函数来计算“和”和“积”,在主函数中调用,并能考虑重载函数,使整数和小数均能计算。 2)分别使用单步调试和断点调试来调试程序。并多次运行力求熟练调试方法。 2.使用函数的模板来实现上述功能。 3.使用一个类来实现上述功能。要求: ? 1)使用类模板 ? 2)使用多文件:类的声明有头文件中;类的函数定义一个源文件中,在主程序文件中设计主函数程序,在实例化输出结果。 三、程序代码 实验内容1题: 1.设计函数来计算“和”和“积”,在主函数中调用,并能考虑重载函数,使整数和小数均能计算。
#include <iostream> using namespace std; int sum(int a,int b) { return a+b; } int product(int a,int b) { return a*b; } float sum(float c,float d) { return c+d; } float product(float c,float d) { return c*d; } int main() { int a,b; float c,d; cout<<"请输入两个整数:"<<endl; cin>>a>>b; cout<<"两个整数之和:"<<sum(a,b)<<endl; cout<<"两个整数之积:"<<product(a,b)<<endl; cout<<endl; cout<<"请输入两个小数:"<<endl; cin>>c>>d; cout<<"两个小数之和:"<<sum(c,d)<<endl; cout<<"两个小数之积:"<<product(c,d)<<endl; return 0; } 实验内容2题 2.使用函数的模板来实现上述功能。 #include <iostream> using namespace std; template <class A,class B> void sum(A a,B b) { cout<<"a + b ="<<a+b<<endl; } template <typename A,typename B> void product(A a,B b) { cout<<"a * b = "<<a*b<<endl; } int main() { float c,d; cout<<"请输入两个数a,b:"<<endl; cin>>c>>d; sum(c,d); product(c,d); return 0; } 实验3题 ? 2)使用多文件:类的声明有头文件中;类的函数定义一个源文件中,在主程序文件中设计主函数程序,在实例化输出结果。 //头文件 ?"3.h" #include <iostream> using namespace std; template <class T> class Count { public: void sum(T a,T b); void product(T a,T b); private: T a; T b; }; <span style="color:#ff6666;"> </span> //源文件 "3.cpp" #include "3.h" int main() { Count<float> count; float n,m; cout<<"请输入两个数:"<<endl; cin>>n>>m; count.sum(n,m); count.product(n,m); return 0; } template <class T> void Count<T>::sum(T a,T b) { cout<<"两个数之和为:"<<a+b<<endl; } template <class T> void Count<T>::product(T a,T b) { cout<<"两个数之积为:"<<a*b<<endl; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- angularjs – 如何通过承诺作为角色的指令属性
- regex – 如何使用grep获取任何刚刚`name =`后?
- 我是如何做软件测试项目的?
- scala – 添加额外的lib文件夹依赖项以在电梯项目中构建sbt
- scala – 如何在Akka Future中包装java.util.concurrent.Fu
- anglejs:ng-repeat-start和ng-repeat-end与内部ng重复
- angularjs – 在模板中非法使用ngTransclude指令
- 单元测试 – 使用Vimrunner对Vim脚本本地函数进行单元测试
- webService开发
- angular – 无法解析AuthenticationService的所有参数:([o