Last active
April 5, 2019 02:59
-
-
Save Gumball12/daa6187cb659d19c62a4fe461882345b to your computer and use it in GitHub Desktop.
c# assignments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://repl.it/@Gumball12/Windows-programming-c-sharp-assignment-5-1 | |
using System; | |
using System.Linq; | |
class Problem_1 | |
{ | |
static void Main(string[] args) | |
{ | |
// tool | |
Func<string, string[]> splitBySpace = s => s.Split(' '); | |
Console.WriteLine("이름, 학번, 학년 입력"); | |
// insert data | |
string[] input = splitBySpace(Console.ReadLine()); | |
// create student instance | |
Student student = new Student(input[0], input[1], Convert.ToInt32(input[2])); | |
Console.WriteLine("\n중간, 기말 점수 입력"); | |
// insert data | |
int[] input2 = splitBySpace(Console.ReadLine()).Select(s => Convert.ToInt32(s)).ToArray(); | |
// set values | |
student.setMidScore(input2[0]); | |
student.setFinalScore(input2[1]); | |
// calculate summation | |
student.calcSum(); | |
student.calcAvg(); | |
// print Student instance | |
Console.WriteLine($"\n{student}"); | |
} | |
} | |
// student class | |
class Student | |
{ | |
/* instance variables */ | |
private string name; | |
private string id; | |
private int grade; | |
private int midScore = 0; | |
private int finalScore = 0; | |
private double sum = 0; | |
private double avg = 0; | |
/* methods */ | |
// constructor | |
public Student (string name, string id, int grade) | |
{ | |
this.name = name; | |
this.id = id; | |
this.grade = grade; | |
} | |
// set mid score | |
public void setMidScore (int midScore) | |
{ | |
this.midScore = midScore; | |
} | |
// set final score | |
public void setFinalScore (int finalScore) | |
{ | |
this.finalScore = finalScore; | |
} | |
// calculate sum | |
public double calcSum () | |
{ | |
return sum = Convert.ToDouble(midScore + finalScore); | |
} | |
// calculate average | |
public double calcAvg () | |
{ | |
return avg = sum / 2; | |
} | |
// override ToString method | |
public override string ToString () | |
{ | |
return $"이름: {name}, 학번: {id}, 학년: {grade}\n총점: {sum}, 평균: {avg}"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://repl.it/@Gumball12/Windows-programming-c-sharp-assignment-5-2 | |
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
class Problem_2 | |
{ | |
static void Main(string[] args) | |
{ | |
// tool | |
Func<string, string[]> splitBySpace = s => s.Split(' '); | |
Console.Write("학생의 명 수를 입력하세요: "); | |
// insert data | |
int studentCnt = Convert.ToInt32(Console.ReadLine()); | |
// student list | |
List<Student> students = new List<Student>(); | |
for (int i = 0; i < studentCnt; i++) { | |
Console.WriteLine("\n=\n\n이름, 학번, 학년 입력"); | |
// insert data | |
string[] input = splitBySpace(Console.ReadLine()); | |
// create student instance | |
Student student = new Student(input[0], input[1], Convert.ToInt32(input[2])); | |
Console.WriteLine("\n중간, 기말 점수 입력"); | |
// insert data (convert to integer) | |
int[] input2 = splitBySpace(Console.ReadLine()).Select(s => Convert.ToInt32(s)).ToArray(); | |
// set values | |
student.MidScore = input2[0]; | |
student.FinalScore = input2[1]; | |
// calculate summation | |
student.calcSum(); | |
student.calcAvg(); | |
// print Student instance | |
Console.WriteLine($"\n{student}"); | |
students.Add(student); | |
} | |
// calculate | |
double avgMidScore = students.Aggregate(0.00, (acc, student) => acc + Convert.ToDouble(student.MidScore)) / studentCnt; | |
double avgFinalScore = students.Aggregate(0.00, (acc, student) => acc + Convert.ToDouble(student.FinalScore)) / studentCnt; | |
Console.WriteLine($"\n\n==\n\n총 학생수는 {studentCnt} 명이며, 평균 중간 점수는 {avgMidScore.ToString("#.##")}점, 평균 기말 점수는 {avgFinalScore.ToString("#.##")}점 입니다.\n"); | |
} | |
} | |
// student class | |
class Student | |
{ | |
/* instance variables */ | |
private string name; | |
private string id; | |
private int grade; | |
private int midScore = 0; | |
private int finalScore = 0; | |
private double sum = 0; | |
private double avg = 0; | |
/* methods */ | |
// default constructor | |
public Student () { | |
// init instance variables | |
name = ""; | |
id = ""; | |
grade = 0; | |
} | |
// constructor | |
public Student (string name, string id, int grade) | |
{ | |
this.name = name; | |
this.id = id; | |
this.grade = grade; | |
} | |
// using properties | |
public int MidScore { | |
get { return midScore; } | |
set { midScore = value; } | |
} | |
public int FinalScore { | |
get { return finalScore; } | |
set { finalScore = value; } | |
} | |
// calculate sum | |
public double calcSum () | |
{ | |
return sum = Convert.ToDouble(midScore + finalScore); | |
} | |
// calculate average | |
public double calcAvg () | |
{ | |
return avg = sum / 2; | |
} | |
// override ToString method | |
public override string ToString () | |
{ | |
return $"이름: {name}, 학번: {id}, 학년: {grade}\n총점: {sum}, 평균: {avg}"; | |
} | |
// destructor | |
~Student () { | |
Console.WriteLine("Destructor..."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://repl.it/@Gumball12/Windows-programming-c-sharp-assignment-5-3 | |
using System; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
class Problem_3 | |
{ | |
static void Main(string[] args) | |
{ | |
Console.Write("수식을 입력하세요: "); | |
// insert expression | |
// separate operator and operands using 'Regex.Split()' method | |
string[] input = Regex.Split(Console.ReadLine(), "[\\s]").Select(s => s.Trim()).ToArray(); | |
Console.WriteLine($">> 답 = {new Calc(input[0], input[2], input[1]).Result()}"); | |
} | |
} | |
class Calc { | |
// store field | |
private double result; | |
public Calc (string a, string b, string operand) { | |
int ia = Convert.ToInt32(a); | |
int ib = Convert.ToInt32(b); | |
// switch | |
switch (operand) { | |
case "+": | |
result = ia + ib; | |
break; | |
case "-": | |
result = ia - ib; | |
break; | |
case "*": | |
result = ia * ib; | |
break; | |
case "/": | |
result = ia / ib; | |
break; | |
} | |
} | |
public double Result () { | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://repl.it/@Gumball12/Windows-programming-c-sharp-assignment-5-4 | |
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
class Problem_4 | |
{ | |
static void Main(string[] args) | |
{ | |
Parking parking = new Parking(); | |
string command; | |
while (true) { | |
Console.Write("\nenter number (0: car in, 1: car out): "); | |
command = Console.ReadLine(); | |
if (!(command == "0" || command == "1")) { | |
break; | |
} | |
parking.command(command); | |
} | |
} | |
} | |
class Parking { | |
List<List<string>> db = new List<List<string>>(); | |
public void command (string command) { | |
if (command == "0") { | |
car_in(); | |
} | |
if (command == "1") { | |
car_out(); | |
} | |
} | |
private void car_in () { | |
if (db.ToArray().Length > 9) { | |
Console.WriteLine("db is full!"); | |
return; | |
} | |
Console.Write("enter car information (5885,k5,1): "); | |
db.Add(Console.ReadLine().Split(',').OfType<string>().ToList()); // add car info | |
} | |
private void car_out () { | |
List<string> car; | |
string num, exit; | |
Console.Write("enter car number: "); | |
num = Console.ReadLine(); | |
Console.Write("enter exit time: "); | |
exit = Console.ReadLine(); | |
if ((car = db.Find(data => data[0] == num)) == null) { | |
Console.WriteLine("fail to find the car.. :("); | |
return; | |
} | |
Console.WriteLine($"car has benn out. parking fee = {(Convert.ToInt32(exit) - Convert.ToInt32(car[2])) * 3}$"); | |
db.Remove(car); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Problem_5 { | |
static void Main(string[] args) { | |
Stack stack = new Stack(); | |
stack.push("a"); | |
stack.push("b"); | |
stack.push("c"); | |
Console.WriteLine(stack.pop()); // c | |
stack.push("d"); | |
Console.WriteLine(stack.pop()); // d | |
Console.WriteLine(stack.pop()); // b | |
Console.WriteLine(stack.pop()); // a | |
Console.WriteLine(stack.pop()); // stack underflow | |
Console.WriteLine(stack.peek()); // stack underflow | |
Console.WriteLine(stack.isEmpty()); // true | |
Console.WriteLine(stack.size()); // 0 | |
} | |
} | |
class Stack { | |
/* instance variables */ | |
static readonly int MAX = 100; | |
private int top; | |
private object[] data = new object[MAX]; | |
/* methods */ | |
// constructor | |
public Stack () { | |
top = -1; // init top | |
} | |
// push object | |
public bool push (object value) { | |
if (MAX <= top) { | |
Console.WriteLine("Stack overflow :("); | |
} else { | |
data[++top] = value; | |
} | |
return MAX <= top; | |
} | |
// pop object with remove element | |
public object pop () { | |
if (top == -1) { | |
Console.WriteLine("Stack underflow :("); | |
return null; | |
} else { | |
return data[top--]; | |
} | |
} | |
// tests if this stack is empty? | |
public bool isEmpty () { | |
return top == -1; | |
} | |
// get object without remove element | |
public object peek () { | |
if (top == -1) { | |
Console.WriteLine("Stack underflow :("); | |
return null; | |
} else { | |
return data[top]; | |
} | |
} | |
// get size | |
public int size () { | |
return top + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment