DraculaW

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  19 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks

2008年11月28日 #

     摘要: 我尝试写的一个智能指针
希望大家能帮我提一些意见 谢谢

上面是测试码
下面是代码  阅读全文
posted @ 2008-11-28 17:39 DraculaW 阅读(372) | 评论 (0)编辑 收藏

2007年11月22日 #

     摘要: 红黑树的插入算法的描述 不知道清楚么 希望大家指正  阅读全文
posted @ 2007-11-22 20:47 DraculaW 阅读(298) | 评论 (0)编辑 收藏

2007年11月21日 #

     摘要: 对内存分配算法的一点想法 希望大家指正  阅读全文
posted @ 2007-11-21 22:38 DraculaW 阅读(1884) | 评论 (1)编辑 收藏

2007年11月20日 #

     摘要: 大概的介绍了下deque的实现
为什么要用这种实现方式 谁能解释一下  阅读全文
posted @ 2007-11-20 22:30 DraculaW 阅读(1115) | 评论 (0)编辑 收藏

2007年11月15日 #

template<typename T, typename U>
class Conversion
{
    typedef char Small;
    class Big{char dummy[2];};
    static Small Test(U)    {   }  
    static Big Test(...) { }  
    static T MakeT() { }
   
public:
    enum { exists = sizeof(Test(MakeT())) == sizeof(Small)};   
};

如果T可以转换为U 于是就调用Test(U)这个函数 返回一个char;
如果不能 就调用使用(...)缺省参数的函数 返回一个数组

然后对返回值进行判断....
posted @ 2007-11-15 20:45 DraculaW 阅读(249) | 评论 (0)编辑 收藏

     摘要: STL与boost的type traits  阅读全文
posted @ 2007-11-15 20:43 DraculaW 阅读(907) | 评论 (0)编辑 收藏

# The readfpl accept a file's path while is fpl(foobar play list),
# and return a list which holds all the file'path
sub readfpl
{
    my @files;
    my @chunks;
    my $index = 0;

    open(INPUT, "< $_[0]")
        or die "can't open";

    @chunks = split(m{file://}, <INPUT>);

    foreach(@chunks)
    {   
        if($_ =~ m/.+\.(mp3|wma|m4a)/)
        {
            $files[$index] = $&;
            $index ++;
        }
    }

    print $files[0];
   
    return @files;
}

my @files = readfpl($ARGV[0]);
my $string;

foreach(@files){
     $string = $_;
        # the next while get name from path
     while( substr($string, 1) =~ m{\\.+\.(mp3|wna|m4a)}) {
         $string = $ARGV[0].$&;
         }
     rename $_, string;
}

將這段代碼存為movefpl.pl然后在命令行打入 movefpl.pl 播放列表的全路徑 要存歌曲的新路徑
就可以了呢 呵呵
posted @ 2007-11-15 20:41 DraculaW 阅读(279) | 评论 (0)编辑 收藏

Assessing Infection

Background

According to the World Health Organization, infectious disease ranks as the leading cause of death in the world. In 1998 alone, over 17 million people died from infectious and parasitic diseases such as acute lower respiratory infections, tuberculosis, HIV/AIDS, and malaria. It is forecast that infectious disease will continue to kill millions of people, especially those living in developing countries.

The medical profession and scientific community of the world are fighting the infectious disease threat with new tools and technologies from a variety of fields. From this effort, a new field of research has emerged. Infectious Disease Epidemiology is the study of the variables that influence the growth and spread of infectious diseases. This relatively new field combines molecular biology, immunology, genetics, and the computational sciences. A focus of this field is the study of the factors that influence the growth of an infectious disease within a single organism, and the factors that influence the pattern of infection across an entire population.

Description

This assignment asks you to finish the implementation of a program that assesses the level of infection in a tissue sample. You are given data representing a rectangular tissue sample, overlaid with a grid. Certain portions of the tissue are infected; others are not. Your goal is to help assess the extent of the infection by writing a program that, given the coordinates of a colony of infection, can determine its size.

A typical use of the program follows. The user interacts with the program only through command-line arguments. The user supplies to the program a data filename and the coordinates of a cell in the grid. The coordinates are specified by row and then column, both starting at zero. The program calculates the extent of infection at that coordinate and outputs a two-dimensional representation of the tissue sample. Figure 1 depicts the execution of the program.

A screen shot from a sample solution
Figure 1 Output from a sample solution

For the purpose of this assessment, we consider a "colony" of infected tissue to be a set of adjacent and infected cells. In Figure 1, we can see three separate colonies. The smallest colony consists of two cells and is located in the lower left corner of the grid. Another colony consisting of three infected cells exists on the far right edge of the grid. The largest colony of eight cells resides primarily in the middle of the grid. This colony has a small arm into the upper left corner of the grid. Notice from this colony that cells residing in diagonals are considered "adjacent." The plus signs next to the cells in this largest colony indicate that they all belong to the colony that contains the user entered coordinate.


solution :

#ifndef GRID_H
#define GRID_H

#include <string>
#include <vector>

using namespace std;

/*
* IMPORTANT NOTE:
*
* For this assignment, you might need to add state to the
* class and/or augment existing methods, and/or create private helper
* methods, but you should not delare new public methods
*/

const bool INFECTED = true;
const bool NOT_INFECTED = false;

class grid;

class grid {

private:
    int rows;
    int cols;
    vector<bool> *area;
    vector<bool> *infect;
    int indexof (int row, int col) const;
    bool infected(int row, int col) const;

public:
    grid (string file);
    ~grid ();

    int count (int row, int col);

    friend ostream &operator<<(ostream &stream, const grid& ob);

};

#endif

============================================================================

#include <iostream>
#include <fstream>

using namespace std;

#include "grid.h"

// You do not need to alter function indexof.
int grid::indexof (int row, int col) const {
    return row*cols+col;
}

// You do not need to alter function infected.
bool grid::infected(int row, int col) const {
    return (area->operator[](indexof(row, col)) == INFECTED);
}

// You may need to alter the constructor
grid::grid (string file) {

    ifstream grid_file;

    grid_file.open (file.c_str());

    grid_file >> rows;
    grid_file >> cols;

    area = new vector<bool>(rows*cols, NOT_INFECTED);
    infect = new vector<bool>(rows*cols, NOT_INFECTED);
   
    while (true) {

        int blob_row;
        int blob_col;

        grid_file >> blob_row;
        grid_file >> blob_col;

        if (grid_file.eof()) {
            break;
        }

        area->operator[](indexof(blob_row,blob_col)) = INFECTED;
    }

    grid_file.close();
}

// You may need to alter the destructor
grid::~grid () {
    delete area;
    delete infect;
}

// You will need to alter this function to display the
// plus signs (+) next to the cells that belong to
// a counted colony.
ostream &operator<<(ostream &stream, const grid& ob) {

    for (int row=0; row < ob.rows; row++) {
   
        for (int col=0; col < ob.cols; col++) {

            stream << ob.area->operator[](ob.indexof(row, col));
            if( ob.infect->operator[] ( ob.indexof(row, col) ) )
                stream << "+ ";
            else
                stream << "   ";
        }

        stream << endl;
    }

    stream << endl;
    return stream;
}

// Replace the return statement in this function with your
// recursive implementation of this method */
int grid::count (int row, int col) {

    if( row < 0 || col < 0 || row == rows || col == cols)
        return 0;

    if( area->operator[](indexof(row,col) ) == NOT_INFECTED )
        return 0;

    if(infect->operator[](indexof(row,col)) == INFECTED)
        return 0;

    infect->operator[](indexof(row,col)) = INFECTED;

    // Recursive test the 8 point near the point
    // which area is INEFCTED and infect is NOT_INFECTED

    return    count( row - 1, col - 1 ) + count ( row - 1, col )
        + count( row - 1, col + 1 ) + count( row, col - 1 )
        + count( row, col ) + 1 + count( row, col + 1 )
        + count( row + 1, col - 1 ) + count( row + 1, col )
        + count( row + 1, col + 1 );
}
posted @ 2007-11-15 20:40 DraculaW 阅读(452) | 评论 (0)编辑 收藏

     摘要: 一道简单的作业贴 呵呵   阅读全文
posted @ 2007-11-15 20:39 DraculaW 阅读(413) | 评论 (0)编辑 收藏

其实这个题目也很简单 有很多种做法...

就是给一个array你 然后你找出 i,j使从第i个加到第j个最大就好了啊

最简单的算法就是两个for 算下来不到n^2的时间复杂度 可是还有更快的算法哦

首先 可以使用分治算法 这样的算法大概时间复杂度是 n*lg n, 但是这样还不是最好的

最好的其实是把前一个状态储存下来然后进行比较 这个算法时间复杂度只有n哦 很快的呢

先不要看 给个 int a[10] = { 31, -41, 59, 26, -53, 58, 97, -93, -23, 84 }

求它的最大子串有多大哦

inline int
max( int a, int b)
{
    return a > b ? a : b;
}

/*****************************************************************************
* This Function count a array find the largest string count max              *
* Function : CountMax                                                        *
* int    *a : the array of int                                                *
* int     n : the range of array                                              *
* return    : the sum of max this function find                               *
*****************************************************************************/
int
CountMax ( int *a, int n )
{
    int sum = 0, tmp = 0;
    for( int i = 0; i < n; i++ )
    {
        tmp = max( 0, tmp + a[i] );
        sum = max( sum, tmp );
    }

    return sum;
}
/* -----   end of function CountMax   ----- */
posted @ 2007-11-15 20:37 DraculaW 阅读(126) | 评论 (0)编辑 收藏

仅列出标题  下一页