본문 바로가기

프로그래밍/C#

C# 구조체, 클래스, 리스트

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace number4
{
class Program
{
public struct rentcar
{
public string carID;
public string carTime1; //대여
public string carTime2; //반납
public int renttime;

public rentcar(string _carID, string _carTime1, string _carTime2)
{
carID = _carID;
carTime1 = _carTime1; //대여
carTime2 = _carTime2; //반납
renttime = 0;
}
}
public struct rentcar_file
{
public string carID;
public string type;
public string carDate;

public rentcar_file(string _carID, string _type, string _carDate)
{
carID = _carID;
type = _type;
carDate = _carDate;
}
}

static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Invalid filename");
return;
}
string filename = args[0];
StreamReader file = new StreamReader(filename);

List<rentcar> list = new List<rentcar>();
string line = string.Empty;
line = file.ReadLine();

List<rentcar_file> filelist = new List<rentcar_file>();

while ((line = file.ReadLine()) != null)
{
string[] str = line.Split(' ');
rentcar_file t = new rentcar_file(str[0], str[1], str[2]);
filelist.Add(t);
}

filelist.Sort(delegate(rentcar_file a, rentcar_file b)
{
return string.Compare(a.carDate, b.carDate);
});

for(int j=0; j< filelist.Count; j++)
{
rentcar_file t = filelist[j];
bool bFind = false;
for(int i=0;i<list.Count;i++) {
if(list[i].carID == t.carID) {
bFind = true;

rentcar temp = list[i];
if (t.type == "S") //대여
{
if ((temp.carTime1 == "") && (temp.carTime2 == ""))
{
//비어있으면 대여시각만 쓰기
temp.carTime1 = t.carDate;
list.RemoveAt(i);
list.Add(temp);
}
else if ((temp.carTime1 == "") && (temp.carTime2 != ""))
{
//반납시간만 있는 경우
temp.carTime1 = "20170601000000";
temp.renttime = temp.renttime + get_diff_dtime(temp.carTime1, temp.carTime2);
temp.carTime1 = t.carDate;
temp.carTime2 = "";
list.RemoveAt(i);
list.Add(temp);
}
else if (temp.carTime1 != "")
{
//이미 대여시간이 있고 반납이 없는 경우, 처리해주고
temp.carTime2 = "20170701000000";
temp.renttime = temp.renttime + get_diff_dtime(temp.carTime1, temp.carTime2);
//새로 들어온 대여 시간 추가
temp.carTime1 = t.carDate;
temp.carTime2 = "";
list.RemoveAt(i);
list.Add(temp);
}
}
else if (t.type == "E") //반납
{
//반납하려는데 대여시간이 없는 경우, 처리
if ((temp.carTime1 == "") && (temp.carTime2 == ""))
{
temp.carTime1 = "20170601000000";
temp.carTime2 = t.carDate;
temp.renttime = temp.renttime+get_diff_dtime(temp.carTime1, temp.carTime2);
temp.carTime1 = "";
temp.carTime2 = "";
list.RemoveAt(i);
list.Add(temp);
}
else if (temp.carTime1 != "")
{
//반납시간을 쓰고, 시간 계산
temp.carTime2 = t.carDate;
temp.renttime = temp.renttime+get_diff_dtime(temp.carTime1, temp.carTime2);
list.RemoveAt(i);
temp.carTime1 = "";
temp.carTime2 = "";
list.Add(temp);
}
}
break;
}
else
continue;
}
if(bFind) continue;

rentcar rc;
if(t.type == "S") //대여
{
rc = new rentcar(t.carID, t.carDate, "");
list.Add(rc);
}
else if(t.type=="E") //반납
{
rc = new rentcar(t.carID, "", t.carDate);
list.Add(rc);
}
}
file.Close();

for (int i = 0; i < list.Count; i++)
{
if (list[i].carTime1 != "")
{
rentcar temp = list[i];
temp.carTime2 = "20170701000000";
temp.renttime = temp.renttime + get_diff_dtime(temp.carTime1, temp.carTime2);
list.RemoveAt(i);
temp.carTime1 = "";
temp.carTime2 = "";
list.Add(temp);
}
else if(list[i].carTime2 != "")
{
rentcar temp = list[i];
temp.carTime1 = "20170601000000";
temp.renttime = temp.renttime + get_diff_dtime(temp.carTime1, temp.carTime2);
list.RemoveAt(i);
temp.carTime1 = "";
temp.carTime2 = "";
list.Add(temp);
}
}

list.Sort(delegate(rentcar a, rentcar b)
{
if (a.renttime > b.renttime) return -1;
else if (a.renttime < b.renttime) return 1;
return 0;
});

Console.WriteLine("총 차량 대수: {0}대", list.Count);
for (int i = 0; i<list.Count; i++)
{
Console.WriteLine("{0}, {1}초", list[i].carID, list[i].renttime);
}
}
static int get_diff_dtime(string date1, string date2)
{
/*
string myStringDate = "1999/9/9";
// String을 DateTime으로 변환
DateTime myDate = DateTime.Parse(myStringDate);
// DateTime을 String으로 변환
string myConvertedDate = myDate.ToString("yyyy/MM/dd hh:mm:ss");
*/
IFormatProvider KR_Format = new System.Globalization.CultureInfo("ko-KR", true);

DateTime d1 = DateTime.ParseExact(date1, "yyyyMMddHHmmss", KR_Format);
DateTime d2 = DateTime.ParseExact(date2, "yyyyMMddHHmmss", KR_Format);
TimeSpan t = d2 - d1;

return (int)t.TotalSeconds;
}
}
}

반응형

'프로그래밍 > C#' 카테고리의 다른 글

C# 역문자열  (0) 2019.03.24
C# 문자열, 숫자, 영어 검사  (0) 2019.03.24
C# 비트연산  (0) 2019.03.24
C# 해시테이블  (0) 2019.03.24
C# Dictionary 딕셔너리  (0) 2019.03.24