Skip to content

Instantly share code, notes, and snippets.

@tabvn
Created September 6, 2019 10:02
Show Gist options
  • Select an option

  • Save tabvn/75129e3d746ef20e336e88ded36539a0 to your computer and use it in GitHub Desktop.

Select an option

Save tabvn/75129e3d746ef20e336e88ded36539a0 to your computer and use it in GitHub Desktop.
#include "Windows.h"
#include <iostream>
#include <conio.h>
#include <cmath>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
/**
* System Menu Do not Edit
*/
const int ENTER = 13;
const int UP = 72;
const int DOWN = 80;
#define MENU_DEFAULT_COLOR 10
#define MENU_ACTIVE_COLOR 14
struct MenuItem{
string title;
void (*func)();
MenuItem(const string &title, void (*func)()) : title(title), func(func) {}
};
class Menu{
private:
vector<MenuItem> items;
int x0,y0;
char ch;
int selectedIndex, lastSelectedIndex;
void gotoxy(int x, int y){
#ifdef WIN32
COORD cur = {static_cast<SHORT>(x), static_cast<SHORT>(y)};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cur);
#else
printf("\033[%dG\033[%dd", x+1, y+1);
#endif
}
void rect(int x1,int y1,int x2,int y2){
int x,y;
Menu::gotoxy(x1,y1); cout<<"É";
Menu::gotoxy(x2,y1); cout<<"»";
Menu::gotoxy(x1,y2); cout<<"È";
Menu::gotoxy(x2,y2); cout<<"¼";
for(x=x1+1;x<x2;x++)
{
Menu::gotoxy(x,y1); cout<<"Í";
Menu::gotoxy(x,y2); cout<<"Í";
}
for(y=y1+1;y<y2;y++)
{
Menu::gotoxy(x1,y); cout<<"º";
Menu::gotoxy(x2,y); cout<<"º";
}
}
public:
Menu() {
lastSelectedIndex = 0;
selectedIndex = 0;
x0 = 5;
y0 = 5;
}
const vector<MenuItem> &getItems() const {
return items;
}
void add(const string& title, void (*func)()){
MenuItem item(title, func);
Menu::items.push_back(item);
}
void next(){
lastSelectedIndex = selectedIndex;
selectedIndex ++;
if(selectedIndex > (int)items.size() -1){
selectedIndex = 0;
}
Menu::writeItem(lastSelectedIndex, MENU_DEFAULT_COLOR);
Menu::writeItem(selectedIndex, MENU_ACTIVE_COLOR);
}
void prev(){
lastSelectedIndex = selectedIndex;
selectedIndex --;
if(selectedIndex < 0){
selectedIndex = (int) items.size() -1;
}
Menu::writeItem(lastSelectedIndex, MENU_DEFAULT_COLOR);
Menu::writeItem(selectedIndex, MENU_ACTIVE_COLOR);
}
int textColor(int Color)
{
HANDLE h;
h = GetStdHandle(STD_OUTPUT_HANDLE);
return SetConsoleTextAttribute(h, Color);
}
void write(const string& s, int x, int y, int color){
textColor(color);
Menu::gotoxy(x,y); cout<<s;
textColor(15);
}
void writeItem(int index, int color){
string prefix = Menu::to_string(index+1) + ". ";
Menu::write(prefix + items[index].title, x0, y0 + index, color);
}
string to_string(int i){
std::stringstream ss;
ss << i;
return ss.str();
}
void select(int index){
system("cls");
Menu::rect(x0-2,y0-1,x0+35,y0+(int)Menu::items.size());
for(int i=0;i<items.size();i++){
if(i==index) {
Menu::write(Menu::to_string(i) + ". " + items[i].title,x0,y0+i,MENU_ACTIVE_COLOR);
Menu::writeItem(i, MENU_ACTIVE_COLOR);
}
else {
Menu::writeItem(i, MENU_DEFAULT_COLOR);
}
}
Menu::copyright();
}
void copyright(){
Menu::write("Copyright (c) 2019 by Pham Anh Phuong.",x0-4,y0+(int)items.size()+5,10);
Menu::write("Danh sach nhom:",x0+40,y0,14);
Menu::write("1. Nguyen Dinh Toan",x0+40,y0+1,14);
}
void render(){
system("cls");
selectedIndex = 0; // default menu selection.
lastSelectedIndex = 0;
Menu::select(0);
bool ok = false;
do{
ch = getch();
switch ((int)ch){
case UP:
Menu::prev();
break;
case DOWN:
Menu::next();
break;
case ENTER:
// execute select menu item
system("cls");
Menu::items[selectedIndex].func();
cout << endl << "Press Enter to exit.";
getch();
Menu::select(selectedIndex);
break;
default:
break;
}
}while (ch != 27);
}
};
/**
* End System Menu do not edit above;
*/
struct Multiply {
struct Row {
vector<int> v;
};
vector<Row> rows;
vector<int> v1, v2;
Multiply(string s1, string s2) {
for (int i = 0; i < s1.size(); ++i) {
Multiply::v1.push_back(s1[i] - '0');
}
for (int i = 0; i < s2.size(); ++i) {
Multiply::v2.push_back(s2[i] - '0');
}
}
Multiply *calculate() {
if (v1.empty() || v2.empty()) {
return this;
}
int level = 0;
for (int i = v2.size() - 1; i >= 0; --i) {
int carry = 0;
Row row;
for (int k = 0; k < level; ++k) {
row.v.push_back(0);
}
for (int j = v1.size() - 1; j >= 0; --j) {
int value = v1[j] * v2[i] + carry;
row.v.push_back(value % 10);
carry = value / 10;
}
if (carry > 0) {
row.v.push_back(carry);
}
rows.push_back(row);
level++;
}
// cong cac hang lai voi nhau
int maxSize = 1;
for (int i = 0; i < rows.size(); ++i) {
if (rows[i].v.size() > maxSize) {
maxSize = rows[i].v.size();
}
}
// tao matran dong cot maxSize
//int arr[maxSize][maxSize];
vector<vector<int> > arr(maxSize, vector<int> (maxSize, 0));
for (int i = 0; i < rows.size(); ++i) {
Row row = rows[i];
for (int j = 0; j < row.v.size(); ++j) {
arr[i][j] = row.v[j];
}
}
vector<int> result;
int carry = 0;
for (int column = 0; column < maxSize; ++column) {
int value = 0;
for (int row = 0; row < maxSize; ++row) {
value += arr[row][column];
}
value += carry;
result.push_back(value %10);
carry = value /10;
}
if(carry > 0){
result.push_back(carry);
}
// output
for (int i = 0; i < result.size() - v1.size(); ++i) {
cout << " ";
}
for (int i = 0; i < v1.size(); ++i) {
cout << v1[i] << " ";
}
cout << endl;
for (int i = 0; i < result.size(); ++i) {
if(i == 0){
cout << "* ";
}else{
cout << " ";
}
}
cout << endl;
for (int i = 0; i < result.size() - v2.size(); ++i) {
cout << " ";
}
for (int i = 0; i < v2.size(); ++i) {
cout << v2[i] << " ";
}
cout << endl;
for (int i = 0; i < maxSize; ++i) {
cout << "--" ;
}
cout << endl;
for (int i = result.size() -1; i >= 0; --i) {
cout << result[i] <<" ";
}
return this;
}
};
void phep_nhan_cach_1(){
Multiply *multiply = new Multiply("567", "1234");
multiply->calculate();
}
int main()
{
Menu menu;
menu.add("Phep Nhan Cach 1", phep_nhan_cach_1);
menu.render();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment