POJ C++程序设计 编程题#3 编程作业—运算符重载
编程题 #3
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
写一个二维数组类 Array2,使得下面程序的输出结果是:
0,1,2,3,
4,5,6,7,
8,9,10,11,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,
程序:
#include <iostream>
#include <cstring>
using namespace std;
// 在此处补充你的代码
int main() {
Array2 a(3,4);
int i,j;
for( i = 0;i < 3; ++i )
for( j = 0; j < 4; j ++ )
a[i][j] = i * 4 + j;
for( i = 0;i < 3; ++i ) {
for( j = 0; j < 4; j ++ ) {
cout << a(i,j) << ",";
}
cout << endl;
}
cout << "next" << endl;
Array2 b; b = a;
for( i = 0;i < 3; ++i ) {
for( j = 0; j < 4; j ++ ) {
cout << b[i][j] << ",";
}
cout << endl;
}
return 0;
}
输入
无
输出
0,1,2,3,
4,5,6,7,
8,9,10,11,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,
样例输入
无
样例输出
0,1,2,3,
4,5,6,7,
8,9,10,11,
next
0,1,2,3,
4,5,6,7,
8,9,10,11,
#include <iostream>
#include <cstring>
using namespace std;
// 在此处补充你的代码
class Array2 {
private:
int * a;
int i, j;
public:
Array2() {a = NULL;}
Array2(int i_, int j_) {
i = i_;
j = j_;
a = new int[i*j];
}
Array2(Array2 &t){
i = t.i;
j = t.j;
a = new int[i * j];
memcpy(a, t.a, sizeof(int)*i*j);
}
Array2 & operator=(const Array2 &t) {
if (a != NULL) delete []a;
i = t.i;
j = t.j;
a = new int[i*j];
memcpy(a, t.a, sizeof(int)*i*j);
return *this;
}
~Array2() {if (a != NULL) delete []a;}
// 将返回值设为int的指针,则可以应用第二个【】,不用重载第二个【】操作符
int *operator[](int i_) {
return a+i_*j;
}
int &operator()(int i_, int j_) {
return a[i_*j + j_];
}
}; int main() {
Array2 a(,);
int i,j;
for( i = ;i < ; ++i )
for( j = ; j < ; j ++ )
a[i][j] = i * + j;
for( i = ;i < ; ++i ) {
for( j = ; j < ; j ++ ) {
cout << a(i,j) << ",";
}
cout << endl;
}
cout << "next" << endl;
Array2 b; b = a;
for( i = ;i < ; ++i ) {
for( j = ; j < ; j ++ ) {
cout << b[i][j] << ",";
}
cout << endl;
}
return ;
}
POJ C++程序设计 编程题#3 编程作业—运算符重载的更多相关文章
- POJ C程序设计进阶 编程题#3:运算符判定
编程题#3:运算符判定 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 两个 ...
- POJ C++程序设计 编程题#1 编程作业—运算符重载
编程题 #2 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的MyIn ...
- C#编程(四十)----------运算符重载
运算符重载 所谓的运算符重载是指允许用户使用用户定义的类型编写表达式的能力. 例如,通常需要编写类似与以下内容的代码,入江两个数字相加,很明显,sum是两个数字之和. int i=5,j=4; int ...
- 04737_C++程序设计_第9章_运算符重载及流类库
例9.1 完整实现str类的例子. #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> ...
- C++程序设计方法3:数组下标运算符重载
数组下标运算符重载 函数声明形式 返回类型operator[](参数): 如果返回类型是引用,则数组运算符调用可以出现在等式的左边,接受赋值,即: Obj[index] = value; 如果返回类型 ...
- C++第五次作业--运算符重载和函数重载
C++ 运算符重载和函数重载 C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载. 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是 ...
- POJ C++程序设计 编程题#1 编程作业—STL1
编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出结 ...
- POJ C++程序设计 编程题#3 编程作业—文件操作与模板
编程题#3: 整数的输出格式 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 利 ...
- POJ C++程序设计 编程题#2 编程作业—文件操作与模板
编程题#2: 实数的输出格式 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 ...
随机推荐
- UILabel使用技巧
UILabel的各种属性与方法的使用(转) #import "LabelTestViewController.h" @implementation LabelTestViewCon ...
- Java中的装箱拆箱
一) 装箱与拆箱 Java中有概念是一切皆对象,因为所有的类都默认继承自Object.但是,对于数据类型是个例外,如short,int,long,float,double, byte,char,bo ...
- console,和自己定义事件
console.log这个指令是在浏览器控制台输出日志,用来调试程序 跟alert 类似 但不像alert那样会打断程序.
- BestCoder Round #79 (div.2)
1001.没推到题解那么细,枚举一下也可以.用通分可以避免小数精度问题. #include<iostream> #include<stdio.h> using namespac ...
- LeetCode Lowest Common Ancestor of a Binary Serach Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- (转)C#操作PPT
原文地址:http://blog.163.com/loveyingchun_1314/blog/static/2382425120124312627530/ 引用Microsoft.Office.Co ...
- [转] iOS开发之使用lipo命令制作模拟器与真机通用静态库
转自 http://blog.csdn.net/jinglijun/article/details/8276089 通常在项目中使用静态库的时候都会有两个版本,一个用于模拟器,一个用于真机,因为Mac ...
- github入门
一.先了解 相比CVS\SVN优势: - 支持离线开发,离线Repository- 强大的分支功能,适合多个独立开发者协作- 速度快 github 本地有仓库,储存着所有repository的历史: ...
- vc 取windows系统信息 版本 cpu信息 内存信息 ie版本信息 office版本
头文件: /*! Copyright (C) *---------------------------------------------------------------------------- ...
- Orchard官方文档翻译(八) 为站点增加博客
原文地址:http://docs.orchardproject.net/Documentation/Adding-a-blog-to-your-site 想要查看文档目录请用力点击这里 最近想要学习了 ...