반응형
https://gist.github.com/parkground/222fce59293b8680c622666b7fb1cbe8
[Zero-base] 1주차 과제 - 3번 과제 (AlphabetFinder.java)
[Zero-base] 1주차 과제 - 3번 과제 (AlphabetFinder.java). GitHub Gist: instantly share code, notes, and snippets.
gist.github.com

public class AlphabetFinder {
//알파벳 문자열의 번호를 저장할 int형 전역변수 선언 및 초기화
static int alphabet;
//전체 알바벳 갯수를 저장할 전역변수 선언 및 초기화
static int alphabet_count;
//각 알바벳별 갯수를 저장할 전역변수 선언 및 초기화
static int countA = 0;
static int countB = 0;
static int countC = 0;
static int countD = 0;
static int countE = 0;
static int countF = 0;
static int countG = 0;
static int countH = 0;
static int countI = 0;
static int countJ = 0;
static int countK = 0;
static int countL = 0;
static int countM = 0;
static int countN = 0;
static int countO = 0;
static int countP = 0;
static int countQ = 0;
static int countR = 0;
static int countS = 0;
static int countT = 0;
static int countU = 0;
static int countV = 0;
static int countW = 0;
static int countX = 0;
static int countY = 0;
static int countZ = 0;
//각 알바벳별 비율를 저장할 전역변수 선언 및 초기화
static double ratioA = 0.0;
static double ratioB = 0.0;
static double ratioC = 0.0;
static double ratioD = 0.0;
static double ratioE = 0.0;
static double ratioF = 0.0;
static double ratioG = 0.0;
static double ratioH = 0.0;
static double ratioI = 0.0;
static double ratioJ = 0.0;
static double ratioK = 0.0;
static double ratioL = 0.0;
static double ratioM = 0.0;
static double ratioN = 0.0;
static double ratioO = 0.0;
static double ratioP = 0.0;
static double ratioQ = 0.0;
static double ratioR = 0.0;
static double ratioS = 0.0;
static double ratioT = 0.0;
static double ratioU = 0.0;
static double ratioV = 0.0;
static double ratioW = 0.0;
static double ratioX = 0.0;
static double ratioY = 0.0;
static double ratioZ = 0.0;
public static void main (String[] args) {
//제19대 대통령 취임사 영어 문장
String speech = "";
speech += "My fellow Koreans,";
speech += "\n";
speech += "I am grateful to you all. I bow my head in deep appreciation for the choice of you, the people. Today, serving as President in the 19th presidential term of the Republic of Korea, I take the first step toward a new Korea. My shoulders are now burdened with heavy mandates entrusted to me by the people, and my heart is burning with enthusiasm to create the kind of country that we have never been able to live in before. My head is now filled with blueprints for ushering in a new world characterized by unity and coexistence.";
speech += "\n";
speech += "The new Republic of Korea we are trying to build is the nation that those who came before us have consistently aspired to in the face of countless frustrations and defeats. It is the nation that our young people have longed for so ardently, in spite of many sacrifices and dedicated efforts. To build such a Republic of Korea, I declare before history and the people with a fearful but humble mind that I will faithfully fulfill my responsibilities and missions as President.";
speech += "\n";
speech += "I offer my gratitude and sympathy from deep in my heart to the other presidential contenders. There is no winner or loser in this election. We are all partners who are required to lead the new Republic of Korea together. Now is the time to leave behind the heat of competition and move forward together hand in hand.";
speech += "\n";
speech += "My fellow Koreans,";
speech += "\n";
speech += "We have gone through unprecedented political upheavals over the past few months. Politics was in disarray but our people were great. Even with the impeachment and arrest of the sitting President, our people paved a way forward for the Republic of Korea. They never got discouraged but instead turned the crisis into an opportunity, eventually opening the way to a new world today. The greatness of the Republic of Korea is the greatness of the people.";
speech += "\n";
speech += "During this presidential election, our people made history again. A new President was elected through even support from all across the country. Starting today, I will become a president for everyone. Even those who did not support me are my people, and I will serve them all alike.";
speech += "\n";
speech += "I dare to make a promise. This date—May 10, 2017—will go down in history as the beginning of the genuine unity of the people.";
speech += "\n";
speech += "My fellow Koreans,";
speech += "\n";
speech += "Over the past several troubling months, many people asked whether this country can indeed be called a country. From this very question, I will make a new start as President. From today, I will become a president dedicated to building a country worthy of being called a country.";
speech += "\n";
speech += "I will boldly break from the malpractices of old days. As President, I will take the lead in starting anew.";
speech += "\n";
speech += "First and foremost, I will strive to get rid of authoritarian practices in the presidency. When preparations are completed, I am going to leave Cheong WaDae to usher in an era of the presidential office in Gwanghwamun Square. There, my aides and I will put our heads together and have discussions. I will frequently engage in communication with the people. On key issues, I myself will hold press briefings.";
speech += "\n";
speech += "On my way home, I will drop by markets to talk freely with citizens I encounter. Large public forums will be occasionally held at Gwanghwamun Square.";
speech += "\n";
speech += "The President's imperial power will be shared as much as possible. I will make sure that agencies that have great authority remain completely independent from politics. There will be a system to keep these agencies in check so that none of them will be able to wield absolute power. ";
speech += "\n";
speech += "I will perform my duties with humility. I will become a president who is at eye level with the people.";
speech += "\n";
speech += "I will endeavor to address the security crisis promptly. For the sake of peace on the Korean Peninsula, I will crisscross the globe. If needed, I will immediately fly to Washington. I will also visit Beijing and Tokyo and even Pyongyang under the right circumstances.";
speech += "\n";
speech += "I remain committed to doing all I can for the establishment of peace on the Korean Peninsula. The ROK-U.S. alliance will be further strengthened. In the meantime, I will have serious discussions with the United States and China for the resolution of issues related to THAAD.";
speech += "\n";
speech += "Strong security is made possible by strong defense capabilities. The Government will also strive to further enhance independent defense capabilities. It will also lay the foundation for the resolution of the North Korean nuclear problem. The Government will endeavor to establish peace in Northeast Asia, thus setting a milestone in alleviating tensions on the Korean Peninsula.";
speech += "\n";
speech += "I will make efforts to change the landscape of politics characterized by division and conflicts. Confrontations between conservatives and progressives must come to an end. As President, I will take the lead in engaging in dialogues. Opposition parties are partners in the administration of state affairs. Discussions will be held on a regular basis, and I will take time to have meetings.";
speech += "\n";
speech += "Officials will be appointed regardless of where they are from. Competence and the need to put the right person in the right place will become the overriding criteria. I am ready to appoint capable individuals regardless of whether or not they support me.";
speech += "\n";
speech += "Both here and abroad, the economy is going through a difficult time. Ordinary people’s livelihoods are under threat. As I promised during my campaign, I will tend to the employment issue first. At the same time, I will take the initiative in reforming conglomerates. Under the Moon Jae-in Administration, the cozy relationship between political and business circles will completely disappear.";
speech += "\n";
speech += "I will strive to resolve conflicts between regions, social classes and generations and seek ways to solve the problems faced by irregular workers. I will help create a world without discrimination.";
speech += "\n";
speech += "I repeat. The Administration led by Moon Jae-in and the Democratic Party of Korea will promote equal opportunities. The process will be fair, and the result will be just.";
speech += "\n";
speech += "My fellow Koreans,";
speech += "\n";
speech += "This presidential election was held in the aftermath of the impeachment of the former President. The unfortunate history of the presidency still continues. On the occasion of this election, this unfortunate history must end.";
speech += "\n";
speech += "I will set a new example as the President of the Republic of Korea. I will make my utmost efforts to become a president who will be viewed as a success by the public and by history. By doing so, I will repay your support.";
speech += "\n";
speech += "I will become a clean president. I take office empty-handed, and I will leave office the same way. Someday, I will return home and become an ordinary citizen and friendly neighbor. I will continue to be a person all of you can be proud of. ";
speech += "\n";
speech += "I will become an honest president who keeps promises. I will meticulously honor the pledges I made during the campaign. Genuine political progress will be possible only when the president takes the initiative in engaging in trustworthy politics. I will not talk big about doing the impossible. I will admit to the wrong I did. I will not cover up unfavorable public opinion with lies.";
speech += "\n";
speech += "I will be a fair president. I will explore ways to ceate a world without privileges and foul play. I will endeavor to create a world where those who follow common sense will benefit. I will not overlook the pain of our neighbors. I will always be vigilant with a great sense of caring so that there will be no one left behind. I will become a president who comforts people in sorrow.";
speech += "\n";
speech += "I will be a president who communicates with others. I will exert my authority in a humble and modest manner; I will build a country stronger than it has ever been. I will not be a president who is domineering and authoritative but one who communicates and promotes dialogue.";
speech += "\n";
speech += "I will stay close to the people, working near Gwanghwamun. I will become a president who remains warmhearted and friendly to the people.";
speech += "\n";
speech += "My fellow Koreans,";
speech += "\n";
speech += "The Republic of Korea starts anew today, on May 10, 2017. A great history of building a decent nation begins. I ask you all to join me on this journey. I will fully dedicate myself to it.";
speech += "\n";
speech += "Thank you very much. ";
//전체 문자열의 길이를 저장
int speechLength = speech.length();
//전체 문자열에 대하여 문자를 가져오는 반복문
for (int i = 0; i < speechLength; i++) {
// 문자열을 int형으로 강제 형변환 하여서 변수에 저장
alphabet = (int) (speech.charAt(i));
//알파벳 대문자(65~90)이거나 소문자(97~122)인 경우를 찾는 조건문
if(((alphabet >= 65) && (alphabet <= 90)) ||
((alphabet >= 97) && (alphabet <= 122))) {
//알파벳인 경우 카운터를 증가시킴
alphabet_count += 1;
//알바벳에 따라서 개별 카운터를 증가시킴
switch (alphabet) {
case 65:
case 97:
countA += 1;
break;
case 66:
case 98:
countB += 1;
break;
case 67:
case 99:
countC += 1;
break;
case 68:
case 100:
countD += 1;
break;
case 69:
case 101:
countE += 1;
break;
case 70:
case 102:
countF += 1;
break;
case 71:
case 103:
countG += 1;
break;
case 72:
case 104:
countH += 1;
break;
case 73:
case 105:
countI += 1;
break;
case 74:
case 106:
countJ += 1;
break;
case 75:
case 107:
countK += 1;
break;
case 76:
case 108:
countL += 1;
break;
case 77:
case 109:
countM += 1;
break;
case 78:
case 110:
countN += 1;
break;
case 79:
case 111:
countO += 1;
break;
case 80:
case 112:
countP += 1;
break;
case 81:
case 113:
countQ += 1;
break;
case 82:
case 114:
countR += 1;
break;
case 83:
case 115:
countS += 1;
break;
case 84:
case 116:
countT += 1;
break;
case 85:
case 117:
countU += 1;
break;
case 86:
case 118:
countV += 1;
break;
case 87:
case 119:
countW += 1;
break;
case 88:
case 120:
countX += 1;
break;
case 89:
case 121:
countY += 1;
break;
case 90:
case 122:
countZ += 1;
break;
}
}
}
//저장된 개별 카운터 값을 이용하여 비율을 계산
ratioA = ((double)countA / (double)alphabet_count * 100);
ratioB = ((double)countB / (double)alphabet_count * 100);
ratioC = ((double)countC / (double)alphabet_count * 100);
ratioD = ((double)countD / (double)alphabet_count * 100);
ratioE = ((double)countE / (double)alphabet_count * 100);
ratioF = ((double)countF / (double)alphabet_count * 100);
ratioG = ((double)countG / (double)alphabet_count * 100);
ratioH = ((double)countH / (double)alphabet_count * 100);
ratioI = ((double)countI / (double)alphabet_count * 100);
ratioJ = ((double)countJ / (double)alphabet_count * 100);
ratioK = ((double)countK / (double)alphabet_count * 100);
ratioL = ((double)countL / (double)alphabet_count * 100);
ratioM = ((double)countM / (double)alphabet_count * 100);
ratioN = ((double)countN / (double)alphabet_count * 100);
ratioO = ((double)countO / (double)alphabet_count * 100);
ratioP = ((double)countP / (double)alphabet_count * 100);
ratioQ = ((double)countQ / (double)alphabet_count * 100);
ratioR = ((double)countR / (double)alphabet_count * 100);
ratioS = ((double)countS / (double)alphabet_count * 100);
ratioT = ((double)countT / (double)alphabet_count * 100);
ratioU = ((double)countU / (double)alphabet_count * 100);
ratioV = ((double)countV / (double)alphabet_count * 100);
ratioW = ((double)countW / (double)alphabet_count * 100);
ratioX = ((double)countX / (double)alphabet_count * 100);
ratioY = ((double)countY / (double)alphabet_count * 100);
ratioZ = ((double)countZ / (double)alphabet_count * 100);
//출력 형식에 맞게 결과값을 출력
System.out.printf("A:%4d개, %5.2f%%\n", countA, ratioA);
System.out.printf("B:%4d개, %5.2f%%\n", countB, ratioB);
System.out.printf("C:%4d개, %5.2f%%\n", countC, ratioC);
System.out.printf("D:%4d개, %5.2f%%\n", countD, ratioD);
System.out.printf("E:%4d개, %5.2f%%\n", countE, ratioE);
System.out.printf("F:%4d개, %5.2f%%\n", countF, ratioF);
System.out.printf("G:%4d개, %5.2f%%\n", countG, ratioG);
System.out.printf("H:%4d개, %5.2f%%\n", countH, ratioH);
System.out.printf("I:%4d개, %5.2f%%\n", countI, ratioI);
System.out.printf("J:%4d개, %5.2f%%\n", countJ, ratioJ);
System.out.printf("K:%4d개, %5.2f%%\n", countK, ratioK);
System.out.printf("L:%4d개, %5.2f%%\n", countL, ratioL);
System.out.printf("M:%4d개, %5.2f%%\n", countM, ratioM);
System.out.printf("N:%4d개, %5.2f%%\n", countN, ratioN);
System.out.printf("O:%4d개, %5.2f%%\n", countO, ratioO);
System.out.printf("P:%4d개, %5.2f%%\n", countP, ratioP);
System.out.printf("Q:%4d개, %5.2f%%\n", countQ, ratioQ);
System.out.printf("R:%4d개, %5.2f%%\n", countR, ratioR);
System.out.printf("S:%4d개, %5.2f%%\n", countS, ratioS);
System.out.printf("T:%4d개, %5.2f%%\n", countT, ratioT);
System.out.printf("U:%4d개, %5.2f%%\n", countU, ratioU);
System.out.printf("V:%4d개, %5.2f%%\n", countV, ratioV);
System.out.printf("W:%4d개, %5.2f%%\n", countW, ratioW);
System.out.printf("X:%4d개, %5.2f%%\n", countX, ratioX);
System.out.printf("Y:%4d개, %5.2f%%\n", countY, ratioY);
System.out.printf("Z:%4d개, %5.2f%%\n", countZ, ratioZ);
}
}

반응형
'cs > java-spring-boot' 카테고리의 다른 글
[Zero-base] 2-2. 객체지향 언어의 4가지 특징 (0) | 2022.01.18 |
---|---|
[Zero-base] 2-1. 객체지향 프로그래밍의 이해 (0) | 2022.01.18 |
[Zero-base] 1주차 과제 - 2번 과제 (ClassManagement.java) (0) | 2022.01.14 |
[Zero-base] 1주차 과제 - 1번 과제 (FindName.java) (0) | 2022.01.14 |
[Zero-base] 1-10. 제어문과 반복문 (0) | 2022.01.14 |