将下面的c语言代码转化为java代码:#include <iostream> #include <cstring> #include<cmath> #include<stdio.h> #include<map> using namespace std; //词典结构 struct S { char des[15]; //目标语言 char src[15]; //英语 }dic[100010]; //定义比较器,目标语言字典序升序排列 int cmp(const void *a, const voi
时间: 2024-04-28 20:23:58 浏览: 166
c语言头文件
5星 · 资源好评率100%
Java代码如下:
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
//词典结构
static class S {
String des; //目标语言
String src; //英语
}
static S[] dic = new S[100010];
//定义比较器,目标语言字典序升序排列
static class MyComparator implements Comparator<S> {
public int compare(S s1, S s2) {
return s1.des.compareTo(s2.des);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Map<String, String> map = new HashMap<String, String>();
int cnt = 0;
while (scanner.hasNext()) {
String src = scanner.next();
String des = scanner.next();
dic[cnt] = new S();
dic[cnt].des = des;
dic[cnt].src = src;
cnt++;
}
scanner.close();
//按目标语言字典序升序排列
Arrays.sort(dic, 0, cnt, new MyComparator());
//构建映射表
for (int i = 0; i < cnt; i++) {
map.put(dic[i].des, dic[i].src);
}
//查询并输出翻译结果
while (scanner.hasNext()) {
String word = scanner.next();
if (map.containsKey(word)) {
System.out.println(map.get(word));
} else {
System.out.println("eh");
}
}
}
}
阅读全文