Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. #include "Windows.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <cmath>
  5. #include <vector>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11.  
  12. /**
  13. * System Menu Do not Edit
  14. */
  15.  
  16. const int ENTER = 13;
  17. const int UP = 72;
  18. const int DOWN = 80;
  19. #define MENU_DEFAULT_COLOR 10
  20. #define MENU_ACTIVE_COLOR 14
  21.  
  22. struct MenuItem{
  23. string title;
  24. void (*func)();
  25.  
  26. MenuItem(const string &title, void (*func)()) : title(title), func(func) {}
  27. };
  28.  
  29.  
  30. class Menu{
  31.  
  32. private:
  33. vector<MenuItem> items;
  34. int x0,y0;
  35. char ch;
  36. int selectedIndex, lastSelectedIndex;
  37.  
  38. void gotoxy(int x, int y){
  39. #ifdef WIN32
  40. COORD cur = {static_cast<SHORT>(x), static_cast<SHORT>(y)};
  41. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
  42. #else
  43. printf("\033[%dG\033[%dd", x+1, y+1);
  44.  
  45. #endif
  46.  
  47.  
  48. }
  49. void rect(int x1,int y1,int x2,int y2){
  50. int x,y;
  51. Menu::gotoxy(x1,y1); cout<<"É";
  52. Menu::gotoxy(x2,y1); cout<<"»";
  53. Menu::gotoxy(x1,y2); cout<<"È";
  54. Menu::gotoxy(x2,y2); cout<<"¼";
  55. for(x=x1+1;x<x2;x++)
  56. {
  57. Menu::gotoxy(x,y1); cout<<"Í";
  58. Menu::gotoxy(x,y2); cout<<"Í";
  59. }
  60. for(y=y1+1;y<y2;y++)
  61. {
  62. Menu::gotoxy(x1,y); cout<<"º";
  63. Menu::gotoxy(x2,y); cout<<"º";
  64. }
  65. }
  66.  
  67.  
  68. public:
  69.  
  70. Menu() {
  71. lastSelectedIndex = 0;
  72. selectedIndex = 0;
  73. x0 = 5;
  74. y0 = 5;
  75. }
  76.  
  77. const vector<MenuItem> &getItems() const {
  78. return items;
  79. }
  80.  
  81. void add(const string& title, void (*func)()){
  82. MenuItem item(title, func);
  83. Menu::items.push_back(item);
  84. }
  85.  
  86. void next(){
  87. lastSelectedIndex = selectedIndex;
  88. selectedIndex ++;
  89. if(selectedIndex > (int)items.size() -1){
  90. selectedIndex = 0;
  91. }
  92.  
  93. Menu::writeItem(lastSelectedIndex, MENU_DEFAULT_COLOR);
  94. Menu::writeItem(selectedIndex, MENU_ACTIVE_COLOR);
  95. }
  96. void prev(){
  97. lastSelectedIndex = selectedIndex;
  98. selectedIndex --;
  99. if(selectedIndex < 0){
  100. selectedIndex = (int) items.size() -1;
  101. }
  102.  
  103. Menu::writeItem(lastSelectedIndex, MENU_DEFAULT_COLOR);
  104. Menu::writeItem(selectedIndex, MENU_ACTIVE_COLOR);
  105. }
  106.  
  107. int textColor(int Color)
  108. {
  109. HANDLE h;
  110. h = GetStdHandle(STD_OUTPUT_HANDLE);
  111. return SetConsoleTextAttribute(h, Color);
  112. }
  113.  
  114. void write(const string& s, int x, int y, int color){
  115. textColor(color);
  116. Menu::gotoxy(x,y); cout<<s;
  117. textColor(15);
  118. }
  119.  
  120. void writeItem(int index, int color){
  121. string prefix = Menu::to_string(index+1) + ". ";
  122. Menu::write(prefix + items[index].title, x0, y0 + index, color);
  123. }
  124.  
  125. string to_string(int i){
  126.  
  127. std::stringstream ss;
  128. ss << i;
  129. return ss.str();
  130. }
  131. void select(int index){
  132.  
  133. system("cls");
  134. Menu::rect(x0-2,y0-1,x0+35,y0+(int)Menu::items.size());
  135. for(int i=0;i<items.size();i++){
  136. if(i==index) {
  137. Menu::write(Menu::to_string(i) + ". " + items[i].title,x0,y0+i,MENU_ACTIVE_COLOR);
  138. Menu::writeItem(i, MENU_ACTIVE_COLOR);
  139. }
  140. else {
  141. Menu::writeItem(i, MENU_DEFAULT_COLOR);
  142. }
  143. }
  144.  
  145. Menu::copyright();
  146.  
  147. }
  148.  
  149. void copyright(){
  150. Menu::write("Copyright (c) 2019 by Pham Anh Phuong.",x0-4,y0+(int)items.size()+5,10);
  151. Menu::write("Danh sach nhom:",x0+40,y0,14);
  152. Menu::write("1. Nguyen Dinh Toan",x0+40,y0+1,14);
  153. }
  154.  
  155. void render(){
  156.  
  157. system("cls");
  158.  
  159. selectedIndex = 0; // default menu selection.
  160. lastSelectedIndex = 0;
  161. Menu::select(0);
  162. bool ok = false;
  163. do{
  164. ch = getch();
  165. switch ((int)ch){
  166.  
  167. case UP:
  168. Menu::prev();
  169. break;
  170.  
  171. case DOWN:
  172. Menu::next();
  173. break;
  174.  
  175. case ENTER:
  176. // execute select menu item
  177.  
  178. system("cls");
  179. Menu::items[selectedIndex].func();
  180. cout << endl << "Press Enter to exit.";
  181. getch();
  182. Menu::select(selectedIndex);
  183.  
  184.  
  185. break;
  186.  
  187.  
  188. default:
  189.  
  190. break;
  191. }
  192.  
  193.  
  194.  
  195.  
  196. }while (ch != 27);
  197.  
  198. }
  199. };
  200. /**
  201. * End System Menu do not edit above;
  202. */
  203.  
  204.  
  205.  
  206.  
  207. struct Multiply {
  208.  
  209. struct Row {
  210. vector<int> v;
  211. };
  212.  
  213. vector<Row> rows;
  214. vector<int> v1, v2;
  215.  
  216. Multiply(string s1, string s2) {
  217.  
  218. for (int i = 0; i < s1.size(); ++i) {
  219. Multiply::v1.push_back(s1[i] - '0');
  220. }
  221. for (int i = 0; i < s2.size(); ++i) {
  222. Multiply::v2.push_back(s2[i] - '0');
  223. }
  224.  
  225. }
  226.  
  227. Multiply *calculate() {
  228.  
  229. if (v1.empty() || v2.empty()) {
  230. return this;
  231. }
  232.  
  233.  
  234. int level = 0;
  235.  
  236. for (int i = v2.size() - 1; i >= 0; --i) {
  237. int carry = 0;
  238. Row row;
  239. for (int k = 0; k < level; ++k) {
  240. row.v.push_back(0);
  241. }
  242. for (int j = v1.size() - 1; j >= 0; --j) {
  243. int value = v1[j] * v2[i] + carry;
  244. row.v.push_back(value % 10);
  245. carry = value / 10;
  246. }
  247. if (carry > 0) {
  248. row.v.push_back(carry);
  249. }
  250. rows.push_back(row);
  251. level++;
  252.  
  253.  
  254. }
  255.  
  256. // cong cac hang lai voi nhau
  257. int maxSize = 1;
  258. for (int i = 0; i < rows.size(); ++i) {
  259. if (rows[i].v.size() > maxSize) {
  260. maxSize = rows[i].v.size();
  261. }
  262. }
  263.  
  264. // tao matran dong cot maxSize
  265. //int arr[maxSize][maxSize];
  266. vector<vector<int> > arr(maxSize, vector<int> (maxSize, 0));
  267.  
  268. for (int i = 0; i < rows.size(); ++i) {
  269. Row row = rows[i];
  270. for (int j = 0; j < row.v.size(); ++j) {
  271. arr[i][j] = row.v[j];
  272. }
  273. }
  274.  
  275.  
  276. vector<int> result;
  277. int carry = 0;
  278.  
  279. for (int column = 0; column < maxSize; ++column) {
  280. int value = 0;
  281. for (int row = 0; row < maxSize; ++row) {
  282. value += arr[row][column];
  283. }
  284. value += carry;
  285. result.push_back(value %10);
  286. carry = value /10;
  287.  
  288. }
  289. if(carry > 0){
  290. result.push_back(carry);
  291. }
  292.  
  293.  
  294. // output
  295. for (int i = 0; i < result.size() - v1.size(); ++i) {
  296. cout << " ";
  297. }
  298.  
  299. for (int i = 0; i < v1.size(); ++i) {
  300. cout << v1[i] << " ";
  301. }
  302.  
  303. cout << endl;
  304. for (int i = 0; i < result.size(); ++i) {
  305. if(i == 0){
  306. cout << "* ";
  307. }else{
  308. cout << " ";
  309. }
  310. }
  311.  
  312. cout << endl;
  313.  
  314. for (int i = 0; i < result.size() - v2.size(); ++i) {
  315. cout << " ";
  316. }
  317.  
  318. for (int i = 0; i < v2.size(); ++i) {
  319. cout << v2[i] << " ";
  320. }
  321. cout << endl;
  322. for (int i = 0; i < maxSize; ++i) {
  323. cout << "--" ;
  324. }
  325. cout << endl;
  326. for (int i = result.size() -1; i >= 0; --i) {
  327. cout << result[i] <<" ";
  328. }
  329.  
  330. return this;
  331.  
  332.  
  333. }
  334.  
  335. };
  336.  
  337. void phep_nhan_cach_1(){
  338.  
  339. Multiply *multiply = new Multiply("567", "1234");
  340. multiply->calculate();
  341.  
  342. }
  343.  
  344. int main()
  345. {
  346.  
  347. Menu menu;
  348. menu.add("Phep Nhan Cach 1", phep_nhan_cach_1);
  349. menu.render();
  350.  
  351. return 0;
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement