《数据结构》实验一: VC编程工具的灵活使用

一..实验目的

复习巩固VC编程环境的使用,以及C++模板设计。

1.回顾并掌握VC单文件结构程序设计过程。

2.回顾并掌握VC多文件工程设计过程

3.掌握VC程序调试过程。

4.回顾C++模板和模板的程序设计。

二.实验时间

第二周第二次课。2个学时。

三.实验内容

1. 设计一个文件结构程序完成从键盘输入两个数,输出二者的“和”和“积”的结果。要求如下:

1)设计函数来计算“和”和“积”,在主函数调用,并能考虑重载函数,使整数和小数均能计算。

2)分别使用单步调试和断点调试来调试程序。并多次运行力求熟练调试方法

2.使用函数的模板来实现上述功能

3.使用一个类来实现上述功能。要求:

1)使用类模板

2)使用多文件:类的声明有头文件中;类的函数定义一个文件中,在主程序文件中设计主函数程序,在实例化输出结果。

程序如下:1.

#include<iostream>
using namespace std;
int sum(int a,int b)
{
	int he;
	he=a+b;
	return he;
}
float sum(float a,float b)
{
	float he1;
	he1=a+b;
	return he1;
}
int ji(int x,int y)
{
	int cheng;
	cheng=x*y;
	return cheng;
}
float ji(float x,float y)
{
	float cheng1;
	cheng1=x*y;
	return cheng1;
}
int main()
{
    float c,d,e,f;
	cout<<"请输入两个数:"<<endl;
	cin>>c>>d;
	e=sum(c,d);
	f=ji(c,d);
	cout<<c<<"+"<<d<<"="<<e<<endl;
	cout<<c<<"*"<<d<<"="<<f<<endl;
	return 0;
}


结果截图:


2.
#include<iostream>
using namespace std;
template<class T>
T he(T x,T y)
{
	return x+y;
}
template<class T>
T ji(T x,T y)
{
	return x*y;
}
int main()
{
	float a,b,c,d;
	cout<<"请输入两个数:"<<endl;
	cin>>a>>b;
	c=he(a,b);
	d=ji(a,b);
	cout<<a<<"+"<<b<<"="<<c<<endl;
	cout<<a<<"*"<<b<<"="<<d<<endl;
	return 0;
}

结果图如上图。
3.
主程序文件
#include "和积计算.h"
#include<iostream>
using namespace std;
int main()
{
	HeJi.p;
	float a,b;
	cout<<"请输入两个数:"<<endl;
	cin>>a>>b;
	p.sum(a,b);
	p.cheng(a,b);
	return 0;
}

文件
#include "和积计算.h"
void HeJi::sum()
{
	T x,y,a;
	a=x+y;
	cout<<x<<"+"<<y<<"="<<a<<endl;
}
void HeJi::cheng()
{
	T x,b;
	b=x*y;
	cout<<x<<"*"<<y<<"="<<b<<endl;
}

文件
template<class T>  
class HeJi
{
	public:
	    void sum(T x,T y);
		void cheng(T x,T y);
	private:
		T x;
		T y;
};

--------------------Configuration: f3 - Win32 Debug--------------------
Compiling...
和积计算2.cpp
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(4) : error C2065: 'T' : undeclared identifier
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(4) : error C2146: Syntax error : missing ';' before identifier 'x'
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(4) : error C2065: 'x' : undeclared identifier
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(4) : error C2065: 'y' : undeclared identifier
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(4) : error C2065: 'a' : undeclared identifier
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(6) : error C2065: 'cout' : undeclared identifier
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(6) : error C2297: '<<' : illegal,right operand has type 'char [2]'
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(6) : error C2065: 'endl' : undeclared identifier
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(10) : error C2146: Syntax error : missing ';' before identifier 'x'
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(10) : error C2065: 'b' : undeclared identifier
E:\数据结构文件夹\数据结构 实验一\f3\和积计算2.cpp(12) : error C2297: '<<' : illegal,right operand has type 'char [2]'
执行 cl.exe 时出错.

和积计算2.obj - 1 error(s),0 warning(s)

第3个程序总是改不好,我对于类模板、多文件很不熟悉,而且也忘记了C++的很多知识,我要恶补一下了。我把第3个程序弄上来,希望老师帮我看看哪些是错误的,或者给我意见该复习哪些内容,麻烦老师了,谢谢。

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...