VisiCalc Java项目
我想做一个VisiCalc风格的项目,但我有一个问题。我应该能够引用一个单元格,例如输入A1 = ( 2 + 3 )
然后我会通过A2 = ( A1 + 2 )
或类似的方式引用A1
。
我不知道从哪里开始,但我的代码如下,非常感谢您的帮助。
顺便说一句,如果你开始使用代码,应该有6个类
VisiCalc。班级
public class VisiCalc
{
public static void main(String[] args)
{
final int MAX_ROWS = 10; //rows
final int MAX_COLS = 7; //columns
final int DISPLAY_WIDTH = 12;//width of cell
Scanner console = new Scanner(System.in);
Grid grid = new Grid(MAX_ROWS, MAX_COLS, DISPLAY_WIDTH);
System.out.println("Welcome to VisiCalc!");
System.out.println();
String input = "";
//allow user to enter multiple commands
while(!input.equals("quit"))
{
System.out.print("Enter a command: ");
input = console.nextLine();
if(input.equals("print"))
{
System.out.println();
System.out.print(grid);
System.out.println();
}
else if(input.equals("quit"))
{
System.out.println();
System.out.println("Farewell!");
}
else if(input.equals(""))
{
//Do nothing
}
else
{
String Reference = "";//next few lines are Mr.Gilbrough's code
int equals = input.indexOf(" = ");
if(equals < 0)
{
Reference = input.toUpperCase();
input = "";
}
else
{
Reference = input.substring(0, equals).toUpperCase();
input = input.substring(equals + 3);
}
if(Helpers.isValidCell(Reference, MAX_ROWS, MAX_COLS))
{
if(input.length() > 0)
{
if(!grid.setCell(Reference, input))
{
System.out.println("please enter a valid command");
}
}
else
{
input = grid.getCell(Reference);
System.out.print(Reference + " = ");
if(input.equals(""))
{
System.out.print("please enter a valid command");
System.out.println();
System.out.println();
}
else
{
System.out.print(input);
System.out.println();
System.out.println();
}
}
}
}
}
}
}
网格。班级
public class Grid
{
private int max_rows;
private int max_cols; // makes the constructors
private int display_width;
private Cell[][] spreadsheet;
//creates the grid class
public Grid(int row, int col, int width)
{
display_width = width;
max_rows = row;
max_cols = col;
spreadsheet = new Cell[row + 1][col + 1];
setSheet();
setHead();
}
//heading
public void setHead()
{
for(int col = 1; col <= max_cols; col++)
{
spreadsheet[0][col] = new Cell(Helpers.getColLetter(col));
}
for(int row = 1; row <= max_rows; row++)
{
spreadsheet[row][0] = new Cell("" + row);
}
}
public void setSheet()
{
for(int col = 0; col <= max_cols; col++)
{
for(int row = 0; row <= max_rows; row++)
{
spreadsheet[row][col] = new Cell();
}
}
}
// print
public String toString()
{
String output = "";
for(int row = 0; row <= max_rows; row++)
{
for(int col = 0; col <= max_cols; col++)
{
output += center(spreadsheet[row][col].toString());
if(col < max_cols)
{
output += "|";
}
}
output += "\n";
if(row < max_rows)
{
output += lines();
}
}
return output;
}
private String center(String contents)
{
if(contents.length() > display_width)
{
return contents.substring(0, display_width - 1) + ">";
}
int buffer = display_width - contents.length();
int left = buffer / 2;
int right = display_width - contents.length() - left;
return spacing(left, " ") + contents + spacing(right, " ");
}
//Mr.Gilbrough's method
private String spacing(int width, String spacer)
{
String space = "";
for(int i = 1; i <= width; i++)
{
space += spacer;
}
return space;
}
//Creates the lines between each row
private String lines()
{
String line = "";
for(int a = 0; a <= max_cols; a++)
{
line += spacing(display_width, "-");
if(a < max_cols)
{
line += "+";
}
}
return line + "\n";
}
//Mr.Gilbrough's method
public boolean setCell(String cellRef, String con)
{
int col = Helpers.getColIndexFromString(cellRef);
int row = Helpers.getRowIndexFromString(cellRef);
if(con.charAt(0) == '"')
{
spreadsheet[row][col] = new Cell(con);
return true;
}
else if(con.charAt(0) == '(')
{
spreadsheet[row][col] = new FormulaCell(con);
return true;
}
double num = 0;
try
{
num = Double.parseDouble(con);
}
catch(Exception e)
{
return false;
}
spreadsheet[row][col] = new DoubleCell(num);
return true;
}
//Mr.Gilbrough's method
public String getCell(String cellRef)
{
int col = Helpers.getColIndexFromString(cellRef);
int row = Helpers.getRowIndexFromString(cellRef);
return spreadsheet[row][col].getCon();
}
}
细胞。班级
public class Cell
{
private String contents;
public Cell(String data)
{
contents = data;
}
public Cell()
{
this("");
}
public String toString()
{
if(contents.length() > 2)
{
return contents.substring(1, contents.length() - 1);
}
else
{
return contents;
}
}
public String getCon()
{
return contents;
}
}
公式化细胞。班级
//extends to use stuff from cell and to set cell
public class FormulaCell extends Cell
{
private String contents;
public FormulaCell(String data)
{
contents = data;
}
public FormulaCell()
{
this("");
}
public String toString()
{
return "" + calc();
}
public String getCon()
{
return contents;
}
public double getNumber(String token)
{
return Double.parseDouble(token);
}
public double calc()
{
Scanner tokens = new Scanner(contents);
double result = 0;
String op = "";
boolean start = true;
while(tokens.hasNext())
{
String token = tokens.next();
if(token.equals("(") || token.equals(")"))
{
//nothing
}
else if(ifNumber(token))
{
double number = getNumber(token);
if(op.equals(""))
{
result = number;
}
else
{
if(op.equals("+"))
{
result = result + number;
}
else if(op.equals("-"))
{
result = result - number;
}
else if(op.equals("*"))
{
result = result * number;
}
else
{
result = result / number;
}
}
}
else
{
op = token;
}
}
return result;
}
//this is to throw exception
public boolean ifNumber(String token)
{
double number = 0;
try
{
number = Double.parseDouble(token);
}
catch(Exception e)
{
return false;
}
return true;
}
}
助手。班级
//Evan Zverev
public class Helpers
{
// returns column letter where A = first column
public static String getColLetter(int col)
{
return "" + (char) ('A' + col - 1);
}
// returns column index where 1 = first column
public static int getColIndex(char col)
{
return col - 'A' + 1;
}
// returns true/false if the given character is a valid column given the max
public static boolean isValidCol(int col, int maxCol)
{
return col > 0 && col <= maxCol;
}
// returns true/false if the given row is a valid row given the max
public static boolean isValidRow(int row, int maxRow)
{
return row > 0 && row <= maxRow;
}
// take in a cell ID and return the column index (ie: C4 will return 3)
public static int getColIndexFromString(String cellID)
{
char col = cellID.charAt(0);
return getColIndex(col);
}
// take in a cell ID such as C4 and return the row index (ie: C4 will return 4)
public static int getRowIndexFromString(String cellID)
{
String row = cellID.substring(1);
return Integer.parseInt(row);
}
// take in a cell ID and a max row/col and return true/false if the cell is valid
public static boolean isValidCell(String cellID, int maxRows, int maxCols)
{
int colIndex = getColIndexFromString(cellID);
int rowIndex = -1;
try
{
rowIndex = getRowIndexFromString(cellID);
}
catch(Exception e)
{
return false;
}
return isValidCol(colIndex, maxCols) && isValidRow(rowIndex, maxRows);
}
}
共 (0) 个答案