帮忙把这段pascal翻译成c程序

Romantic007 2004-10-11 05:09:19
偶没学过pascal 看不懂

type
by200=record {建立数组D}
da:array[1..200,1..200] of byte;
end;
ds1=record {ds1:用于存放任务数据(单个任务)的数据类型}
st:string; {字符串变}
word:array[1..20] of string; {单词数组}
m:integer; {单词个数}
k:integer; {分为k份}
end;
ds=record {ds:用于存放所有任务数据的数据类型}
n:integer; {任务个数}
da:array[1..6] of ds1; {任务数据}
end;
var
i,j,max,a,a1,b,c,d,e,f:integer;
dt,p:^by200; {建立数组D}
dat:array[1..3] of ^by200; {建立数组D}
lo:ds; {lo: 用于存放任务数据}
t:boolean;
st:string;
labels:array[1..200] of byte; {labels:用于存放查找到的单词长度}
procedure loadfile(sn:string); {读文件的过程}
var
fi:text;
t1,t2,t3,t4,t5,t6:integer;
st1:string;
begin
assign(fi,sn);
reset(fi);
readln(fi,lo.n);
for t1:=1 to lo.n do
begin
readln(fi,t2,lo.da[t1].k);
for t3:=1 to t2 do
begin
readln(fi,st1);
lo.da[t1].st:=lo.da[t1].st+st1;
end;
readln(fi,t2);
lo.da[t1].m:=t2;
for t3:=1 to t2 do
readln(fi,lo.da[t1].word[t3]);
end;
close(fi);
end;

begin
writeln;
readln(st);
writeln('Open File : ',st);
loadfile(st);
for a:=1 to 3 do
new(dat[a]); {建立数组D}
for a:=1 to lo.n do
begin
for i:=1 to lo.da[a].m-1 do
for j:=i+1 to lo.da[a].m do
if lo.da[a].word[i][0]>lo.da[a].word[j][0] then {首先对单词进行排序,使单词的长度由小到大排列。
为了保证程序算出的是最优值,长度较小的单词将优先处理}
begin
st:=lo.da[a].word[i];
lo.da[a].word[i]:=lo.da[a].word[j];
lo.da[a].word[j]:=st;
end;
for i:=1 to 200 do
labels[i]:=255;
f:=length(lo.da[a].st); {f是字符串的长度}
for i:=1 to lo.da[a].m do
begin
c:=length(lo.da[a].word[i]);
for j:=1 to f-c+1 do
if (labels[j]=255) and (copy(lo.da[a].st,j,c)=lo.da[a].word[i]) then
labels[j]:=c; {labels[j]表示首字母占用字符串中第j个位置的单词的长度}
end;
for j:=1 to f do
begin
d:=0; {d用于计算D(i,j,1)}
for i:=j downto 1 do
begin
if labels[i]<=j-i+1 then d:=d+1; {如果单词的长度小于i到j的距离,
那么这个单词就包含在ST(i,j)内,如果单词含在ST(i,j)内,那么这个单词一定包含在ST(1 ~ i-1,j)内}
dat[1]^.da[i][j]:=d;
end;
end; {D(1~i,i+1~end,1) 计算完毕(1≦i≦end-1)}
for a1:=1 to lo.da[a].k-1 do {计算D(1,end,k)}
begin
if a1=1 then
begin
i:=1;
j:=2;
end
else
begin
i:=2+(a1 mod 2);
j:=2+((a1+1) mod 2); {通过i,j的交换来实现D(1,end,2~k)的计算}
end;
for b:=1 to f-a1 do
for c:=b+a1 to f do
begin
max:=0;
for d:=b+a1 to c do
begin
e:=dat[i]^.da[b][d-1]+dat[1]^.da[d][c];
if e>max then max:=e; {计算MAX(D(b,d,a)+D(d+1,c,a))}
end;
dat[j]^.da[b][c]:=max;
end;
end;
if lo.da[a].k=1 then j:=1;
writeln(dat[j]^.da[1][f]);
end;
writeln;
end.


或者说说这段程序的算法也可以
...全文
112 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
柯本 2004-10-12
  • 打赏
  • 举报
回复
人家早编好了,记得很久前就有一个DOS下的,我过的是linux下的,有源码.不过生成的东西还是要手工处理一下
conquer2004 2004-10-11
  • 打赏
  • 举报
回复
召唤牛人写一个pascal转c的编译器
o1n 2004-10-11
  • 打赏
  • 举报
回复
原来还有软件可以用呀.
学习一下,楼上的.
柯本 2004-10-11
  • 打赏
  • 举报
回复

/*我用p2c转后再改了一下,编译可行*./
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


typedef struct by200 {
/*建立数组D*/
unsigned char da[200][200];
} by200;

typedef struct ds1 {
/*ds1:用于存放任务数据(单个任务)的数据类型*/
char st[256]; /*字符串变*/
char word[20][256]; /*单词数组*/
long m; /*单词个数*/
long k; /*分为k份*/
} ds1;

typedef struct ds {
/*ds:用于存放所有任务数据的数据类型*/
long n; /*任务个数*/
ds1 da[6]; /*任务数据*/
} ds;


static long i, j, max, a, a1, b, c, d, e, f;
static by200 *dt, *p; /*建立数组D*/
static by200 *dat[3]; /*建立数组D*/
static ds lo; /*lo: 用于存放任务数据*/
static int t;
static char st[256];
static unsigned char labels[200]; /*labels:用于存放查找到的单词长度*/

char *strsub( char *d,const char *s,int p,int n)
{
strncpy(d,s+p,n);
d[n]=0;
return d;
}


static void loadfile(char *sn)

{
/*读文件的过程*/
FILE *fi; // = NULL;
long t1, t2, t3;
char st1[256];
long FORLIM;
char *TEMP;

fi=fopen(sn,"r");
fscanf(fi, "%ld%*[^\n]", &lo.n);
getc(fi);
FORLIM = lo.n;
for (t1 = 0; t1 < FORLIM; t1++) {
fscanf(fi, "%ld%ld%*[^\n]", &t2, &lo.da[t1].k);
getc(fi);
for (t3 = 1; t3 <= t2; t3++) {
fgets(st1, 256, fi);
TEMP = strchr(st1, '\n');
if (TEMP != NULL)
*TEMP = 0;
strcat(lo.da[t1].st, st1);
}
fscanf(fi, "%ld%*[^\n]", &t2);
getc(fi);
lo.da[t1].m = t2;
for (t3 = 0; t3 < t2; t3++) {
fgets(lo.da[t1].word[t3], 256, fi);
TEMP = strchr(lo.da[t1].word[t3], '\n');
if (TEMP != NULL)
*TEMP = 0;
}
}
if (fi != NULL)
fclose(fi);
//fi = NULL;
}


main(argc, argv)
int argc;
char *argv[];
{
char STR1[256];
long FORLIM, FORLIM1, FORLIM2, FORLIM3, FORLIM4;

putchar('\n');
gets(st);
printf("Open File : %s\n", st);
loadfile(st);
for (a = 1; a <= 3; a++) /*建立数组D*/
dat[a-1] = (by200 *)malloc(sizeof(by200));
FORLIM = lo.n;
for (a = 1; a <= FORLIM; a++) {
FORLIM1 = lo.da[a-1].m;
for (i = 1; i < FORLIM1; i++) {
FORLIM2 = lo.da[a-1].m;
for (j = i + 1; j <= FORLIM2; j++) {
if (strlen(lo.da[a-1].word[i-1]) > strlen(lo.da[a-1].word[j-1])) {
/*首先对单词进行排序,使单词的长度由小到大排列。
为了保证程序算出的是最优值,长度较小的单词将优先处理*/
strcpy(st, lo.da[a-1].word[i-1]);
strcpy(lo.da[a-1].word[i-1], lo.da[a-1].word[j-1]);
strcpy(lo.da[a-1].word[j-1], st);
}
}
}
for (i = 1; i <= 200; i++)
labels[i-1] = 255;
f = strlen(lo.da[a-1].st); /*f是字符串的长度*/
FORLIM1 = lo.da[a-1].m;
for (i = 1; i <= FORLIM1; i++) {
c = strlen(lo.da[a-1].word[i-1]);
FORLIM2 = f - c + 1;
for (j = 1; j <= FORLIM2; j++)
{ /*labels[j]表示首字母占用字符串中第j个位置的单词的长度*/
if ((labels[j-1] == 255) & (strcmp(strsub(STR1, lo.da[a-1].st, (int)j,
(int)c),
lo.da[a-1].word[i-1]) == 0))
labels[j-1] = c;
}
}
FORLIM1 = f;
for (j = 1; j <= FORLIM1; j++) {
d = 0; /*d用于计算D(i,j,1)*/
for (i = j; i >= 1; i--) {
if (labels[i-1] <= j - i + 1)
d++;
/*如果单词的长度小于i到j的距离,
那么这个单词就包含在ST(i,j)内,如果单词含在ST(i,j)内,那么这个单词一定包含在ST(1 ~ i-1,j)内*/
dat[0]->da[i-1][j-1] = d;
}
} /*D(1~i,i+1~end,1) 计算完毕(1≦i≦end-1)*/
FORLIM1 = lo.da[a-1].k;
for (a1 = 1; a1 < FORLIM1; a1++) { /*计算D(1,end,k)*/
if (a1 == 1) {
i = 1;
j = 2;
} else { /*通过i,j的交换来实现D(1,end,2~k)的计算*/
i = (a1 & 1) + 2;
j = ((a1 + 1) & 1) + 2;
}
FORLIM2 = f - a1;
for (b = 1; b <= FORLIM2; b++) {
FORLIM3 = f;
for (c = b + a1; c <= FORLIM3; c++) {
max = 0;
FORLIM4 = c;
for (d = b + a1; d <= FORLIM4; d++) {
e = dat[i-1]->da[b-1][d-2] + dat[0]->da[d-1][c-1];
if (e > max) /*计算MAX(D(b,d,a)+D(d+1,c,a))*/
max = e;
}
dat[j-1]->da[b-1][c-1] = max;
}
}
}
if (lo.da[a-1].k == 1)
j = 1;
printf("%12d\n", dat[j-1]->da[0][f-1]);
}
putchar('\n');
exit(0);
}

DuoFG 2004-10-11
  • 打赏
  • 举报
回复
我知道 其中用到了动态规划
:^)
Flood1984 2004-10-11
  • 打赏
  • 举报
回复
好长啊
kobefly 2004-10-11
  • 打赏
  • 举报
回复
这么长
累死啊
哈哈
grooving 2004-10-11
  • 打赏
  • 举报
回复
晕呀!

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧