69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
const int height1 = 125;
const int width1 = 80;
const int height2 = 100;
const int width2 = 100;
// 根据题目要求,设定数据的行数和列数
/*
resource 读入数据指针
result 结果数据指针
rh 读入数据的行数
rw 读入数据的列数
temp 模版数据指针
temph 模版数据的行数
tempw 模版数据的列数
void cov(int *resource,int *result,int rh,int rw,int *temp,int temph,int tempw)函数计算卷积结果,无返回值。
*/
void cov(int *resource,int *result,int rh,int rw,int *temp,int temph,int tempw)
{
int i,j,k,l;
printf("The function has done\n");//test
//计算数据过程中中,当所剩下的行数和列数小于模版窗口的行数和列数时就停止计算。
for(i=0; (rh-i) < temph; i++)
{printf("The function1 has done\n");//test
for(j=0;(rw-j)<tempw;j++)
{printf("The function2 has done\n");
int res=0;
//进行一次卷积计算所得到的的结果,初始值记为零。
for(k=0;k<temph;k++)
{printf("The function3 has done\n");
for(l=0;l<tempw;l++)
{printf("The function4 has done\n");
res+=resource[(i+k)*rw+j+l]*temp[l*tempw+k];
//计算卷积结果
printf("%d",res);
}
}
result[(i+1)*rw+j+1]=res;
//将计算结果赋给结果数组
}
}
}
//main 函数
int main(void)
{
FILE *data,*result1,*result2;
//data 原始数据文件指针
//result1 第一次计算结果保存的数据文件指针
//result2 第二次计算结果保存的数据文件指针
int resource[10000],result[10000];
// 读入数据存放在resource所指的数组中
// 计算结果存放在result所指的数组中
int temp1[9]={0,-1,0,-1,5,-1,0,-1,0};
int temp2[25]={0,0,-1,0,0,0,-1,-2,-1,0,-1,-2,16,-2,-1,0,-1,-2,-1,0,0,0,-1,0,0};
// 根据题目要求,设定模版
int box;
// 每读进一个数,暂时存于box中
data=fopen("SourceData.txt","r");
if(data==NULL)
{
printf("can not open this file!\n");
exit(0);
}
//检验数据文件是否成功打开
result1=fopen("result1.txt","w");
result2=fopen("result2.txt","w");
//建立两个结果文件
//将数据读入resource中,并且将result初始化。
int i;
for(i=0;i<10000;i++)
{
fscanf(data,"%d",&box);
resource[i]=box;
}
//先将数据理解为125*80,模版1(temp1)进行卷积运算。
cov(resource,result,height1,width1,temp1,3,3);
//将卷积运算的结果存入result1文件中,并关闭result1指针。
for(i=0;i<10000;i++)
{
fprintf(result1,"%6d",result[i]);
}
if(fclose(result1))
printf("Can't close this file.\n");
//将数据理解为100*100,用模版2(temp2)进行卷积运算。
cov(resource,result,height2,width2,temp2,5,5);
//将运算结果存入result2文件中,并关闭result2指针。
for(i=0;i<10000;i++)
{
fprintf(result2,"%6d",result[i]);
}
if(fclose(result2) || fclose(data))
printf("Dont't close this file.\n");
//关闭data文件指针
printf("work done!");
return 0;
}