Skip to content

Instantly share code, notes, and snippets.

@Gumball12
Last active March 18, 2019 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gumball12/072d1e9a930eb09177ad27eedf348536 to your computer and use it in GitHub Desktop.
Save Gumball12/072d1e9a930eb09177ad27eedf348536 to your computer and use it in GitHub Desktop.
Windows programming (c-sharp) assignment 1
/**
* Windows programming (c#) assignment 1.1
* https://repl.it/@Gumball12/Windows-programming-c-sharp-assignment-11
*
* @author HaeJun Seo
* @since Mar 14, 2019
*/
using System;
class Program {
public static void Main (string[] args) {
/* variables */
// declare string variable
string name;
// define 1d array(string)
string[] names = { "발리", "둘리", "몰리", "물리", "살리" };
// user input
Console.Write("찾고자 하는 이름을 입력해주세요: ");
name = Console.ReadLine();
// foreaching
foreach (string n in names) { // n: an array element ('names' array)
if (n == name) {
Console.WriteLine($"\n 일치하는 단어를 찾았습니다: {n}");
break; // break foreach
}
}
// exit interrupt
Console.Write("종료하려면 엔터 키를 눌러주십시오...");
Console.ReadLine();
}
}
/**
* Windows programming (c#) assignment 1.2
* https://repl.it/@Gumball12/Windows-programming-c-sharp-assignment-12
*
* @author HaeJun Seo
* @since Mar 13, 2019
*/
using System; // Convert, Func, Math, ...
using System.Linq; // Enumerable
using System.Collections.Generic; // List
class Program {
public static void Main (string[] args) {
/* tools (using c# lambda expression) */
Func<string, string> trim = s => s.Trim();
Func<string, string[]> splitRest = s => s.Split(',');
Func<string[], string[]> trimMap = sa => sa.Select(trim).ToArray();
Func<double, int, double> computeDecimal = (target, d) => Math.Floor(target * 10 * d) / (10.0 * d);
Func<string[], int> summation = sa => sa.Aggregate(0, (acc, s) => acc + Convert.ToInt32(s));
Func<string[], double> mean = sa => summation(sa) / Convert.ToDouble(sa.OfType<string>().ToList().Count);
Func<int, int, int> intCompare = (a, b) => a.CompareTo(b);
// quicksort
Func<List<List<string>>, List<List<string>>> qs = null;
Func<List<List<string>>, int, int, List<List<string>>> swap = (list, a, b) => {
// swap list elements
List<string> tmp = new List<string>(list[a]);
list[a] = list[b];
list[b] = tmp;
return list;
};
/* variables */
int studentCnt;
/* getting user input */
Console.Write("Insert number of student: ");
// 학생의 수를 입력받음
studentCnt = Convert.ToInt32(Console.ReadLine());
// 2차원 배열 선언
List<List<string>> scores = new List<List<string>>();
/* compute scores */
Console.WriteLine("Insert score (이름/국어/영어/수학):");
// 성적을 입력받고 배열에 저장
// 학생의 이름, 총점, 평균 계산
for (int ind = 0; ind < studentCnt; ind++) {
scores.Add(new List<string>( // add element
// getting user input
trimMap(splitRest(Console.ReadLine()))
));
// compute a total score (index: 4)
scores[ind].Add(
Convert.ToString(summation(scores[ind].GetRange(1, 3).ToArray()))
);
// compute a mean score (index: 5)
scores[ind].Add(
Convert.ToString(
computeDecimal(mean(scores[ind].GetRange(1, 3).ToArray()), 1)
)
);
}
/**
* compute 'rank' using QUICKSORT
* reference: https://en.wikipedia.org/wiki/Quicksort
*/
// 학생의 석차 계산
qs = (ul) => { // ul: Unsorted List
int pivot = 0;
int j = 0, i = 1;
while (i <= ul.Count - 1) { // iterate ul
int comp = intCompare( // comparing
Convert.ToInt32(ul[pivot][4]),
Convert.ToInt32(ul[i][4])
);
if (comp == 1) { // greater than
swap(ul, ++j, i); // swapping of two elements (j + 1, i)
}
i++; // next element
}
// swapping of two elements (pivot, j)
swap(ul, pivot, j);
pivot = j;
// recursion
if (ul.Count - 1 > 1) {
List<List<string>> slice = null;
if (0 < pivot) {
slice = ul.GetRange(0, pivot);
ul.RemoveRange(0, pivot);
ul.InsertRange(0, qs(slice));
}
if (pivot < ul.Count - 1) {
slice = ul.GetRange(pivot + 1, ul.Count - pivot - 1);
ul.RemoveRange(pivot + 1, ul.Count - pivot - 1);
ul.InsertRange(pivot + 1, qs(slice));
}
}
return ul;
};
// sorting
qs(scores).Reverse();
/* print */
// 학생의 이름, 국어, 영어, 수학, 총점, 평균, 석차 출력
int rank = 1;
Console.WriteLine("\n\n\n======= 결 과 =======\n");
scores.ForEach(score => Console.WriteLine($"이름: {score[0]}, 국어: {score[1]}, 영어: {score[2]}, 수학: {score[3]}, 총점: {score[4]}, 평균: {score[5]}, 석차: {rank++}"));
// exit interrupt
Console.Write("\n종료하려면 엔터 키를 눌러주십시오...");
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment