Created
April 8, 2013 02:35
-
-
Save masui/5333800 to your computer and use it in GitHub Desktop.
report1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main(){ | |
printf("hello"); | |
} |
package sample03;
import java.util.Scanner;
public class Sample15 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
System.out.print("アルファベット入力:");
Scanner scan_string = new Scanner(System.in);
String user = scan_string.next();
String message=user;//ユーザーが入力したものを文字列として以後扱う。
//エスケープシーケンスの際の一連の処理
if(user.indexOf('\\')!=-1){//お金マークがあれば、エスケープシーケンスの始まりである。それを含むか否か判定から始まる。
int escape_number = user.indexOf("\\");
String escape = user.substring(escape_number,escape_number+2);
String remove = user.replace(escape,"");//removeはエスケープ文字を取り除いた文字列を格納する。[tt\t]→[tt]
String okane = user.substring(escape_number,escape_number+1);
String array = user.replace(okane, "");//arrayはお金マークを取り除いた入力値の文字列を格納する。[tt\t]→[ttt]
//エスケープシーケンスを取り除く濾過装置
if((user.indexOf(escape))!=-1){//escape(エスケープシーケンス)を取り除いた文字列
char c = user.charAt(escape_number+1);
if(c=='t'){
String new_remove1 = array.replaceAll(""+c, "9");
message= new_remove1;
}else if(c=='b'){
String new_remove2 = array.replaceAll(""+c, "8");
message= new_remove2;
}else if(c=='n'){
String new_remove4 = array.replaceAll(""+c, "4");
message= new_remove4;
}else if(c=='v'){
String new_remove5 = array.replaceAll(""+c, "1");
message= new_remove5;
}else if(c=='f'){
String new_remove6 = array.replaceAll(""+c, "2");
message= new_remove6;
}else if(c=='r'){
String new_remove7 = array.replaceAll(""+c, "3");
message= new_remove7;
}else if(c=='a'){
String new_remove7 = array.replaceAll(""+c, "7");//注意書きとしてHTMLに記述する必要がある。
message= new_remove7;
}
}//濾過終了。エスケープシーケンスが数字を表す文字列に置換されている。そしてこの後、アルファベットの部分だけを16進数に変換する。
}
//エスケープシーケンスの[\]を取り除いた文字の変換に失敗する場合がある。
char[] first = new char[message.length()];//お金マークを抜かれた入力値の文字数
String[] new_array = new String[message.length()];//16進数化した文字を格納する新しい文字配列。下のif文の分岐した後で用いる。
for(int i=0;i<message.length();i++){
first[i]=message.charAt(i);//一文字ずつchar型の配列に格納
}
for(int k=0;k<message.length();k++){
if(first[k]>='7'&&first[k]<='9'){//数字であるから、16進数化する必要がない。
char ch = first[k];
Character character= new Character(ch);
new_array[k]="0"+character.toString();
}else if(first[k]<='3'&&first[k]>='0'){//エスケープシーケンスが二桁を超えるとき
char ch = first[k];
Character character= new Character(ch);
new_array[k]="1"+character.toString();//一文字「1」を足す。
}else if(first[k]=='4'){//文字列"10"はchar型の要素との一致を判定できない。
new_array[k]="10";
}else{//アルファベットであるため、16進数に変換して、
int y = first[k];
String z = Integer.toHexString(y);
new_array[k]=z;
}
}
//表示
System.out.println("");
System.out.print("-配列-\n[");
for(int i=0;i<message.length();i++){
System.out.print("0x"+new_array[i]+",");
}
System.out.print("]");
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
int main(void)
{
}