插入排序

#include <stdio.h>

int main(){
        int a[6]={21,45,58,64,71};
        int i,j,k,x;
        printf("插入前的数组:");
        for (i=0;i<6;i++){
                printf(" %d",a[i]);
        }
        printf("\n请输入要插入的数:");
        scanf("%d",&x);
        for (i=0;i<5;i++){
                if(a[i] > x)
                        break;
        }
        for (j=5;j>i;j--){
                a[j]=a[j-1];
        }
        a[i]=x;
        printf("\n插入后的数组:");
        for(i=0;i<6;i++){
                printf(" %d",a[i]);
        }
        printf("\n");
        return 0;
}
发表在 C | 留下评论

选择排序

#include <stdio.h>

int main(){
        int a[5]={21,45,58,34,11};
        int i,j,k;
        printf("排序前的数组:");
        for(i=0;i<5;i++){
                printf(" %d",a[i]);
        }
        for (i=0;i<4;i++){
                for(j=i+1;j<5;j++){
                        if(a[i]>a[j]){
                                k=a[i];
                                a[i]=a[j];
                                a[j]=k;
                        }
                }
        }
        printf("\n排序后的数组:");
        for (i=0;i<5;i++){
                printf(" %d",a[i]);
        }
        printf("\n");
        return 0;
}
发表在 C | 留下评论

冒泡排序

#include <stdio.h>

int main(){
        int a[5]={21,45,58,34,11};
        int i,j,k;
        printf("排序前的数组:");
        for(i=0;i<5;i++){
                printf(" %d",a[i]);
        }
        for (i=0;i<4;i++){
                for(j=0;j<4-i;j++){
                        if(a[j]>a[j+1]){
                                a[j] ^= a[j+1];
                                a[j+1] ^= a[j];
                                a[j] ^= a[j+1];
                        }
                }
        }
        printf("\n排序后的数组:");
        for (i=0;i<5;i++){
                printf(" %d",a[i]);
        }
        printf("\n");
        return 0;
}
发表在 C | 留下评论

makefile练习

#Makefile example
OBJ=main.o test1.o test2.o
CC=gcc
CFLAG=-Wall -g -O

all:hello test

hello:hello.c
    gcc -c $< -o $@
test:$(OBJ)
    $(CC) $(OBJ) $(CFLAG) -o $@
main.o:main.c
    $(CC) -c $< -o main.o
test1.o:test1.c
    $(CC) -c $< -o test1.o
test2.o:test2.c
    $(CC) -c $< -o test2.o
install:
    cp test /home/train
clean:
    rm -f *.o
    rm -f test
发表在 C | 留下评论

理解gcc编译

#正常使用的一步编译
gcc hellogcc.c -o hgcc

gcc -I/home/train hellogcc.c -o h2 

#a.分步进行
gcc -c star.c -o star.o
gcc -c hello.c -o hello.o
gcc hello.o star.o -o mypro
#b 一步编译
gcc star.c hello.c -o mypro1

#不显示警告
gcc -w star.c hello.c -o mypro1
#显示所有警告
gcc -Wall star.c hello.c -o mypro1

#a.生成动态链接库
gcc -fpic -shared -s hello.c -o libhello.so
file libhello.so
sudo cp libhello.so /usr/lib #讲库放到系统路径
gcc -lhello star.c -o myprolib
ldd myprolib

#b使用静态库
gcc -c hello.c -o hello.o
ar -rc libhello.a hello.o 
#ar t libhello.a 
gcc star.c -L /home/train/linux_c/day_2/doit -lhello -o mystar
./mystar


#gdb调试
gcc -g new.c -o new
gdb new
    list
    run
    break 8
    break 14
    break 18 
    info break
    run
    next
    continue
    info local
    print s #根据变量名称打印其信息
    continue
    quit
发表在 C | 留下评论

爬虫练习

from bs4 import BeautifulSoup
import requests
import os
import shutil

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8",
    "Connection": "close",
    "Cookie": "_gauges_unique_hour=1; _gauges_unique_day=1; _gauges_unique_month=1; _gauges_unique_year=1; _gauges_unique=1",
    "Referer": "http://www.infoq.com",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER"
}

# url = 'https://www.infoq.com/presentations/'

def download_jpg(imageurl,image_location_path):
    resopnse = requests.get(imageurl,stream=True)
    if resopnse.status_code == 200:
        with open(image_location_path,'wb') as f:
            resopnse.raw.deconde_content = True
            shutil.copyfileobj(resopnse.raw,f)


def craw3(url):
    response = requests.get(url,headers=headers)
    soup = BeautifulSoup(response.text,'lxml')
    isExists = os.path.exists('./download_pic')
    if not isExists:
        os.mkdir('./download_pic')
    for pic_href in soup.find_all('div',class_ = 'card__content'):
        print(pic_href.find_all('img'))
        for pic in pic_href.find_all('img'):
            imgurl = pic.get('src')
            dir = os.path.abspath('./download_pic')
            filename = os.path.basename(imgurl)
            imgpath = os.path.join(dir,filename)
            print('开始下载 %s' %imgurl)
            download_jpg(imgurl,imgpath)

for i in range(12, 37, 12):
    url = 'http://www.infoq.com/cn/presentations' + str(i)
    craw3(url)
发表在 Python | 留下评论