《数据结构》实验一: VC编程工具的灵活使用
一..实验目的
复习巩固VC编程环境的使用,以及C++模板设计。
1.回顾并掌握VC单文件结构程序设计过程。
2.回顾并掌握VC多文件工程设计过程
3.掌握VC程序调试过程。
4.回顾C++模板和模板的程序设计。
二.实验内容
1. 设计一个单文件结构程序完成从键盘输入两个数,输出二者的“和”和“积”的结果。要求如下:
1)设计函数来计算“和”和“积”,在主函数中调用,并能考虑重载函数,使整数和小数均能计算。
2)分别使用单步调试和断点调试来调试程序。并多次运行力求熟练调试方法。
#include<iostream> using namespace std; void aaa(int a,int b) { cout<<"两数之和为:"<<a+b<<endl; cout<<"两数之积为:"<<a*b<<endl; } void aaa(int a,float b) { cout<<"两数之和为:"<<a+b<<endl; cout<<"两数之积为:"<<a*b<<endl; } void aaa(float a,int b) { cout<<"两数之和为:"<<a+b<<endl; cout<<"两数之积为:"<<a*b<<endl; } void aaa(float a,float b) { cout<<"两数之和为:"<<a+b<<endl; cout<<"两数之积为:"<<a*b<<endl; } void main() { float a,b; cin>>a>>b; int i=(int)a; int j=(int)b; if(a>i) { if(b>j) { aaa(a,b) ; } else { aaa(a,j); } } else { if(b>j) { aaa(i,b); } else { aaa(i,j); } } }
2.使用函数的模板来实现上述功能。
#include<iostream> using namespace std; template <typename T> void out(T a,T b) { cout<<"两数之和为:"<<a+b<<endl; cout<<"两数之积为:"<<a*b<<endl; } void main() { float a,b; cin>>a>>b; out(a,b); }
3.使用一个类来实现上述功能。要求:
1)使用类模板
2)使用多文件:类的声明有头文件中;类的函数定义一个源文件中,在主程序文件中设计主函数程序,在实例化输出结果。
头文件:aaa.h
#include <iostream> using namespace std; template <class T,class Z> class aaa { public: aaa(T a,Z b) { cout<<"两数之和为:"<<a+b<<endl; cout<<"两数之积为:"<<a*b<<endl; } };
主函数单独一个cpp
#include <iostream> using namespace std; #include "aaa.h" void main() { float a,b; cin>>a>>b; int i = (int) a; int j = (int) b; if(a>i) { if(b>j) { aaa<float,float> a(a,b); } else { aaa<float,int> a(a,b); } } else { if(b>j) { aaa<int,b); } else { aaa<int,b); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。