게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
C++ 왜 무한루프가 돌까요??
게시물ID : computer_111215짧은주소 복사하기
작성자 : 아버지레드불
추천 : 0
조회수 : 626회
댓글수 : 5개
등록시간 : 2013/09/09 00:04:17
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#define MAXS 500                //최대 받을 수 있는 학생의 수
#define MAXM 10                    //최대 달의 수
#define MAXL 20                    //달의 최대 강의의 수
#define MAX 3                    //0 = 학생번호, 1 = 달,  2 = 강의번호
using namespace std;

class Attend{
public:
    Attend(){
        for(i=0;i<MAXS;i++){
            for(j=0;j<MAX;j++){
                Student[i][j] = 0;
            }
        }
        for(i=0;i<MAXM;i++){
            for(j=0;j<MAXL;j++){
                OutNumber[i][j] = 0;
            }
        }
        InStudent = 0;
        OutTemp   = 0;
    }
    void InputFile();                                    //외부에서 파일을 입력 받아오고, 그 자료를 출력해서 낸다.
    void StudentCount(int StuNumber,int Month, int LectureNumber);        //받을 학생들을 저장하고 배열한다.
    void Count();                                        //달,강의번호에 해당하는 만큼 인원수를 카운트 한다.
    void Compair();                                        //가장 많이 결석한 달,강의번호를 찾아낸다.
private:
    int Student[MAXS][MAX];
    int OutNumber[MAXM][MAXL];
    int InStudent;
    int OutTemp;
    int i;
    int j;
};

void Attend::InputFile(){
    ifstream file1;
    ofstream file2;

    int         SN = 0;
    int          M = 0;
    int         LN = 0;
    InStudent   = 0;
    
    string strTemp;        //학생 수(파일로 받을시는 string으로 받았기 때문에, int형식으로 바꿀예정)
    string str1;        //학번
    string str2;        //달
    string str3;        //강의번호
    
    file1.open("1.inp");
    file2.open("output.out");
    file1>>strTemp;

    InStudent = stoi(strTemp);        //int 형으로 변경

    for(i=0;i!=InStudent;i++){
        file1>>str1>>str2>>str3;        //입력 받은 파일에서 자료값드을 받는다.
        SN = stoi(str1);            //int형으로 변경
        M = stoi(str2);                //int형으로 변경
        LN = stoi(str3);            //int형으로 변경
        StudentCount(SN,M,LN);        //받은 학생의 명단으로 부터 학생 수를 달, 수업 등으로 카운트한다.
    }
    Count();
    Compair();

    for(i=0;i<MAXM;i++){                        //가장 많이 결석한 달, 강의번호, 수를 출력해서 파일로 내보낸다.
        for(j=0;j<MAXL;j++){
            if(OutNumber[i][j] == OutTemp){
                file2<<i<<" "<<j<<" "<<OutTemp<<endl;
            }
            else
                continue;
        }
    }

    file1.close();
    file2.close();
}

void Attend::StudentCount(int StuNumber,int Month, int LectureNumber){

    int temp;

    for(i=0;i<InStudent;i++){            //학생번호,달,강의번호 순으로 자료를 저장하되, 겹치는 경우 그 것을 빼버린다.
        if(Student[i][0] == StuNumber){
            if(Student[i][1] == Month){
                if(Student[i][2] == LectureNumber){
                    for(j=0;j<MAX;j++){
                        Student[i][j] = 0;
                    }
                }
                else
                    continue;
            }
            else
                continue;
        }
        else if(Student[i][0] != 0)
            continue;
        else if(Student[i][0] = 0){
            Student[i][0] = StuNumber;
            Student[i][1] = Month;
            Student[i][2] = LectureNumber;
            break;
        }
    }

    for(i=0;i<InStudent;i++){
        if(Student[i][0] == 0){
            if(Student[i+1][0] != 0){
                for(j=0;j<MAX;j++){
                    temp = Student[i][j];
                    Student[i][j] = Student[i+1][j];
                    Student[i+1][j] = temp;
                }
            }
            else if(Student[i+1][0] == 0){
                break;
            }
        }
        else if(Student[i][0] < Student[i+1][0]){
            for(j=0;j<MAX;j++){
                temp = Student[i][j];
                Student[i][j] = Student[i+1][j];
                Student[i+1][j] = temp;
            }
        }
        else if(Student[i][0] == Student[i+1][0]){
            if(Student[i][1] < Student[i+1][1]){
                for(j=0;j<MAX;j++){
                    temp = Student[i][j];
                    Student[i][j] = Student[i+1][j];
                    Student[i+1][j] = temp;
                }
            }
        }
        else if(Student[i][0] == Student[i+1][0]){
            if(Student[i][1] == Student[i+1][1]){
                if(Student[i][2] < Student[i+1][2]){
                    for(j=0;j<MAX;j++){
                        temp = Student[i][j];
                        Student[i][j] = Student[i+1][j];
                        Student[i+1][j] = temp;
                    }
                }
            }
        }
    }
}


void Attend::Count(){

    for(i=0;Student[i][0] != 0;i++){
        ++OutNumber[Student[i][1]][Student[i][2]];
    }
}

void Attend::Compair(){

    for(i=0;i<MAXM;i++){
        for(j=0;j<MAXL;j++){
            if(OutTemp == 0){
                if(OutNumber[i][j] != 0){
                    OutTemp = OutNumber[i][j];
                }
                else
                    continue;
            }
            else if(OutTemp != 0){
                if(OutTemp < OutNumber[i][j]){
                    OutTemp = OutNumber[i][j];
                }
                else{
                    continue;
                }
            }
            else{
                continue;
            }
        }
    }
}
void main(){

    Attend Student;
    
    Student.InputFile();

}

무한루프를 미친듯이 도네요..
어딘지를 못찼겠어요..ㅡㅜ
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호