2022/10/05 18:57 完成作业5

This commit is contained in:
zyx 2022-10-05 18:57:48 +08:00
parent 7f4916cfbb
commit 442a867c4f
10 changed files with 513 additions and 79 deletions

1
.idea/modules.xml generated
View File

@ -6,6 +6,7 @@
<module fileurl="file://$PROJECT_DIR$/Week3/Week3.iml" filepath="$PROJECT_DIR$/Week3/Week3.iml" />
<module fileurl="file://$PROJECT_DIR$/Week4/Week4.iml" filepath="$PROJECT_DIR$/Week4/Week4.iml" />
<module fileurl="file://$PROJECT_DIR$/Week5/Week5.iml" filepath="$PROJECT_DIR$/Week5/Week5.iml" />
<module fileurl="file://$PROJECT_DIR$/Week6/Week6.iml" filepath="$PROJECT_DIR$/Week6/Week6.iml" />
<module fileurl="file://$PROJECT_DIR$/java_class.iml" filepath="$PROJECT_DIR$/java_class.iml" />
<module fileurl="file://$PROJECT_DIR$/week1/week1.iml" filepath="$PROJECT_DIR$/week1/week1.iml" />
<module fileurl="file://$PROJECT_DIR$/work8/work8.iml" filepath="$PROJECT_DIR$/work8/work8.iml" />

11
Week6/Week6.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

101
Week6/src/Exercise5.java Normal file
View File

@ -0,0 +1,101 @@
import java.io.IOException;
import java.util.Scanner;
/**
* @author : 张宇轩
* @createTime : 2022/10/5 - 18:24
*/
public class Exercise5 {
public static void main(String[] args) throws IOException {
while (true) {
int opt = menu();
switch (opt) {
case 1:
checkPasswordFormat();
break;
case 2:
break;
case 3:
break;
case 4:
return;
}
System.out.println("按任意键继续...");
System.in.read();
}
}
/**
* 检查菜单选项输入合法性
* @param low 选项下界
* @param high 选项上界
* @return 合法选项值
*/
public static int checkInput(int low, int high) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int opt = scanner.nextInt();
if (opt >= low && opt <= high) {
return opt;
}
System.out.println("输入超限,请输入" + low + "-" + high + "的数字");
} else {
System.out.println("输入数据类型错误,请输入" + low + "-" + high + "的数字");
scanner.next();
}
return -1;
}
public static int menu() {
System.out.println("1.密码有效性检查");
System.out.println("2.运算表达式求解(支持嵌套)");
System.out.println("3.超市收银");
System.out.println("4.退出");
System.out.print(">>>请选择(1-4)");
int opt = -1;
while (opt == -1) {
opt = checkInput(1,4);
}
return opt;
}
public static void checkPasswordFormat() {
System.out.println();
while (true) {
System.out.println("密码要求:\n1.长度必须在8到15个字符之间\n" +
"2.不得包含空格\n" +
"3.必须至少包含一个非数字的大写字符和一个非数字的小写字符\n" +
"4.必须包含至少一个数字数字");
System.out.println(">>请输入密码:");
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine();
String info = "";
if (password.length() >= 8 && password.length() <= 15) {
int numCnt = 0, lowCnt = 0, upCnt = 0, spaceCnt = 0;
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) >= '0' && password.charAt(i) <= '9')
numCnt++;
else if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z')
lowCnt++;
else if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z')
upCnt++;
else if (password.charAt(i) == ' ')
spaceCnt++;
}
if (numCnt >= 1 && lowCnt >= 1 && upCnt >= 1 && spaceCnt == 0) {
info = "符合要求";
System.out.println("恭喜你,你的密码'" + password + "'" + info);
return;
}
else{
if (numCnt == 0) info += "密码至少需要一个数字\n";
if (upCnt == 0) info += "密码至少需要一个大写字母\n";
if (lowCnt == 0) info += "密码至少需要一个小写字母\n";
if (spaceCnt == 1) info += "密码包含了空格\n";
}
} else info += "密码长度不符合要求\n";
System.out.println("你的密码" + password + "无效,原因如下:\n" + info);
}
}
}

View File

@ -0,0 +1,46 @@
import java.util.Scanner;
/**
* @author : 张宇轩
* @createTime : 2022/10/5 - 8:17
*/
public class PasswordCheck {
public static void main(String[] args) {
System.out.println("密码要求:\n长度必须在8到15个字符之间\n" +
"不得包含空格\n" +
"必须至少包含一个非数字的大写字符和一个非数字的小写字符\n" +
"必须包含至少一个数字数字");
System.out.println("请输入密码:");
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine();
System.out.println(checkFormat(password));
}
public static String checkFormat(String pwd) {
String info = "";
if (pwd.length() >= 8 && pwd.length() <= 15) {
int numCnt = 0, lowCnt = 0, upCnt = 0, spaceCnt = 0;
for (int i = 0; i < pwd.length(); i++) {
if (pwd.charAt(i) >= '0' && pwd.charAt(i) <= '9')
numCnt++;
else if (pwd.charAt(i) >= 'a' && pwd.charAt(i) <= 'z')
lowCnt++;
else if (pwd.charAt(i) >= 'A' && pwd.charAt(i) <= 'Z')
upCnt++;
else if (pwd.charAt(i) == ' ')
spaceCnt++;
}
if (numCnt >= 1 && lowCnt >= 1 && upCnt >= 1 && spaceCnt == 0) {
info = "符合要求";
return "恭喜你,你的密码'" + pwd + "'" + info;
}
else{
if (numCnt == 0) info += "密码至少需要一个数字\n";
if (upCnt == 0) info += "密码至少需要一个大写字母\n";
if (lowCnt == 0) info += "密码至少需要一个小写字母\n";
if (spaceCnt == 1) info += "密码包含了空格\n";
}
} else info += "密码长度不符合要求";
return "你的密码" + pwd + "无效,原因如下:\n" + info;
}
}

View File

@ -0,0 +1,62 @@
package exercise5;
import java.io.IOException;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws IOException {
while (true) {
int opt = menu();
switch (opt) {
case 1:
PasswordCheck.checkPasswordFormat();
break;
case 2:
ExpressionCalculate.calculate();
break;
case 3:
MarketCashier.cashier();
break;
case 4:
return;
}
System.out.println("按任意键继续...");
System.in.read();
}
}
/**
* 检查菜单选项输入合法性
* @param low 选项下界
* @param high 选项上界
* @return 合法选项值
*/
public static int checkInput(int low, int high) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int opt = scanner.nextInt();
if (opt >= low && opt <= high) {
return opt;
}
System.out.println("输入超限,请输入" + low + "-" + high + "的数字");
} else {
System.out.println("输入数据类型错误,请输入" + low + "-" + high + "的数字");
scanner.next();
}
return -1;
}
public static int menu() {
System.out.println("1.密码有效性检查");
System.out.println("2.运算表达式求解(支持嵌套)");
System.out.println("3.超市收银");
System.out.println("4.退出");
System.out.print(">>>请选择(1-4)");
int opt = -1;
while (opt == -1) {
opt = checkInput(1,4);
}
return opt;
}
}

View File

@ -0,0 +1,114 @@
package exercise5;
import java.util.Scanner;
public class ExpressionCalculate {
public static void calculate() {
System.out.println("add将两个操作数相加\n" +
"sub将两个操作数相减\n" +
"max求解两个数中的较大数\n" +
"min求解两个数中的较小数\n" +
"doubleMe将一个操作数翻倍乘以2");
System.out.println("请输入待运算的表达式:");
String expression = new Scanner(System.in).nextLine();
if (!checkFormat(expression)){
System.out.println("输入格式错误!");
return;
}
String[] opts = expression.split(",");
double res;
if(opts.length == 2){
res = calcSingletonExpression(expression);
} else {
res = calcMultipleExpression(opts);
}
System.out.println(expression + "=" + res);
}
/**
* 检查输入格式(括号是否匹配)
* @param expression 输入的表达式
* @return true为表达式括号格式正确
*/
public static boolean checkFormat(String expression){
int cnt = 0;
for (int i = 0; i < expression.length(); i++) {
if ('(' == expression.charAt(i)){
cnt++;
} else if (')' == expression.charAt(i)){
cnt--;
}
}
return cnt == 0;
}
/**
* 计算单运算符的表达式
* @param expression 输入的单运算符的表达式
* @return 运算结果
*/
public static double calcSingletonExpression(String expression){
double res = 0;
String exp = expression.substring(0, expression.indexOf("("));
if ("doubleMe".equals(exp)){
double num = Double.parseDouble(expression.substring(expression.indexOf("(")+1,expression.indexOf(")")));
res = num*2;
} else {
double num1 = Double.parseDouble(expression.substring(expression.indexOf("(")+1, expression.indexOf(",")));
double num2 = Double.parseDouble(expression.substring(expression.indexOf(",")+1, expression.indexOf(")")));
switch (exp){
case "add":
res = num1+num2;
break;
case "sub":
res = num1-num2;
break;
case "max":
res = Math.max(num1, num2);
break;
case "min":
res = Math.min(num1, num2);
break;
}
}
return res;
}
/**
* 计算多运算符(运算符嵌套)的表达式
* @param opts 输入的多运算符的表达式以','分割的String数组
* @return 运算结果
*/
public static double calcMultipleExpression(String[] opts){
double res = 0;
for (int i = opts.length-1; i >= 0; i--) {
String exp = opts[i].substring(0, opts[i].indexOf("("));
double num;
if(i != opts.length-1){
num = Double.parseDouble(opts[i].substring(opts[i].indexOf("(")+1));
} else {
num = Double.parseDouble(opts[i].substring(opts[i].indexOf("(") + 1, opts[i].indexOf(")")));
}
switch (exp){
case "add":
res += num;
break;
case "sub":
res -= num;
break;
case "max":
res = Math.max(res, num);
break;
case "min":
res = Math.min(res, num);
break;
case "doubleMe":
res = num*2;
break;
}
}
return res;
}
}

View File

@ -0,0 +1,37 @@
package exercise5;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class MarketCashier {
public static void cashier(){
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String serialNumber = df.format(date);
Scanner scanner = new Scanner(System.in);
int cnt = 1, allSaleNum = 0;
double allSaleMoney = 0;
StringBuilder info = new StringBuilder();
System.out.println("请依次输入销售记录(商品名称,销售数量,商品单价),输入'0000'时结束输入:");
while (true){
String input = scanner.nextLine();
if ("0000".equals(input)){
break;
}
String tmpSerialNumber = serialNumber + String.format("%04d",cnt++);
String[] detail = input.split(",");
int saleNum = Integer.parseInt(detail[1]);
double saleMoney = Double.parseDouble(detail[2]);
allSaleNum += saleNum;
allSaleMoney += saleNum*saleMoney;
info.append(tmpSerialNumber).append("\t").append(detail[0]).append("\t").append(detail[1]).append("\t\t").append(detail[2]).append("\t\t").append(saleNum * saleMoney).append("\n");
}
System.out.println("流水号\t\t\t商品名称\t商品数量\t单价\t总价");
System.out.println(info);
System.out.println("总计:销售笔数 " + (cnt-1) + ",销售数量 " + allSaleNum + ",销售金额 " + allSaleMoney);
}
}

View File

@ -0,0 +1,45 @@
package exercise5;
import java.util.Scanner;
public class PasswordCheck {
public static void checkPasswordFormat() {
System.out.println();
while (true) {
System.out.println("密码要求:\n1.长度必须在8到15个字符之间\n" +
"2.不得包含空格\n" +
"3.必须至少包含一个非数字的大写字符和一个非数字的小写字符\n" +
"4.必须包含至少一个数字数字");
System.out.println(">>请输入密码:");
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine();
String info = "";
if (password.length() >= 8 && password.length() <= 15) {
int numCnt = 0, lowCnt = 0, upCnt = 0, spaceCnt = 0;
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) >= '0' && password.charAt(i) <= '9')
numCnt++;
else if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z')
lowCnt++;
else if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z')
upCnt++;
else if (password.charAt(i) == ' ')
spaceCnt++;
}
if (numCnt >= 1 && lowCnt >= 1 && upCnt >= 1 && spaceCnt == 0) {
info = "符合要求";
System.out.println("恭喜你,你的密码'" + password + "'" + info);
return;
}
else{
if (numCnt == 0) info += "密码至少需要一个数字\n";
if (upCnt == 0) info += "密码至少需要一个大写字母\n";
if (lowCnt == 0) info += "密码至少需要一个小写字母\n";
if (spaceCnt != 0) info += "密码包含了空格\n";
}
} else info += "密码长度不符合要求\n";
System.out.println("你的密码" + password + "无效,原因如下:\n" + info);
}
}
}

View File

@ -1,13 +1,30 @@
import java.util.Scanner;
/**
* @author : 张宇轩
* @createTime : 2022/10/1 - 22:21
*/
public class Exercise1 {
public static void main(String[] args) {
String expression = new Scanner(System.in).nextLine();
if (!checkFormat(expression)){
System.out.println("输入格式错误!");
return;
}
String[] opts = expression.split(",");
double res = 0;
if(opts.length == 2){
res = calcSingletonExpression(expression);
} else {
res = calcMultipleExpression(opts);
}
System.out.println(expression + "=" + res);
}
/**
* 检查输入格式(括号是否匹配)
* @param expression 输入的表达式
* @return true为表达式括号格式正确
*/
public static boolean checkFormat(String expression){
int cnt = 0;
for (int i = 0; i < expression.length(); i++) {
if ('(' == expression.charAt(i)){
@ -16,64 +33,74 @@ public class Exercise1 {
cnt--;
}
}
if (cnt != 0){
System.out.println("输入格式错误!");
return;
}
return cnt == 0;
}
String[] opts = expression.split(",");
/**
* 计算单运算符的表达式
* @param expression 输入的单运算符的表达式
* @return 运算结果
*/
public static double calcSingletonExpression(String expression){
double res = 0;
if(opts.length == 2){
String exp = expression.substring(0, expression.indexOf("("));
if ("doubleMe".equals(exp)){
double num = Double.parseDouble(expression.substring(expression.indexOf("(")+1,expression.indexOf(")")));
res = num*2;
} else {
double num1 = Double.parseDouble(expression.substring(expression.indexOf("(")+1, expression.indexOf(",")));
double num2 = Double.parseDouble(expression.substring(expression.indexOf(",")+1, expression.indexOf(")")));
switch (exp){
case "add":
res = num1+num2;
break;
case "sub":
res = num1-num2;
break;
case "max":
res = Math.max(num1, num2);
break;
case "min":
res = Math.min(num1, num2);
break;
}
}
String exp = expression.substring(0, expression.indexOf("("));
if ("doubleMe".equals(exp)){
double num = Double.parseDouble(expression.substring(expression.indexOf("(")+1,expression.indexOf(")")));
res = num*2;
} else {
for (int i = opts.length-1; i >= 0; i--) {
String exp = opts[i].substring(0, opts[i].indexOf("("));
double num;
if(i != opts.length-1){
num = Double.parseDouble(opts[i].substring(opts[i].indexOf("(")+1));
} else {
num = Double.parseDouble(opts[i].substring(opts[i].indexOf("(") + 1, opts[i].indexOf(")")));
}
switch (exp){
case "add":
res += num;
break;
case "sub":
res -= num;
break;
case "max":
res = Math.max(res, num);
break;
case "min":
res = Math.min(res, num);
break;
case "doubleMe":
res = num*2;
break;
}
double num1 = Double.parseDouble(expression.substring(expression.indexOf("(")+1, expression.indexOf(",")));
double num2 = Double.parseDouble(expression.substring(expression.indexOf(",")+1, expression.indexOf(")")));
switch (exp){
case "add":
res = num1+num2;
break;
case "sub":
res = num1-num2;
break;
case "max":
res = Math.max(num1, num2);
break;
case "min":
res = Math.min(num1, num2);
break;
}
}
System.out.println(expression + "=" + res);
return res;
}
/**
* 计算多运算符(运算符嵌套)的表达式
* @param opts 输入的多运算符的表达式以','分割的String数组
* @return 运算结果
*/
public static double calcMultipleExpression(String[] opts){
double res = 0;
for (int i = opts.length-1; i >= 0; i--) {
String exp = opts[i].substring(0, opts[i].indexOf("("));
double num;
if(i != opts.length-1){
num = Double.parseDouble(opts[i].substring(opts[i].indexOf("(")+1));
} else {
num = Double.parseDouble(opts[i].substring(opts[i].indexOf("(") + 1, opts[i].indexOf(")")));
}
switch (exp){
case "add":
res += num;
break;
case "sub":
res -= num;
break;
case "max":
res = Math.max(res, num);
break;
case "min":
res = Math.min(res, num);
break;
case "doubleMe":
res = num*2;
break;
}
}
return res;
}
}

View File

@ -1,44 +1,34 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
* @author : 张宇轩
* @createTime : 2022/10/1 - 23:00
*/
public class Exercise2 {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String serialNumber = df.format(date);
int cnt = 0;
int cnt = 1, allSaleNum = 0;
double allSaleMoney = 0;
Scanner scanner = new Scanner(System.in);
List<String> details = new ArrayList<>();
String info = "";
while (true){
String input = scanner.nextLine();
if ("0000".equals(input)){
break;
}
String tmpSerialNumber = serialNumber + String.format("%04d",cnt++);
String detail = tmpSerialNumber + "," +input;
details.add(detail);
}
System.out.println("流水号\t\t\t商品名称\t商品数量\t单价\t总价");
int allSaleNum = 0;
double allSaleMoney = 0;
for (String each : details) {
String[] detail = each.split(",");
System.out.print(detail[0] + "\t" + detail[1] + "\t" + detail[2] + "\t\t" + detail[3] + "\t\t" );
int saleNum = Integer.parseInt(detail[2]);
double saleMoney = Double.parseDouble(detail[3]);
System.out.println(saleNum*saleMoney);
String[] detail = input.split(",");
int saleNum = Integer.parseInt(detail[1]);
double saleMoney = Double.parseDouble(detail[2]);
allSaleNum += saleNum;
allSaleMoney += saleNum*saleMoney;
info += tmpSerialNumber + "\t" + detail[0] + "\t" + detail[1] + "\t\t" + detail[2] + "\t\t" + saleNum*saleMoney + "\n";
}
System.out.println("总计:销售笔数 " + details.size() + ",销售数量 " + allSaleNum + ",销售金额 " + allSaleMoney);
System.out.println("流水号\t\t\t商品名称\t商品数量\t单价\t总价");
System.out.println(info);
System.out.println("总计:销售笔数 " + (cnt-1) + ",销售数量 " + allSaleNum + ",销售金额 " + allSaleMoney);
}
}