有 40 多个 C 源码文件,每一个里面都有 main 函数,都是从 stdin 或者文件读取参数,结果直接 print 到 stdout 或者 stderr 。
我想把它全部改写成普通函数,直接传参数调用函数,然后返回状态码(编译成动态链接库然后从 Python 里面调用)。
我尝试用正则替换实现,但是只能做到把 errorprint
函数提取出来,读取参数和输出不知道怎么处理。
求 V 友们指点,非常感谢!
样本 1 :
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define Pi 3.141592654
double aa[4096];
void error_print();
void main(int argc, char *argv[]){
double *x, dum;
int i , nn, j, im, jm ;
long ik;
FILE *fr ;
if (argc< 2 || argc >2 ){
error_print(argv[0]);
exit(1);
}
fr=fopen(argv[1],"r");
x = (double *) calloc( 1, sizeof(double) );
ik=0;
while ((fscanf(fr,"%le", &dum))!= EOF){
x = (double *) realloc ( x, sizeof(double)*(ik+1));
if(x == NULL ){
fprintf(stderr,"%s: Allocation error while reading, exiting\n", argv[0]);
exit(1);
}
x[ik]=dum;
/* fprintf(stderr," %e \n", x[ik]); */
ik++;
}
nn=(int) sqrt( (double) ik);
fclose(fr);
for(i=1; i<=nn; i++){
im = nn+1-i;
for (j=1; j<=nn; j++){
double fdd=0;
jm=nn+1-j;
ik=(i-1)*nn+j-1;
fdd += x[ik];
ik=(j-1)*nn+i-1;
fdd += x[ik];
ik=(j-1)*nn+im-1;
fdd += x[ik];
ik=(i-1)*nn+jm-1;
fdd += x[ik];
/* ik=(j-1)*nn+i-1;
fdd += x[ik];
ik=(j-1)*nn+im-1;
fdd += x[ik]; */
printf("%g\n", fdd);
}
printf("\n");
}
free(x);
fclose(fr);
}
void error_print(char *arr) {
fprintf(stderr,"\n%s converts the intensity file into sum of itself three times rotated 90 degrees\n", arr);
fprintf(stderr,"\nUSAGE: %s F > M_F where F is the file in gnuplot format, M_F is the output file in the gnuplot format\n\n", arr);
}
样本 2
#include "pipes.h"
#include <math.h>
#include <string.h>
void main(int argc, char *argv[]){
void error_print();
int i,j, n2;
double sum, sum1r, sum1i, sum1, sum2, dx,dx2, s_p, x,y, x_c, y_c;
double sum1x, sum1y;
long ik1;
/* Processing the command line argument */
if (argc!= 2){
error_print(argv[0]);
exit(1);
}
read_field();
dx =field.size/(field.number);
dx2 = dx*dx;
n2=field.number/2+1;
/* Calculating the power */
sum=sum1r=sum1i=sum2=0.;
ik1=0;
for (i=1;i<=field.number ;i++){
for (j=1;j<=field.number ;j++){
s_p=(field.real[ik1]*field.real[ik1]+ \
field.imaginary[ik1]*field.imaginary[ik1]);
sum2 += s_p;
sum += sqrt(s_p);
sum1r += field.real[ik1];
sum1i += field.imaginary[ik1];
ik1++;
}
}
sum1=(sum1r*sum1r+sum1i*sum1i);
if (sum == 0) {
fprintf(stderr,"Strehl: Zero beam power, program terminated\n");
exit(1);
}
if(strstr(argv[1], "y")!= NULL)fprintf(stderr,"Strehl: ratio= %e energy= %e\n",sum1/sum/sum, sum2*dx2);
/* Calculating the center of gravity: */
sum=sum1r=sum1i=sum2=0.;
ik1=0;
for (i=1;i<=field.number ;i++){
y=(i-n2)*dx;
for (j=1;j<=field.number ;j++){
x=(j-n2)*dx;
sum2=(field.real[ik1]*field.real[ik1]\
+field.imaginary[ik1]*field.imaginary[ik1]);
sum1r += sum2*x;
sum1i += sum2*y;
sum += sum2;
ik1++;
}
}
x_c=sum1r/sum;
y_c=sum1i/sum;
fprintf(stderr,"Center_of_gravity: x= %e y= %e\n", x_c, y_c);
/* Calculating moments of the distribution */
sum1r=sum1x=sum1y=0.;
ik1=0;
for (i=1;i<=field.number ;i++){
double y_y_c;
y=(i-n2)*dx;
y_y_c=y-y_c;
for (j=1;j<=field.number ;j++){
double temp_int, x_x_c;
x=(j-n2)*dx;
x_x_c=x-x_c;
temp_int = (field.real[ik1]*field.real[ik1]\
+field.imaginary[ik1]*field.imaginary[ik1]);
sum1r += temp_int*(x_x_c*x_x_c+y_y_c*y_y_c);
sum1x += temp_int*(x_x_c*x_x_c);
sum1y += temp_int*(y_y_c*y_y_c);
ik1++;
}
}
fprintf(stderr,"Standard deviation: S_r=%e S_x= %e S_y= %e\n", sqrt(sum1r/sum), sqrt(sum1x/sum), sqrt(sum1y/sum));
fprintf(stderr,"Grid size: %e, Grid sampling: %d\n", field.size, field.number);
write_field();
}
void error_print(char *arr) {
fprintf(stderr,"\n%s: prints the general info to the stderr\n",arr);
fprintf(stderr,"\n%s y, y prevents arrival of this message\n\n",arr);
}
1
wentian 2017-06-20 12:38:45 +08:00
要是我, 我会手动去做这件事情
如果要用代码实现, 可能一天过去了, 一个屁都还没有写出来 |