LAREINA.JO 2021-03-18 02:47 采纳率: 60%

# Java 带字母和数字的排序算法

``````import java.io.*;

public class Sorter{
public static String InputFile;
public static String OutputFile;
private String[] list;

//constructor
public Sorter() {
list=new String[100];
}

//sort the numbers in ascending order, and return a string array
public String[] sortNumbs(double[] arr){
if(arr != null && arr.length > 1){
for(int i = 0; i < arr.length - 1; i++){
boolean flag = true;
for(int j = 0; j < arr.length - i - 1 ; j++){
if(arr[j] > arr[j+1]){
double temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = false;
}
}
if(flag){
break;
}
}
}
//convert the ordered double array to string
String[] ascending=new String [arr.length];
for(int i=0;i<arr.length;i++) {
ascending[i]=String.valueOf(arr[i]);
}
return ascending;
}

//judge if the string is a natural number
public boolean isNumberic(String str) {
for(int i=str.length();--i>=0;) {
if(str.charAt(i)<48||str.charAt(i)>57)
return false;
}
return true;
}

//clear all empty elements in a double array
public static double[] replaceNull(double[] str){
int length=str.length;
//count the number of the 0 and calculate the length of the new array;
for(int i=0;i<str.length;i++) {
if(0==str[i]) {
length--;
}
}
double[] arr=new double[length];
int k=0,j=0;
while(k<str.length&&j<arr.length) {
if(0!=str[k]) {
arr[j]=str[k];
k++;
j++;
}
else if(0==str[k]) {
k++;
}
}
return arr;
}

//clear all empty elements in a string array
public static String[] replaceNull(String[] str){
int length=str.length;
//count the number of the null and calculate the length of the new array;
for(int i=0;i<str.length;i++) {
if(null==str[i]) {
length--;
}
}
String[] arr=new String[length];
int k=0,j=0;
while(k<str.length&&j<arr.length) {
if(null!=str[k]) {
arr[j]=str[k];
k++;
j++;
}
else if(null==str[k]) {
k++;
}
}
return arr;
}

//write the string in the output file
public static void WriteOutput(String out) {
try {
File file =new File(OutputFile);
//if the fill doesn't exist, create a new one
if(!file.exists()){
file.createNewFile();
}
//append the new element in the file
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(out);
bufferWritter.close();
}
catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws Exception {
Sorter sort=new Sorter();
InputFile=args[0];
OutputFile=args[1];
File file=new File(InputFile);
double[] dou=new double[100];

try {
int i=0;
//judge it's the end or not
while (line!= null) {
//if this is the blank line, throw an error
if(line.equals("")) {
WriteOutput("Input error.");
throw new Exception("Input error.");
}
//avoid 0 be cleared when the replaceNull used
if(line.equals("0")) {
dou[i]=0.1;
i++;
}
//convert all the natural numbers to the double
else if(sort.isNumberic(line)&&!line.equals("0")) {
dou[i]=Double.parseDouble(line);
//System.out.println(dou[i]);
i++;
}
//if input is Do, use 0.5 to represent Do because 0<Do<1
else if(line.equals("Do")) {
dou[i]=0.5;
//System.out.println(dou[i]);
i++;
}
//if input is &, use 3.6 to represent & because 3<@<&<4
else if(line.equals("&")) {
dou[i]=3.6;
//System.out.println(dou[i]);
i++;
}
//if input is @, use 3.4 to represent @ because 3<@<&<4
else if(line.equals("@")) {
dou[i]=3.4;
//System.out.println(dou[i]);
i++;
}
//if input is Fa, use 15.5 to represent Fa because 15<Fa<16
else if(line.equals("Fa")) {
dou[i]=15.5;
//System.out.println(dou[i]);
i++;
}
//if input is \$, use 20.5 to represent \$ because 20<\$<21
else if(line.equals("\$")) {
dou[i]=20.5;
//System.out.println(dou[i]);
i++;
}
//if input is Asymbolwithareallylongname, use 55.5 to represent Asymbolwithareallylongname
//because 55<Asymbolwithareallylongname<56
else if(line.equals("Asymbolwithareallylongname")) {
dou[i]=55.5;
//System.out.println(dou[i]);
i++;
}
//if input is Re, use 100.5 to represent Re because 100<Re<101
else if(line.equals("Re")) {
dou[i]=100.5;
//System.out.println(dou[i]);
i++;
}
//if input is One, use 103.3 to represent One because 103<One<Two<Three<104
else if(line.equals("One")) {
dou[i]=103.3;
//System.out.println(dou[i]);
i++;
}
//if input is OTwo, use 103.6 to represent Two because 103<One<Two<Three<104
else if(line.equals("Two")) {
dou[i]=103.6;
//System.out.println(dou[i]);
i++;
}
//if input is Three, use 103.9 to represent Three because 103<One<Two<Three<104
else if(line.equals("Three")) {
dou[i]=103.9;
//System.out.println(dou[i]);
i++;
}
//if input is Mi, use 1000.5 to represent Mi because 1000<One<1001
else if(line.equals("Mi")) {
dou[i]=1000.5;
//System.out.println(dou[i]);
i++;
}
//if input is %, use 1005000.5 to represent One because 1005000<%<1005001
else if(line.equals("%")) {
dou[i]=1005000.5;
//System.out.println(dou[i]);
i++;
}
//other inputs will cause error
else {
WriteOutput("Input error.");
throw new Exception("Input error.");
}
}

//if there are 666s, change them to one @
int num6=0;
for(int k=0;k<dou.length;k++) {
if(dou[k]==666) {
if(num6==0) {
dou[k]=3.4;
num6++;
}
else {
dou[k]=0;
num6++;
}
}
}
dou=replaceNull(dou);

//sort the double array and convert them to string
String[] str=new String[dou.length];
str=sort.sortNumbs(dou);

//change all double numbers which is natural numbers originally back to the integer
for(int j=0;j<str.length;j++) {
for(int k=0;k<str[j].length()-1;k++) {
if(str[j].charAt(k)=='.'&&str[j].charAt(k+1)=='0') {
str[j]=str[j].substring(0,k);
}
}
}

//change all symbols back
for(int j=0;j<str.length;j++) {
if(str[j].equals("0.5")) {
str[j]="Do";
}
else if(str[j].equals("3.6")) {
str[j]="&";
}
else if(str[j].equals("3.4")) {
str[j]="@";
}

else if(str[j].equals("15.5")) {
str[j]="Fa";
}
else if(str[j].equals("20.5")) {
str[j]="\$";
}
else if(str[j].equals("55.5")) {
str[j]="Asymbolwithareallylongname";
}
else if(str[j].equals("100.5")) {
str[j]="Re";
}
else if(str[j].equals("103.3")) {
str[j]="One";
}
else if(str[j].equals("103.6")) {
str[j]="Two";
}
else if(str[j].equals("103.9")) {
str[j]="Three";
}
else if(str[j].equals("1000.5")) {
str[j]="Mi";
}
else if(str[j].equals("1005000.5")) {
str[j]="%";
}
}

//if there is 666, the array will be ascending
if(num6>0) {
for(int k=0;k<str.length;k++) {
sort.list[k]=str[k];
}
}
//if there is no 666, the result list should be sorted in descending order;
else {
int reverse=str.length;
for(int k=0;k<str.length;k++) {
sort.list[reverse-1]=str[k];
reverse--;
}
}
sort.list=replaceNull(sort.list);

//if there is 0 originally, change it back because it was changed to 0.1 before
for(int j=0;j<sort.list.length;j++) {
if(sort.list[j].equals("0.1")) {
sort.list[j]="0";
}
}

for(int j=0;j<sort.list.length-1;j++) {
WriteOutput(sort.list[j]+"\n");
//System.out.println(sort.list[j]);
}
WriteOutput(sort.list[sort.list.length-1]);

} catch (IOException e) {
e.printStackTrace();
}
//close the file
finally {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}
}``````
• 写回答

#### 9条回答默认 最新

• limit、T 2021-03-19 16:26
关注

没有了~~~

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥15 Qt 不小心删除了自带的类，该怎么办
• ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
• ¥15 有没有哪位厉害的人可以用C#可视化呀
• ¥15 可以帮我看看代码哪里错了吗
• ¥15 设计一个成绩管理系统
• ¥15 PCL注册的选点等函数如何取消注册
• ¥15 问一下各位，为什么我用蓝牙直接发送模拟输入的数据，接收端显示乱码呢，米思齐软件上usb串口显示正常的字符串呢？
• ¥15 Python爬虫程序
• ¥15 crypto 这种的应该怎么找flag？
• ¥15 代码已写好，求帮我指出错误，有偿！