เป็นโปรแกรมประยุกต์ การตรวจสอบ CRC อ่ะคะ
โค้ดนี้ เราหามาจากเน็ต แล้วมาพิมพ์ตาม เราค่อยๆแก้ error ไปได้เท่าที่ได้แล้ว
แต่ติด error อันสุดท้าย แก้ไม่ได้ ไม่ทราบว่าผิดอะไร ช่วยดูหน่อยได้ไหมค่ะ
=================== CODE ================
import java.io.*;
class crc
{
public static void main(String a[]) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int[] message;
int[] gen;
int[] app_message;
int[] rem;
int[] trans_message;
int message_bits,gen_bits, total_bits;
System.out.println("n Enter number of bits in message : ");
message_bits=Integer.parseInt(br.readLine());
message=new int[message_bits];
System.out.println("n Enter message bits : ");
for(int i=0; i<message_bits; i++)
message=Integer.parseInt(br.readLine());
System.out.println("n Enter number of bits in gen : ");
gen_bits=Integer.parseInt(br.readLine());
gen=new int[gen_bits];System.out.println("n Enter gen bits : ");
for(int i=0; i<gen_bits; i++)
{
gen=Integer.parseInt(br.readLine());
}
total_bits=message_bits+gen_bits-1;
app_message=new int[total_bits];
rem=new int[total_bits];
trans_message=new int[total_bits];
for(int i=0;i<message.length;i++)
{
app_message=message;
}
System.out.print("n Message bits are : ");
for(int i=0; i< message_bits; i++)
{
System.out.print(message);
}
System.out.print("n Generators bits are : ");
for(int i=0; i< gen_bits; i++)
{
System.out.print(gen);
}
System.out.print("n Appended message is : ");
for(int i=0; i< app_message.length; i++)
{
System.out.print(app_message);
}
for(int j=0; j<app_message.length; j++)
{
rem[j] = app_message[j];
}
rem=computecrc(app_message, gen, rem);
for(int i=0;i<app_message.length;i++)
{
trans_message=(app_message^rem);
}
System.out.println("n Transmitted message from the transmitter is : ");
for(int i=0;i<trans_message.length;i++)
{
System.out.print(trans_message);
}
System.out.println("n Enter received message of "+total_bits+" bits at receiver end : ");
for(int i=0; i<trans_message.length; i++)
{
trans_message=Integer.parseInt(br.readLine());
}
System.out.println("n Received message is :");
for(int i=0; i< trans_message.length; i++)
{
System.out.print(trans_message);
}
for(int j=0; j<trans_message.length; j++)
{
rem[j] = trans_message[j];
}
rem=computecrc(trans_message, gen, rem);
for(int i=0; i< rem.length; i++)
{
if(rem!=0)
{
System.out.println("n There is Error in the received message!!!");
break;
}
if(i==rem.length-1)
{
System.out.println("n There is No Error in the received message!!!");
}
}
static int[] computecrc(int app_message[],int gen[], int rem[]) ///////// ERROR ตรงนี้ค่ะ
{
int current=0;
while(true)
{
for(int i=0;i<gen.length;i++)
{
rem[current+i]=(rem[current+i]^gen);
}
while(rem[current]==0 && current!=rem.length-1)
{
current++;
}
if((rem.length-current)<gen.length)
{
break;
}
}
return rem;
}
}
มัน Error อะคะ ช่วยแนะนำหน่อยได้ไหมคะ ไม่ค่อยถนัด java
โค้ดนี้ เราหามาจากเน็ต แล้วมาพิมพ์ตาม เราค่อยๆแก้ error ไปได้เท่าที่ได้แล้ว
แต่ติด error อันสุดท้าย แก้ไม่ได้ ไม่ทราบว่าผิดอะไร ช่วยดูหน่อยได้ไหมค่ะ
=================== CODE ================
import java.io.*;
class crc
{
public static void main(String a[]) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int[] message;
int[] gen;
int[] app_message;
int[] rem;
int[] trans_message;
int message_bits,gen_bits, total_bits;
System.out.println("n Enter number of bits in message : ");
message_bits=Integer.parseInt(br.readLine());
message=new int[message_bits];
System.out.println("n Enter message bits : ");
for(int i=0; i<message_bits; i++)
message=Integer.parseInt(br.readLine());
System.out.println("n Enter number of bits in gen : ");
gen_bits=Integer.parseInt(br.readLine());
gen=new int[gen_bits];System.out.println("n Enter gen bits : ");
for(int i=0; i<gen_bits; i++)
{
gen=Integer.parseInt(br.readLine());
}
total_bits=message_bits+gen_bits-1;
app_message=new int[total_bits];
rem=new int[total_bits];
trans_message=new int[total_bits];
for(int i=0;i<message.length;i++)
{
app_message=message;
}
System.out.print("n Message bits are : ");
for(int i=0; i< message_bits; i++)
{
System.out.print(message);
}
System.out.print("n Generators bits are : ");
for(int i=0; i< gen_bits; i++)
{
System.out.print(gen);
}
System.out.print("n Appended message is : ");
for(int i=0; i< app_message.length; i++)
{
System.out.print(app_message);
}
for(int j=0; j<app_message.length; j++)
{
rem[j] = app_message[j];
}
rem=computecrc(app_message, gen, rem);
for(int i=0;i<app_message.length;i++)
{
trans_message=(app_message^rem);
}
System.out.println("n Transmitted message from the transmitter is : ");
for(int i=0;i<trans_message.length;i++)
{
System.out.print(trans_message);
}
System.out.println("n Enter received message of "+total_bits+" bits at receiver end : ");
for(int i=0; i<trans_message.length; i++)
{
trans_message=Integer.parseInt(br.readLine());
}
System.out.println("n Received message is :");
for(int i=0; i< trans_message.length; i++)
{
System.out.print(trans_message);
}
for(int j=0; j<trans_message.length; j++)
{
rem[j] = trans_message[j];
}
rem=computecrc(trans_message, gen, rem);
for(int i=0; i< rem.length; i++)
{
if(rem!=0)
{
System.out.println("n There is Error in the received message!!!");
break;
}
if(i==rem.length-1)
{
System.out.println("n There is No Error in the received message!!!");
}
}
static int[] computecrc(int app_message[],int gen[], int rem[]) ///////// ERROR ตรงนี้ค่ะ
{
int current=0;
while(true)
{
for(int i=0;i<gen.length;i++)
{
rem[current+i]=(rem[current+i]^gen);
}
while(rem[current]==0 && current!=rem.length-1)
{
current++;
}
if((rem.length-current)<gen.length)
{
break;
}
}
return rem;
}
}