#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;
}
여기까지 코드를 짯는데요.
자꾸 오류가 나요 ㅜㅜ
뭐가 잘못된 걸까요????????????????
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.