1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | #include <iostream> #include <vector> using namespace std; class Bigint { public : typedef unsigned int ui; typedef unsigned long long ull; private : vector<ui> content; int degree; public : Bigint() { } Bigint(ui _content) : degree(0) { content.push_back(_content); } void expand(ui target) { degree++; content.push_back(target); } Bigint& operator= (const Bigint& target) { content.clear(); int degree = target.degree; for (int i = 0; i <= degree; i++) { content.push_back(target.content[i]); } return *this; } Bigint operator* (const int target) { Bigint save(*this); ull s =0; ull trans; for (int i = 0; i <= degree; i++) { trans = (ull)content[i]; trans = trans * target; trans = trans + s; s = trans >> 16; trans = trans & 0x0000FFFF; content[i] = (int)trans; } if (s) { save.expand(s); } return save; } Bigint operator+ (const Bigint& target) { Bigint save(*this); ull s = 0; ull trans; for (int i = 0; i <= degree; i++) { trans = (ull)content[i]; trans = trans + s; if (i <= target.degree) { trans = trans + (ull)target.content[i]; } s = trans >> 16; trans = trans & 0x0000FFFF; content[i] = (int)trans; } if (degree < target.degree) { for (int i = degree + 1; i <= target.degree; i++) { save.expand(target.content[i]); trans = (ull)content[i]; trans = trans + s; s = trans >> 16; trans = trans & 0x0000FFFF; content[i] = (int)trans; } } if (s) { save.expand(s); } return save; } bool operator< (const Bigint& target) { if (degree < target.degree) { return true; } else if (degree == target.degree) { if (content[degree] < target.content[target.degree]) { return true; } else { return false; } } else { return false; } } void show() { for (int i = degree; i >= 0; i--) { cout << content[i]; } cout << endl; } ~Bigint() { content.clear(); cout << "disable" << endl; } }; int main() { Bigint *solid[4];//0:a, 1:b, 2:전에먹은량, 3:지금 먹은량 int a, b, c; cin >> a >>b; solid[0] = new Bigint(a); solid[1] = new Bigint(b); solid[2] = new Bigint(0); cin >> a >> b >> c; solid[0]->show(); *solid[0] = *solid[0] * a; *solid[1] = *solid[1] * b; solid[0]->show(); delete solid[0]; delete solid[1]; delete solid[2]; return 0; } | cs |
뻑은 안나는데 원하는 계산은 되지 않네요 5 500 이 나와야되는데 5 5 가 나오네요
오~ 빠른것.
제가 클래스 구현해보는게 사실 처음이라서 많이 모자릅니다.
너무 기초적인걸 몰라도 너른 이해로 봐주시길 ㅜ
save에 정상적인 값이 들어오는데 바로 이게 소멸되는거 같아요 제 생각에는
*solid[0] = *solid[0] * a; 이게 되야 되는데
*solid[0] * a
*solid[0] = *solid[0]
디버깅해보면서 이렇게 따로 연산된다는 느낌이네요
근데 왜 main 에서 동적 생성해요?
음 객체 포인터를 이용해서 바로 생성자를 이용해서 초기화시켜보고 싶었습니다.
배열을 만들고 초기화함수 따로 해도 되지만요. 배운걸 써먹어보고 싶었기 때문에 ...
BigInt solid[ 3 ] = { a, b, 0 }; 해도 될걸유?
굳이 생성자를 부르고 싶으시다면~
글구 push_back 보단 emplace_back~
잘되네용
emplace_back? 음?
글구 degree 의 역할이 vector의 size() 와 겹치겠네유~
c++11 이네용 스택오버플로우 방지가 추가된 코드같아요
넴 맞아요. 같아요. vector 함수를 또 불러오는거도 나쁘진 않지만 비교연산도 그렇고 쓸대가 많아서 그냥 유지했죠
그냥 size() 쓰셔도 최적화 될듯.
매번 함수에서 size()을 실행하는 시간과 expand 함수에서 ++을 해주는 시간 하면 비슷하지 않을까요.
size()쓰면 코드가 더 길어질거 같은 느낌도 듭니다.
컴파일러가 최적해 내서 그냥 변수를 끄집어 쓸거예용. 간단한 멤버 return 은 인라인이쥬.
즉, 오히려 더 빨라질터인데?
해당 return 코드가 cpp 안에 선언되어 독립함수화 되면 모를까, class 헤더 선언에 들어있다면 인라인임다~
아긔발//되야->돼야 (되어 = 돼임) [리듬 맞춤법 봇♬]