C语言到Java的代码转换,难!!!

zyg158 2004-12-12 09:50:45
#include "stdio.h"
void _copy(int *to, int *from, int count)
{
int n=(count+7)/8;
switch (count%8)
{
case 0:
do { *to++ = *from++;
case 8: *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n > 0);
}
}
void main()
{
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
int b[10] = { 9,8,7,6,5,4,3,2,1,0 };
_copy(b,a,10);
for(int i=0;i<10;i++)
printf("%d\n",b[i]);
}
...全文
194 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
taolei 2004-12-18
  • 打赏
  • 举报
回复
呵呵,在VC里还确实能编译通过。不过c/c++的语法确实是不允许这样写的。
taolei 2004-12-16
  • 打赏
  • 举报
回复
switch(xxx){
case 0:
do{
case 1:
case 2:
}
}while(xxx);

这样的写法是不正确的,c/c++里也不行。switch和do/while的语法java 和 c/c++一样
zyg158 2004-12-15
  • 打赏
  • 举报
回复
自己顶一下,高手帮忙!!!
zyg158 2004-12-14
  • 打赏
  • 举报
回复
public class file {
public void _copy(int[] to, int[] from, int count) {
int n = (count + 7) / 8;
int i = 0;
int j = 0;
do {
switch (count % 8) {
case 0:
to[i++] = from[j++];
case 8:
to[i++] = from[j++];
case 7:
to[i++] = from[j++];
case 6:
to[i++] = from[j++];
case 5:
to[i++] = from[j++];
case 4:
to[i++] = from[j++];
case 3:
to[i++] = from[j++];
case 2:
to[i++] = from[j++];
case 1:
to[i++] = from[j++];
}
} while (--n > 0);

}

public static void main(String[] args) {
file f = new file();
int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int b[] = new int[10];
f._copy(b, a, 10);
for (int i = 0; i < 10; i++)
System.out.println(b[i]);
}
}

这样做不行啊,能运行,但结果不正确的.
我想解决的是这个问题:

switch(xxx){
case 0:
do{
case 1:
case 2:
}
}while(xxx);

也就是上面那个C程序的语句嵌套的问题,想不出解决办法
希望高手指点!!!
taolei 2004-12-12
  • 打赏
  • 举报
回复

void _copy(int[]to, int[]from, int count)
{

int n=(count+7)/8;
int i=0;int j=0;
do{
switch (count%8)
{
case 0:
to[i++] = from[j++];

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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