Advanced Programming-Week15
仅供学习交流,请勿复制粘贴用于提交作业!
Summary
- 文件类 File
- Scanner 的初始化
- PrintWriter 类
- 数组列表 ArrayList
Task84
package Week15;
import java.util.*;
public class Task84
{
public static char[] remove(char []arr,int n)
{
char t=' ';
for(int i=0;i<n;i++)
{
t=arr[arr.length-1];
for(int j=arr.length-1;j>0;j--)
{
arr[j]=arr[j-1];
}
arr[0]=t;
}
return arr;
}
public static char[] input()
{
Scanner in= new Scanner(System.in);
String s=in.nextLine();
char []arr=s.toCharArray();
// in.close();
return arr;
}
public static void main(String[] args)
{
Scanner in= new Scanner(System.in);
char []arr=input();
int n=in.nextInt();
in.close();
int len=arr.length;
n=n%len;
arr=remove(arr,n);
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]);
}
in.close();
}
}
The vote
import java.util.*;
import java.io.*;
public class vote {
public static void input(ArrayList<String> BandList,String BandNumber)throws Exception
{
File file = new File(BandNumber);
String BandInfo="";
if(file.exists())
{
Scanner in = new Scanner(file);
while(in.hasNextLine())
{
BandList.add(in.nextLine());
}
}else{
System.out.println("not found");
}
}
public static int votes() throws Exception
{
int []voteCount=new int[1000];
Scanner in = new Scanner(System.in);
while(in.hasNext())
{
int i=Integer.parseInt(in.next());
voteCount[i]++;
}
int maxCount = -1;
int flag=-1;
for(int i=0;i<voteCount.length;i++)
{
if(voteCount[i]>maxCount)
{
maxCount = voteCount[i];
flag=i;
}
}
return flag;
}
public static void output(ArrayList<String> BandList,int winer,String Votes)throws Exception
{
String []inforList=new String[2];
for(String i:BandList)
{
inforList=i.split(",");
int tag=Integer.parseInt(inforList[0]);
if(winer==tag)
{
PrintWriter out=new PrintWriter(Votes);
out.println(inforList[1]);
out.close();
return;
}
}
}
public static void main(String []args)throws Exception
{
String BandNumber="BandNumber.txt";
String Votes="votes.txt";
ArrayList<String> BandList = new ArrayList<String>();
input(BandList,BandNumber);
int winer=votes();
output(BandList,winer,Votes);
}
}
The delete
package Week15.delete;
import java.util.*;
import java.io.*;
import java.util.regex.*;
public class delete {
public static void delete(String filename,String Id)throws Exception
{
File file= new File(filename);
Scanner in= new Scanner(file);
ArrayList<String>content=new ArrayList<String>();
String temp="";
Pattern pattern=Pattern.compile(Id);
Matcher matcher=pattern.matcher("");
if(file.exists())
{
while(in.hasNext())
{
temp=in.nextLine();
matcher=pattern.matcher(temp);
if(!matcher.find())
{
content.add(temp);
}
}
}
PrintWriter out = new PrintWriter(filename);
for(var i:content)
{
out.println(i);
}
out.close();
}
public static void main(String []args)throws Exception
{
String filename="Week15/delete/classList.txt";
String Id="CS4092";
delete(filename,Id);
}
}
The merge
package Week15.merge;
import java.util.*;
import java.io.*;
public class merge {
public static void input(TreeSet<String> dataSet, String filename1,String filename2) throws IOException {
File file1 = new File(filename1);
File file2 = new File(filename2);
if(file1.exists())
{
Scanner in1= new Scanner(file1);
while(in1.hasNext())
{
dataSet.add(in1.next());
}
}
if(file2.exists())
{
Scanner in2= new Scanner(file2);
while(in2.hasNext())
{
dataSet.add(in2.next());
}
}
}
public static void output (TreeSet<String> dataSet, String filename)throws IOException
{
PrintWriter out = new PrintWriter(filename);
for(var i:dataSet)
{
out.println(i);
}
out.close();
}
public static void main(String []args)throws IOException
{
String filename1="Week15/merge/datafile1.txt";
String filename2="Week15/merge/datafile2.txt";
String filename3="Week15/merge/datafile3.txt";
TreeSet<String> dataSet=new TreeSet<String>();
input(dataSet,filename1,filename2);
output(dataSet,filename3);
}
}