디시인사이드 갤러리

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

갤러리 본문 영역

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

하양(221.159) 2011.11.06 17:31:27
조회 305 추천 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 - -
공지 프로그래밍 갤러리 이용 안내 [97] 운영자 20.09.28 48785 65
2905356 입시 면접 FM 합격 솔루션(필승 전략 해법)!U 프갤러(121.142) 23:15 6 1
2905353 조갑제도 “국힘은 이적단체”…여당은 ‘내란 가짜뉴스’ 대응 중 발명도둑잡기(118.216) 22:57 10 0
2905352 해외 투자 증세에 관해 [1] 발명도둑잡기(118.216) 22:54 35 0
2905351 나님 달러 미국주식 풀매수중 ㅇㅅㅇ [4] ♥멘헤라냥덩♥갤로그로 이동합니다. 22:50 21 0
2905350 미국주식갤에서 친미매국노들 욕하다가 차단당함 [2] 손발이시립디다갤로그로 이동합니다. 22:43 18 0
2905349 이태원 발언으로 모욕죄 벌금 100만원 싸게쳤다 vs 과하다 [5] ㅇㅇ(39.7) 22:36 30 0
2905348 영어를 못해서 구글을 못가네 [1] ㅇㅇ갤로그로 이동합니다. 22:35 17 0
2905345 원티드 자격요건은 거기 직원들도 다 못할거 같은데 ㅇㅇ(182.228) 22:25 12 0
2905344 이태원 모욕죄로 100만원 벌금받았는데 걍 목매달고 죽을까 [2] ㅇㅇ(39.7) 22:24 20 0
2905343 오픈소스에서는 취약점을 감시하는 사람이 더 많다 발명도둑잡기(118.216) 22:23 13 0
2905342 사타구니 털 제모하면 이상한 사람인가요? [5] 넥도리아(220.74) 22:22 28 0
2905341 노말틱도 말해주잖아 오픈소스의 취약점 [1] ㅇㅇ(114.30) 22:17 23 0
2905340 <복면가왕> 관련 생각나는 예전 글 발명도둑잡기(118.216) 22:14 14 0
2905339 정치가 어쨌든 나라가 어쨌든 국가가 어쨌든 삶이 어쨌든 [8] 넥도리아(220.74) 22:13 38 0
2905338 이태원 모욕죄로 구약식 벌금100만원 = 재산잃고 전과남고 인생조진거지? [10] ㅇㅇ(39.7) 22:11 26 0
2905337 저좀 어떻게 성공시켜 주실 분 없나요? [2] 넥도리아(220.74) 22:07 23 0
2905335 사실 제가 틀딱이 아니라 귀여운 여자임을 인증합니다 [6] 헬마스터갤로그로 이동합니다. 21:58 41 0
2905332 페이커닮은 개발자 찾습니다 프갤러(106.101) 21:38 22 0
2905331 진짜 크게 벌리는 건 아니고… 그냥 생활비용 차트부자(1.233) 21:35 16 0
2905325 ❤✨☀⭐⚡☘⛩☃나님 시작합니당☃⛩☘⚡⭐☀✨❤ [1] ♥멘헤라냥덩♥갤로그로 이동합니다. 20:52 22 0
2905324 프로그래머, 내가 하다 하다 3D 공부한다. [4] 프갤러(59.16) 20:52 43 1
2905319 출퇴근길에 소소하게 짭짤하네요 [1] 존버장군(1.233) 20:15 30 0
2905316 나 좀 찾지 말아달라고 했음 ㅇㅇ(211.234) 20:02 29 0
2905313 나 아까 실수했네 발명도둑잡기(118.216) 19:45 23 0
2905299 서울 유명 스웨디시 후기 모음집 ㅇㅇ(118.235) 18:50 28 0
2905298 어제부터 저녁에 배가 안 고픔;; [6] ♥멘헤라냥덩♥갤로그로 이동합니다. 18:49 49 0
2905297 클래스 101 결제해본 사람있나 ㅇㅇ(140.248) 18:28 26 0
2905296 국비 말고 혼자 자격증 따서 취직하는것도 가능함? [1] ㅇㅇ갤로그로 이동합니다. 18:27 31 0
2905294 후 시발 이런건 15분만에 최라락 써서 뚝딱 해야하는건데 프갤러(14.52) 17:56 31 0
2905293 회원가입 겨우 완성했다. 프갤러(14.52) 17:53 32 0
2905292 <서울 자가에 대기업 다니는 김부장 이야기>관련 생각나는 글 발명도둑잡기(118.216) 17:31 35 0
2905291 맨땅에 헤딩하기에는 c가 가장 적절한듯 ㅇㅇ(118.235) 17:29 22 0
2905289 AI 로 뚝딱해서 돈 벌었다는 사람 통장 까봐. 프갤러(59.16) 17:09 31 1
2905288 미디어에서 AI 로 1 분만에 뚝딱 개발했다니까 정말 그런줄 알아. 프갤러(59.16) 17:04 30 1
2905287 프로그래머가 되려면 꿈과 희망을 버려라. [3] 프갤러(59.16) 16:57 51 0
2905284 “시간이 돈을 버는 구조 만들기: 매달 ‘짭짤한 복리’ 얻는 방식” 88아재 (119.15) 16:32 18 0
2905283 <우주메리미>가 인기래서 생각나는 예전 글 발명도둑잡기(118.216) 16:28 20 0
2905282 면접문제를 만들어봤다. 프갤러(49.165) 16:11 31 0
2905281 "찬송가 부르고 주식 사"‥ 이불말이는 주가 올리는 '제물' 발명도둑잡기(118.216) 16:03 13 0
2905280 시대의 어르신들이 하나 둘 돌아가시는게 참 먹먹하구낭.. [3] ♥멘헤라냥덩♥갤로그로 이동합니다. 15:55 37 0
2905279 바지가 내려가 넥도리아(223.38) 15:49 34 0
2905278 ■si회사갈거면 포트폴리오 프론트엔드 뭘로하는게 좋냐? [9] ㅇㅇ갤로그로 이동합니다. 15:47 58 0
2905277 Ada 인생 40 년 갈아 넣었습니다. 프갤러(59.16) 15:33 28 0
2905276 착한 중국인 환영⭐+ ♥멘헤라냥덩♥갤로그로 이동합니다. 15:30 24 0
2905275 Ada 언어는 공부하면 할수록 너무 신기하다.. ㅎㅎ 나르시갤로그로 이동합니다. 15:14 26 0
2905274 나님 왤케 소중하실깡..?⭐+ [2] ♥멘헤라냥덩♥갤로그로 이동합니다. 15:08 42 0
2905272 SK네트웍스 FAMILY AI 캠프 24기 프갤러(118.235) 14:46 26 0
2905271 미국정치 갤러리 이미지, 동영상 첨부 차단 중 [1] 발명도둑잡기(118.216) 14:46 33 0
2905270 열심히 살아라 RyuDOG갤로그로 이동합니다. 14:46 32 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2