java模拟堆栈,时间类MyDate,字符串链表节点类,类BankCustomer
Posted Pistachiout
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java模拟堆栈,时间类MyDate,字符串链表节点类,类BankCustomer相关的知识,希望对你有一定的参考价值。
1、将你在实验3中设计的模拟堆栈程序,封装成一个类(注意访问控制的运用、getter和setter的运用、构造方法的设计等),并用这个堆栈类重写中缀表达式转换成后缀表达式程序。
public class StackImiate {
private int maxSize;
private char[] a;
private int top;
StackImiate(int maxSize) {
this.maxSize=maxSize;
a=new char[maxSize];
top=-1;
}
public void push(char str) {
a[++top]=str;
}
public char pop() {
return a[top--];
}
public boolean isEmpty() {
return(top==-1);
}
public boolean isFull() {
return(top==maxSize);
}
public int length()
{return top+1;}
public char peek() {
return a[top];
}
public char peekN(int n) {
//System.out.println(a[n]);
return a[n];
}
public void displayStack(String s) {
System.out.print(s);
System.out.print(" Stack (bottom-->top): ");
for(int j=0;j<maxSize;j++) {
System.out.print(peekN(j)+" ");
}
System.out.println();
}
}
public class InToPost {
public static void main(String[] args) {
String strResult=new String();
String Input="a+b*c+(d*e+f)*g";
System.out.println("中缀表达式"+Input);
strResult=strResult+doTrans(Input);
System.out.println();
System.out.println("后缀表达式"+strResult);
}
public static StringBuffer doTrans(String Input) {
StackImiate str=new StackImiate(50);
StringBuffer strResultTemp=new StringBuffer();
for (int i = 0; i <Input.length(); i++) {
char ch = Input.charAt(i);
str.displayStack("Get " + ch + " ");
switch (ch) {
case '+':
case '-':
gotOper(ch, 1,str,strResultTemp);
break;
case '*':
case '/':
case '%':
gotOper(ch, 2,str,strResultTemp);
break;
case '(':
str.push(ch);
break;
case ')':
gotParen(ch,str,strResultTemp);
break;
default:
strResultTemp =strResultTemp.append(ch);
break;
}
}
while (!str.isEmpty()) {
str.displayStack("out ");
strResultTemp = strResultTemp.append(str.pop());
}
str.displayStack("End ");
return strResultTemp;
}
public static void gotParen(char ch,StackImiate str,StringBuffer strResultTemp) {
// TODO Auto-generated method stub
while (!str.isEmpty()) {
char chx = (char) str.pop();
if (chx == '(') {
break;
} else {
strResultTemp =strResultTemp.append(chx);
}
}
}
private static void gotOper(char ch, int i,StackImiate str,StringBuffer strResultTemp) {
// TODO Auto-generated method stub
while (!str.isEmpty()) {
char opTop = (char) str.pop();
if (opTop == '(') {
str.push('(');
break;
} else {
int k;
if (opTop == '+' || opTop == '-') {
k = 1;
} else {
k = 2;
}
if (k < i) {
str.push(opTop);
break;
} else {
strResultTemp = strResultTemp.append(opTop);
}
}
}
str.push(ch);
}
}
2、设计一个时间类MyDate:(日期均大于1900年)
a、其中包括年、月、日、时、分、秒。其他成员变量可根据需要增设;
b、可以直接获得时间的年、月、日、小时、分钟、秒数;(public修饰)
c、计算日期的dayOfWeek(星期几);
d、输入任意一个月份,将此月的日历输出(按星期格式);
e、输入任意年份,将此年的年历输出;
f、输出时间,要求三种格式:
yyyy年MM月dd日hh小时mm分ss秒;(Chinese)
yyyy-MM-dd hh:mm:ss(European)
yyyyMMddhhmmss(compact)
g、设计三个构造方法:
1、参数只有年月日的构造方法,小时分钟秒设为0;
2、完全参数的构造方法;
3、如果使用无参数的构造方法则初始化为系统时间。
h、不要直接使用Java提供的Date类,那样就达不到练习的效果了。
import java.text.SimpleDateFormat;
public class MyDate {
public int year,month,day,hour,minute,second;
String strDate;
public MyDate()
{SimpleDateFormat myDateFormat =new SimpleDateFormat("yyyyMMddHHmmss");
java.util.Date date0=new java.util.Date();
strDate=myDateFormat.format(date0);
year = Integer.parseInt( strDate.substring(0,4));
month = Integer.parseInt( strDate.substring(4,6));
day = Integer.parseInt( strDate.substring(6,8));
hour = Integer.parseInt( strDate.substring(8,10));
minute = Integer.parseInt( strDate.substring(10,12));
second = Integer.parseInt( strDate.substring(12,14));
}
public MyDate(int year0,int month0,int day0)
{year=year0;
month=month0;
day=day0;
hour=0;
minute=0;
second=0;}
public MyDate(int year0,int month0,int day0,int hour0,int minite0,int second0)
{year=year0;
month=month0;
day=day0;
hour=hour0;
minute=minite0;
second=second0;}
public int getYear()
{return year;}
public int getMonth()
{return month;}
public String getDate()
{return day+"/"+month+"/"+year;}
public int getHour()
{return hour;}
public int getMinite()
{return minute;}
public int getSecond()
{return second;}
public void displayChinese()
{System.out.println(year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"+second+"秒");}
public void displayEU()
{System.out.println(year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+" ");}
public void displayCompact()
{System.out.println(year+" "+month+" "+day+" "+hour+" "+minute+" "+second);}
public void displayMonth(int year,int month) //输出一月的日历
{ int sum=0;
for(int i=1900;i<year;i++)
{ if(i%4==0&&i%100!=0||i%400==0)
{sum+=366;}
else
{sum+=365;}
}
for(int i=1;i<month;i++)
{ if(i==2)
{if(year%4==0&&year%100!=0||year%400==0)
{sum+=29;}
else
{sum+=28;}
}
else
{ if(i==4||i==6||i==9||i==11)
{sum+=30;}
else
{sum+=31;}
}
}
sum+=1;
int wekday=sum%7;
System.out.println("日\\t一\\t二\\t三\\t四\\t五\\t六");
for(int i=1;i<=wekday;i++)
{System.out.print("\\t");}
if(year%4==0&&year%100!=0||year%400==0)
{if(month!=2)
{if(month==4||month==6||month==9||month==11)
{for(int i=1;i<=30;i++)
{if(sum%7==6)
{System.out.print(i+"\\n");}
else{
System.out.print(i+"\\t");}
sum++;}}
else
{for(int i=1;i<=31;i++)
{if(sum%7==6)
{System.out.print(i+"\\n");}
else
{System.out.print(i+"\\t");}
sum++;}
}}
else
{for(int i=1;i<=29;i++)
{if(sum%7==6)
{System.out.print(i+"\\n");}
else
{System.out.print(i+"\\t");}
sum++;
}}}
else {if(month!=2)
{if(month==4||month==6||month==9||month==11)
{for(int i=1;i<=30;i++)
{if(sum%7==6)
{System.out.print(i+"\\n");}
else{
System.out.print(i+"\\t");}
sum++;}}
else
{for(int i=1;i<=31;i++)
{if(sum%7==6)
{System.out.print(i+"\\n");}
else
{System.out.print(i+"\\t");}
sum++;}
}}
else
{for(int i=1;i<=28;i++)
{if(sum%7==6)
{System.out.print(i+"\\n");}
else
{System.out.print(i+"\\t");}
sum++;
}}}}
public void displayYear(int year) //输出一年的日历
{for(int i=1;i<13;i++)
{System.out.println(" 第"+i+"月");
displayMonth(year,i);
System.out.println();
}
}
public String dayOfWeek(int year,int month,int day) //判断某天为一周的星期几
{int sum=0Python模拟 堆栈,队列,链表