加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 06:06:29 所属栏目:安全 来源:网络整理
导读:一..实验目的 复习巩固VC编程环境的使用,以及C++模板设计。 1.回顾并掌握VC单文件结构程序设计过程。 2.回顾并掌握VC多文件工程设计过程 3.掌握VC程序调试过程。 4.回顾C++模板和模板的程序设计。 二.实验时间 第二周第二次课。2个学时。 三.实验内容 1.

一..实验目的

复习巩固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个程序弄上来,希望老师帮我看看哪些是错误的,或者给我意见该复习哪些内容,麻烦老师了,谢谢。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读