PAT B1036 跟奥巴马一起编程

#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
    int col, row;
    char c;
    scanf("%d %c", &col, &c);
    if (col % 2 == 1){
        row = col / 2 + 1;
    } else {
        row = col / 2;
    }

    for (int i = 0; i < col; i++){
        putchar(c);
    }
    putchar('\n');

    for (int i = 0; i < row - 2; i++){
        putchar(c);
        for (int k = 0; k < col - 2; k++){
            putchar(' ');
        }
        putchar(c);
        putchar('\n');
    }
    for (int i =0; i < col;i++){
        putchar(c);
    }
    return 0;
}

 

codeup 1934 找x

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    int maxn = 210;
    int n, a[maxn];
    while (cin >> n){
        for (int i=0; i<n; i++){
            scanf("%d", &a[i]);
        } 
        int x,k;
        cin >> x;
        for ( k = 0; k < n; k++){
            if (a[k] == x)
                cout << k;
        }

        if ( k == n ){
            cout << -1;
        }
    }
}

 

PAT B1032 挖掘机技术哪家强

include <iostream>
using namespace std;

const int maxn = 100010;
int school[maxn] = {0};

int main()
{
    int n, schID, score;
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> schID >> score;
        school[schID] += score;
    }

    int k, MAX = -1;
    for (int i = 1; i<= n; i++){
        if(school[i] > MAX){
            max = school[i];
            k = i;
        }
    }

    cout << k << ' ' << MAX;
    return 0;
}

 

PAT B1001害死人不偿命的(3n+1)猜想

#include <iostream>
using namespace std;

int main(void)
{
    int n, step = 0;
    cin >> n;
    while(n != 1){
        if ( n % 2 == 0 ){
            n /= 2;
        } else {
            n = ( 3 * n + 1 ) / 2)
        }
        step++;
    }
    cout << step;
}

 

浮点数的比较

class FloatCmp 
{
    private:
        const double eps = 1e—8;
    public:
        static bool equ(const double a, const double b){
            return fabs(a - b) < eps;
        }
        static bool more(const double a, const double b){
            return a - b > eps;
        }
        static bool less(const double a, const double b){
            return a - b < -eps;
        }
        static bool more_equ(const double a, const double b){
            return a - b > -eps;
        }
        static bool less_equ(const double a, const double b){
            return a - b < eps;
        }
};

 

[GNU/Linux]标准Makefile 1.2 使用动态库

######### 标准Makefile Lv1.2 / 使用动态库 ########
EXE=helloworld
SUBDIR=src object

#CXXFLAGS:编译选项, LDFLAGS:链接选项
CXXFLAGS += -I/home/mytest/example/include/
LDFLAGS += -L/home/mytest/example/lib -lexample

CXX_SOURCES =$(foreach dir,$(SUBDIR), $(wildcard $(dir)/*.cpp))
CXX_OBJECTS=$(patsubst  %.cpp, %.o, $(CXX_SOURCES))
DEP_FILES  =$(patsubst  %.o,  %.d, $(CXX_OBJECTS))

$(EXE): $(CXX_OBJECTS)
    g++  $(CXX_OBJECTS) -o $(EXE) $(LDFLAGS)
    
%.o: %.cpp
    g++  -c  $(CXXFLAGS) -MMD $<  -o  $@

-include $(DEP_FILES)

clean: 
    rm  -rf  $(CXX_OBJECTS)  $(DEP_FILES)  $(EXE)

test:
    echo $(CXX_OBJECTS)

 

[GNU/Linux]代码中调用系统命令

#include <stdio.h>

// 返回: -1, 命令行出错; >=0, 实际输出的字节数
int execute(const char* cmdline, char* output, int maxsize)
{
    // 执行命令, 获取输出流
    FILE* fp = popen(cmdline, "r");
    if(!fp) return -1;
    
    // 读取maxsize-1个字节
    int size = 0; // 已读取的字节数
    maxsize -= 1;
    while(!feof(fp))
    {
        int n = fread(output+size, 1, maxsize - size, fp);
        if(n<=0) break;
        size += n;
    }
    
    // 关闭输出
    pclose(fp);
    
    output[size] = 0;
    return size; // 实际读取的字节数	
}

int main()
{
    char buf[1024];
    int n = execute("ifconfig", buf, 1024);
    if(n> 0)
    {
        buf[n] = 0;
        printf("------ output -------\n");
        printf("%s\n", buf);
    }
    return 0;
}

 

[GNU/Linux]系统调用读取目录

dir.h

#ifndef _DIR_INFO_H
#define _DIR_INFO_H

// 《语法篇》的第30章,STL里的一个类模板
#include <vector>
using namespace std;

struct DirInfo
{
    char name[256];
};

int listdir(const char*  dir, vector<DirInfo>& results);


#endif

dir.cpp

#include <stdio.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>

#include "dir.h"

// 失败:返回-1;成功,返回0
int listdir(const char* dir, vector<DirInfo>& results)
{
    // 打开目录
    DIR *  _dir = opendir(dir);
    if(!_dir)
    {	
        return -1;
    }
    
    // 读取目录
    dirent* entry = readdir(_dir);
    while(entry)
    {
        const char* name = entry->d_name;
        if(strcmp(name, ".") != 0
            && strcmp(name, "..") != 0)
        {
            //printf("found entry: %s \n", name);
            DirInfo info;
            strcpy(info.name, name);
            results.push_back(info);
        }
        
        entry = readdir(_dir);
    }
    
    // 关闭目录
    closedir(_dir);
    return 0;

}

main.cpp

#include <stdio.h>

#include "dir.h"

int main()
{
    vector<DirInfo> results;
    int ret = listdir("/home/mytest/project/K12_02/aaa", results);
    if(ret == 0)
    {
        for(int i=0; i<results.size(); i++)
        {
            printf("got: %s \n", results[i].name);
        }	
    }	
    
    return 0;
}

 

[GNU/Linux]互斥体和信号量

互斥体:概念请参考《应用篇》里的讲解。
在pthread库中提供互斥体的实现。
创建Mutex对象
pthread_mutex_t hMutex;
pthread_mutex_init(&hMutex, NULL);  // 创建
pthread_mutex_destroy(&hMutex);  // 销毁

 

使用Mutex
// 获取锁,阻塞等待。。。
if(pthread_mutex_lock(&hMutex) ==0)
{
//… 访问共享数据 …
// 释放锁
pthread_mutex_unlock(&hMutex);
}

 

另一种方式:trylock, 如果不能获锁,则立即返
回。
// 获取锁,阻塞等待。。。
if(pthread_mutex_trylock(&hMutex) == 0)
{
//… 访问共享数据 …
// 释放锁
pthread_mutex_unlock(&hMutex);
}

 

信号量:概念请参考《应用篇》里的讲解。
#include <unistd.h>
#include <semaphore.h>
创建Semaphore对象
sem_t hSem;
sem_init(&hSem, 1, initial_value);
sem_destroy(&hSem);

wait/post操作
sem_wait(&hSem);
sem_post(&hSem);
超时等待 sem_timedwait

 

[GNU/Linux]线程的面向对象封装

Thread.h

#ifndef _THREAD_H
#define _THREAD_H

#include <pthread.h>
#include <unistd.h>
#include <time.h>

class Thread
{
public:
    Thread();
    virtual ~Thread();

    // 创建并启动
    virtual int Run();

    // 等待和收回资源
    static void Join(Thread* thrd);

    // Sleep函数
    static void Msleep(int ms);
    static void Sleep(int s);

public:
    virtual int Routine() = 0;

private:
    pthread_t hThread;
};






#endif

Thread.cpp

#include <stdio.h>
#include "Thread.h"

Thread::Thread() 
{
}

Thread::~Thread()
{
}

static void* Thread_Proc_Linux(void* param)
{
    Thread* thrd = (Thread*) param;
    thrd->Routine();
    return NULL;
}

int Thread::Run()
{
    // 创建线程
    if(pthread_create(&hThread, NULL, 
        Thread_Proc_Linux, this) < 0)
    {
        return -1;
    }

    return 0;
}

void Thread::Join(Thread* th)
{
    pthread_join(th->hThread, NULL);
}

void Thread::Msleep(int ms)
{
    timespec ts;
    ts.tv_sec = ms / 1000;
    ts.tv_nsec = (ms % 1000) * 1000000;
    nanosleep(&ts, NULL);
}

void Thread::Sleep(int s)
{
    sleep(s);
}

 

main.cpp

#include <stdio.h>

#include "Thread.h"

class MyTask : public Thread
{
public:
    virtual int Routine()
    {
        for(int i=0; i<10; i++)
        {
            printf("MyTask: %d \n", i);
            Thread::Sleep(1);
        }
        return 0;
    }
};
// 
int main()
{
    MyTask t;
    t.Run();
    
    Thread::Join(&t);	
    printf("main exit.\n");
    return 0;
}