posts - 9,comments - 21,trackbacks - 0
今天装了linuxfans.org社区、由国内LINUX爱好者推出的Magic Linux。
使用KWrite编写C++源码,然后在Shell里使用g++手动编译。
在Windows里能运行的代码,放到这也照样可行。
标准真好!
posted @ 2006-05-19 10:02 蕃豆仁 阅读(383) | 评论 (0)编辑 收藏
php可以使用include,require,include_once,require_once来将其它文件包含到当前文件中.
include和require的区别:
当被包含文件不存在时,include给出一个警告,并继续执行后面的代码;而require则会抛出一个错误,并停止执行代码.
include_once和require的区别同上.
include和include_once的区别在于:
如果文件已经包含进来了include_once不会再包含了,而include则继续包含.
require和require_once的区别同上.
posted @ 2006-04-28 10:37 蕃豆仁 阅读(327) | 评论 (0)编辑 收藏
<? php
// 引用
class  myclass
{
    
var   $name ;
    
var   $age ;
    
function  myclass( $name_ = " " ,   $age_ = 23 )
    {
        
$this   ->  name  =   $name_ ;
        
$this   ->  age  =   $age_ ;
    }
    
function  showinfo( $obj )
    {
        
echo   " <div>\$ " . $obj . "  -> name =  " .   $this   ->  name . " ;\$ " . $obj . "  -> age= " . $this   ->  age . " </div> "  ;
    }
}

$myfun   =   new  myclass();
$myfun   ->  showinfo( " myfun " );
$myfun1   =   &   new  myclass( " 张三 " );  // 无法理解
$myfun1   ->  showinfo( " myfun1 " );
$myfun2   =   &   $myfun // 和$myfun指向同一个地方
$myfun2   ->  showinfo( " myfun2 " );

// 简单数据类型的引用
$a   =   0 ;
$b   =   &   $a ;
$b   =   1 ;
echo   $a // 是1不是0
?>
posted @ 2006-04-24 09:09 蕃豆仁 阅读(596) | 评论 (0)编辑 收藏
通用方法:(使用参数的默认值实现)
<?php
class myclass
{
    
var $name;
    
var $age;
    
function myclass($name_="", $age_=23)
    {
        
$this -> name = $name_;
        
$this -> age = $age_;
    }
    
function showinfo($obj)
    {
        
echo "<div>\$".$obj." -> name = ". $this -> name.";\$".$obj." -> age=".$this -> age."</div>" ;
    }
}

$myfun = new myclass();
$myfun -> showinfo("myfun");
$myfun1 = new myclass("张三");
$myfun1 -> showinfo("myfun1");
$myfun2 = new myclass("李四",25);
$myfun2 -> showinfo("myfun2");
?>

另一种方式(使用系统函数实现)

<?php
//函数重载

class myfun
{
    
var $name;
    
var $age;
    
function myfun1($name_)
    {
        
$this -> name = $name_;
        
$this -> age = 23;
    }
    
function myfun2($name_, $age_)
    {
        
$this -> name = $name_;
        
$this -> age = $age_;
    }
    
function myfun()
    {
        
$args_num = func_num_args(); //获取参数个数
        $args = func_get_args(); //获取参数列表
        
        
switch($args_num)
        {
            
case 1:
                
$this -> myfun1($args[0]);
                
break;
            
case 2:
                
$this -> myfun2($args[0], $args[1]);
                
break;
            
default:
                
$this -> name = "";
                
$this -> age = 23;
                
break;
        }
    }
    
function showinfo($obj)
    {
        
echo "<div>\$".$obj." -> name = ". $this -> name.";\$".$obj." -> age=".$this -> age."</div>" ;
    }
}

$myfun = new myfun();
$myfun -> showinfo("myfun");
$myfun1 = new myfun("张三");
$myfun1 -> showinfo("myfun1");
$myfun2 = new myfun("李四",25);
$myfun2 -> showinfo("myfun2");
?>
posted @ 2006-04-24 08:52 蕃豆仁 阅读(3562) | 评论 (13)编辑 收藏

书上说到2D数组时,用了这个例子:

#include  < iostream >
const   int  Cities  =   5 ;
const   int  Years  =   4 ;

int  main()
{
    
using   namespace  std;
    
   
      
const   char   *  cities[Cities] =  
    {
          
" Gribble city " ,
          
" Gribble town " ,
          
" New Gribble " ,
          
" San Gribble " ,
          
" Gribble Vista "
    };
    
    
    
    
int  maxtemps[Years][Cities]  =
    {
        {
95 99 86 100 104 },
        {
95 97 90 106 102 },
        {
96 100 940 107 105 },
        {
97 102 89 108 104 }
    };
    
    cout 
<<   " Maximum tempevatures for 2002 - 2005\n\n " ;
    
    
for  ( int  city  =   0 ; city  <  Cities;  ++  city)
    {
        cout 
<<  cities[city]  <<   " : \t " ;
        
for  ( int  year  =   0 ; year  <  Years;  ++  year)
            cout 
<<  maxtemps[year][city]  <<   " \t " ;
        cout 
<<  endl;
    }
    
    cin.
get ();
    
return   0 ;
}

完了之后,书上说,可以这样定义cities 2D数组:
const char  cities[25][Cities]= 
    {
          
"Gribble city",
          
"Gribble town",
          
"New Gribble",
          
"San Gribble",
          
"Gribble Vista"
    };
书上说:这样定义,你就保证每个地名不超过24个字符。当初看到这段代码,我百思不得其解:25行,每行5列(Cities常量)???也就是说,有25个城市,每个城市的名称不能超过4个字符(字符串还得加个“\0”),这是正确的吗?
编译器告诉我,这样写是错的!
按照自己的理解改成:
 const char  cities[Cities][25]= 
    {
          
"Gribble city",
          
"Gribble town",
          
"New Gribble",
          
"San Gribble",
          
"Gribble Vista"
    };
顺利通过编译,并且结果和原例一样。
当然,改成string更简单,也更好理解。不过,这时候就是一维数组了:
   const string  cities[Cities]= 
    {
          
"Gribble city",
          
"Gribble town",
          
"New Gribble",
          
"San Gribble",
          
"Gribble Vista"
    };
我更喜欢用string,因为我是从VB转过来的,习惯了string;同时,书上都说了,string是C++对C的扩充,是面向对象的~
posted @ 2006-04-12 16:15 蕃豆仁 阅读(449) | 评论 (0)编辑 收藏

书上有个例子,只允许用户输入数字,这个例子是用来计算高尔夫球结果的。把其中的核心提出来:

 1 #include  < iostream >
 2
 3 using   namespace  std;
 4
 5 int  main()
 6 {
 7      int  i;
 8     
 9     cout  <<   " 请输入: " ;
10      while  ( ! (cin  >>  i))
11      {
12               cin.clear();
13                while  (cin. get ()  !=   ' \n ' )
14                      continue ;
15               cout  <<   " 输入错误,请输入一个数字: " ;
16     }

17     
18     cout  <<   " 你输入的是: "   <<  i  <<  endl; 
19     
20     system( " PAUSE " );
21     
22      return   0 ;
23 }

24

当输入:a时,提示输入错误;当输入5a时,显示出5.这与我想象中(或者说,其它语言中的isnumeric函数)有一丝出入
原因应该出自cin,该如何解决????

posted @ 2006-04-12 15:44 蕃豆仁 阅读(432) | 评论 (0)编辑 收藏

书上说,string可以看作char数组:
#include <iostream>

using namespace std;

int main()
{
    const int SIZE = 20;
    string str;
    char ch[SIZE];
   
    cin >> str;
    cin >> ch;
    cout << str << "," << ch << endl;

    system("PAUSE");
    return 0;
}
在使用 cin >> 时,无论是string还是char[],编译器都能通过编译。可是,当使用cin.getline()时:
#include <iostream>

using namespace std;

int main()
{
    const int SIZE = 20;
    string str;
    char ch[SIZE];
   
  
    cin.getline(ch,SIZE);
    cin.getline(str,SIZE);
   
   
    system("PAUSE");
    return 0;
}
编译器无法编译,出错信息:13(行号)  no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::string&, const int&)'
不是说,string是char的数组吗?可是,为什么会出现没有匹配的函数???

再有:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int SIZE = 20;
    string str;
    char ch[SIZE];
    char c;
  
    cin.get(c);
    cin.get(ch);
    cin.get(str);   
   
    system("PAUSE");
    return 0;
}
cin.get()只允许接受char类型的参数,不接受char[]、string类型的参数。所以,从cin.get(ch);开始,就无法编译了。

(本人使用的编译器:Dev-C++ 4.9.9.2)

posted @ 2006-04-12 15:17 蕃豆仁 阅读(3912) | 评论 (3)编辑 收藏

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    const char filename[] = "mytext.txt";
    ofstream o_file;
    ifstream i_file;
    string out_text;
   
    //写
    o_file.open(filename);
    for (int i = 1; i <= 10; i++)
    {
         o_file << "第" << i << "行\n"; //将内容写入到文本文件中
    }
    o_file.close();
   
    //读
    i_file.open(filename);
    if (i_file.is_open())
    {
                         while (i_file.good())
                         {
                              i_file >> out_text; //将读取的内容存储到变量out_text中
                              cout << out_text << endl; //在控制台输出读取的内容。为什么最后一行的内容会出现两次
                         }
    }
    else
        cout << "打开文件:" << filename << " 时出错!";
    i_file.close();
   
    system("PAUSE");
    return 0;
}

为什么总会将最后一行显示两遍?我的循环似乎没错呀。

posted @ 2006-04-12 15:01 蕃豆仁 阅读(26568) | 评论 (4)编辑 收藏

#include <iostream>

typedef int prablint; //定义别名

int main()
{
    using namespace std;
    prablint a = 0;
    cout << sizeof(a) << endl;
    cout << sizeof(int) << endl;
   
    system("PAUSE");
    return 0;
}

posted @ 2006-04-03 14:36 蕃豆仁 阅读(581) | 评论 (1)编辑 收藏
仅列出标题