Search

Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Jan 18, 2024

Bài tập sinh viên - Code Bezout, Nghịch đảo, Số dư Trung Hoa

 Bezout

#include<stdio.h>
#include<conio.h>

int Bezout(int a,int b)
{
    int d,x,y;
    int x1,x2,y1,y2,q,r;
    if(b<0)
    {
           return 0; /* Khong dung dieu kien */
           }
    else if(a<b || a==b)
    {
           return 0; /* Khong dung dieu kien */
           }     
    else if(b==0)
    {
         d = a; x = 1; y = 0;
         return (d,x,y);      
         }
    else
    {
        x1=0; x2=1; y1=1; y2=0;
        while(b>0)
        {
                  q=a/b; r=a-q*b; x=x2-q*x1; y=y2-q*y1;
                  a=b; b=r; x2=x1; x1=x; y2=y1; y1=y;
                  }
        d = a; x=x2; y=y2;
        return (d,x,y);
        }          
}       

int main()
{
    int d,x,y;
    Bezout(10,3);
    getch();
    }


Nghịch đảo

#include<stdio.h>
#include<conio.h>

int NghichDao(int a,int b)
{
    int d,x,y;
    int x1,x2,y1,y2,q,r,m;
    m=a;      
    if(b==0)
    {
         d = a; x = 1; y = 0;
         if(d>1)
         {
                 return 0; /* Khong ton tai */
                 }
         else if(d==1)
         {
                 return (y%m);
                 }          
         }
    else
    {
        x1=0; x2=1; y1=1; y2=0;
        while(b>0)
        {
                  q=a/b; r=a-q*b; x=x2-q*x1; y=y2-q*y1;
                  a=b; b=r; x2=x1; x1=x; y2=y1; y1=y;
                  }
        d = a; x=x2; y=y2;
        if(d>1)
        {
                 return 0; /* Khong ton tai */
                 }
        else if(d==1)
        {
                 return (y%m);
                 }      
        }             
}       

int main()
{
    int a,b;
    printf("%d", NghichDao(3,35));
    getch();
}


Số dư Trung Hoa

#include<stdio.h>
#include<conio.h>

int NghichDao(int a,int b)
{
    int d,x,y;
    int x1,x2,y1,y2,q,r,m;
    m=a;      
    if(b==0)
    {
         d = a; x = 1; y = 0;
         if(d>1)
         {
                 return 0; /* Khong ton tai */
                 }
         else if(d==1)
         {
                 return (y%m);  
                 }          
         }
    else
    {
        x1=0; x2=1; y1=1; y2=0;
        while(b>0)
        {
                  q=a/b; r=a-q*b; x=x2-q*x1; y=y2-q*y1;
                  a=b; b=r; x2=x1; x1=x; y2=y1; y1=y;
                  }
        d = a; x=x2; y=y2;
        if(d>1)
        {
                 return 0; /* Khong ton tai */
                 }
        else if(d==1)
        {
                 return (y%m);
                 }      
        }             
}

int SDTH(int m1, int m2, int m3, int n1, int n2, int n3)
{
    int tich = n1*n2*n3;
    int tich1 = n2*n3;
    int tich2 = n1*n3;
    int tich3 = n1*n2;
    int c1 = NghichDao(n1,tich1);
    int c2 = NghichDao(n2,tich2);
    int c3 = NghichDao(n3,tich3);
    int x0 = m1*tich1*c1 + m2*tich2*c2 + m3*tich3*c3;
    return x0;
}     

int main()
{
    printf("KQ = %d", SDTH(2, 3, 2, 3, 5, 7)); 
    getch();
}

Sep 25, 2023

Ubuntu: DNS server not responding, unable to resolve host - temporary failure in name resolution

1. Check 2 files /etc/hostname and  /etc/hosts 

sudo nano /etc/hostname

samuel

sudo nano /etc/hosts 

127.0.0.1       localhost localhost.localdomain samuel
127.0.1.1       samuel-ubuntu
127.0.1.1       samuel


2. Make sure the name is the same in both files


3. Check content in /etc/resolv.conf file
nano /etc/resolv.conf



4. Add nameserver to the resolv.conf
nameserver 8.8.8.8


5. Restart service
sudo systemctl restart systemd-resolved.service 

Sep 22, 2023

installed tab not showing in software center in ubuntu

Remove gnome-software

sudo apt-get remove --purge gnome-software


Re-install gnome-software

sudo apt-get install gnome-software

May 4, 2016

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Solution:
sudo service mysql stop
sudo /etc/init.d/apparmor reload
sudo service mysql start 

Apr 25, 2016

PHP Get Id youtube from URL link

<?php
if (strpos($video, 'youtube') > 0) {
    preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $video, $matches);
    $_video = 'https://www.youtube.com/embed/' . $matches[0];
}

Dec 18, 2015

Back link URL by Javascript


/**
 * Created by kongltn on 12/25/2015.
 */

$(document).ready(function () {
    $("#backButtonInheaderBar").click(function () {
        if (sessionStorage.backURL !== undefined) {
            sessionStorage.backIsClick = 1;
            var curArray = JSON.parse(sessionStorage.backURL);
            if (curArray.length > 1) curArray.pop();
            sessionStorage.backURL = JSON.stringify(curArray);
            window.location.replace(curArray[curArray.length - 1]);
        }
    });

    if (!sessionStorage.backURL || sessionStorage.backURL === undefined) {
        sessionStorage.backURL = JSON.stringify([window.location.origin]);
    }

    var curUrlHref = window.location.href;
    if (sessionStorage.backIsClick && sessionStorage.backIsClick === "1") {
        sessionStorage.backIsClick = 0;
    } else {
        var urlArray = JSON.parse(sessionStorage.backURL);
        if (curUrlHref != urlArray[urlArray.length - 1]) {
            urlArray.push(curUrlHref);
        }
        sessionStorage.backURL = JSON.stringify(urlArray);
    }

    //console.log(sessionStorage.backURL);
    //console.log(sessionStorage.backIsClick);
});

Dec 16, 2015

Action download file in PHP


/**
 * Action Download file
 * @return undefined
 */
public function actionDownload()
{
    $file = Yii::app()->request->getParam('file');
    $modelArchives = Archives::model()->find('path=:path', array(':path' => $file));
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-type:application/octet-stream");
    header('Content-Disposition: attachment; filename="' . $modelArchives->fileName . '"');
    $contentArchive = Yii::getPathOfAlias('webroot') . Yii::app()->params["archiveFolderPath"] . '/' . $file;
    echo file_get_contents($contentArchive);
    exit;
}

Dec 9, 2015

CSS trick: Sort String and add 3 dot at last point of string


.sortTitle {
    width: calc(100% - 20px);
    display: inline;
    text-overflow: ellipsis;   
    white-space: nowrap; 
    overflow: hidden;
}

Dec 7, 2015

Create thumbnail image in PHP

Image.php

<?php

/**
 * Class Image
 * @author UTC.HuyTD
 */
class Image
{

    private $file;
    private $image;
    private $info;

    /**
     * @param $file
     */
    public function __construct($file)
    {
        if (file_exists($file)) {
            $this->file = $file;

            $info = getimagesize($file);

            $this->info = array(
                'width' => $info[0],
                'height' => $info[1],
                'bits' => $info['bits'],
                'mime' => $info['mime'],
            );

            $this->image = $this->create($file);
        } else {
            exit('Error: Could not load image ' . $file . '!');
        }
    }

    /**
     * @param $image
     * @return bool|resource
     */
    private function create($image)
    {
        $mime = $this->info['mime'];

        if ($mime == 'image/gif') {
            return imagecreatefromgif($image);
        } elseif ($mime == 'image/png') {
            return imagecreatefrompng($image);
        } elseif ($mime == 'image/jpeg') {
            return imagecreatefromjpeg($image);
        }
        return false;
    }

    /**
     * @param $file
     * @param int $quality
     */
    public function save($file, $quality = 90)
    {
        $info = pathinfo($file);

        $extension = strtolower($info['extension']);

        if (is_resource($this->image)) {
            if ($extension == 'jpeg' || $extension == 'jpg') {
                imagejpeg($this->image, $file, $quality);
            } elseif ($extension == 'png') {
                imagepng($this->image, $file);
            } elseif ($extension == 'gif') {
                imagegif($this->image, $file);
            }

            imagedestroy($this->image);
        }
    }

    /**
     * Resize function
     * @param int $width
     * @param int $height
     * @param string $default char [default, w, h]
     *    default = scale with white space,
     *    w = fill according to width,
     *    h = fill according to height
     */
    public function resize($width = 0, $height = 0, $default = '')
    {
        if (!$this->info['width'] || !$this->info['height']) {
            return;
        }

        $positionX = 0;
        $positionY = 0;
        $scale = 1;

        $scale_w = $width / $this->info['width'];
        $scale_h = $height / $this->info['height'];

        if ($default == 'w') {
            $scale = $scale_w;
        } elseif ($default == 'h') {
            $scale = $scale_h;
        } else {
            $scale = min($scale_w, $scale_h);
        }

        if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') {
            return;
        }

        $new_width = (int)($this->info['width'] * $scale);
        $new_height = (int)($this->info['height'] * $scale);
        $positionX = (int)(($width - $new_width) / 2);
        $positionY = (int)(($height - $new_height) / 2);

        $image_old = $this->image;
        $width = $width === 0 ? $this->info['width'] : $width;
        $height = $height === 0 ? $this->info['height'] : $height;
        $this->image = imagecreatetruecolor($width, $height);

        if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
            imagealphablending($this->image, false);
            imagesavealpha($this->image, true);
            $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
            imagecolortransparent($this->image, $background);
        } else {
            $background = imagecolorallocate($this->image, 255, 255, 255);
        }

        imagefilledrectangle($this->image, 0, 0, $width, $height, $background);

        imagecopyresampled($this->image, $image_old, $positionX, $positionY, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
        imagedestroy($image_old);

        $this->info['width'] = $width;
        $this->info['height'] = $height;
    }

    /**
     * @param $file
     * @param string $position
     */
    public function watermark($file, $position = 'bottomright')
    {
        $watermark = $this->create($file);

        $watermark_width = imagesx($watermark);
        $watermark_height = imagesy($watermark);
        $watermark_pos_x = null;
        $watermark_pos_y = null;
        switch ($position) {
            case 'topleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = 0;
                break;
            case 'topright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = 0;
                break;
            case 'bottomleft':
                $watermark_pos_x = 0;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
            case 'bottomright':
                $watermark_pos_x = $this->info['width'] - $watermark_width;
                $watermark_pos_y = $this->info['height'] - $watermark_height;
                break;
        }

        imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40);

        imagedestroy($watermark);
    }

    /**
     * Crop function
     * @param $top_x
     * @param $top_y
     * @param $bottom_x
     * @param $bottom_y
     */
    public function crop($top_x, $top_y, $bottom_x, $bottom_y)
    {
        $image_old = $this->image;
        $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);

        imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']);
        imagedestroy($image_old);

        $this->info['width'] = $bottom_x - $top_x;
        $this->info['height'] = $bottom_y - $top_y;
    }

    /**
     * Rotate function
     * @param $degree
     * @param string $color
     */
    public function rotate($degree, $color = 'FFFFFF')
    {
        $rgb = $this->html2rgb($color);

        $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));

        $this->info['width'] = imagesx($this->image);
        $this->info['height'] = imagesy($this->image);
    }

    /**
     * @param $filter
     */
    private function filter($filter)
    {
        imagefilter($this->image, $filter);
    }

    /**
     * Text function
     * @param $text
     * @param int $x
     * @param int $y
     * @param int $size
     * @param string $color
     */
    private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000')
    {
        $rgb = $this->html2rgb($color);

        imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
    }

    /**
     * Merge function
     * @param $file
     * @param int $x
     * @param int $y
     * @param int $opacity
     */
    private function merge($file, $x = 0, $y = 0, $opacity = 100)
    {
        $merge = $this->create($file);

        $merge_width = imagesx($this->$image);
        $merge_height = imagesy($this->$image);

        imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity);
    }

    /**
     * @param $color
     * @return array|bool
     */
    private function html2rgb($color)
    {
        if ($color[0] == '#') {
            $color = substr($color, 1);
        }

        if (strlen($color) == 6) {
            list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
        } elseif (strlen($color) == 3) {
            list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
        } else {
            return false;
        }

        $r = hexdec($r);
        $g = hexdec($g);
        $b = hexdec($b);

        return array($r, $g, $b);
    }

}

ImageTool.php


<?php

/**
 * Class ImageTool
 * @author UTC.HuyTD
 */
class ImageTool
{
    /**
     * Resize thumbnail Image
     * @param $folderPathOfThumbnail
     * @param $filename
     * @param int $width
     * @param int $height
     * @return string
     */
    public static function resizeThumbnailImage($folderPathOfThumbnail, $filename, $width = 0, $height = 0)
    {
        try {
            if (file_exists($filename)) {
                $fileInfo = pathinfo($filename);
                $fileExtension = $fileInfo['extension'];

                $thumbnailFileName = $fileInfo['filename'] . "_thumbnail." . $fileExtension;

                // Create image with width & height
                $image = new Image($filename);
                $image->resize($width, $height);
                $image->save($folderPathOfThumbnail . "/" . $thumbnailFileName);

                return $folderPathOfThumbnail . "/" . $thumbnailFileName;
            }
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return "";
    }
}


Using function createThumbnail


/**
 * Create thumbnail image
 * @param $folderPathOfThumbnail
 * @param string $directImage
 * @param int $defaultWidth
 * @return string
 */
public static function createThumbnailImage($folderPathOfThumbnail, $directImage = 'images/no_image.jpg', $defaultWidth = 150)
{
    $thumbnailPathCreated = "";

    if ($defaultWidth == "") {
        return $directImage;
    }

    if (file_exists($directImage)) {
        $size = getimagesize($directImage);

        // Get image width & height
        $imgWidth = $size[0];
        $imgHeight = $size[1];

        if ($imgHeight > $imgWidth) {
            if ($imgHeight != $defaultWidth) {
                $imgHeightNew = $defaultWidth;
                $scaleHeight = round($imgHeightNew / $imgHeight, 2);
                $imgWidthNew = $imgWidth * $scaleHeight;
                // Create thumbnail image
                $thumbnailPathCreated = ImageTool::resizeThumbnailImage($folderPathOfThumbnail, $directImage, $imgWidthNew, $imgHeightNew);
            }
        } else {
            if ($imgWidth != $defaultWidth) {
                $scale = round($defaultWidth / $imgWidth, 2);
                // Create thumbnail image
                $thumbnailPathCreated = ImageTool::resizeThumbnailImage($folderPathOfThumbnail, $directImage, $imgWidth*$scale, $imgHeight*$scale);
            }
        }
    }

    if ($thumbnailPathCreated != "") {
        if (substr($thumbnailPathCreated, 0, 1) == "/")
            $thumbnailPathCreated = substr($thumbnailPathCreated, 1);
    }
    return $thumbnailPathCreated;
}


$thumbFolder = Yii::getPathOfAlias('webroot').'/upload/thumbnails';
$img = Yii::getPathOfAlias('webroot') . '/upload/avatars/Tulips.jpg' ;
$this->createThumbnailImage($thumbFolder, $img  , 50);