성적표만들기

C#/Study 2019. 9. 27. 13:17

출력 :

메인화면
메인화면에서 1입력시
메인에서 2입력시
추가 페이지에서 저장하는 모습
메인에서 1을 입력후 조회 확인
다수의 데이터 조회
메인에서 3을 입력후 수정할 데이터 선택
수정할 데이터 입력
수정된 데이터 확인
메인에서 4 입력후 삭제할 데이터 선택 (4번선택)
삭제된 데이터 확인

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Syntax
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] array = new string[1006];
            int keynum = 0;
            string fmt = "0000";
            string formatString = "{0,0:" + fmt + "}";
            int language = 0;
            int math = 0;
            int english = 0;
            int mean = 0;
            string datasname = null;
            string datalanguage = null;
            string datamath = null;
            string dataenglish = null;
            string strmean = null;
            float avglanguage = 0.00f;
            float avgmath = 0.00f;
            float avgenglish = 0.00f;
            float avgtotal = 0.00f;
            while (true)
            {
            Restart1:
                Console.WriteLine("1.조회");
                Console.WriteLine("2.추가");
                Console.WriteLine("3.수정");
                Console.WriteLine("4.삭제");
                Console.WriteLine("0.종료");
                string finput = Console.ReadLine();
 
                if (finput == "1")//조회
                {
                    Console.Clear();
                    Console.Write("Total Student: ");
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine(keynum);
                    Console.ResetColor();
                    Console.WriteLine();
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    Console.WriteLine("|번호|     학생명     | 국어 | 수학 | 영어 | 평균 |");
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    for (int i = 0; i < keynum; i++)
                    {
                        int namebytelen= Encoding.Default.GetBytes(array[i,1]).Length;
                        int padlen = 8 - (namebytelen / 2);
                        Console.Write("|");
                        Console.Write(formatString, (i + 1));
                        Console.Write("|");
                        if (namebytelen % 2 != 0)
                        {
                            Console.Write("{0}""".PadLeft(padlen) + array[i,1+ "".PadRight(padlen - 1+ "|");
                        }
                        else
                        {
                            Console.Write("{0}""".PadLeft(padlen) + array[i, 1+ "".PadRight(padlen) + "|");
                        }
                         //이름
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(String.Format("{0,6}", array[i, 2])); //국어
                        Console.ResetColor();
                        Console.Write("|");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(String.Format("{0,6}", array[i, 3]));//수학
                        Console.ResetColor();
                        Console.Write("|");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(String.Format("{0,6}", array[i, 4]));//영어
                        Console.ResetColor();
                        Console.Write("|");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(String.Format("{0,6}", array[i, 5])); //평균
                        Console.ResetColor();
                        Console.WriteLine("|");
 
                    }
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    Console.WriteLine(String.Format("|    | 전체 학생 평균 |{0,6}|{1,6}|{2,6}|{3,6}|", avglanguage.ToString("F2"), avgmath.ToString("F2"), avgenglish.ToString("F2"), avgtotal.ToString("F2"))); //전체학생평균
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                } //조회
                else if (finput == "2")//추가
                {
                Restart2:
                    Console.Clear();
                    Console.Write("학생명: ");
                    datasname = Console.ReadLine();
                    Console.Write("국어: ");
                    datalanguage = Console.ReadLine();
                    Console.Write("수학: ");
                    datamath = Console.ReadLine();
                    Console.Write("영어: ");
                    dataenglish = Console.ReadLine();
                    Console.Write("위의 데이터를 저장하겠습니까? (예: 1 / 아니오 : 0) :");
                    string datasave = Console.ReadLine();
                    if (datasave == "1" && datalanguage.Length<=2 && datamath.Length <= 2 && dataenglish.Length <= 2)
                    {
                        array[keynum, 0= Convert.ToString(keynum + 1);
                        array[keynum, 1= datasname;
                        array[keynum, 2= datalanguage;
                        array[keynum, 3= datamath;
                        array[keynum, 4= dataenglish;
                        language = Convert.ToInt32(array[keynum, 2]);
                        math = Convert.ToInt32(array[keynum, 3]);
                        english = Convert.ToInt32(array[keynum, 4]);
                        mean = (language + math + english) / 3;
                        strmean = Convert.ToString(mean);
                        array[keynum, 5= strmean;
                        keynum++;
                        float sumlanguage = 0.00f;
                        float summath = 0.00f;
                        float sumenglish = 0.00f;
                        float sumtotal = 0.00f;
                        for (int i2 = 0; i2 < keynum; i2++)
                        {
                            sumlanguage = sumlanguage + Convert.ToInt32(array[i2, 2]);
                            summath = summath + Convert.ToInt32(array[i2, 3]);
                            sumenglish = sumenglish + Convert.ToInt32(array[i2, 4]);
                            sumtotal = sumtotal + Convert.ToInt32(array[i2, 5]);
                        }
                        avglanguage = sumlanguage / keynum;
                        avgmath = summath / keynum;
                        avgenglish = sumenglish / keynum;
                        avgtotal = sumtotal / keynum;
                    }
                    else if (datasave == "0")
                    {
                        goto Restart2;
                    }
                    Console.Clear();
 
                }//추가
                else if (finput == "3")//수정
                {
                    Console.Clear();
                    Console.Write("Total Student: ");
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine(keynum);
                    Console.ResetColor();
                    Console.WriteLine();
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    Console.WriteLine("|번호|     학생명     | 국어 | 수학 | 영어 | 평균 |");
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    for (int i = 0; i < keynum; i++)
                    {
 
                        int namebytelen = Encoding.Default.GetBytes(array[i, 1]).Length;
                        int padlen = 8 - (namebytelen / 2);
                        Console.Write("|");
                        Console.Write(formatString, (i + 1));
                        Console.Write("|");
                        if (namebytelen % 2 != 0)
                        {
                            Console.Write("{0}""".PadLeft(padlen) + array[i, 1+ "".PadRight(padlen - 1+ "|");
                        }
                        else
                        {
                            Console.Write("{0}""".PadLeft(padlen) + array[i, 1+ "".PadRight(padlen) + "|");
                        }
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(String.Format("{0,6}", array[i, 2])); //국어
                        Console.ResetColor();
                        Console.Write("|");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(String.Format("{0,6}", array[i, 3]));//수학
                        Console.ResetColor();
                        Console.Write("|");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(String.Format("{0,6}", array[i, 4]));//영어
                        Console.ResetColor();
                        Console.Write("|");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(String.Format("{0,6}", array[i, 5])); //평균
                        Console.ResetColor();
                        Console.WriteLine("|");
 
                    }
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    Console.WriteLine(String.Format("|    | 전체 학생 평균 |{0,6}|{1,6}|{2,6}|{3,6}|", avglanguage.ToString("F2"), avgmath.ToString("F2"), avgenglish.ToString("F2"), avgtotal.ToString("F2"))); //전체학생평균
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    Console.Write($"수정할 데이터 번호를 입력하세요({array[0, 0]}~{keynum}) [0 : Exit]: ");
                    int fixinput = Convert.ToInt32(Console.ReadLine());
                    if (fixinput <= keynum && fixinput != 0)
                    {
                        Console.Clear();
                        Console.Write($"기존 학생명: {array[fixinput - 1, 1]} >> ");
                        Console.ForegroundColor = ConsoleColor.Green;
                        datasname = Console.ReadLine();
                        Console.ResetColor();
                        Console.Write($"기존 국어점수: {array[fixinput - 1, 2]} >> ");
                        Console.ForegroundColor = ConsoleColor.Green;
                        datalanguage = Console.ReadLine();
                        Console.ResetColor();
                        Console.Write($"기존 수학점수: {array[fixinput - 1, 3]} >> ");
                        Console.ForegroundColor = ConsoleColor.Green;
                        datamath = Console.ReadLine();
                        Console.ResetColor();
                        Console.Write($"기존 영어점수: {array[fixinput - 1, 4]} >> ");
                        Console.ForegroundColor = ConsoleColor.Green;
                        dataenglish = Console.ReadLine();
                        Console.ResetColor();
                        array[fixinput - 11= datasname;
                        array[fixinput - 12= datalanguage;
                        array[fixinput - 13= datamath;
                        array[fixinput - 14= dataenglish;
                        language = Convert.ToInt32(array[fixinput - 12]);
                        math = Convert.ToInt32(array[fixinput - 13]);
                        english = Convert.ToInt32(array[fixinput - 14]);
                        mean = (language + math + english) / 3;
                        strmean = Convert.ToString(mean);
                        array[fixinput - 15= strmean;
                        float sumlanguage = 0.00f;
                        float summath = 0.00f;
                        float sumenglish = 0.00f;
                        float sumtotal = 0.00f;
                        for (int i2 = 0; i2 < keynum; i2++)
                        {
                            sumlanguage = sumlanguage + Convert.ToInt32(array[i2, 2]);
                            summath = summath + Convert.ToInt32(array[i2, 3]);
                            sumenglish = sumenglish + Convert.ToInt32(array[i2, 4]);
                            sumtotal = sumtotal + Convert.ToInt32(array[i2, 5]);
                        }
                        avglanguage = sumlanguage / keynum;
                        avgmath = summath / keynum;
                        avgenglish = sumenglish / keynum;
                        avgtotal = sumtotal / keynum;
                    }
                    else if (fixinput == 0)
                    {
                        goto Restart1;
                    }
                    Console.Clear();
 
                }//수정
                else if (finput == "4")//삭제
                {
                    Console.Clear();
                    Console.Write("Total Student: ");
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine(keynum);
                    Console.ResetColor();
                    Console.WriteLine();
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    Console.WriteLine("|번호|     학생명     | 국어 | 수학 | 영어 | 평균 |");
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    for (int i = 0; i < keynum; i++)
                    {
                        int namebytelen = Encoding.Default.GetBytes(array[i, 1]).Length;
                        int padlen = 8 - (namebytelen / 2);
                        Console.Write("|");
                        Console.Write(formatString, (i + 1));
                        Console.Write("|");
                        if (namebytelen % 2 != 0)
                        {
                            Console.Write("{0}""".PadLeft(padlen) + array[i, 1+ "".PadRight(padlen - 1+ "|");
                        }
                        else
                        {
                            Console.Write("{0}""".PadLeft(padlen) + array[i, 1+ "".PadRight(padlen) + "|");
                        }
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(String.Format("{0,6}", array[i, 2])); //국어
                        Console.ResetColor();
                        Console.Write("|");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(String.Format("{0,6}", array[i, 3]));//수학
                        Console.ResetColor();
                        Console.Write("|");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write(String.Format("{0,6}", array[i, 4]));//영어
                        Console.ResetColor();
                        Console.Write("|");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(String.Format("{0,6}", array[i, 5])); //평균
                        Console.ResetColor();
                        Console.WriteLine("|");
 
                    }
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    Console.WriteLine(String.Format("|    | 전체 학생 평균 |{0,6}|{1,6}|{2,6}|{3,6}|", avglanguage.ToString("F2"), avgmath.ToString("F2"), avgenglish.ToString("F2"), avgtotal.ToString("F2"))); //전체학생평균
                    Console.WriteLine("+----+----------------+------+------+------+------+");
                    if (keynum == 0)
                    {
                        Console.WriteLine("저장된 데이터 정보가 없습니다.");
                        
                    }
                    else
                    {
                        Console.Write($"삭제할 데이터 번호를 입력하세요({array[0, 0]}~{keynum}) [0 : Exit]: ");
                        int sinput = Convert.ToInt32(Console.ReadLine());
                        if (sinput <= keynum && sinput != 0)
                        {
                            for (int i5 = sinput; i5 < keynum + 1; i5++)
                            {
                                array[i5 - 10= Convert.ToString(i5);
                                array[i5 - 11= array[i5, 1];
                                array[i5 - 12= array[i5, 2];
                                array[i5 - 13= array[i5, 3];
                                array[i5 - 14= array[i5, 4];
                                array[i5 - 15= array[i5, 5];
                            }
                            keynum--;
                            float sumlanguage = 0.00f;
                            float summath = 0.00f;
                            float sumenglish = 0.00f;
                            float sumtotal = 0.00f;
                            if (keynum!=0)
                            {                            
                            for (int i2 = 0; i2 < keynum; i2++)
                            {
                                sumlanguage = sumlanguage + Convert.ToInt32(array[i2, 2]);
                                summath = summath + Convert.ToInt32(array[i2, 3]);
                                sumenglish = sumenglish + Convert.ToInt32(array[i2, 4]);
                                sumtotal = sumtotal + Convert.ToInt32(array[i2, 5]);
                            }
                            avglanguage = sumlanguage / keynum;
                            avgmath = summath / keynum;
                            avgenglish = sumenglish / keynum;
                            avgtotal = sumtotal / keynum;
                            }
                            else if (keynum == 0)
                            {
                                avglanguage = 0.00f;
                                avgmath = 0.00f;
                                avgenglish = 0.00f;
                                avgtotal = 0.00f;
                            }
                            Console.Clear();
 
                        }
                        else if (sinput == 0)
                        {
                            goto Restart1;
                        }
 
                    }
 
                }//삭제
                else if (finput == "0")//종료
                {
                    break;
                }
                else
                {
                    Console.WriteLine("없는 메뉴입니다.");
                }
 
            }
 
        }
 
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

아직 함수를 배우지 않아서 표를 출력하는 메소드가 3번씩이나 반복되었다. 

함수를 배운다면 코드가 훨씬 짧아질것이다.

또한 데이터를 삭제할때 다음 행의 값을 한칸씩 밀려서 덮어씌우는 방식으로 해서 표에서는 보이진않지만 실질적으로 배열 안에는 마지막 행의 데이터가 그대로남아있다. 배열을 좀더 공부해서 데이터를 완전히 삭제할 수 있으면 좋겠다.

'C# > Study' 카테고리의 다른 글

박싱과 언박싱  (0) 2019.10.31
배열 내 요소중 최대값 구하기  (0) 2019.10.15
1차원 배열  (0) 2019.09.24
09.23 Row와 Column 설정하기  (0) 2019.09.23
문자열 표현방식  (0) 2019.09.19
: