作业数组和c++的指针
homework arrays and pointers of c ++
在这个问题中,首先,您必须编写两个函数:
new_array (char** a, int n, int m): create a two-dimension matrix of characters whose size is m*n.
del_array (char** a, int n, int m): delete a two-dimension matrix of characters whose size is m*n.
之后,您可以使用上面的两个函数来执行以下任务:您会得到一个M*N
大小的大图像和一些m*n
大小的小图像。每个图像都由具有其大小的字符矩阵表示。你的任务是找出每个小图像在大图像中出现的位置的数量。
输入文件:image.inp
。
输入文件的第一行包含两个正整数M和N,它们分别是大图像的高度和宽度。每条线路2
。。。M+1
由描述一行大图像的N
个字符(a
…z
、A
…Z
(组成。
随后,有一些小图像,你必须找到大图像。每个小图像都是在大图像的形成过程中书写的。特别地,有一行的m
=0和n
=0,您必须结束查找过程。
输出文件:image.out
。
对于输入文件中的每个小图像,必须写入一个数字,该数字表示该小图像在大图像中出现的位置数。
image.inp image.out
4 4 3
Aaaa 1
Aaaa
Aaab
Aaaa
2 2
Aa
Aa
2 2
aa
ab
0 0
我做到了:
文件头:
image.h
:#ifndef _IMAGE_H_ #define _IMAGE_H_ using namespace std; void new_array (char** , int , int ); void del_array (char** , int , ); bool small_image(char**,char**,int,int,int,int) int count_small_image(char** , char** , int ,int ,int ,int ); #endif
文件
image.cpp
:#include<iostream> #include "image.h" #include <fstream> using namespace std; void new_array(char** a, int n,int m)
{ifstream-inStream;inStream.open("image.inp"(;
a=new char* [m] ; for(int i=0; i<m; i++) { a[i]=new char[n]; for(int j=0;j<n; j++) inStream>>a[i][j]; } } void del_array(char** a,int m) { for(int i=0;i<m ;i++) { delete [] a[i]; } delete [] a; } bool small_image(char** a,char** b, int i,int j,int p,int q) { for(int u=i;u<i+p;u++ ) { for(int v=j;v<j+q;v++ ) { if(a[u][v]!=b[u-i][v-j]) return false; } } return true; } int count_small_image(char** a,char** b,int m,int n,int p, int q) { int COUNT=0; for(int i=0;i<m;i++ ) for(int j=0;j<n;j++ ) { if(a[i][j]==b[0][0]) { if((m-i+1)>=p && (n-j+1)>=q) { if(small_image(a,b,i,j,p,q)==false) break; else COUNT++; } } } return COUNT; }
文件
main_count_small_image.cpp
:#include <iostream> #include "image.h" #include <fstream> using namespace std; int main() { ifstream inStream; inStream.open("image.inp"); ofstream outStream; outStream.open("image.out"); int m,n,p,q; char** a; char** b; inStream>>n>>m; new_array(a,n,m); inStream>>q>>p; new_array(b,q,p); int c; c=count_small_image(a,b,m,n,p,q); outStream<<c; del_array(a,m); del_array(b,p); return 0; getchar(); }
但是,我明白:
[error]:已停止工作。。。
这是一段简单的代码,最好使用调试器逐步完成。OP将比从一个固定的答案中了解到更多关于低执行的信息。
残酷的力量是有效的,但之前的一个问题有一个答案,建议采取更好的方法。请参阅如何在较大图像中检测小图像的出现。
新的数组方法实现错误。它无法返回构建的数组已经被讨论过了,所以我跳过它。规范中没有任何地方说new_array应该从文件中读取数据。此外,重新打开文件将要求新流从一开始就开始,并在获取图像数据之前重读m和n。这一点没有考虑在内。
缺乏描述性的变量名称使该程序难以阅读,也不利于辅助OP。同样,缺乏合理的缩进和大括号的使用。这个程序从外观上看似乎是在要求读者不要提供帮助。
在给定调用的count_small_image中
count_small_image(a,b,m,n,p,q);
两个for循环为超出范围的数组访问设置small_image。我相信这是在试图阻止。
if((m-i+1)>=p && (n-j+1)>=q)
也许是这样,但这是一种复杂而笨拙的方法。记住:未编写的代码没有错误。相反,尝试一些类似的方法
for(int m = 0; m < largeMaxM - smallMaxM; m++)
{
for(int n = 0; n < largeMaxM - smallMaxN; n++)
其中smallMaxM和smallMaxN是小图像的m和n边界,而largeMaxM和largeMaxN是大图像的m边界和n边界。
少量计数也过于复杂。对其进行排序,使其基于对小图像的迭代,从而消除了瑕疵。描述性变量名称也使函数可读性更强。
bool small_image(char** a,char** b, int offsetM,int offsetN,int maxM,int maxN)
{
for(int m = 0; m < maxM; m++)
{
for(int n = 0; n < maxN; n++)
{
if(a[m+offsetM][n+offsetN]!=b[m][n]) return false;
}
}
return true;
}
我在平板电脑上操作,没有编译器,所以请原谅我。
你被告知错误(或者你误解了被告知的内容(。像这样重写你的代码
char** new_array(int n, int m)
{
char** a;
...
return a;
}
int main()
{
...
char** a = new_array(n, m);
等等。
您应该了解函数如何返回值(包括指针(。还阅读了如何使用指针来实现数组。
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 使用指向成员的指针将成员函数作为参数传递
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错