Skip to content

Instantly share code, notes, and snippets.

@Gumball12
Last active April 14, 2019 03:23
Show Gist options
  • Save Gumball12/decd75fe19932f384b354fdaea8e660b to your computer and use it in GitHub Desktop.
Save Gumball12/decd75fe19932f384b354fdaea8e660b to your computer and use it in GitHub Desktop.
c# assignment - week7
// import modules
using System;
using System.Collections.Generic;
using System.Linq;
class Problem_1
{
static void Main(string[] args)
{
// define Student list
List<Student> students = new List<Student>();
// insert student counts
Console.Write("학생의 수를 입력하시오: ");
for(int i = 0, j = Convert.ToInt32(Console.ReadLine()); i < j; i++)
{
Console.Write("학생의 정보를 입력하시오 (학번 이름 성별 생년월일): ");
string[] inputs = Console.ReadLine().Split(" ");
students.Add(new Student(inputs)); // create Student instance
}
// print students
Console.WriteLine("\n");
students.ForEach(Console.WriteLine);
Console.Write("\n\n 수정할 학생의 학번을 입력하시오: ");
string id = Console.ReadLine();
// find student
foreach (Student s in students)
{
if (s.Id == id)
{
// insert new data
Console.Write($"수정 전 정보:\n{s}\n\n학생의 이름을 입력하시오: ");
s.Name = Console.ReadLine();
Console.Write("\n학생의 성별을 입력하시오: ");
s.Gender = Console.ReadLine();
Console.Write("\n 학생의 생년월일을 입력하시오: ");
s.Birth = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"수정 후 정보:\n{s}");
break; // break foreach statement
}
}
}
}
class Student
{
// instance variables (private)
private string name;
private string id; // 00000000
private string gender;
private int birth; // yymmdd
// default constructor
public Student ()
{
// init vatiables
name = null;
id = null;
gender = null;
birth = 0;
}
// constructor
public Student (string name, string id, string gender, int birth)
{
this.name = name;
this.id = id;
this.gender = gender;
this.birth = birth;
}
// constructor (using array argument)
public Student (string[] args)
{
this.id = args[0];
this.name = args[1];
this.gender = args[2];
this.birth = Convert.ToInt32(args[3]);
}
// properties
public string Name
{
get { return name; }
set { name = value; }
}
public string Id
{
get { return id; }
set { id = value; }
}
public string Gender
{
get { return gender; }
set { gender = value; }
}
public int Birth
{
get { return birth; }
set { birth = value; }
}
// print method
public void print()
{
Console.WriteLine(ToString());
}
// override ToString() method
public override string ToString()
{
return $"{id}\t{name}\t{gender}\t{birth}";
}
}
using System;
using System.Collections.Generic;
using System.Linq;
class Problem_2
{
static void Main (string[] args)
{
int i = 50;
// print numbers
Console.WriteLine("Odd numbers... to " + i);
new Odd()[i].ForEach(Console.WriteLine);
Console.WriteLine("\nEven numbers... to " + i);
new Even()[i].ForEach(Console.WriteLine);
Console.WriteLine("\nPrime numbers... to " + i);
new Prime()[i].ForEach(Console.WriteLine);
}
}
// parent class
class Number
{
// define result list
protected List<int> result = new List<int>();
// tool
protected int safeNumber (int n)
{
if (n > 99)
{
n = 99;
}
if (n < 0)
{
n = 0;
}
return n;
}
}
// Odd number class
class Odd: Number
{
// using indexer
public List<int> this[int n]
{
get
{
n = safeNumber(n);
// calcaulte odd numbers
for (int i = 1; i < n; i += 2)
{
result.Add(i);
}
return result;
}
}
}
// Even number class
class Even: Number
{
// using indexer
public List<int> this[int n]
{
get
{
n = safeNumber(n);
// calculate even numbers
for (int i = 0; i < n; i += 2)
{
result.Add(i);
}
return result;
}
}
}
// Prime number class
class Prime: Number
{
public List<int> this[int n]
{
get
{
n = safeNumber(n);
result.Add(2);
result.Add(3);
// calculate prime numbers
for (int i = 4; i < n; i++)
{
bool isPrime = true;
for (int j = 2; j <= i / 2; j++)
{
if (i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime == true)
{
result.Add(i);
}
}
return result;
}
}
}
using System;
using System.Linq;
class Problem_3
{
static void Main(string[] args)
{
// Insert size and Create instances
Console.Write("삼각형의 크기를 입력하시오: ");
Triangle t = new Triangle(Convert.ToInt32(Console.ReadLine()));
Console.Write("사각형의 가로, 세로 크기를 입력하시오: ");
int[] inputs = Console.ReadLine().Split(',').Select(s => Convert.ToInt32(s.Trim())).ToArray();
Rectangle r = new Rectangle(inputs[0], inputs[1]);
// draw
t.Draw();
Console.WriteLine("\n");
r.Draw();
}
}
abstract class Shape
{
// instance variable
private int width = 0;
private int height = 0;
// constructor
public Shape (int size)
{
this.width = size;
this.height = size;
}
public Shape (int width, int height)
{
this.width = width;
this.height = height;
}
// properties
protected int Size
{
get { return (width + height) / 2; }
set {
width = value;
height = value;
}
}
protected int Width
{
get { return width; }
set { width = value; }
}
protected int Height
{
get { return height; }
set { height = value; }
}
// declaration abstact Draw() method
public abstract void Draw();
// tools
public static Func<char, int, string> getChar = (c, length) => new string(c, length);
public static Func<int, string> getSpace = length => getChar(' ', length);
public static Func<int, string> getAsterisk = length => getChar('*', length);
}
class Triangle: Shape
{
// constructor
public Triangle (int size): base(size) // base: parent constructor
{
// nothing
}
// override Draw() method
public override void Draw ()
{
for (int i = 0; i < Size; i++)
{
Console.WriteLine($"{Shape.getSpace(Size - i)}{Shape.getAsterisk(1 + i * 2)}");
}
}
}
class Rectangle: Shape
{
// constructor
public Rectangle (int width, int height): base(width, height)
{
// nothing
}
// override Draw() method
public override void Draw()
{
for (int i = 0; i < Height; i++)
{
Console.WriteLine(Shape.getAsterisk(Width));
}
}
}
using System;
class Problem_4
{
static void Main(string[] args)
{
Console.WriteLine($"사각형 {new Rectangle()}");
Console.WriteLine($"사각형 {new Rectangle(5, 7)}");
Console.WriteLine($"원 {new Circle()}");
Console.WriteLine($"원 {new Circle(5)}");
Console.WriteLine($"삼각형 {new Triangle()}");
Console.WriteLine($"삼각형 {new Triangle(5, 7)}");
}
}
abstract class Shape
{
// instance variables
protected double width, length, radius, a, b, c;
public abstract double area();
public abstract double perimeter();
public override string ToString()
{
return $"넓이: {area()}, 둘레: {perimeter()}";
}
}
class Rectangle: Shape
{
// default constructor
public Rectangle ()
{
width = 1;
length = 1;
}
// constructor
public Rectangle (int width, int length)
{
this.width = width;
this.length = length;
}
public override double area()
{
return width * length;
}
public override double perimeter()
{
return width * 2 + length * 2;
}
}
class Circle: Shape
{
// default constructor
public Circle ()
{
radius = 1;
}
// constructor
public Circle (int radius)
{
this.radius = radius;
}
public override double area()
{
return Math.PI * radius * radius;
}
public override double perimeter()
{
return 2 * Math.PI * radius;
}
}
class Triangle: Shape
{
// default constructor
public Triangle ()
{
this.width = 1;
this.length = 1;
}
// constructor
public Triangle (int width, int length)
{
this.width = width;
this.length = length;
}
public override double area()
{
return width * length / 2;
}
public override double perimeter()
{
return width + Math.Sqrt(Math.Pow(width / 2, 2) + Math.Pow(length, 2)) * 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment