电子科大实验报告之四:罐装饮料瓶(Matlab的解法)

题目:
设易拉罐的侧面的厚度1单位,
s=2*PI*r*h+4*PI*r^2
h高度,r为半径,体积V=PI*h*r^2.
V一定时,求使s最小的r和h.

Matlab的解法:
解法1:
v=350;
s=0;h=0;r=0;
for j=1:6

 r_=(v/(j*pi))^(1/3)
 h_=j*r_
 s_=2*pi*r_*h_+4*pi*r_*r_
 
 if(s>s_ | s==0)

  s=s_;
  h=h_;
  r=r_;

 end
end
s
r
h
r/h


解法2:
v=350;
s=[];h=[];r=[];
for j=1:6

 r(j)=(v/(j*pi))^(1/3)
 h(j)=j*r(j)
 s(j)=2*pi*r(j)*h(j)+4*pi*r(j)^2

end

[minCost,j]=min(s)
r(j)
h(j)
r(j)/h(j)


解法3:
cost=inline('(2*pi*x+4*pi)*(350/(pi*x))^(2/3)');
s=fminbnd(cost,0.001,25)
feval(cost,s)

fplot(cost,[0.1 50]);grid on;

posted @ 2006-04-06 14:13 张沈鹏 阅读(478) | 评论 (0)编辑 收藏
 

这学期电脑被家中没收,只能在网吧上网,什么也干不了,很是郁闷。每天到图书馆翻看C++和神经网络的书,在纸上写代码...................


秋天的时候,
落叶带着一生的惆怅,
第一次学会飞翔。

春天的期望,
夏天的梦想,
在微凉的风中,
成为没有回声的吟唱。

许多年以后,
我们的故事,
终究还是被所有的人遗忘。

一段尘封的记忆,
层层叠叠,
埋藏在亘古的时光。

吻别,
芬芳,歌声和阳光。

最后的最后,
我才明白,
一切的尽头,
就是我出发的地方。

posted @ 2006-03-22 17:25 张沈鹏 阅读(271) | 评论 (2)编辑 收藏
 

http://giallo.sourceforge.net/
Giallo is a C++ library for asynchronous network programming, based on proactor style notification, independent of underlying OS demultiplexing methods. The aim is to get this accepted into Boost.

http://asio.sourceforge.net/
asio is a cross-platform C++ library for network programming that provides developers with a consistent asynchronous I/O model using a modern C++ approach.

posted @ 2006-03-08 21:44 张沈鹏 阅读(554) | 评论 (0)编辑 收藏
 

看到有前辈写了一个UTF-8与UNICODE相互转换的代码,顺便提一下,希望可以给大家提供一点帮助.
下面是一些编码格式的bit长

Examples of fixed-width encoding forms:

Type Each character
encoded as
Notes
  7-bit a single 7-bit quantity example: ISO 646
  8-bit G0/G1 a single 8-bit quantity with constraints on use of C0 and C1 spaces
  8-bit a single 8-bit quantity with no constraints on use of C1 space
  8-bit EBCDIC a single 8-bit quantity with the EBCDIC conventions rather than ASCII conventions
16-bit (UCS-2) a single 16-bit quantity within a code space of 0..FFFF
32-bit (UCS-4) a single 32-bit quantity within a code space 0..7FFFFFFF
32-bit (UTF-32) a single 32-bit quantity within a code space of 0..10FFFF
16-bit DBCS process code a single 16-bit quantity example: UNIX widechar implementations of Asian CCS's
32-bit DBCS process code a single 32-bit quantity example: UNIX widechar implementations of Asian CCS's
DBCS Host two 8-bit quantities following IBM host conventions

Examples of variable-width encoding forms:

Name Characters are encoded as Notes
UTF-8 a mix of one to four 8-bit code units in Unicode
and one to six code units in 10646
used only with Unicode/10646
UTF-16 a mix of one to two 16 bit code units used only with Unicode/10646

Boost中提供了一个UTF-8 Codecvt Facet,可以在utf8和UCS-4(Unicode-32)之间转换.
使用方式如下

  //...
  // My encoding type
  typedef wchar_t ucs4_t;

  std::locale old_locale;
  std::locale utf8_locale(old_locale,new utf8_codecvt_facet<ucs4_t>);

  // Set a New global locale
  std::locale::global(utf8_locale);

  //  UCS-4 转换为 UTF-8
  {
    std::wofstream ofs("data.ucd");
    ofs.imbue(utf8_locale);
    std::copy(ucs4_data.begin(),ucs4_data.end(),
          std::ostream_iterator<ucs4_t,ucs4_t>(ofs));
  }

  // 读入 UTF-8 ,转换为 UCS-4 
  std::vector<ucs4_t> from_file;
  {
    std::wifstream ifs("data.ucd");
    ifs.imbue(utf8_locale);
    ucs4_t item = 0;
    while (ifs >> item) from_file.push_back(item);
  }
  //...
UTF-8 Codecvt Facet详见
http://www.boost.org/libs/serialization/doc/codecvt.html

posted @ 2006-02-15 17:19 张沈鹏 阅读(2610) | 评论 (2)编辑 收藏
 

boost在路上...tokenizer
tokenizer - Break of a string or other character sequence into a series of tokens, from John Bandela
tokenizer - 分解字串,提取内容.作者: John Bandela

例一:
// simple_example_1.cpp
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>

int main(){
   using namespace std;
   using namespace boost;
   string s = "This is,  a test";
   tokenizer<> tok(s);
   for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end();++beg){
       cout << *beg << "\n";
   }
}

输出
This
is
a
test

tokenizer默认将单词以空格和标点为边界分开.

例二:
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>

int main(){
   using namespace std;
   using namespace boost;
   string s = "Field 1,\"putting quotes around fields, allows commas\",Field 3";
   tokenizer<escaped_list_separator<char> > tok(s);
   for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end();++beg){
       cout << *beg << "\n";
   }
}
输出
Field 1
putting quotes around fields, allows commas
Field 3

双引号之间可以有标点.


例三:
// simple_example_3.cpp
#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>

int main(){
   using namespace std;
   using namespace boost;
   string s = "12252001";
   int offsets[] = {2,2,4};
   offset_separator f(offsets, offsets+3);
   tokenizer<offset_separator> tok(s,f);
   for(tokenizer<offset_separator>::iterator beg=tok.begin(); beg!=tok.end();++beg){
       cout << *beg << "\n";
   }
}

把12252001分解为
12
25
2001

例4:
// char_sep_example_1.cpp
#include <iostream>
#include <boost/tokenizer.hpp>
#include <string>

int main()
{
  std::string str = ";!!;Hello|world||-foo--bar;yow;baz|";
  typedef boost::tokenizer<boost::char_separator<char> >
    tokenizer;
  boost::char_separator<char> sep("-;|");
  tokenizer tokens(str, sep);
  for (tokenizer::iterator tok_iter = tokens.begin();
       tok_iter != tokens.end(); ++tok_iter)
    std::cout << "<" << *tok_iter << "> ";
  std::cout << "\n";
  return EXIT_SUCCESS;
}

输出
<!!> <Hello> <world> <foo> <bar> <yow> <baz>
自定义分隔的标点

例5:
    // char_sep_example_2.cpp
    #include <iostream>
    #include <boost/tokenizer.hpp>
    #include <string>

    int main()
    {
        std::string str = ";;Hello|world||-foo--bar;yow;baz|";
        typedef boost::tokenizer<boost::char_separator<char> >
            tokenizer;
        boost::char_separator<char> sep("-;", "|", boost::keep_empty_tokens);
        tokenizer tokens(str, sep);
        for (tokenizer::iterator tok_iter = tokens.begin();
             tok_iter != tokens.end(); ++tok_iter)
          std::cout << "<" << *tok_iter << "> ";
        std::cout << "\n";
        return EXIT_SUCCESS;
    }

The output is:

    <> <> <Hello> <|> <world> <|> <> <|> <> <foo> <> <bar> <yow> <baz> <|> <>
去除-; , 保留|但将它看作是分隔符,当两个分隔符相邻的时候会自动加空格

例6:
    // char_sep_example_3.cpp
    #include <iostream>
    #include <boost/tokenizer.hpp>
    #include <string>

    int main()
    {
       std::string str = "This is,  a test";
       typedef boost::tokenizer<boost::char_separator<char> > Tok;
       boost::char_separator<char> sep; // default constructed
       Tok tok(str, sep);
       for(Tok::iterator tok_iter = tok.begin(); tok_iter != tok.end(); ++tok_iter)
         std::cout << "<" << *tok_iter << "> ";
       std::cout << "\n";
       return EXIT_SUCCESS;
    }

The output is:

    <This> <is> <,> <a> <test>
保留标点但将它看作分隔符

posted @ 2006-01-25 18:00 张沈鹏 阅读(734) | 评论 (0)编辑 收藏
 

MinGW的下载安装我就不多说了
1.
设置MinGW的环境变量,在
E:\!程序\C++\boost_1_33_0\tools\build\jam_src
目录下运行
build.bat mingw
编译bjam,在
E:\!程序\C++\boost_1_33_0\tools\build\jam_src\bin.ntx86
下生成bjam.exe,Copy it to windows directory.

2.
在Boost的解压目录下运行
bjam "-sTOOLS=mingw" install
就可以了,编译完成的文件会自动放在C:\Boost\lib下
注意,boost的解压目录的路径中不要有中文或!之类的标点,不然可能有许多奇怪的错误.

3.
可以在lib目录下你需要的库中找到有jam文件的目录,运行
bjam "-sTOOLS=mingw"
就可以只编译该库,编译完成后会防止stage\lib目录下

posted @ 2006-01-25 17:59 张沈鹏 阅读(500) | 评论 (0)编辑 收藏
 
用dll要一个导入库和头文件,对于Gcc/G++可以用工具dlltool来生成这个导入库.命令如下:

dlltool --dllname foo.dll --def foo.def --output-lib libfoo.a

dlltool在MinGW的工具包中有.
然后可以用 -l libfoo 调用库(libfoo的lib前缀可以省略,注意libfoo不要加后缀名,-L可以指定库的目录)
posted @ 2006-01-19 02:41 张沈鹏 阅读(950) | 评论 (0)编辑 收藏
 

用mingw32-make前修改一下makefile文件,改为如下

# DEBUG can be set to YES to include debugging info, or NO otherwise(不是DEBUG)
DEBUG          := NO

# PROFILE can be set to YES to include profiling info, or NO otherwise
PROFILE        := NO

# TINYXML_USE_STL can be used to turn on STL support. NO, then STL
# will not be used. YES will include the STL files.(使用STL,选择的话,则可以使用std::string)
TINYXML_USE_STL := YES

另外可以tinyxml_guide.zip可以下载.

以下转载:

TinyXml学习笔记

张弛<zhangchi@china.com>

一、      TinyXml的特点

TinyXml是一个基于DOM模型的、非验证的轻量级C++解释器。

1.      SAX和DOM

目前XML的解析主要有两大模型:SAX和DOM。

其中SAX是基于事件的,其基本工作流程是分析XML文档,当发现了一个新的元素时,产生一个对应事件,并调用相应的用户处理函数。这种方式占用内存少,速度快,但用户程序相应得会比较复杂。

而DOM(文档对象模型),则是在分析时,一次性的将整个XML文档进行分析,并在内存中形成对应的树结构,同时,向用户提供一系列的接口来访问和编辑该树结构。这种方式占用内存大,速度往往慢于SAX,但可以给用户提供一个面向对象的访问接口,对用户更为友好。

2.      验证和非验证

对于一个特定的XML文档而言,其正确性分为两个层次。首先是其格式应该符合XML的基本格式要求,比如第一行要有声明,标签的嵌套层次必须前后一致等等,符合这些要求的文件,就是一个合格的XML文件,称作well-formatted。但除此之外,一个XML文档因其内容的不同还必须在语义上符合相应的标准,这些标准由相应的DTD文件或者Schema文件来定义,符合了这些定义要求的XML文件,称作valid。

因此,解析器也分为两种,一种是验证的,即会跟据XML文件中的声明,用相应的DTD文件对XML文件进行校验,检查它是否满足DTD文件的要求。另一种是忽略DTD文件,只要基本格式正确,就可以进行解析。

就我所知,验证的解析器通常都是比较重量级的。TinyXml不支持验证,但是体积很小,用在解析格式较为简单的XML文件,比如配置文件时,特别的合适。

二、 TinyXml的构建和使用
1.      获取

TinyXml首页在http://www.grinninglizard.com/tinyxml/index.html,从这里可以找到最新版本的源代码,目前的版本是2.3.4。

2.构建

TinyXml在构建时可以选择是否支持STL,选择的话,则可以使用std::string,所以通常应该打开这个选项。

在Windows上,TinyXml的源码包里提供了VC6的工程文件,直接用它就可以生成两个静态库(带STL和不带STL),非常容易。唯一需要注意的是,默认生成的库是单线程的,如果用在多线程的项目中,需要改动一下配置,生成相应的多线程库。

在Unix平台上,TinyXml的源码包里只提供了一个Makefile,对于典型的Linux系统,或装了gcc和gmake的其他Unix,这个Makefile足够用了,我在RH9和RHEL4上测试,简单的make就成功了。需要注意的有以下几点:默认的编译是不支持STL的,可以通过编辑Makefile的TINYXML_USE_STL := NO那一行,把NO改成YES就可以支持STL了;还有默认只生成了一个测试程序,没有生成任何库,如果要生成静态库的话,可以用ar命令,将生成的几个目标文件打包就行了,如果要生成动态库,则需要加上-fpic参数重新编译。

3.      使用

构建了相应的库之后,在使用了它们的工程中,只要在连接时把他们连上就行了。需要注意的是,如果需要STL支持,在编译用到了TinyXml的文件时,需要定义一个宏TIXML_USE_STL,对gcc,可以使用参数-DTIXML_USE_STL,对cl.exe(VC),可以使用参数/DTIXML_USE_STL,如果嫌麻烦,可以直接定义在 tinyxml.h文件里。

三、 TinyXml的编程模型1.      类之间的关系

TinyXml实现的时DOM访问模型,因此提供了一系列的类对应XML文件中的各个节点。主要类间的关系如下图所示:

TiXmlBase:其他类的基类,是个抽象类

TiXmlNode:表示一个节点,包含节点的一般方法,如访问自节点、兄弟节点、编辑自身、编辑子节电

TiXmlDocument:表示整个XML文档,不对应其中某个特定的节点。

TiXmlElement:表示元素节点,可以包含子节点和TiXmlAttribute

TiXmlComment:表示注释

TiXmlDeclaration:表示声明

TiXmlText:表示文本节点

TiXmlUnknown:表示未知节点,通常是出错了

TiXmlAttribute:表示一个元素的属性

下面是一个简单的例子:

<?xml version="1.0" encoding="utf-8" ?>

<!-This is only a sample-->

<book>

       <name>TinyXml How To</name>

       <price unit=”RMB”>20</price>

       <description>Some words…</description>

</ book >

整个文档,对应TiXmlDocument

book,name,price, description,都对应TiXmlElement

第一行对应一个TiXmlDeclaration

第二行对应一个TiXmlComment

“TinyXml How To”对应一个TiXmlText

unit则是price的一个TiXmlAttribute

这些类与XML文件中的相应元素都有很好的对应关系,因此相信参照TinyXml的文档,可以很容易的掌握各个方法的使用。

2.  需要注意的问题

各类之间的转换

由于各个节点类都从TiXmlNode继承,在使用时常常需要将TiXmlNode*类型的指针转换为其派生类的指针,在进行这种转换时,应该首先使用由TiXmlNode类提供的一系列转换函数,如ToElement(void),而不是c++的dynamic_cast

检查返回值

由于TinyXml是一个非校验的解析器,因此当解析一个文件时,很可能文件并不包含我们预期的某个节点,在这种情况下,TinyXml将返回空指针。因此,必须要对返回值进行检查,否则将很容易出现内存访问的错误。

如何重头建立一个XML文件

先建立一个TiXmlDocument对象,然后,载入某个模板,或者直接插入一个节点作为根节点,接着就可以像打开一个已有的XML文件那样对它进行操作了。

四、总结

TinyXml最大的特点就是它很小,可以很方便的静态连接到程序里。对于像配置文件、简单的数据文件这类文件的解析,它很适合。但是由于它是非验证的,因此需要在程序里做许多检查工做,加重了程序编写的负担。因此对于复杂的XML文件,我觉得最好还是用验证的解析器来处理。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=477335

posted @ 2006-01-15 15:19 张沈鹏 阅读(2293) | 评论 (6)编辑 收藏
 
提取未确定数目的参数
在标准库的<cstdarg>中专门提供一组宏来访问它们.
例:
void error(int severity ...)
{
 va_list ap;
 //用va_start初始化ap,第二个参数是函数的最后一个有名的形式参数的名字
 va_start(ap,severity);
 for(;;){
  //宏va_arg是按顺序提取各个无名参数,第二个参数是假定的该无名参数的类型
  chap* p = va_arg(ap,char*);
  if(p==0)break;
  cerr<<p<<' ';
 }
 va_end(ap);//调用va_start后必须用va_end退出
 cerr<<'n';
 if(severity) exit(severity);
}
posted @ 2006-01-14 21:31 张沈鹏 阅读(210) | 评论 (0)编辑 收藏
 
网页中可以用,将来也可以用的XUL的程序中去

http://free5.ys168.com/?ak747

下的XUL的文件夹
将来可以在XUL程序中使用,下一步的改进计划是成为类似Windows Media Player五个星标。
posted @ 2006-01-11 14:05 张沈鹏 阅读(254) | 评论 (0)编辑 收藏
仅列出标题
共7页: 1 2 3 4 5 6 7