作业数组和c++的指针

homework arrays and pointers of c ++

本文关键字:指针 c++ 数组 作业      更新时间:2023-10-16

在这个问题中,首先,您必须编写两个函数:

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个字符(azAZ(组成。

随后,有一些小图像,你必须找到大图像。每个小图像都是在大图像的形成过程中书写的。特别地,有一行的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);

等等。

您应该了解函数如何返回值(包括指针(。还阅读了如何使用指针来实现数组。