一.实验目的
复习巩固VC编程环境的使用,以及C++模板设计。
1.回顾并掌握VC单文件结构程序设计过程。
2.回顾并掌握VC多文件工程设计过程
3.掌握VC程序调试过程。
4.回顾C++模板和模板的程序设计。
二.实验内容
1. 设计一个单文件结构程序完成从键盘输入两个数,输出二者的“和”和“积”的结果。要求如下:
1)设计函数来计算“和”和“积”,在主函数中调用,并能考虑重载函数,使整数和小数均能计算。
2)分别使用单步调试和断点调试来调试程序。并多次运行力求熟练调试方法。
2.使用函数的模板来实现上述功能。
3.使用一个类来实现上述功能。要求:
1)使用类模板
2)使用多文件:类的声明有头文件中;类的函数定义一个源文件中,在主程序文件中设计主函数程序,在实例化输出结果。
三.实验结果
题目一:设计函数来计算“和”和“积”,在主函数中调用,并能考虑重载函数,使整数和小数均能计算
具体代码如下:
#include<iostream> using namespace std; void jugdment(float,float);//判断函数的声明 void C_out(int,int );//加法跟乘法的输出函数 void C_out(float,float);//函数重载 void C_out(float,int);//函数重载 void C_out(int,float);//函数重载 int main() { cout << "请输入两个数" << endl; float a,b; cin >> a >> b; int c = a; int d = b; if (c==a&&d==b) { C_out((int)a,(int)b); } if (c!=a&&d!=b) { C_out(a,b); } if (c == a&&d != b) { C_out((int)a,b); } if (c!=a&&d==b) { C_out(a,(int)b); } system("pause"); return 0; } void C_out(int a,int b)//加法和乘法的结果输出函数 { cout << "你输入了两个整形" << endl; cout << "两个数的和为" << a + b << endl; cout << "两个数的积为" << a*b << endl; } void C_out(float a,float b) { cout << "你输入了两个浮点型" << endl; cout << "两个数的和为" << a + b << endl; cout << "两个数的积为" << a*b << endl; } void C_out(int a,float b) { cout << "你输入了一个整形一个浮点型" << endl; cout << "两个数的和为" << a + b << endl; cout << "两个数的积为" << a*b << endl; } void C_out(float a,int b) { cout << "你输人了一个浮点型一个整形" << endl; cout << "两个数的和为" << a + b << endl; cout << "两个数的积为" << a*b << endl; }
运行结果截图:
代码分析:
首先,提示用户输入两个数字以用于计算。然后,通过“类型判断语句块”识别用户所输入的数字为整形还是浮点型。最后,“强制类型转换实参”调用函数。最后,利用“函数的重载”应对多种数字组合来完成任务。
题目二:使用函数的模板来实现上述功能
具体代码如下:
#include<iostream> using namespace std; template<typename onet_pe,typename twot_pe>//定义两个模板 void C_out(onet_pe,twot_pe);//加法跟乘法的输出函数的声明 int main() { cout << "请输入两个数" << endl; float a,b; cin >> a >> b; int c = a; int d = b; if (c == a&&d == b) //判断类型语句块 { C_out((int)a,(int)b); } if (c != a&&d != b) { C_out(a,b); } if (c != a&&d == b) { C_out(a,(int)b); } system("pause"); return 0; } template<typename onet_pe,typename twot_pe>//模板 void C_out(onet_pe a,twot_pe b)//加法和乘法的结果输出函数 { cout << "两个数的和为" << a + b << endl; cout << "两个数的积为" << a*b << endl; }
运行结果如下:
代码分析:
首先,提示用户输入两个数字以用于计算。然后,通过“类型判断语句块”识别用户所输入的数字为整形还是浮点型。最后,“强制类型转换实参”调用函数。最后,利用“函数的模板”应对多种数字组合来完成任务。
题目三:使用类模板,使用多文件来实现上述功能
具体代码如下:
头文件代码:
源文件代码:
#include<iostream> using namespace std; template<class onet_pe,class twot_pe>//定义模板 class operation //定义操作类 { public: operation(onet_pe T,twot_pe F);//声明构造函数 void C_out(); //声明输出结果成员函数 private: onet_pe a; twot_pe b; }; template<class onet_pe,class twot_pe>//模板 operation < onet_pe,twot_pe>::operation(onet_pe T,twot_pe F)//定义构造函数 { a = T; b = F; } template<class onet_pe,class twot_pe>//模板 void operation < onet_pe,twot_pe>::C_out()//定义输出结果成员函数 { cout << "两个数的和为" << a + b << endl; cout << "两个数的积为" << a*b << endl; }
源文件代码:
#include<iostream> #include"operation.h" using namespace std; int main() { cout << "请输入两个数" << endl; float a,b; //判断类型语句块 cin >> a >> b; int c = a; int d = b; if (c == a&&d == b) { operation<int,int> cpp((int)a,(int)b);//定义对象 cpp.C_out(); //调用成员函数 } if (c != a&&d != b) { operation<float,float> cpp(a,b); //定义对象 cpp.C_out(); //调用成员函数 } if (c == a&&d != b) { operation<int,float> cpp((int)a,b); //定义对象 cpp.C_out(); //调用成员函数 } if (c != a&&d == b) { operation<float,int> cpp(a,(int)b); //定义对象 cpp.C_out(); //调用成员函数 } system("pause"); //系统指令 return 0; }
运行结果截图:
代码分析:
在头文件中,定义操作类以及其成员函数的定义。在源文件中,利用“判断语句块”确定输入的类型。接着,用“类模板”定义与类型相对应的对象,并调用成员函数输出结果。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。