디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

c++ 연결리스트를 이용한 더하기 빼기 곱하기 문제입니다. 도와주세요 ㅜ

하양(221.159) 2011.11.06 17:31:27
조회 306 추천 0 댓글 4


#include <iostream>
#include <fstream>
using namespace std;
enum SignType{PLUS, MINUS};

struct NodeType
{
 int info;
 NodeType *next;
 NodeType *back;
};

class SpecializedList{
public:
        SpecializedList();
        ~SpecializedList();
        SpecializedList(const SpecializedList& someList);
        void ResetForward();
        void GetNextItem(int& item, bool& finished);
        void ResetBackward();
        void GetPriorItem(int& item, bool& finished);
        void InsertFront(int item);
        void InsertEnd(int item);
        int LengthInt();
private:
        NodeType* list;
        NodeType* currentNextPos;
        NodeType* currentBackPos;
        int length;
};

SpecializedList::SpecializedList(){
        length = 0;
        list = NULL;
}        
int SpecializedList::LengthInt(){
        return length;
}
void SpecializedList::ResetForward(){
        currentNextPos = NULL;
}
void SpecializedList::GetNextItem(int& item,bool& finished){
        if(currentNextPos == NULL)
                currentNextPos = list->next;
        else
                currentNextPos = currentNextPos -> next;
        item = currentNextPos -> info;
        finished = (currentNextPos == list);
}

void SpecializedList::ResetBackward(){
        currentBackPos = NULL;
}

void SpecializedList::GetPriorItem(int& item, bool& finished){
        if(currentBackPos == NULL)
                currentBackPos = list;
        else
                currentBackPos = currentBackPos -> back;
        item = currentBackPos -> info;
        finished = (currentBackPos == list->next);
}

void SpecializedList::InsertFront(int item){
        NodeType* newNode;
        newNode = new NodeType;
        newNode->info = item;
        if(list == NULL){
                newNode->back = newNode;
                newNode->next = newNode;
                list = newNode;
        }
        else{
                newNode->back = list;
                newNode->next = list->next;
                list->next->back = newNode;
                list->next = newNode;
        }
        length++;
}

void SpecializedList::InsertEnd(int item){
        NodeType* newNode;
        newNode = new NodeType;
        newNode->info = item;
        NodeType* location;
        location = list;
        if(list == NULL){
                newNode->back = newNode;
                newNode->next = newNode;
                list = newNode;
        }
        else{
                while(list->next != NULL){
                        location = location -> next;
                }
                location->next = newNode;
                newNode->next = NULL;
                newNode->back = location;
        }
        length++;
}

class LargeInt
{  
public: 
         LargeInt();
         bool operator<(LargeInt second);
        //bool operator==(LargeInt second);
        LargeInt operator+(LargeInt second);
        //LargeInt operator-(LargeInt second);
        void InsertDigit(int itme);
        void Write(ostream& out);
private:
        SpecializedList number;
        SignType sign;
        int numDigits;
};
 
void Add(SpecializedList first, SpecializedList second, SpecializedList& result){
        int carry = 0;
        bool finished1 = false;
        bool finished2 = false;
        int temp;
        int digit1;
        int digit2;

        first.ResetBackward();
        second.ResetBackward();
        
        while(!finished1 && !finished2){
                first.GetPriorItem(digit1, finished1);
                second.GetPriorItem(digit2, finished2);
                temp = digit1 + digit2 + carry;
                carry = temp / 10;
                result.InsertFront(temp%10);
        }
        while(!finished1){
                first.GetPriorItem(digit1, finished1);
                temp = digit1 + carry;
                carry = temp / 10;
                result.InsertFront(temp%10);
        }
        while(!finished2){
                second.GetPriorItem(digit2, finished2);
                temp = digit2 + carry;
                carry = temp / 10;
                result.InsertFront(temp%10);
        }
        if(carry != 0)
                result.InsertFront(carry);
}


void Sub(SpecializedList first, SpecializedList second, SpecializedList& result){
        int carry = 0;
        bool finished1 = false;
        bool finished2 = false;
        int temp;
        int digit1;
        int digit2;

        first.ResetBackward();
        second.ResetBackward();
        
        while(!finished1 && !finished2){
                first.GetPriorItem(digit1, finished1);
                second.GetPriorItem(digit2, finished2);
                temp = digit1 - digit2 - carry;
                if(temp<0){
                        carry = 1;
                        result.InsertFront(temp+10);
                }
                else
                        result.InsertFront(temp);
        }
        while(!finished1){
                first.GetPriorItem(digit1, finished1);
                temp = digit1 - carry;
                if(temp<0){
                        carry = 1;
                        result.InsertFront(temp+10);
                }
                else
                        result.InsertFront(temp);
        }

}


LargeInt LargeInt::operator+(LargeInt second){
        SignType selfSign;
        SignType secondSign;
        LargeInt result;
        
        if(sign == second.sign){
                Add(number, second.number, result.number);
                result.sign = sign;
        }
        else{
                selfSign = sign;
                secondSign = second.sign;
                sign = PLUS;
                second.sign = PLUS;
                if(*this < second){
                        Sub(second.number, number, result.number);
                        result.sign = secondSign;
                }
                else if(second < *this){
                        Sub(number, second.number, result.number);
                        result.sign = selfSign;
                }
                sign = selfSign;
        }
        result.numDigits = result.number.LengthInt();
        return result;
}
enum RelationType {LESS, GREATER, EQUAL};
RelationType compareDigits(SpecializedList first, SpecializedList second);
RelationType CompareDigits(SpecializedList first, SpecializedList second){
        bool same = true;
        bool finished = false;
        int digit1;
        int digit2;

        first.ResetForward();
        second.ResetForward();
        while(!finished){
                first.GetNextItem(digit1, finished);
                second.GetNextItem(digit2, finished);
                if(digit1 < digit2)
                        return LESS;
                if(digit1 > digit2)
                        return GREATER;
        }
        return EQUAL;
}
bool LargeInt::operator<(LargeInt second){
        RelationType relation;
        
        if(sign == MINUS && second.sign == PLUS)
                return true;
        else if(sign == PLUS && second.sign == MINUS)
                return false;
        else if(sign == PLUS && numDigits < second.numDigits)
                return true;
        else if(sign == MINUS && numDigits > second.numDigits)
                return false;
        else if(sign == MINUS && numDigits > second.numDigits)
                return true;
        else if(sign == MINUS && numDigits < second.numDigits)
                return false;
        else{
                relation = CompareDigits(number, second.number);
                if(sign == PLUS && relation == GREATER)
                        return true;
                else if(sign == PLUS && relation == GREATER)
                        return false;
                else if(sign == MINUS && relation == GREATER)
                        return true;
                else return false;
        }
}

void LargeInt::InsertDigit(int item){
        number.InsertEnd(item);
}
void LargeInt::Write(ostream& out){
        bool finished = false;
        int digit1;

        number.ResetForward();
        while(!finished){
                number.GetNextItem(digit1, finished);
                out << digit1 << " ";
        }
}

int main()
{
 LargeInt list1;
 LargeInt list2;
 LargeInt list3;
 ifstream in("input.txt");
 ofstream out("out.txt", ios::out | ios::ate);
 char c;
// char op;
 while(1)
 {
  //in.get(c);//\'a\'읽어오기
  //op = c;
  //in.get(c);//엔터 문자 읽기
  if(in.eof()) break;//파일의끝이면 루프 종료
  //첫번째줄 읽기
  while(1)
  { 
   in.get(c);//한 문자씩 읽어오기
   if(c == \'-\'){
        }
   if(c == \'\\n\') break;
   list1.InsertDigit(c - \'0\');
  }
  //두번째줄 읽기
  while(1)
  {
   in.get(c);
   if(c == \'\\n\') break;
   list2.InsertDigit(c - \'0\');
  }
  list3 = list1 + list2;
list3.Write(cout);
  //더하기

 }

 in.close();
 out.close();
 return 0;
}

여기까지 코드를 짯는데요.

자꾸 오류가 나요 ㅜㅜ

뭐가 잘못된 걸까요????????????????

추천 비추천

0

고정닉 0

0

원본 첨부파일 1

댓글 영역

전체 댓글 0
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 뛰어난 운동 신경으로 남자와 싸워도 이길 것 같은 여자 스타는? 운영자 25/11/24 - -
AD 따뜻한 겨울나기! 방한용품 SALE 운영자 25/11/27 - -
286595 프갤형들 좀 도와주세요 ㅜㅜ [9] 아라라리요갤로그로 이동합니다. 11.11.07 105 0
286594 지구에서 마지막 밤을 보내라 [2] 거칠게갤로그로 이동합니다. 11.11.07 76 0
286592 그러고보니 소행성 결과나기 별로 시간 안남았네 [6] Stan(220.244) 11.11.07 72 0
286590 우하하하하하! ㅅㅅ 했다!!! [3] Adelposs갤로그로 이동합니다. 11.11.07 134 0
286587 다음 면접 후기.... ㅎㄷㄷ [5] [성대아싸]갤로그로 이동합니다. 11.11.07 630 0
286586 소행성 떨어져서 퇴보되면.. 우리는 설자리가 없겠지.. [3] 쿄스케갤로그로 이동합니다. 11.11.07 78 0
286585 진짜 소행성 떨어져서 500년정도 퇴보됬으면 좋겠다ㅋㅋㅋㅋㅋㅋ [2] ㄴㄹㅇ(218.48) 11.11.07 85 0
286584 퀵소트보다 빠른 정렬알거리듬이있음? [5] ㅋㅌㅊ(210.178) 11.11.07 120 0
286583 아스형님 아까 깝쳐서 죄송합니다 얼빠진호랑이갤로그로 이동합니다. 11.11.07 67 0
286582 캐나다로 취직해야겠다 [1] 三didas갤로그로 이동합니다. 11.11.07 112 0
286581 아 진짜 살빼야지 ㅠㅠ.... [1] Lover♥갤로그로 이동합니다. 11.11.07 49 0
286580 c에서 %출력 어떻게 해잉.. 조찌따(203.237) 11.11.07 44 0
286577 기독교인한테 내일 소행성에 대해서 어떻게 생각하냐고 물어보면 [1] Stan(220.244) 11.11.07 59 0
286576 횽들... 석사와 연구실 어떻게 해야될까? [5] 작은아우(180.231) 11.11.07 137 0
286574 횽들 list에서 text 옮기는 법 좀....ㅠㅠ [4] 젠장(180.66) 11.11.07 51 0
286573 자바에서 네트워크프로그래밍 해보신분^^ [3] 드럼좋아요갤로그로 이동합니다. 11.11.07 82 0
286572 [ 세 기 말 ] 역시섹스는 번지섹스... [4] 다림줄갤로그로 이동합니다. 11.11.07 238 0
286571 무슨 프로토스냐 / 스타쉽트루퍼스 안봤냐? ㄷㄷ(112.221) 11.11.07 39 0
286569 프로토스같은 외계인들이 지구가 태란같이 쌔지기전에 포맷시키는거일지도 Stan(220.244) 11.11.07 35 0
286568 흔한 귀찬귀찬 열매먹은 겜돌이의 RTTI [1] 햏햏했(115.90) 11.11.07 51 0
286567 운석충돌해서 쎾쓰파티 했으면 좋겠따 [5] 개쉛기갤로그로 이동합니다. 11.11.07 98 0
286566 유명인사들이 벙커속에 지들만 살려고 숨을까?? 우리는 버리고?? [7] 쿄스케갤로그로 이동합니다. 11.11.07 81 0
286565 우리가 하하호호 그딴 소행성 다 허풍이야~ 하고있을때 [1] Stan(220.244) 11.11.07 60 0
286563 바닥에 침 뱉은것도 지옥가는 사유가 되나요? [5] Stan(220.244) 11.11.07 74 0
286561 원의 신비 다림줄갤로그로 이동합니다. 11.11.07 66 0
286560 철업는 고딩들 죄진것도 지옥에선 미성년자니까 봐줄까? [1] Stan(220.244) 11.11.07 60 0
286559 근데 만약에 지구를 안치구 달은 친다면 어케될까여 [3] Rocket Queen갤로그로 이동합니다. 11.11.07 55 0
286558 여기사 소행성 겔이라는는게 사실인가여?? 거칠게갤로그로 이동합니다. 11.11.07 45 0
286557 불교가 말하는 지옥.jpg [1] Stan(220.244) 11.11.07 90 0
286556 소행성이 지구에 떨어지긴 개뿔... 이문동쮸쮸바갤로그로 이동합니다. 11.11.07 44 0
286555 소행성이 떨어지때 풍압만으로 사람은 날라가고 건물은 파괴된다!!! [1] 거칠게갤로그로 이동합니다. 11.11.07 55 0
286553 근데 진짜 행성 핵미사일로 쪼갤수 있어여?? [2] Rocket Queen갤로그로 이동합니다. 11.11.07 55 0
286552 소행성이 과연 얌전히 지나가줄까? [1] 꼬꼬월드갤로그로 이동합니다. 11.11.07 42 0
286551 행성이고나발이고 三didas갤로그로 이동합니다. 11.11.07 46 0
286550 영상관련 진짜 처음배울때 묻는 질문 ! [1] 꺌꺌(210.123) 11.11.07 48 0
286549 종말드립 치는거 보니 생각난다.. [1] 이문동쮸쮸바갤로그로 이동합니다. 11.11.07 40 0
286548 근대 젼나 불교에서 말하는 지옥과 서양에서 말하는 지옥 전나 비슷함ㅋ [2] Stan(220.244) 11.11.07 101 0
286547 프로그래머들아 빨리 핵시설이라도 해킹해서 핵 미사일 좀 소행성에 쏴라 거칠게갤로그로 이동합니다. 11.11.07 56 0
286546 어떡하지..sbs [4] ㅠㅠ(115.93) 11.11.07 86 0
286545 소행성형님 나가시는데 종교집단은 뭐하고있냐 ㅂㅈㄷ(210.178) 11.11.07 54 0
286544 쿄게이 원하면 제가 먹어드림 [2] Lover♥갤로그로 이동합니다. 11.11.07 60 0
286542 어어!!!! 행성떨어진다!!!!!! 지금 미국인데 행성보임!!!!!!!! [5] Stan(220.244) 11.11.07 113 0
286541 빨리 회사고 뭐고 다 때려치고 오늘은 너가 하고 싶은거 해라 [1] 거칠게갤로그로 이동합니다. 11.11.07 73 0
286536 어차피 내일 뒤질꺼 오늘 하고 싶은거 다 하자!! [1] 거칠게갤로그로 이동합니다. 11.11.07 57 0
286535 400미터 소혹성이 떨어지면 지구 생물은 전멸이라는게 사실인가여?? [4] 거칠게갤로그로 이동합니다. 11.11.07 98 0
286533 씨발놈들아 그동안 잘 있었냐? [1] 2005YU55(121.182) 11.11.07 69 0
286532 만약 나사의 시나리오가 틀려서 소행성에 핵쏜다면 어떻게될까염? [1] Stan(220.244) 11.11.07 78 0
286531 반지하 존나 춥다. 쥬어없는그분(210.181) 11.11.07 52 0
286530 횽들앙 쉬운 코드인데 한번만 봐주심 안될까요? [3] 뿌잉뿌잉(65.49) 11.11.07 68 0
286529 소행성 얘기 트루?? [4] 에이시아(125.31) 11.11.07 87 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2