안녕하세요. 질문 좀 드리겠습니다.
큐티베이비
나름 c++을 통해 로또코드를짜봤는데요.
try catch를 어째쓰는지 아직도 잘 적응이 안되네요;;
게다가 프로그램을 실행시키니 제대로 작동을 안해서 디버그 모드로 가보았더니
많은 예외처리가 되어있다면서 안되네요;;;
그래서 제가 드리는 질문은
1. 오류 해결법
2. 지금 코드에서 try catch를 어떻게 사용하는지;;(앞으로 더 많은 코드를 짜보며 알아가야될 부분같네요;)
3. class로도 링크드 리스트 구현이 가능한가요?
(구조체가 아닌 클래스로도 구현이 가능할거 같은데 비슷하게 구현하면 되는지 여쭤봅니다.)
4. 제가 짠 코드가 c++스러운 코드인지도 평가 좀 부탁드리겠습니다;ㅂ;ㅋ
위 네가지 입니다.
빠르고 친절한 답변 감사하구요~ 즐거운 명절 남은 마무리 잘하시길 바랍니다 ^ -^
아래는 제가 일단 대충 짜본 코드입니다.
================
lotto.h
#ifndef _LOTTO_H_
#define _LOTTO_H_
#include iostream
#include string
#include fstream
#include time.h
#pragma warning(disable : 4290)
using namespace std;
#define MAX 6
#define PATH c:\\LottoNum.txt
#define COUNT c:\\CountNum.txt
int menu();
int calLotto();
int fOutput();
int theEnd();
class Lotto
{
public :
Lotto();
protected :
int count;
int array[MAX];
Lotto *NextNode;
private :
Lotto *Head;
Lotto *Tail;
};
class LottoSetNum : public Lotto
{
public :
int setCount();
int setRandNum();
int getCount();
int getRandNum();
int isCompare();
};
class LottoOutput : public Lotto
{
public :
int outputFile();
};
/*
class LottoSearch : public Lotto
{
public :
LottoSearch();
};
*/
#endif
lotto.cpp
#include Lotto.h
Lotto::Lotto()
{
count = 0;
for(int i = 0 ; i MAX ; i++)
{
array[i] = 0;
}
NextNode = NULL;
Head = NULL;
Tail = NULL;
}
int LottoSetNum::setRandNum()
{
srand(time(NULL));
for(int i = 0; i MAX ; i++)
{
this-array[i] = (rand()%45)+1;
}
return 0;
}
int LottoSetNum::getRandNum()
{
ofstream fText;
fText.open(PATH);
if(fText.fail())
throw PATH;
this-count++;
cout this-count 회차. ;
for(int i = 0 ; i MAX ; i++)
{
fText this-array[i] . ;
cout this-array[i] . ;
}
fText endl;
cout endl;
fText.close();
return 0;
}
int LottoSetNum::isCompare()
{
for(int i = 1 ; i MAX ; i++)
{
if(this-array[0]==this-array[i])
{
return 1;
}
}
return 0;
}
int LottoSetNum::setCount()
{
ifstream fText;
fText.open(COUNT);
if(fText.fail())
throw COUNT;
fText this-count;
fText.close();
return 0;
}
int LottoSetNum::getCount()
{
ofstream fText;
fText.open(COUNT);
if(fText.fail())
throw COUNT;
fText this-count;
fText.close();
return 0;
}
int LottoOutput::outputFile()
{
char ch = NULL;
ifstream fText;
fText.open(PATH);
if(fText.fail())
throw PATH;
while(fText.eof()==false)
{
fText ch;
cout ch;
}
fText.close();
return 0;
}
lottoctl.cpp
#include Lotto.h
int main()
{
int result = 0;
do
{
result = menu();
switch(result)
{
case 1 : calLotto();
break;
case 2 : fOutput();
break;
case 3: theEnd();
break;
default :
throw result;
}
} while (result != 3);
return 0;
}
int menu()
{
int input = 0;
cout 1. 로또번호 추출 endl;
cout 2. 로또번호 출력 endl;
cout 3. 프로그램 종료 endl;
cout 입력 ;
cin input;
return input;
}
int calLotto()
{
int result = 0;
LottoSetNum lotto;
//try
//{
do
{
lotto.setCount();
lotto.setRandNum();
result = lotto.isCompare();
} while (result != 0);
lotto.getRandNum();
lotto.getCount();
//}
//catch(CException* e)
//{
//cout e 를 열지 못하였습니다. endl;
//}
return 0;
}
int fOutput()
{
LottoOutput lotto;
lotto.outputFile();
return 0;
}
int theEnd()
{
cout 프로그램을 종료하겠습니다.;
return 0;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |
2691483 | 파일 오픈시 에러 질문드립니다. (2) | 호습다 | 2025-04-14 |
2691450 | [visual c++ 툴]기초 질문 (3) | 해긴 | 2025-04-13 |