次の操作をサポートする単純な行エディターをシミュレートする Java のプログラムについて助けが必要でした。
$insert
後続の各行は、次の行エディター コマンドまで、テキストに挿入されます。指定された現在行がある場合、各行はその現在行の前に挿入されます。それ以外の場合、各行はテキストの最後に挿入されます。
$delete m n
行 m と行 n の間のテキストの各行が削除されます。
これまでのところ、次のようにプログラムを開始しました。
ユーザーに入力したい行を尋ねることができ、リンクリスト方式を使用して行を追加できると思いますが、次に何をすべきかわかりません。どんな助けでも大歓迎です!
私も従わなければならない特定の制限があります:
間違ったコマンドとそのパラメーターをチェックし、適切なメッセージを表示します。たとえば、テキストに 10 行目がなく、ユーザーが 10 行目を入力すると、エラー メッセージが表示されます。
リンクされたリストを使用して、ユーザーが提供するすべてのテキストを保存します。この単純化されたライン エディターのすべてのコマンドは、$ 記号で始まります。
コマンドの構文が正しくない場合は、エラー メッセージを表示して、ユーザーに再試行させます。
コマンドinsertを受け取り、次の行の最初の文字が$記号でない場合、それをテキスト行として扱い、連結リストのノードに格納します。必要に応じて、StringTokenizer を使用してテキスト行を単語ごとに処理します。
コマンドまたはテキスト行を受信した後、常に「コマンドまたはテキスト行を入力してください」というメッセージを表示します。表示されたテキストで常に現在の行を示します。デフォルトの現在の行は最初の行であり、最初の行には行 1 として番号が付けられます。
組み込みのリスト パッケージは使用できません
java.io をインポートします。; java.util をインポートします。;
public class Program_4 { public static Node head; //これが私です
public class Node //from ppt
{
String data;
Node next;
public Node()
{
data="";
next=null;
}
public Node(String x, Node n)
{
data=x;
next=n;
}
}
public void BuildList() //from ptt
{
int line_no=1;
Node q = new Node("",null);
head=q;
String oneLine;
try
{
BufferedReader indata = new BufferedReader(new InputStreamReader(System.in)); //read data from terminal
System.out.println("Please Choose a Command From the List:");
System.out.println("---------------------------------------------------------");
System.out.println("$insert");
System.out.println("$delete m n");
System.out.println("$print m n");
System.out.println("$line m");
System.out.println("$search String");
System.out.println("$done");
System.out.println("NOTE: m and n are line number parameters and string is a word you wish to search for");
System.out.println();
System.out.println("---------------------------------------------------------");
oneLine=indata.readLine();
int x=0;
while(!oneLine.equals("$done"))
{
String[] array=oneLine.split(" "); //breaks the command into an array 0,1,2
if(array[0].equals("$insert"))
{
System.out.println("Please Enter Your Text (Enter $ if you wish to terminate)");
oneLine=indata.readLine();
while(!oneLine.equals("$"))
{
String line_number = String.valueOf(line_no);
Node p = new Node(line_number +" " + oneLine, null);
q.next=p;
q=p;
line_no++;
x++;
oneLine=indata.readLine();
}
}
else if(array[0].equals("$delete"))
{
q=head.next;
Node z = new Node(q.data,q);
for(int i=1; i<x; i++)
{
String[] line=q.data.split(" ");
int lower_bound=Integer.parseInt(array[1]);
int lower_bound_2=Integer.parseInt(line[1]);
if(lower_bound==(lower_bound_2+1))
{
z=q;
break;
}
else
{
q=q.next;
}
}
for(int i=1; i<x;i++)
{
String[] line=q.data.split(" ");
if(line[0].equals(array[2]))
{
z.next=q.next;
break;
}
else
{
q=q.next;
}
}
//System.out.println(z.next.data);
}
else if(array[0].equals("$print"))
{
q=head.next;
if(array.length>1)
{
int lower_bound = Integer.parseInt(array[1]);
int upper_bound = Integer.parseInt(array[2]);
for(int i=1;i<=x;i++)
{
String[] line=q.data.split(" ");
if(line[0].equals(array[1]))
{
while(lower_bound<=upper_bound)
{
System.out.println(q.data);
q=q.next;
lower_bound++;
}
break;
}
else
{
q=q.next;
}
}
}
else
{
while(q!=null)
{
System.out.println(q.data);
q=q.next;
}
}
//see if the first array[0] is equal to m
//if it is print it out from m to n by creating a variable and doing n-m
//else q=q.next and run look again until q=!=null
}
else if(oneLine.equals("$line"))
{
System.out.println("This portion is working");
//code here for current line
}
else if(array[0].equals("$search"))
{
System.out.println("This portion is working");
//code here for search string
}
else
{
System.out.println("You have entered an incorrect command");
System.out.println("Please re-enter your Command");
}
System.out.println("Please enter your next command");
oneLine=indata.readLine();
}
System.out.println("The program is done");
}
catch(Exception e)
{
System.out.println("Error --" + e.toString());
}
}
public static void main(String[] args)
{
Program_4 mylist = new Program_4();
mylist.BuildList();
// mylist.print_list(head);