加载中...

编程


C 基础

  1. strcpy(newstr,str)                        /* 拷贝 str 到 newstr */
  2. expr1 ? expr2 : expr3                     /* if (expr1) expr2 else expr3 */
  3. = (> z) ? y : z;                      /* if (y > z) x = y; else x = z; */
  4. int a[]={0,1,2};                          /* 初始化数组 (或者 a[3]={0,1,2}; */
  5. int a[2][3]={{1,2,3},{4,5,6}};            /* 初始化二维数组 */
  6. int i = 12345;                            /* 从 i 转换成 char str */
  7. char str[10];
  8. sprintf(str, "%d", i);

C 实例

一个最小化的 C 程式 simple.c:

  1. #include <stdio.h>
  2. main() {
  3.     int number=42;
  4.     printf("The answer is %i\n", number);  
  5. }

编译: # gcc simple.c -o simple

 ./simple

  1. The answer is 42

C++ 基础

  1. *pointer                                  // 指向对象的指针
  2. &obj                                      // 对象 obj 的地址
  3. obj.x                                     // 类(对象) obj 成员 x
  4. pobj->x                                   // 指针 pobj 指向类(对象)成员 x
  5.                                           // (*pobj).x 同 pobj->x

C++ 实例

来一个稍微现实一点的 C++ 程序,我们在一个头文件(IPv4.h)中创建一个类并且实现它(IPv4.cpp),然后创建一个程式来使用其功能。这个类的成员方法实现了 IP 地址从一串整数转换成我们熟知的点分格式。这是一个最小化的 C++ 程式和多源文件(multi-source)的编译。

IPv4 class

IPv4.h:

  1. #ifndef IPV4_H
  2. #define IPV4_H
  3. #include <string>
  4. namespace GenericUtils {                              // 创建 namespace
  5.     class IPv4 {                                      // 类定义
  6.         public:
  7.             IPv4();
  8.             ~IPv4();
  9.             std::string IPint_to_IPquad(unsigned long ip);// 成员方法接口
  10.     };
  11. } //namespace GenericUtils
  12. #endif // IPV4_H

IPv4.cpp:

  1. #include "IPv4.h"
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;                              // 使用 namespace
  5. using namespace GenericUtils;
  6. IPv4::IPv4() {}                                   // 默认构造/析构函数
  7. IPv4::~IPv4() {}
  8. string IPv4::IPint_to_IPquad(unsigned long ip) {  // 成员方法实现
  9.     ostringstream ipstr;                          // 使用字符串流
  10.     ipstr << ((ip &0xff000000) >> 24)             // 位右移
  11.           << "." << ((ip &0x00ff0000) >> 16)
  12.           << "." << ((ip &0x0000ff00) >> 8)
  13.           << "." << ((ip &0x000000ff));
  14.     return ipstr.str();
  15. }

程序 simplecpp.cpp

  1. #include "IPv4.h"
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int main (int argc, char* argv[]) {
  6.     string ipstr;                                 // 定义变量
  7.     unsigned long ipint = 1347861486;             // 数字形式的 IP
  8.     GenericUtils::IPv4 iputils;                   // 创建一个类的对象
  9.     ipstr = iputils.IPint_to_IPquad(ipint);       // 调研类的成员方法
  10.     cout << ipint << " = " << ipstr << endl;      // 输出结果
  11.     return 0;
  12. }

编译和执行:

  1. # g++ -c IPv4.cpp simplecpp.cpp                # 编译成目标文件
  2. # g++ IPv4.o simplecpp.o -o simplecpp.exe      # 连接目标代码,生成可执行文件
  3. # ./simplecpp.exe 
  4. 1347861486 = 80.86.187.238

使用 ldd 脚本检查并列出可执行程序所依赖的共享库文件。这个命令同样可以用来检查共享库的丢失。 # ldd /sbin/ifconfig

简单的 Makefile

相应的最小化多源文件(multi-source)编译 Makefile 显示如下。每一个命令行必须以 tab 开始!可以将一个较长行使用反斜线"\"来分解为多行。

  1. CC = g++
  2. CFLAGS = -OOBJS = IPv4.o simplecpp.o
  3. simplecpp: ${OBJS}
  4. ${CC} -o simplecpp ${CFLAGS} ${OBJS}
  5. clean:
  6. rm -f ${TARGET} ${OBJS}

还没有评论.