建立两个java文件ReAlloc.java
TestReAlloc.java
实现c中realloc()的功能提供另一个java program:GenStrings.java用来为程序的输入用
import java.util.*;class GenStrings {
static final int MAX_STR_LEN = 80;
static int numStrs;
static int maxSlen;
Random r = new Random();
void process() {
int sLen;
for(int i = 0; i < numStrs; i++) {
sLen = r.nextInt(maxSlen) + 1;
for(int j = 0; j < sLen; j++) {
System.out.printf("%c",randChar());
}
System.out.println();
}
} char randChar() {
int iVal;
char retVal;
iVal = r.nextInt(maxSlen) % 26 + 'a';
retVal = (char)iVal;
return retVal;
} public static void main(String[] args) {
if(args.length != 2) {
System.err.println(
"usage: java GenStrings numStrs maxStrLen");
System.exit(1);
}
numStrs = Integer.parseInt(args[0]);
maxSlen = Integer.parseInt(args[1]); if(maxSlen > MAX_STR_LEN) {
System.err.println("ERROR: maximum string length = "
+ MAX_STR_LEN);
System.exit(1);
} new GenStrings().process();
}
}
最后显示结果如下:
F:\java>java GenStrings
usage: java GenStrings numStrs maxStrLenF:\java>java GenStrings 5 70
hpkerarxvppssqjhmsbmsgodeicrvlefila
lfsximljzwbkfazcavdvsufvpakesieapckiatxegrhunlumaktfhd
cdtpgrbkeixdgixvwofttfaqqenjfmjharkcefhnkbkyjlszkptx
humrxyndozdiajayccruqhnuqaxeh
baogihxbccsdkpageqknkiduiipmwuoilfjpiiswyjviqckpdwxquzomwdqpsjavac TestReAlloc.java
java TestReAlloc
usage: TestReAlloc chunkSize
java TestReAlloc 5
hi
there
a
test of
stdin
^D
chunkSize: 5
total bytes read: 25
total lines read: 5
total allocs: 5java GenStrings 1000 80 | java TestReAlloc 1
chunkSize: 1
total bytes read: 42128
total lines read: 1000
total allocs: 42128java GenStrings 1000 80 | java TestReAlloc 100
chunkSize: 100
total bytes read: 41674
total lines read: 1000
total allocs: 417java GenStrings 1000 80 | java TestReAlloc 1000
chunkSize: 1000
total bytes read: 41421
total lines read: 1000
total allocs: 42java GenStrings 10000 80 | java TestReAlloc 1000
chunkSize: 1000
total bytes read: 412568
total lines read: 10000
total allocs: 413java GenStrings 100000 80 | java TestReAlloc 1000
chunkSize: 1000
total bytes read: 4151668
total lines read: 100000
total allocs: 4152
tips:
1. chunkSize为要分配的一块内存大小,假设是5时,当输入到第六个值需要再分配一个大小是5的内存,做法为生成出一个大小为10的 内存,将6个值装入,下面再继续输入的值紧接着第六个值后面开始装入
2. ^D是用来结束输入
3. EOL(End Of Line)占用一个byte,windows下的EOL占用2个byte.所以上面输入的:
问题补充:
3. EOL(End Of Line)占用一个byte,所以上面输入的:
hi
there
a
test of
stdin
一共是25个byte看错了,是下周二交老师不让用开发工具吗,要求手工代码,周二交作业,跪求答案
TestReAlloc.java
实现c中realloc()的功能提供另一个java program:GenStrings.java用来为程序的输入用
import java.util.*;class GenStrings {
static final int MAX_STR_LEN = 80;
static int numStrs;
static int maxSlen;
Random r = new Random();
void process() {
int sLen;
for(int i = 0; i < numStrs; i++) {
sLen = r.nextInt(maxSlen) + 1;
for(int j = 0; j < sLen; j++) {
System.out.printf("%c",randChar());
}
System.out.println();
}
} char randChar() {
int iVal;
char retVal;
iVal = r.nextInt(maxSlen) % 26 + 'a';
retVal = (char)iVal;
return retVal;
} public static void main(String[] args) {
if(args.length != 2) {
System.err.println(
"usage: java GenStrings numStrs maxStrLen");
System.exit(1);
}
numStrs = Integer.parseInt(args[0]);
maxSlen = Integer.parseInt(args[1]); if(maxSlen > MAX_STR_LEN) {
System.err.println("ERROR: maximum string length = "
+ MAX_STR_LEN);
System.exit(1);
} new GenStrings().process();
}
}
最后显示结果如下:
F:\java>java GenStrings
usage: java GenStrings numStrs maxStrLenF:\java>java GenStrings 5 70
hpkerarxvppssqjhmsbmsgodeicrvlefila
lfsximljzwbkfazcavdvsufvpakesieapckiatxegrhunlumaktfhd
cdtpgrbkeixdgixvwofttfaqqenjfmjharkcefhnkbkyjlszkptx
humrxyndozdiajayccruqhnuqaxeh
baogihxbccsdkpageqknkiduiipmwuoilfjpiiswyjviqckpdwxquzomwdqpsjavac TestReAlloc.java
java TestReAlloc
usage: TestReAlloc chunkSize
java TestReAlloc 5
hi
there
a
test of
stdin
^D
chunkSize: 5
total bytes read: 25
total lines read: 5
total allocs: 5java GenStrings 1000 80 | java TestReAlloc 1
chunkSize: 1
total bytes read: 42128
total lines read: 1000
total allocs: 42128java GenStrings 1000 80 | java TestReAlloc 100
chunkSize: 100
total bytes read: 41674
total lines read: 1000
total allocs: 417java GenStrings 1000 80 | java TestReAlloc 1000
chunkSize: 1000
total bytes read: 41421
total lines read: 1000
total allocs: 42java GenStrings 10000 80 | java TestReAlloc 1000
chunkSize: 1000
total bytes read: 412568
total lines read: 10000
total allocs: 413java GenStrings 100000 80 | java TestReAlloc 1000
chunkSize: 1000
total bytes read: 4151668
total lines read: 100000
total allocs: 4152
tips:
1. chunkSize为要分配的一块内存大小,假设是5时,当输入到第六个值需要再分配一个大小是5的内存,做法为生成出一个大小为10的 内存,将6个值装入,下面再继续输入的值紧接着第六个值后面开始装入
2. ^D是用来结束输入
3. EOL(End Of Line)占用一个byte,windows下的EOL占用2个byte.所以上面输入的:
问题补充:
3. EOL(End Of Line)占用一个byte,所以上面输入的:
hi
there
a
test of
stdin
一共是25个byte看错了,是下周二交老师不让用开发工具吗,要求手工代码,周二交作业,跪求答案
有某个地方不懂再来问,这样才会有人回答的。
LZ加油吧