第九周第三项目——数组类
来源:互联网 发布:饭菜先生餐饮软件 编辑:程序博客网 时间:2024/05/21 06:21
/**Copyright (c) 2016,烟台大学计算机学院*All rights reserved.*文件名称 :*作 者 : 田志伟*完成日期 : 2016年5月10号*版 本 号 : v1.0**问题描述 : 我的数组类*输入描述 :*程序输出 :*/#include<iostream>using namespace std;class MyArray{private: int *arrayAddr; //保存一个有len个整型元素的数组的首地址 int len; //记录动态数组的长度 int max; //动态数组中的最大值(并非动态数组中必须要的数据成员)public: MyArray(int *a, int n); ~MyArray(); int getValue(int i); //获得数组中下标为i的元素的值 int getLen(); //返回数组长度 int getMax( ); //返回数组中的最大值};MyArray::MyArray(int *a,int n) { int i; arrayAddr=new int[n]; len=n; for(i=0;i<n;i++) arrayAddr[i]=a[i]; } int MyArray::getValue(int i) { return arrayAddr[i]; }int MyArray::getLen(){ return len;}int MyArray::getMax(){ int i,max=0; for(i=0;i<len;i++) { if(arrayAddr[i]>max) max=arrayAddr[i]; } return max;}MyArray::~MyArray(){ delete []arrayAddr;}//写出各成员函数的定义int main(){ int b[10]= {75, 99, 90, 93, 38, 15, 5, 7, 52, 4}; MyArray r1(b,10); cout<<"最大值:"<<r1.getMax()<<endl; int c[15] = {18,68,10,52,3,19,12,100,56,96,95,97,1,4,93}; MyArray r2(c,15); int i,s=0; for(i=0; i<r2.getLen(); i++) s+=r2.getValue(i); cout<<"所有元素的和为:"<<s<<endl; return 0;}
程序运行:
0 0
- 第九周第三项目——数组类
- 第九周项目2——我的数组类
- 第九周项目2—我的数组类
- 第九周项目2—我的数组类
- 第九周项目(2)—我的数组类
- 第九周项目2—我的数组类
- 第九周上机时间项目——项目2—我的数组类
- 第九周上机实践项目——项目2-我的数组类
- 第九周项目一—猴子选大王(数组)
- 第九周实践项目2————我的数组类
- 第九周项目四——我的向量类(数组)
- 第九周项目一 第三种做法
- 第九周 数据结构实践项目——数组和广义表【项目3.2-- 稀疏矩阵相加】
- 第九周项目2 我的数组类
- 第九周项目 2.1 我的数组类
- 第九周项目2-我的数组类
- 第九周项目3:我的数组类
- 第九周项目三 我的数组类
- R语言文本分析(2)
- hibernate中关系操作(inverse)和级联操作(cascade)详解
- Jquery换肤
- Fedora 18 系统安装
- String的split()函数的用法总结
- 第九周第三项目——数组类
- uva 1225 - Digit Counting
- 1-(2)
- Fedora 19 系统安装
- zabbix监控mysql
- 判断输入是否合法
- hdu 1233 还是畅通工程
- java线程那些事
- MongoDB-JAVA-Driver 3.2版本常用代码全整理(2) - 查询