TestDefinition:
· Variable: Begin with character set [‘a’ – ‘z’, ‘_’], variable can contain char set [‘a’ – ‘z’, ‘_’, ‘0’ – ‘9’]. Sensitive Case.
· Constant: Only contain the char set [‘0’ – ‘9’], and range is 0 to 2147483647.
· Assignment Expression:
o Assign constant to variable
For example:
A1 = 1;
o Assign variable to variable
For example:
A1 = 1;
A2 = 2;
A1 = A2;
o Assignment : ‘=’
o Assignment separator: ‘;’
· Error (the point ed by * is optional implementation)
o Number format error
For example:
A1 = 1a12;
o Variable format error
For example:
1A1 = 112;
o Assignment expression format error
For example:
A1 == 1a12;
A1 = A2 = 112;o * Assignment separator expected
For example:
A1 = 112
o *Variable expected
For example:
= 112;
o *Constant expected
For example:
A1 = ;
o Assign error
§ No initial value
For example:
A1 = 1;
A3 = A2;
§ Modify Constant
For example:
1 = A1;
Input:
A text file contains a set of assignment expressions.
For example:
A1 = 1;
A2 = 2;
A3 = 3;
A2 = A3;
A1 = 3;Output
List all the variables that have the same value and list with their value together.
For example:
After process the above expressions, you need output the result like this:
[A1, A2, A3] = 3;
Requirement:
· Design document (English only) is required. This document should write before you write any code.
· Code should be consistent with java code convention.
· Program can identify and handle all the errors in definition part.
Test point:
· Correctness
o Identify all error in expression
o Treat expressions correctly
o Output correct result· Reliability
o Processing can not be interrupted by incorrect input or other reason
o Heap will not overflow when big amount of expressions are processed· Performance
o Amount of the expressions can be treated in 1 second· Memory
o Memory occupied when 100000 correct expressions are treated.
· Variable: Begin with character set [‘a’ – ‘z’, ‘_’], variable can contain char set [‘a’ – ‘z’, ‘_’, ‘0’ – ‘9’]. Sensitive Case.
· Constant: Only contain the char set [‘0’ – ‘9’], and range is 0 to 2147483647.
· Assignment Expression:
o Assign constant to variable
For example:
A1 = 1;
o Assign variable to variable
For example:
A1 = 1;
A2 = 2;
A1 = A2;
o Assignment : ‘=’
o Assignment separator: ‘;’
· Error (the point ed by * is optional implementation)
o Number format error
For example:
A1 = 1a12;
o Variable format error
For example:
1A1 = 112;
o Assignment expression format error
For example:
A1 == 1a12;
A1 = A2 = 112;o * Assignment separator expected
For example:
A1 = 112
o *Variable expected
For example:
= 112;
o *Constant expected
For example:
A1 = ;
o Assign error
§ No initial value
For example:
A1 = 1;
A3 = A2;
§ Modify Constant
For example:
1 = A1;
Input:
A text file contains a set of assignment expressions.
For example:
A1 = 1;
A2 = 2;
A3 = 3;
A2 = A3;
A1 = 3;Output
List all the variables that have the same value and list with their value together.
For example:
After process the above expressions, you need output the result like this:
[A1, A2, A3] = 3;
Requirement:
· Design document (English only) is required. This document should write before you write any code.
· Code should be consistent with java code convention.
· Program can identify and handle all the errors in definition part.
Test point:
· Correctness
o Identify all error in expression
o Treat expressions correctly
o Output correct result· Reliability
o Processing can not be interrupted by incorrect input or other reason
o Heap will not overflow when big amount of expressions are processed· Performance
o Amount of the expressions can be treated in 1 second· Memory
o Memory occupied when 100000 correct expressions are treated.
解决方案 »
- HANDLE *phDeviceHandle 这个是.h里面的参数,在java里面应该是什么样子?
- 怎么在java画图板上显示鼠标当前的坐标?
- 哪一句出错了?
- 动态生成复选框
- 怎样分开检索一个double型变量的整数和小数部分
- 关于session 的问题!
- 可以给一个在JDK1.4下用的CachedRowSet包吗?
- 有关 BasicTableHeaderUI 的问题,急急急急急急
- 在Jb6中进行java swing开发, 有很多的IconImage,你是怎样管理他们的?
- 请教:如何配置weblogic6.1? (苯苯)
- Component类问题请教各位达人
- 如何在JTable中实现某一列不可编辑?并且某些行的编辑框是下拉列表而其他是文本框?
import java.util.Iterator;
import java.util.Map;public class T4 { public static HashMap MAP1 = new HashMap();
public static HashMap MAP2 = new HashMap(); public T4() {
} public static void setMAP1() { if (MAP1.size() > 0) {//MAP1 为非空
Iterator it1 = MAP1.entrySet().iterator();
Map.Entry entry1 = (Map.Entry) it1.next();
Object key1 = entry1.getKey();//第一个key
Object value1 = entry1.getValue();//第一个value
MAP2.put(key1, value1);//将MAP1的第一key和value存入MAP2
//搜索MAP1的其他部分,将有相同value的key存入MAP2
while (it1.hasNext()) {
Map.Entry entry2 = (Map.Entry) it1.next();
Object key2 = entry2.getKey();
Object value2 = entry2.getValue();
if (value2.equals(value1)){
MAP2.put(key2,value2);
}
}
} Iterator it2 = MAP2.entrySet().iterator();
while (it2.hasNext()) {
Map.Entry entry3 = (Map.Entry) it2.next();
Object key3 = entry3.getKey();
Object value3 = entry3.getValue();
MAP1.remove(key3);//删除MAP1中与MAP2相同的key
System.out.println(" " + key3.toString() + "\t\t" + value3.toString());
}
System.out.println("******************************************");
MAP2.clear();//清理MAP2
if (!MAP1.isEmpty()) {
setMAP1();
} } public static void main(String[] args) {
for (int i = 1; i < 11; i++) {
MAP1.put("v" + i, "" + i);
}
for (int j = 11; j < 51; j++) {
MAP1.put("v" + j, "" + (j-10));
}
setMAP1();
}
}
* Test
*
* @version 1.0.0.0
*
* Created on 2005-5-19
*
* @author maoren.zhang
*
* @link [email protected]
*/package Test;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;public class Test { public static HashMap MAP1 = new HashMap(); public static HashMap MAP2 = new HashMap(); public Test() {
} /**
* createFile(String fileName)
*
* 作用:根据指定文件名创建一个新文件。如果存在同名文件则删除旧文件并创建新的以替换
*
* @param String
* fileName for example c:/test.txt
*
* @return void
*/
public static void createFile(String fileName) {
try {
File myFile = new File(fileName);
if (myFile.exists()) {
myFile.delete();
myFile.createNewFile();
} else {
myFile.createNewFile();
}
} catch (Exception e) {
System.out.println("Error to Create New File!");
e.printStackTrace();
}
} /**
* readFile(String fileName)
*
* 作用:读取指定名称的txt文本文件内容。
*
* @param String
* fileName for example c:/test.txt
*
* @return void
*/
public static void readFile(String fileName) {
String record = null;
int recCount = 0;
try {
FileReader filereader = new FileReader(fileName);
BufferedReader reader = new BufferedReader(filereader);
record = new String();
while ((record = reader.readLine()) != null) {
recCount++;
System.out.println(recCount + ": " + record);
}
reader.close();
filereader.close();
} catch (IOException e) {
System.out.println("Error to Read the file");
e.printStackTrace();
}
} /**
* writeFile(String fileName,String content)
*
* 作用:将字符串写入指定文件。
*
* @param String
* fileName for example c:/test.txt
*
* @param String
* content for example "This Content will be write into the
* file!"
*
* @return void
*/
public static void writeFile(String fileName, String content) {
try {
RandomAccessFile rf = new RandomAccessFile(fileName, "rw");
rf.seek(rf.length());
rf.writeBytes(content);
rf.close();
} catch (IOException e) {
System.out.println("Error to Write the file");
e.printStackTrace();
}
} public static void setMAP() {
StringBuffer bf = new StringBuffer();
bf.append("[");
if (MAP1.size() > 0) {//MAP1 为非空
Iterator it1 = MAP1.entrySet().iterator();
Map.Entry entry1 = (Map.Entry) it1.next();
Object key1 = entry1.getKey();//第一个key
Object value1 = entry1.getValue();//第一个value
MAP2.put(key1, value1);//将MAP1的第一key和value存入MAP2
//搜索MAP1的其他部分,将有相同value的key存入MAP2
while (it1.hasNext()) {
Map.Entry entry2 = (Map.Entry) it1.next();
Object key2 = entry2.getKey();
Object value2 = entry2.getValue();
if (value2.equals(value1)) {
MAP2.put(key2, value2);
}
}
}
String key3 = null, value3 = null;
Iterator it2 = MAP2.entrySet().iterator();
while (it2.hasNext()) {
Map.Entry entry3 = (Map.Entry) it2.next();
key3 = entry3.getKey().toString();
value3 = entry3.getValue().toString();
bf.append(key3 + ",");
MAP1.remove(key3);//删除MAP1中与MAP2相同的key
}
bf.deleteCharAt(bf.length() - 1);
bf.append("] = " + value3 + "\r\n");
writeFile("Result.txt", bf.toString());
MAP2.clear();//清理MAP2 if (!MAP1.isEmpty()) {
setMAP();
}
} public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date d1 = new Date();
System.out.println(df.format(d1));
System.out.println("Process Begin");
createFile("Result.txt");
createFile("Error.txt");
for (int i = 1; i < 501; i++) {
MAP1.put("V" + i, "" + i);
}
for (int j = 501; j < 1001; j++) {
MAP1.put("V" + j, "" + (j - 500));
}
setMAP();
Date d2 = new Date();
System.out.println(df.format(d2));
System.out.println("Process Begin");
System.out.println("Process End,Pleace See Result.txt And Error.txt");
}
}
* Test
*
* @version 1.0.0.0
*
* Created on 2005-5-19
*
* @author maoren.zhang
*
* @link [email protected]
*/package test;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;public class T4 { public static HashMap MAP1 = new HashMap(); public static HashMap MAP2 = new HashMap(); public T4() {
} /**
* createFile(String fileName)
*
* 作用:根据指定文件名创建一个新文件。如果存在同名文件则删除旧文件并创建新的以替换
*
* @param String
* fileName for example c:/test.txt
*
* @return void
*/
public static void createFile(String fileName) {
try {
File myFile = new File(fileName);
if (myFile.exists()) {
myFile.delete();
myFile.createNewFile();
} else {
myFile.createNewFile();
}
} catch (Exception e) {
System.out.println("Error to Create New File!");
e.printStackTrace();
}
} /**
* readFile(String fileName)
*
* 作用:读取指定名称的txt文本文件内容。
*
* @param String
* fileName for example c:/test.txt
*
* @return void
*/
public static void readFile(String fileName) {
String record = null;
int recCount = 0;
try {
FileReader filereader = new FileReader(fileName);
BufferedReader reader = new BufferedReader(filereader);
record = new String();
while ( (record = reader.readLine()) != null) {
recCount++;
System.out.println(recCount + ": " + record);
}
reader.close();
filereader.close();
} catch (IOException e) {
System.out.println("Error to Read the file");
e.printStackTrace();
}
} /**
* writeFile(String fileName,String content)
*
* 作用:将字符串写入指定文件。
*
* @param String
* fileName for example c:/test.txt
*
* @param String
* content for example "This Content will be write into the
* file!"
*
* @return void
*/
public static void writeFile(String fileName, String content) {
try {
RandomAccessFile rf = new RandomAccessFile(fileName, "rw");
rf.seek(rf.length());
rf.writeBytes(content);
rf.close();
} catch (IOException e) {
System.out.println("Error to Write the file");
e.printStackTrace();
}
} public static void createResult() {
if (!MAP1.isEmpty()) {
Iterator it = MAP1.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
setMap(key, value);
}
}
MAP1.clear();
} private static void createFile() {
StringBuffer bf = new StringBuffer();
Iterator it = MAP2.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
bf.append("[");
bf.append(entry.getKey() + "]=");
bf.append(entry.getValue() + ";\r\n");
writeFile("Result.txt", bf.toString());
bf.setLength(0);
} } public static void setMap(Object k, Object v) {
if (MAP2.isEmpty()) {
MAP2.put(k, v);
} else {
if (MAP2.containsValue(v)) {
Iterator it = MAP2.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
if (value.equals(v)) {
MAP2.remove(key);
MAP2.put(key + "," + k, v);
break;
}
}
} else {
MAP2.put(k, v);
}
}
} public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date d1 = new Date();
System.out.println(df.format(d1));
System.out.println("Process Begin");
createFile("Result.txt");
createFile("Error.txt");
for (int i = 1; i < 501; i++) {
MAP1.put("V" + i, "" + i);
}
for (int j = 501; j < 1001; j++) {
MAP1.put("V" + j, "" + (j - 300));
}
createResult();
Date d2 = new Date();
System.out.println(df.format(d2));
System.out.println("Process End");
System.out.println("Create Result.txt ,Please Wait.....");
createFile();
Date d3 = new Date();
System.out.println(df.format(d3));
System.out.println("All Step End,Pleace See Result.txt And Error.txt");
}
}
* Test
*
* @version 1.0.0.0
*
* Created on 2005-5-19
*
* @author maoren.zhang
*
* @link [email protected]
*/package Test;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;public class Test { private static HashMap MAP1 = new HashMap(); private static HashMap MAP2 = new HashMap(); public Test() {
} /**
* createFile(String fileName)
*
* 作用:根据指定文件名创建一个新文件。如果存在同名文件则删除旧文件并创建新的以替换
*
* @param String
* fileName for example c:/test.txt
*
* @return void
*/
private static void createFile(String fileName) {
try {
File myFile = new File(fileName);
if (myFile.exists()) {
myFile.delete();
myFile.createNewFile();
} else {
myFile.createNewFile();
}
} catch (Exception e) {
System.out.println("Error to Create New File!");
e.printStackTrace();
}
} /**
* writeFile(String fileName,String content)
*
* 作用:将字符串写入指定文件。
*
* @param String
* fileName for example c:/test.txt
*
* @param String
* content for example "This Content will be write into the
* file!"
*
* @return void
*/
private static void writeFile(String fileName, String content) {
try {
RandomAccessFile rf = new RandomAccessFile(fileName, "rw");
rf.seek(rf.length());
rf.writeBytes(content);
rf.close();
} catch (IOException e) {
System.out.println("Error to Write the file");
e.printStackTrace();
}
} /**
* isVarriable(String s) judge s is a Variable
*
* @param String
* s for example "ABCD"
* @return boolean
*/
private static boolean isVariable(String s) {
boolean flag = false;
if (s != null && s.length() > 0) {
if ((s.charAt(0) >= 'a' && s.charAt(0) <= 'z')
|| s.charAt(0) == '_') {
flag = true;
}
}
return flag;
} /**
* isConstant(String s) judge s is a Constant
*
* @param String
* s for example "123"
* @return boolean
*/
private static boolean isConstant(String s) {
boolean flag = true;
if (s != null && s.length() > 0) {
char[] c = s.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] < '0' || c[i] > '9') {
flag = false;
break;
}
}
}
return flag;
} /**
* inPut(String fileName)
*
* 作用:读取指定名称的txt文本文件内容,将其内部的没一行作为一个表达式判断。
*
* @param String
* fileName for example c:/test.txt
*
* @return void
*/
public static void inPut(String fileName) {
try {
FileReader filereader = new FileReader(fileName);
BufferedReader reader = new BufferedReader(filereader);
int recCount = 0;
String record = new String();
while ((record = reader.readLine()) != null) {
recCount++;
analyse("Line " + recCount + ": " + record);
System.out.println(recCount + ":" + record);
}
reader.close();
filereader.close();
} catch (IOException e) {
System.out.println("Error to Read input text file");
e.printStackTrace();
}
}
* analyse(String s)
*
* 作用:如果str为一个合法表达式,将其分解后保存于hashmap中;如为非法表达式,记录起错误信息。
*
* @param String
* str for example "A1 = 3;"
*
* @return void
*/
//更改常量,变量的属性为String,返回三种可能的值(合法,非法,空)
private static void analyse(String s) {
String head = s.substring(0, s.indexOf(":") + 1);
String content = s.substring(s.indexOf(":") + 2);
if (content.length() > 0) {
if (!content.endsWith(";")) {
writeFile("Error.txt", head + "Syntax error,expected ';' \r\n ");
} else {
int p = content.indexOf("=");
switch (p) {
case -1:
writeFile("Error.txt", head + "Sysntax error,miss '=';\r\n");
break;
case 0:
writeFile("Error.txt", head
+ "Sysntax error,Variable expected;\r\n");
break;
default:
String left = content.substring(0, p);
String right = content.substring(p + 1, content
.indexOf(";"));
if (isVariable(left)) {
if (isConstant(right)) {
MAP1.put(left, right);
}
if (isVariable(right)) {
if (!MAP1.containsKey(right)) {
writeFile("Error.txt", head
+ "No initial value;\r\n");
} else {
Iterator it = MAP1.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
if (key.equals(right)) {
MAP1.put(left, value);
}
}
}
}
if (right.trim().length() == 0) {
writeFile("Error.txt", head
+ "Constant expected;\r\n");
}
}
if (isConstant(left)) { writeFile("Error.txt", head + "Modify Constant ;\r\n");
}
if (left.trim().length() == 0) {
writeFile("Error.txt", head + "Variable expected;\r\n");
}
break;
}
}
}
} /**
* createOutMap()
*
* 作用:合并hashmap中具有相同value的key。
*
* @return void
*/
private static void createOutMap() {
if (!MAP1.isEmpty()) {
Iterator it1 = MAP1.entrySet().iterator();
while (it1.hasNext()) {
Map.Entry entry1 = (Map.Entry) it1.next();
Object key1 = entry1.getKey();
Object value1 = entry1.getValue();
if (MAP2.isEmpty()) {
MAP2.put(key1, value1);
} else {
Iterator it2 = MAP2.entrySet().iterator();
boolean bool = false;
while (it2.hasNext()) {
Map.Entry entry2 = (Map.Entry) it2.next();
Object key2 = entry2.getKey();
Object value2 = entry2.getValue();
if (value2.equals(value1)) {
MAP2.remove(key2);
MAP2.put(key2 + "," + key1, value1);
bool = true;
break;
}
}
if (!bool) {
MAP2.put(key1, value1);
}
} }
}
MAP1.clear();
} public static void outPut() {
StringBuffer bf = new StringBuffer();
Iterator it = MAP2.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
bf.append("[");
bf.append(entry.getKey() + "]=");
bf.append(entry.getValue() + ";\r\n");
writeFile("Result.txt", bf.toString());
bf.setLength(0);
}
} //main method
public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS"); createFile("Result.txt");
createFile("Error.txt"); Date d1 = new Date();
System.out.println("Process Begin." + df.format(d1));
System.out.println("Reading the Input text file ,Please Wait...");
inPut("input.txt");
Date d2 = new Date();
System.out.println(df.format(d2));
System.out.println("begin analyse Expression");
createOutMap();
Date d3 = new Date();
System.out.println(df.format(d3));
System.out.println("Create rusult.txt and error.txt,please wait...");
outPut();
Date d4 = new Date();
System.out.println("Process End." + df.format(d4));
System.out.println("Please view result.txt and error.txt");
}}
//使用switch ..case字句分类处理
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;public class T1 { private static HashMap MAP1 = new HashMap(); private static HashMap MAP2 = new HashMap(); public T1() {
} /**
* createFile(String fileName)
*
* 作用:根据指定文件名创建一个新文件。如果存在同名文件则删除旧文件并创建新的以替换
*
* @param String
* fileName for example c:/test.txt
*
* @return void
*/
private static void createFile(String fileName) {
try {
File myFile = new File(fileName);
if (myFile.exists()) {
myFile.delete();
myFile.createNewFile();
} else {
myFile.createNewFile();
}
} catch (Exception e) {
System.out.println("Error to Create New File!");
e.printStackTrace();
}
} /**
* writeFile(String fileName,String content)
*
* 作用:将字符串写入指定文件。
*
* @param String
* fileName for example c:/test.txt
*
* @param String
* content for example "This Content will be write into the
* file!"
*
* @return void
*/
private static void writeFile(String fileName, String content) {
try {
RandomAccessFile rf = new RandomAccessFile(fileName, "rw");
rf.seek(rf.length());
rf.writeBytes(content);
rf.close();
} catch (IOException e) {
System.out.println("Error to Write the file");
e.printStackTrace();
}
} /**
* checkStr(String s)
* @param s
* @return 1- s is null or s.length=1
* 2- s is Constant
* 3- s is Variable
* 4- undefinded
*/
private static int checkStr(String s) {
int r = 4;
if (s == null || s.trim().length() == 0) {
return 1;
} else {
char c = s.charAt(0);
s = s.substring(1);
if (s.length() == 0) {
if (c >= '0' && c <= '9') {
return 2;
}
if (c == '_' || (c >= 'a' && c <= 'z')) {
r = 3;
} else {
r = 4;
}
} else {
char[] c1 = s.toCharArray();
boolean bool = true;
if (c >= '0' && c <= '9') {
for (int i = 0; i < c1.length; i++) {
if (c1[i] < '0' || c1[i] > '9') {
bool = false;
break;
}
}
if (bool) {
return 2;
}
}
if (c == '_' || (c >= 'a' && c <= 'z')) {
for (int i = 0; i < c1.length; i++) {
if (c1[i] > 'z' || c1[i] < '0' ||
(c1[i] > '9' && c1[i] < 'a' && c1[i] != '_')) {
bool = false;
break;
}
}
if (bool) {
r = 3;
}
}
}
}
return r;
}
try {
FileReader filereader = new FileReader(fileName);
BufferedReader reader = new BufferedReader(filereader);
int recCount = 0;
String record = new String();
while ( (record = reader.readLine()) != null) {
recCount++;
analyse("Line " + recCount + ": " + record);
System.out.println(recCount + ":" + record);
}
reader.close();
filereader.close();
} catch (IOException e) {
System.out.println("Error to Read input text file");
e.printStackTrace();
}
}
private static void analyse(String s) {
String head = s.substring(0, s.indexOf(":") + 1);
String content = s.substring(s.indexOf(":") + 2);
if (content.length() > 0) {
if (!content.endsWith(";")) {
writeFile("Error.txt",
head + "Syntax error,expected ';' \r\n ");
} else {
int p = content.indexOf("=");
switch (p) {
case -1:
writeFile("Error.txt",
head + "Sysntax error,miss '=';\r\n");
break;
case 0:
writeFile("Error.txt", head
+ "Sysntax error,Variable expected;\r\n");
break;
default:
String left = content.substring(0, p);
String right = content.substring(p + 1, content
.indexOf(";"));
switch (checkStr(left)) {
case 1:
writeFile("Error.txt",
head +
"Sysntax error,Variable expected;\r\n");
break;
case 2:
writeFile("Error.txt",
head +
"Sysntax error,Modify Constant;\r\n");
break;
case 3:
if (checkStr(right) == 1) {
writeFile("Error.txt",
head +
"Sysntax error,Constant expected;\r\n");
}
if (checkStr(right) == 2) {
MAP1.put(left, right);
}
if (checkStr(right) == 3) {
if (!MAP1.containsKey(right)) {
writeFile("Error.txt", head
+ "No initial value;\r\n");
} else {
Iterator it = MAP1.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.
next();
Object key = entry.getKey();
Object value = entry.getValue();
if (key.equals(right)) {
MAP1.put(left, value);
}
}
}
} if (checkStr(right) == 4) {
writeFile("Error.txt",
head +
"Sysntax error,Constant format error;\r\n");
} break;
case 4:
writeFile("Error.txt",
head +
"Sysntax error,Variable format error;\r\n");
break;
default:
writeFile("Error.txt",
head + "unkown error;\r\n");
break;
}
break;
}
}
}
}
private static void createOutMap() {
if (!MAP1.isEmpty()) {
Iterator it1 = MAP1.entrySet().iterator();
while (it1.hasNext()) {
Map.Entry entry1 = (Map.Entry) it1.next();
Object key1 = entry1.getKey();
Object value1 = entry1.getValue();
if (MAP2.isEmpty()) {
MAP2.put(key1, value1);
} else {
Iterator it2 = MAP2.entrySet().iterator();
boolean bool = false;
while (it2.hasNext()) {
Map.Entry entry2 = (Map.Entry) it2.next();
Object key2 = entry2.getKey();
Object value2 = entry2.getValue();
if (value2.equals(value1)) {
MAP2.remove(key2);
MAP2.put(key2 + "," + key1, value1);
bool = true;
break;
}
}
if (!bool) {
MAP2.put(key1, value1);
}
}
}
}
MAP1.clear();
} public static void outPut() {
StringBuffer bf = new StringBuffer();
Iterator it = MAP2.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
bf.append("[");
bf.append(entry.getKey() + "]=");
bf.append(entry.getValue() + ";\r\n");
writeFile("Result.txt", bf.toString());
bf.setLength(0);
}
}//main method
public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd hh:mm:ss.SSS"); createFile("Result.txt");
createFile("Error.txt"); Date d1 = new Date();
System.out.println("Process Begin." + df.format(d1));
System.out.println("Reading the Input text file ,Please Wait...");
inPut("Input.txt");
Date d2 = new Date();
System.out.println(df.format(d2));
System.out.println("begin analyse Expression");
createOutMap();
Date d3 = new Date();
System.out.println(df.format(d3));
System.out.println("Create rusult.txt and error.txt,please wait...");
outPut();
Date d4 = new Date();
System.out.println("Process End." + df.format(d4));
System.out.println("Please view result.txt and error.txt");
}}
* About the TestPaper:
*
* Definition: Variable: Begin with character set ['a'-'z','_'],can contain char
* set['a'-'z','_','0'-'9']. Constant: Only contain the char set ['0'-'9'],
* constant range is 0 to 2147483647. Expression: Assign constant to variable
* For example: A1 = 1; Assign variable to variable For example: A1 = 1; A2 = 2;
* A1 = A2; Assignment : '=' Assignment separator: ';'
*
* Input: A text file contains a set of assignment expressions.
*
* Output: List all the variables that have the same value and list with their
* value together.
*
* For example: Input text file content like this: A1 = 1; A2 = 2; A3 = 3; A2 =
* A3; A1 = 3; A4 = 5; Output the result like this: [A1,A2,A3] = 3; A4 =5;
*
* For more infomation about testpaper,please see TestPaper.doc.
*//**
* Created on 2005-5-21
*
* @version 1.0.0.1
* @author maoren.zhang
* @linkplain [email protected]
*/import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;//
// input
// |
// |
// |
// v
//expressions---------analyse-------->MAP1------search-------->MAP2
// | |
// | |
// | |
// error ouPut
// | |
// | |
// v v
// ERROR_LOG RESULT_FILE
// public class Test { private static Map MAP1 = new HashMap(); private static Map MAP2 = new HashMap(); private static String RESULT_FILE = "Result.txt"; private static String ERROR_LOG = "Error.txt"; //default constructor
public Test() {
} /**
* createFile(String fileName) creates a new, empty file named by this
* abstract fileName. if a file with fileName was exist,delete it and create
* new one.
*
* @param fileName
* for example c:/test.txt
*
* @exception IOException
* @return void
*/
private static void createFile(String fileName) {
try {
File myFile = new File(fileName);
if (myFile.exists()) {
myFile.delete();
myFile.createNewFile();
} else {
myFile.createNewFile();
}
} catch (IOException e) {
System.out.println("Error to Create New File!");
e.printStackTrace();
}
} /**
* writeFile(String fileName,String content)
*
* Writes the content to the fileName as a sequence of bytes.
*
* @param fileName,content
* fileName for example c:/test.txt. content for example "abcdeft
* sdfas ".
*
* @exception IOException
*
* @return void
*/
private static void writeFile(String fileName, String content) {
try {
RandomAccessFile rf = new RandomAccessFile(fileName, "rw");
rf.seek(rf.length());
rf.writeBytes(content);
rf.close();
} catch (IOException e) {
System.out.println("Error to Write the file");
e.printStackTrace();
}
} /**
* getAttribute(String s)
*
* get special attribute about s: 1 - s is null or s.length=0; 2 - s is
* constant; 3 - s is varivale; 4 - unkown or undefinded
*
* @param s
*
* @return int
*/
private static int checkStr(String s) {
/*
* initialize return value = 4 default return this value
*/
int r = 4; if (s == null || s.length() == 0) {
//s is null or s.length equals zero.
return 1;
} else {
char c = s.charAt(0);
s = s.substring(1); if (s.length() == 0) { //single character
if (c >= '0' && c <= '9') {
//numeral - constant
return 2;
}
if (c == '_' || (c >= 'a' && c <= 'z')) {
//character in character set ['a' – 'z', '_'] - variable
r = 3;
} else {
//special character like @,$,&... - undefinded
r = 4;
}
} else { //multi-character
char[] c1 = s.toCharArray();
boolean bool = true;
if (c >= '0' && c <= '9') {
for (int i = 0; i < c1.length; i++) {
if (c1[i] < '0' || c1[i] > '9') {
bool = false;
break;
}
}
if (bool) {
// convert to number - constant
return 2;
}
}
if (c == '_' || (c >= 'a' && c <= 'z')) {
for (int i = 0; i < c1.length; i++) {
if (c1[i] > 'z' || c1[i] < '0'
|| (c1[i] > '9' && c1[i] < 'a' && c1[i] != '_')) {
bool = false;
break;
}
}
if (bool) {
/*
* tally with variable definition. begin with character
* set ['a'–'z','_'], contain characterset
* ['a'–'z','_','0'–'9']
*/
r = 3;
}
}
}
}
return r;
} /**
* inPut(String fileName)
*
* read a line from Input text,a String containing the contents of the line,
*
* @param fileName,content
* fileName for example c:/test.txt.
*
* @exception IOException
*
* @return void
*/
private static void inPut(String fileName) {
try {
FileReader filereader = new FileReader(fileName);
BufferedReader reader = new BufferedReader(filereader);
int recCount = 0;//current line
String record = new String();
//a String containing the contents of the line
while ((record = reader.readLine()) != null) {
recCount++;
analyse("Line " + recCount + ": " + record);
}
reader.close();
filereader.close();
} catch (IOException e) {
System.out.println("Error to Read input text file");
e.printStackTrace();
}
} /**
* analyse(String s)
*
* save the assignment expression in MAP1 with key(variable) value(constant)
* if expression is proper;record error assignment expression in Error.txt.
*
* @param s
* for example: a1 = 3;
*
* @return void
*/
//line
String head = s.substring(0, s.indexOf(":") + 1);
//A String containing the contents of the line read from input text.
String content = s.substring(s.indexOf(":") + 2);
if (content.length() > 0) {
if (!content.endsWith(";")) {
/*
* assignment expression separator is character ';' record error
* infomation in Error.txt if miss separator.
*/
writeFile(ERROR_LOG, head + "Syntax error,expected ';' \r\n ");
} else {
int p = content.indexOf("=");
switch (p) {
case -1:
/*
* assignment is character '=' record error infomation
* in Error.txt if miss .
*/
writeFile(ERROR_LOG, head + "Sysntax error,miss '=';\r\n");
break;
case 0:
// expression like " = 3;" or " = a1;",expected a variable. writeFile(ERROR_LOG, head
+ "Sysntax error,Variable expected;\r\n");
break;
default:
String left = content.substring(0, p).trim();
String right = content.substring(p + 1,
content.indexOf(";")).trim();
switch (checkStr(left)) {
case 1:
// expected a variable,expression like " =3;"
writeFile(ERROR_LOG, head
+ "Sysntax error,Variable expected;\r\n");
break;
case 2:
// constant not be modify. experssion like " 2 = a2;".
writeFile(ERROR_LOG, head
+ "Sysntax error,Modify Constant;\r\n");
break;
case 3:
//Assign constant to variable.experssion like "a1 = ;"
if (checkStr(right) == 1) {
writeFile(ERROR_LOG, head
+ "Sysntax error,Constant expected;\r\n");
}
//Assign constant to variable.experssion like "a1 = 1;"
if (checkStr(right) == 2) {
MAP1.put(left, right);
}
//Assign variable to variablee.experssion like "a1 =
// a2;"
if (checkStr(right) == 3) {
// variable not be initialized
if (!MAP1.containsKey(right)) {
writeFile(ERROR_LOG, head
+ "No initial value;\r\n");
} else { // variable was initialized
Iterator it = MAP1.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
if (key.equals(right)) {
MAP1.put(left, value);
}
}
}
}
// constant format error .experssion like "a1 = 2ada;"
if (checkStr(right) == 4) {
writeFile(
"Error.txt",
head
+ "Sysntax error,Constant format error;\r\n");
} break;
case 4: // variable format error
writeFile(ERROR_LOG, head
+ "Sysntax error,Variable format error;\r\n");
break;
default://default ,unkown error
writeFile(ERROR_LOG, head + "unkown error;\r\n");
break;
}
break;
}
}
}
} /**
* createOutMap()
*
* find the key from MAP1 that have same value,and save as new key
* set[key1,key2,..] in MAP2.
*/
private static void createOutMap() {
if (!MAP1.isEmpty()) {
Iterator it1 = MAP1.entrySet().iterator();
while (it1.hasNext()) {
Map.Entry entry1 = (Map.Entry) it1.next();//Iterator.next()
Object key1 = entry1.getKey();
Object value1 = entry1.getValue();
if (MAP2.isEmpty()) {
MAP2.put(key1, value1);//save key-value compages in MAP2
} else {
/*
* if (!MAP2.containsValue(value1)) then
* MAP2.put(key1,value1); else update key = key + key1
*/
Iterator it2 = MAP2.entrySet().iterator();
boolean bool = false;
while (it2.hasNext()) {
Map.Entry entry2 = (Map.Entry) it2.next();
Object key2 = entry2.getKey();
Object value2 = entry2.getValue();
if (value2.equals(value1)) {
MAP2.remove(key2);
MAP2.put(key2 + "," + key1, value1);
bool = true;
break;
}
}
if (!bool) {
MAP2.put(key1, value1);
}
}
}
}
MAP1.clear();
} /**
* outPut(String s)
*
* output the rusult to file.
*
* @param fileName
*
*
* @return void
*/
private static void outPut() {
StringBuffer bf = new StringBuffer();
Iterator it = MAP2.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
bf.append("[");
bf.append(entry.getKey() + "]=");
bf.append(entry.getValue() + ";\r\n");
writeFile(RESULT_FILE, bf.toString());
bf.setLength(0);
}
}
/**
* @return Returns the mAP1.
*/
private static Map getMAP1() {
return MAP1;
} /**
* @param map1
* The mAP1 to set.
*/
private static void setMAP1(Map map1) {
MAP1 = map1;
} /**
* @return Returns the mAP2.
*/
private static Map getMAP2() {
return MAP2;
} /**
* @param map2
* The mAP2 to set.
*/
private static void setMAP2(Map map2) {
MAP2 = map2;
} /**
* @return Returns the eRROR_LOG.
*/
private static String getERROR_LOG() {
return ERROR_LOG;
} /**
* @param error_log
* The eRROR_LOG to set.
*/
private static void setERROR_LOG(String error_log) {
ERROR_LOG = error_log;
} /**
* @return Returns the rESULT_FILE.
*/
private static String getRESULT_FILE() {
return RESULT_FILE;
} /**
* @param result_file
* The rESULT_FILE to set.
*/
private static void setRESULT_FILE(String result_file) {
RESULT_FILE = result_file;
} /**
* run(inputFile)
*
* @param inputFile
*
*/
public static void run(String inputFile) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
createFile(RESULT_FILE);
createFile(ERROR_LOG); Date d1 = new Date();
System.out.println("Process Begin On: " + df.format(d1));
System.out.println("reading input text file,please Wait...");
inPut(inputFile);//input
Date d2 = new Date();
System.out.println(df.format(d2));
System.out.println("start analyse Expression,please wait...");
createOutMap();//analyse
Date d3 = new Date();
System.out.println(df.format(d3));
System.out.println("Create " + RESULT_FILE + " and " + ERROR_LOG
+ ", please wait...");
outPut();//output
Date d4 = new Date();
System.out.println("Process End On: " + df.format(d4));
System.out.println("please see " + RESULT_FILE + " and " + ERROR_LOG);
} /*
* main method there is a test case. In this case , the input text file
* named "Input.txt".
*
*/
public static void main(String[] args) {
run("Test.txt");
}}