#include
#include
#include
void run(char *name);
void Readfile1(float *Matrix,char *Filename,int m,int n);
void Readfile2(float *Matrix,char *Filename,int k);
void reshape(float *pB,float *pA);
void Addition(float *pD,float *pE,float *pF,int m);
void Multiply(float *pB,float *pC,float *pD,int m,int n,int k);
float max(float G,int m);
int SoftMax(float *pL,float *pS);
int main(int argc, char *argv[]){
char string[50],str[6];
int b,c=0;
while(1){
printf("Please input filenames:");
gets(string);//read the files' name
//puts(string);
for(b = 0;string[b] != '\0';b++){
if((string[b+1]==',')||(string[b+1] == '\0')){
strncpy(str,string+c,b-c+1);
puts(str);
c = b+2;
run(str);
}
}
char z;
printf("do you want to continue? please input [Y or N]:");
getchar();
scanf("%c",&z);
if(z == 'Y')
continue;
if(z == 'N')//Only input N can we jump out of the loop
break;
}
printf("Bye\n");
return 0;
}
void run(char *name){
FILE *image;
int m = 0,i,j;
float pixel[28][28],A[28][28],B[784];
char skip[256],str[6];
if(strncmp(str+1,".pgm",4)==0){
printf("%s\n",str);
image = fopen("example0.pgm","rb");//open the image
for(i = 0;i < 4;i++){
fgets(skip,256,image);//skip the first four lines
}
for(i = 0;i < 28;i++){
for(j = 0;j < 28;j++){
pixel[i][j] = (float)fgetc(image);
//printf("%.2f ",(float)pixel[i][j]);
A[i][j] = pixel[i][j]/255.0;//assign the value of pixel/255.0 to matrix A
//printf("%.2f ",A[i][j]);
}
//printf("\n");
}
fclose(image);
reshape(B,A[0]);
float C[784][128],D[128];
Readfile1(C[0],"W1.txt",784,128);//Read data from "W1.txt" to C
Multiply(B,&C[0][0],D,1,784,128);
//for(i=0;i<128;i++){
//printf("%.2f\n",D[i]);
//}
float E[128],F[128];
Readfile2(E,"B1.txt",128);//Read data from "B1.txt" to E
Addition(D,E,F,128);
//for(i = 0;i < 128;i++){
//printf("%.2f\n",F[i]);
//}
float G[128];
for(m = 0;m < 128;m++){
G[m] = max(F[m],0);
}
//for(i = 0;i < 128;i++){
//printf("%.2f\n",G[i]);
//}
float M[128][10],H[10];
Readfile1(M[0],"W2.txt",128,10);//Read data from "W2.txt" to M
Multiply(G,M[0],H,1,128,10);
//for(i=0;i<10;i++){
//printf("%f\n",H[i]);
//}
float N[10],L[10];
Readfile2(N,"B2.txt",10);//Read data from "B2.txt" to N
Addition(H,N,L,10);
//for(i = 0;i < 10;i++){
//printf("%.2f\n",L[i]);
//}
float S[10];
int result;
result = SoftMax(L,S);
printf("%s:%d\n",str,result);
}
else{
printf("Invalid file.\n");//If the file name entered dose not meet the requirement
}
return;
}
void Readfile1(float *Matrix,char *filename,int m,int n){
int i,j;
FILE *fp1;
fp1 = fopen(filename,"r");
if(fp1 == NULL)
return ;
for(i = 0;i < m;i++){
for(j = 0;j < n;j++){
fscanf(fp1,"%f",Matrix + i*n + j);//Read two-dimensional array
//printf("%f\n",*(Matrix + i*n + j));
}
}
fclose(fp1);
}
void Readfile2(float *Matrix,char *filename,int k){
int i;
FILE *fp2;
fp2 = fopen(filename,"r");
if(fp2 == NULL)
return ;
for(i = 0;i < k;i++){
fscanf(fp2,"%f",Matrix + i);//Read one-dimensional array
}
fclose(fp2);
}
void reshape(float *pB,float *pA){
for(int m = 0;m < 28;m++){
for(int n = 0;n < 28;n++){
*(pB + m * 28 + n) = *(pA + n + m * 28);////convert two-dimensional array to one-dimension array
}
}
}
void Multiply(float *pB,float *pC,float *pD,int m,int n,int k){
int p,i,j;
//printf("%.2f",*pB);
for(i = 0;i < m;i++){
for(j = 0;j < k;j++){
for(p = 0;p < n;p++){
*(pD + i * k + j) += (*(pB + i * n + p)) * (*(pC + p * k + j));//Multiply the matrix
}
//printf("%.2f ",*(pD + i * k + j));
}
}
}
void Addition(float *pD,float *pE,float *pF,int m){
int i;
for(i = 0;i < m;i++){
*(pF + i) = *(pD + i) + *(pE + i);//Add the matrix
}
return;
}
float max(float num,int m){
if(num >= m)
return num;
else
return 0.000000;//return negative numbers to zero
}
int SoftMax(float *pL,float *pS){
int i,m,Max = 0;
float sum = 0;
double max = 0;
for(i = 0;i < 10;i++){
max = (double)*pL;
if(max < (double)*(pL+i)){
max = (double)*(pL+i);
Max = i;//To find the max in *pL
}
}
for(i = 0;i < 10;i++){
sum += (float)exp(*(pL+i)-max);
}
for(m = 0;m < 10;m++){
*(pS+m) = (float)(exp(*(pL+m)-max)) / sum;//S[m]=e^((L[m]-max?(L)))/(°∆_(i=0)^9?e^((L[i]-max?(L))) )
}
return Max;//Find the number corresponding to the maximum value
}
output:
如果你发现字符串少了一位,说明多加了getchar()把那一个字符读走了
如果你发现读入的字符串是个空,说明读到了个换行符,应该加个getchar()把换行读走
scanf会将换行遗弃在缓冲区里,所以后面需要加getchar
而gets会顺便把换行也读走,后面不需要加getchar