简体   繁体   English

C ++的作业数组和指针

[英]homework arrays and pointers of c ++

In this question, firstly, you have to write two functions: 在这个问题上,首先,您必须编写两个函数:

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.

After that, you use two above functions to perform the following task: You are given a big image with size M*N and some small images with size m*n . 之后,您将使用上述两个函数执行以下任务:给您一个大小为M*N的大图像,以及一些大小为m*n小图像。 Each image is presented by a matrix of characters with its size. 每个图像由具有其大小的字符矩阵表示。 Your task is finding the number of positions which each small image occurs in that big image. 您的任务是查找每个小图像在该大图像中出现的位置数量。

Input file: image.inp . 输入文件: image.inp

The first line of the input file contains two positive integers M and N which are respectively the height and the width of the big image. 输入文件的第一行包含两个正整数M和N,分别是大图像的高度和宽度。 Each line 2 ... M+1 consists of N characters ( a ... z , A ... Z ) which describe a row of the big image. 每行2 ... M+1N字符( a ... zA ... Z )组成,它们描述一行大图像。

Subsequently, there are some small images which you must find the big image. 随后,有一些小图像,您必须找到大图像。 Each small image is written in the formation of the big image. 每个小图像都以大图像的形式写入。 Specially, there is a line having m = 0 and n = 0, you have to end your finding process. 特别是,有一行的m = 0和n = 0,您必须结束查找过程。

Output file: image.out . 输出文件: image.out

For each small image in the input file, you must write a number which presents the number of positions which that small image occurs in the big image. 对于输入文件中的每个小图像,必须编写一个数字,该数字表示该小图像在大图像中出现的位置数。

image.inp            image.out
4 4                   3 
Aaaa                  1 
Aaaa
Aaab
Aaaa
2 2
Aa
Aa
2 2
aa
ab
0 0

I did this: 我这样做:

  • file header: image.h : 文件头: 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 
  • file image.cpp : 文件image.cpp

     #include<iostream> #include "image.h" #include <fstream> using namespace std; void new_array(char** a, int n,int m) 

    { ifstream inStream; {ifstream inStream; inStream.open("image.inp"); 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[ui][vj]) 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; } 
  • file main_count_small_image.cpp : 文件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(); } 

But, I get: 但是,我得到:

[error]: has stopped working ... [错误]:已停止工作...

This is a simple bit of code best stepped through with a debugger. 这是一段简单的代码,最好通过调试器逐步执行。 The OP will learn a lot more tracing the execution low than they will from being handed a canned answer. 与从固定答案中获得答案相比,OP将使他们从跟踪执行的过程中学到的东西更多。

Brute force works, but a previous question has an answer suggesting better approaches. 蛮力行得通,但是前面的问题有一个答案,建议采取更好的方法。 See How to detect occurrencies of a small image in a larger image? 请参阅如何在大图像中检测小图像的出现? .

The new array method is implemented incorrectly. 新的数组方法实现不正确。 Its inability to return the built array has been covered already so I'm skipping it. 它无法返回已构建数组的问题已得到解决,因此我跳过了它。 Nowhere in the specification does it say the new_array should read in the data from the file. 规范中没有任何地方说new_array应该从文件中读取数据。 Further, reopening the file will require the new stream to tart at the beginning and reread m and n before getting to the image data. 此外,重新打开文件将需要新的流在开始时进行提示,并在读取图像数据之前重新读取m和n。 This is not taken into account. 没有考虑到这一点。

The lack of descriptive variable names makes this program difficult to read and is a disincentive to assisting the OP. 缺少描述性的变量名,使得该程序难以阅读,并且不利于协助OP。 Likewise the lack of rational indentation and braces use. 同样,缺乏合理的缩进和花括号使用。 The program by its appearance seems to ask the reader not to render assistance. 该程序从外观上似乎要求读者不要提供帮助。

In count_small_image given the call 在count_small_image中给出了调用

count_small_image(a,b,m,n,p,q);

The two for loops set up small_image for out-of-range array access. 两个for循环设置small_image以进行超出范围的数组访问。 I believe that is that this is trying to prevent. 我相信这是在试图防止。

if((m-i+1)>=p && (n-j+1)>=q)

Maybe it does, but it's a convoluted and clumsy way to do it. 也许可以,但是这是一种复杂且笨拙的方式。 Remember: Code not written has no bugs. 切记:未编写的代码没有错误。 Instead, try something along the lines of 相反,请尝试以下方法

for(int m = 0; m < largeMaxM - smallMaxM; m++)
{
    for(int n = 0; n < largeMaxM - smallMaxN; n++)

Where smallMaxM and smallMaxN are the m and n bounds of the small image and largeMaxM and largeMaxN are the m and n bounds of the large image. 其中smallMaxM和smallMaxN是小图像的m和n边界,largeMaxM和largeMaxN是大图像的m和n边界。

Small count is also overly complicated. 数量少也过于复杂。 Sorting it out so that it is based on iterating through the small image eliminates the cruft. 对其进行分类,使其基于对小图像的迭代,从而消除了残留问题。 descriptive variable names also makes the function much more readable. 描述性的变量名也使函数更具可读性。

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;
}

I'm operating on tablet without a compiler, so forgive me if I'm off by one. 我在没有编译器的平板电脑上运行,因此如果我不满意,请原谅我。

You've been told wrong (or you've misunderstood what you were told). 您被告知错误(或您误解了所告诉的内容)。 Rewrite your code like this 像这样重写代码

char** new_array(int n, int m)
{
    char** a;
    ...
    return a;
}

int main()
{
     ...
     char** a = new_array(n, m);

etc. 等等

You should read up how functions can return values (including pointers). 您应该阅读函数如何返回值(包括指针)。 And also read up on how pointers can be used to implement arrays. 并进一步阅读如何使用指针来实现数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM