当前位置:  编程技术>c/c++/嵌入式
本页文章导读:
    ▪递归统计项目中的非空白代码行数            在准备阅读一个开源项目的代码前,可以大约看看整个项目共有多少代码,估计项目的规模。我就写了一个简单的程序来达到此目的,其中的一些代码参考了apue中的代码。 .........
    ▪Qt实现图片移动(2)定时器和信号槽      接昨天的图片移动:    在实训的时候飞机移动有两种,一种是玩家控制的战机,由键盘来控制;一种是敌机,控制方式是定时器,Qt里面也有定时器这种东西,所以试着实现了一下。&n.........
    ▪c++与c的主要区别      1.c语言是面向过程的一种语言    c++语言是面向对象的一种语言 2.在linux 中 c语言的程序后缀为.c,编译使用gcc                 &.........

[1]递归统计项目中的非空白代码行数
    来源:    发布时间: 2013-10-17

      在准备阅读一个开源项目的代码前,可以大约看看整个项目共有多少代码,估计项目的规模。我就写了一个简单的程序来达到此目的,其中的一些代码参考了apue中的代码。

      代码如下:

1 //程序功能:统计一个文件夹(一个项目)中所有文件的有效代码行数(除去空白行)。
2
3 #include <apue.h>
4 #include <dirent.h>
5 #include <limits.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #define MAXBUF 5000
10
11 typedef int Myfunc(const char *,const struct stat *,int);
12
13 static Myfunc myfunc;
14 static int myftw(char *,Myfunc *);
15 static int dopath(Myfunc *);
16 static int sum_lines=0;//the sum of no empty lines
17
18 static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;
19
20
21 int
22 main(int argc, char *argv[])
23 {
24 int ret;
25
26 if (argc != 2)
27 err_quit("usage: ftw <starting-pathname>");
28
29 ret = myftw(argv[1], myfunc); /* does it all */
30
31 printf("\n\nSum lines: %d lines\n\n\n",sum_lines);
32 exit(ret);
33 }
34
35 /*
36 * Descend through the hierarchy, starting at "pathname".
37 * The caller's func() is called for every file.
38 */
39 #define FTW_F 1 /* file other than directory */
40 #define FTW_D 2 /* directory */
41 #define FTW_DNR 3 /* directory that can't be read */
42 #define FTW_NS 4 /* file that we can't stat */
43
44 static char *fullpath; /* contains full pathname for every file */
45
46 static int /* we return whatever func() returns */
47 myftw(char *pathname, Myfunc *func)
48 {
49 int len;
50 fullpath = path_alloc(&len); /* malloc's for PATH_MAX+1 bytes */
51 /* ({Prog pathalloc}) */
52 strncpy(fullpath, pathname, len); /* protect against */
53 fullpath[len-1] = 0; /* buffer overrun */
54
55 return(dopath(func));
56 }
57
58 static int dopath(Myfunc * func)
59 {
60 struct stat statbuf;
61 struct dirent *dirp;
62 DIR *dp;
63 int ret;
64 char *ptr;
65
66 if(lstat(fullpath,&statbuf)<0)
67 return (func(fullpath,&statbuf,FTW_NS));
68 if(S_ISDIR(statbuf.st_mode)==0)
69 return (func(fullpath,&statbuf,FTW_F));
70

    
[2]Qt实现图片移动(2)定时器和信号槽
    来源:    发布时间: 2013-10-17

接昨天的图片移动:

    在实训的时候飞机移动有两种,一种是玩家控制的战机,由键盘来控制;一种是敌机,控制方式是定时器,Qt里面也有定时器这种东西,所以试着实现了一下。

    在昨天的基础上加了一个cat类,闲话休絮,先把代码贴上,慢慢解释。

    首先是cat.h

1 #ifndef CAT_H
2 #define CAT_H
3
4 #include <QtGui>
5
6 class Cat : public QWidget
7 {
8 Q_OBJECT
9 public:
10 explicit Cat(QWidget *parent = 0);
11 ~Cat();
12 protected:
13 void paintEvent (QPaintEvent *event);
14 void mousePressEvent(QMouseEvent *event);
15
16 private:
17 QPixmap *catImg;
18 QRect *catImgRect;
19 QTimer *catTimer;
20 protected:
21 int shrinkMultiple;
22 int speed;
23 bool stop;
24 signals:
25 void clicked();
26 public slots:
27 void mousePressEventSlot();
28
29 void run();
30 };
31
32 #endif // CAT_H
1 private:
2 QPixmap *catImg;
3 QRect *catImgRect;
4
5 protected:
6 int shrinkMultiple;
7 int speed;
8

    这几个和上一节完全一样,不解释了,加了几个变量,信号槽:

    1、QTimer *catTimer; 就是传说中的定时器,用信号槽的方式来使用

connect(catTimer,SIGNAL(timeout()),this,SLOT(run()));   catTimer->start(20);

    解释:timeout是Qt自己的信号,比如这个例子,start是说让catTimer定时器开始工作,时间是20毫秒,当20毫秒过后,Qt会发出timeout信号,将run这个槽的函数加入到某个队列里面(队列具体的名字和作用我忘了),来依次处理。

    2、bool stop; 因为我想要让运动的图片暂停,所以加了这么一个变量

    3、两个槽

1 public slots:
2 void mousePressEventSlot();
3
4 void run();

    具体用法在cpp文件中解释

1 #include "cat.h"
2
3 Cat::Cat(QWidget *parent) :
4 QWidget(parent),shrinkMultiple(2),
5 speed(10),stop(true)
6 {
7 catImg = new QPixmap(":/img/little_cat.jpg");
8 int width = catImg->width ()/shrinkMultiple;
9 int height = catImg->height ()/shrinkMultiple;
10 catImgRect = new QRect(10,10,width,height);
11
12 catTimer = new QTimer(this);
13
14 connect( this, SIGNAL( clicked() ), this, SLOT( mousePressEventSlot() ) );
15 connect( catTimer,SIGNAL(timeout()),this,SLOT(run()));
16
17 }
18 void Cat::paintEvent (QPaintEvent *event)
19 {
20 QPainter painter(this);
21
22 painter.drawPixmap(*catImgRect,*catImg);
23 }

    
[3]c++与c的主要区别
    来源:    发布时间: 2013-10-17

1.c语言是面向过程的一种语言

   c++语言是面向对象的一种语言

2.在linux 中 c语言的程序后缀为.c,编译使用gcc

                  c++ 程序后缀为.cpp ,编译使用g++

3.c语言没有布尔值

   c++语言有布尔值,成功返回true,失败返回false

4.for循环中,循环变量的声明,

   c语言中  int   i;

               for(i = 0; i < n; i++);

   c++ 语言,除了可以像c语言中那样声明之外,还可以像这样声明for(int i = 9; i < n; i++);

5.c语言中有指针,没有引用

   c++中有引用,引用符号位&,但是没有指针

6.c语言中动态分配内存使用malloc(),释放内存使用free()

   c++中使用new 和 delete来动态分配和释放内存

7.c语言中使用struct结构体

   c++中新增了类的概念,使用语法与结构体想同,但是在类中可以声明函数,叫做方法。

本文链接


    
最新技术文章:
 




特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3