作者watermeter (水表)
看板C_and_CPP
标题[问题] 有时候记忆体会找不到位置
时间Fri Jul 1 15:27:34 2016
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
Xcode
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
std
问题(Question):
我的问题在於,呼叫printmatrix函式时,有时候记忆体会无法读到位置,有时候却可以
找到并执行,但明明Input都是一模一样的,只需要告诉我原因即可,谢谢
喂入的资料(Input):
一样的矩阵跟资料输入数次,却是不同的结果
2
2 3
4 5
3 5
6 8
预期的正确结果(Expected Output):
A =
2 3
4 5
B =
3 5
6 8
A+B =
5 8
10 13
A-B =
-1 -2
-2 -3
A*B =
24 34
42 60
(这是有时候跑出来的执行结果,有时候却跑不出来)
错误结果(Wrong Output):
有时候跑不出来,我完全没头绪@@
程式码(Code):(请善用置底文网页, 记得排版)
//
// Matrix.h
// L4 HW
//
// Created by 叶柏宏 on 2016/7/1.
// Copyright 2016年 Dannel Apple. All rights reserved.
//
#ifndef Matrix_h
#define Matrix_h
template<class _Tx>
void swap(_Tx &a, _Tx &b) {
_Tx t = a;
a = b;
b = t;
}
class Matrix{
size_t n;
double *mtxptr=new double;//a obj with algo idx
public:
void assignDimension(int dim){
this->n=dim;
}
void assignElements(){
for(size_t i=0;i<(n*n);i++) cin>>*(mtxptr+i);
}
void printMatrix(){
for(size_t i=0;i<n*n;i++){
if(i%n==0) cout<<"\n";
std::cout<<*(mtxptr+i)<<" ";
}
std::cout<<std::endl;
}
void assignMatrix(Matrix A){
this->n=A.n;
for(size_t i=0;i<n*n;i++) *((this->mtxptr)+i)=*((A.mtxptr)+i);
}
void transposeMatrix(){
for(size_t i=0;i<n*n;i++)if((i%n)>(i/n)) swap(*((this->mtxptr)+i), *((this->mtxptr)+(i%n)*n+i/n));}
void addMatrix(Matrix A,Matrix B){setzero();
for(size_t i=0;i<n*n;i++) *(mtxptr+i)=*(A.mtxptr+i)+*(B.mtxptr+i);
}
void subtractMatrix(Matrix A,Matrix B){setzero();
for(size_t i=0;i<n*n;i++) *(mtxptr+i)=*(A.mtxptr+i)-*(B.mtxptr+i);
}
void multiplyMatrix(Matrix A,Matrix B){setzero();
for(size_t i=0;i<n*n;i++){
for(size_t j=0;j<n;j++){
*((this->mtxptr)+i)+=*((A.mtxptr)+i/n*n+j)**((B.mtxptr)+n*j+i%n);
}
}
}
void setzero(){
for(size_t i=0;i<n*n;i++) *((this->mtxptr)+i)=0;
}
};
#endif /* Matrix_h */
补充说明(Supplement):
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.114.72.205
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1467358056.A.BDE.html
1F:→ watermeter: 我使用了一些矩阵的性质 07/01 15:28
2F:推 druid0214: 先把你的语法整理 连cin都没有std:: 07/01 15:54
3F:→ druid0214: 还有你的main.cpp也要放上来才知道是哪个函式的问题 07/01 15:55
4F:→ druid0214: 很想嘘 07/01 15:55
5F:→ druid0214: 可能我C++没学好 那个swap好像怪怪的 07/01 15:59
6F:→ druid0214: 程序导向和物件导向不要混在一起 不好阅读 07/01 16:02
7F:→ druid0214: 不好意思刚刚才看懂原来是自己写一个swap 07/01 16:06
8F:→ druid0214: include了哪些东西也放上来一下 不是每个编译器都预设 07/01 16:08
9F:推 bibo9901: 你的mtxptr只给了1个 double 空间? 07/01 16:18
10F:→ watermeter: 谢谢各位的指教,如果使用pointer的话,会有什麽问题 07/02 11:34
11F:嘘 druid0214: 所以你坚持要放我Compiler不能过的程式码? 07/02 13:13
12F:推 steve1012: 什麽意思是程序导向跟物件导向放一起 看不太懂 07/03 00:24
13F:推 steve1012: 你只new 一个double 吗还是我漏掉了 阵列应该是new dou 07/03 00:27
14F:→ steve1012: ble [x]吧 07/03 00:27
15F:推 druid0214: 1.assign很明显是物件导向 却没有建构解构 07/03 06:08
16F:→ druid0214: 2.add sub这种明显是程序导向 丢在class里很怪 07/03 06:09
17F:→ druid0214: 要怎麽用? 大概就是A.sub(A,B) 这样很怪 07/03 06:09
18F:→ druid0214: 3.很帅气的说函式库用std 一开场宣告Template 07/03 06:10
19F:→ druid0214: 我还以为要用样板做matrix运算 结果是自定义swap 07/03 06:11
20F:→ druid0214: 这三点都没关系 我是很度烂语法有错 程式码那麽长 07/03 06:12
21F:→ druid0214: 不使用网页存就算 复制贴上到IDE就有红点 07/03 06:13
22F:→ druid0214: cpp也不贴上来 这种一丢上来开IDE debugger比较快 07/03 06:13