江苏科技大学c++实验报告 下载本文

江苏科技大学

课程实践报告

设计题目: 程序设计(VC++)实践 设计时间 20014-2-24 至 2014-2-28 学院(系): 船舶与海洋工程 专业班级: 卓越(一)班

学生姓名: 朱国栋 学号 1240101138 指导老师: 张晓茹

任务一

一、实践任务

38.建立一个类Sample,对数组中元素用选择法进行升序排序。排序函数定义到Sample类

的友元类Process中。 具体要求如下: 类Sample #define Max 100;

二、详细设计

1、类的描述与定义 (1)私有数据成员

? int A [MAX]:一维整型数组,存放需要排序的数。 ? int n:需要排序的数的个数。

(2)公有成员函数

? Sample ():构造函数,初始化成员数据n,初始值为0。

友元类Process 公有成员函数

? void getdata(Sample &s):从键盘输入数据,对数组A进行赋值。 ? void selectsort(Sample &s):对数组A中的元素进行升序排序。 ? void disp(Sample &s):输出数组中的元素。

2、主要函数设计

在主程序中定义对象对该类进行测试。

三、源程序清单

#include #define MAX 100 class Sample{ private: int A[MAX]; int n; public: Sample() {n=0;} friend class Process; };

class Process{ public: void getdata(Sample &s) { cout<<\请输入需要排序的数的个数:\ cin>>s.n; cout<<\请依次输入\个需要排序的数:\ for(int i=0;i>s.A[i]; } void selectsort(Sample &s) {

int t; for(int i=0;is.A[j]) t=j; } if(t!=i) { j=s.A[t]; s.A[t]=s.A[i]; s.A[i]=j; } } } void disp(Sample &s) { cout<<\数组中的元素为:\ for(int i=0;i

void main() { Sample test1; Process test2; test2.getdata(test1); test2.disp(test1); test2.selectsort(test1); test2.disp(test1); }

四、运行结果

任务二

一、实践任务

4.建立一个类MOVE,将数组中最大元素的值与最小元素的值互换。

二、详细设计

1、类的描述与定义 (1)私有数据成员

? int *array:一维整型数组。 ? int n:数组中元素的个数。

(2)公有成员函数

? ? ? ?

MOVE(int b[],int m):构造函数,初始化成员数据。

void exchange():输出平均值,并将数组中的元素按要求重新放置。 void print():输出一维数组。 ~MOVE():析构函数。

2、主要函数设计

在主程序中用数据{21,65,43,87,12,84,44,97,32,55}对该类进行测试。

三、源程序清单

#include class MOVE{ private: int *array; int n; public: