随笔分类 -  C/C++语言学习笔记

摘要:1 /*帕斯卡三角形(杨辉三角)*/ 2 int Recursive_Pascal_Triangle( int i, int j ) 3 { 4 if( (j == 0) || (i == j) ) 5 return 1; 6 else{ 7 ret... 阅读全文
posted @ 2015-02-27 22:18 永久指针 阅读(669) 评论(0) 推荐(0)
摘要:1 /*递归形式的十进制数转化为二进制数*/ 2 void D2B( int n ) 3 { 4 if( n == 0 ) 5 return ; 6 else{ 7 D2B( n/2 ); 8 printf("%d",n%2); 9 ... 阅读全文
posted @ 2015-02-26 22:06 永久指针 阅读(458) 评论(0) 推荐(0)
摘要:设计一个函数轮转字符串。例如将“abcd”转为"dabc"递归实现方式: 1 /*将字符串循环右移n个单位*/ 2 void move(char s[], int n) 3 { 4 if( n == 0) 5 return ; 6 else{ 7 in... 阅读全文
posted @ 2015-02-26 11:25 永久指针 阅读(631) 评论(0) 推荐(0)
摘要:The difference here is that : char *s = "hello,world!";will place hello,world in read-only part of the memmory and makes s a pointer to that. makin... 阅读全文
posted @ 2015-02-24 21:28 永久指针 阅读(128) 评论(0) 推荐(0)
摘要:以前用C,习惯了define;const,enum 什么的基本不太会用,现在有时间整理一下。/*******************************************************************************************************... 阅读全文
posted @ 2015-02-24 20:37 永久指针 阅读(361) 评论(0) 推荐(0)
摘要:用malloc生成m*n的矩阵的两种方法: 1:使用指针数组 1 int** create_matrix(int m, int n) 2 { 3 int **mat = (int**) malloc (sizeof(int*) * m); 4 int i; 5 for(i=0... 阅读全文
posted @ 2014-06-02 18:10 永久指针 阅读(1930) 评论(0) 推荐(0)
摘要:1 #includestdio.h 2 3 #define IN 1/*在单词内*/ 4 #define OUT 0/*在单词外*/ 5 int main(int argc, char* argv[]) 6 { 7 int c, nl, nw, nc, state; 8 sta... 阅读全文
posted @ 2014-04-22 15:42 永久指针 阅读(664) 评论(0) 推荐(0)
摘要:【实验目的】掌握并灵活使用线程机制掌握并能够灵活使用同步互斥机制了解并能够较灵活地使用IO技术【实验要求】● 基于线程的生产者-消费者的合作问题– 其中(生产者)从外设获取数据进行生产– 另外(消费者)消费后进行输出,并存储输出结果。●在Linux环境下使用POSIX库进行设计实现●鼓励使用QT进行图形化显示●根据情况决定是否进行答辩●可以2人一组,但不能超过2人,在报告中必须要有明确分工【问题描述】 ●完成N个生产者和M个消费者线程之间的并发控制,N、M不低于30,数据发送和接收缓冲区尺寸不小于20个(每个产品占据一个)。 ●其中生产者线程1、3、5、7、9生产的产品供所有奇数编号的消费.. 阅读全文
posted @ 2014-01-11 14:14 永久指针 阅读(5657) 评论(0) 推荐(0)
摘要:Linux在用fork创建进程时,为子进程申请了堆栈、BSS区、全局变量、和代码段等资源。而用pthread_create创建线程时,用户空间分配的资源比线程少得多。可以说,线程就是轻量级的进程。1.函数 pthread_create()用来创建一个新的线程。其函数声明如下:1 /*come from /usr/include/pthread.h*/2 extern int pthread_create (pthread_t *__restrict __newthread,3 __const pthread_attr_t *__restrict __attr,4 ... 阅读全文
posted @ 2013-12-10 14:13 永久指针 阅读(428) 评论(0) 推荐(0)
摘要:客户端向服务器发出连接请求后,服务器端写入数据发送给客户端,在客户端显示。server: 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 11 #define MAXSIZE 102412 13 int main(int argc, char* argv[])14 {15 int sockfd, new_fd;16 struct sockaddr_in server_addr;17 struct so... 阅读全文
posted @ 2013-11-24 15:56 永久指针 阅读(335) 评论(0) 推荐(0)
摘要:#include #include #include #include #include int main(int argc, char** argv){ if(argc<2) { printf("error usage!\n"); exit(1); } int a = atoi(argv[1]); printf("you've input %s\n, after atoi...",argv[1]); printf("you get %d\n",a); return 0;} 阅读全文
posted @ 2013-11-19 13:50 永久指针 阅读(229) 评论(0) 推荐(0)
摘要:如果在Linux Terminal 里面 man system 大家可以看到system()函数的用法,非常简单:函数声明如下:int system(const char *command); 函数失败则返回-1,成功后返回0。 例:system("echo hello > hello.txt") /*将hello 写入到hello.txt*/ 阅读全文
posted @ 2013-11-19 13:42 永久指针 阅读(1689) 评论(0) 推荐(0)
摘要:一、基本I/O操作 Linux的输入/输出(I/O)操作,通常分为5个方面:打开、读取、写入、定位和关闭;对应的有5个系统调用: open()、read()、write()、lseek()、和close()这5个函数,也称为不带缓冲区的I/O操作。程序员可以直接操作硬件,这样为开发驱动等底层的系统应用提供了方便。这些函数属于POSIX的一部分。其原型如下: 1 #include 2 #include 3 #include 4 #include 1 int open(const char *pathname, int flags, mode_t mode);2 3 size_t read(i.. 阅读全文
posted @ 2013-11-17 10:44 永久指针 阅读(337) 评论(0) 推荐(0)
摘要:在ubuntu12.04下的共享内存+多线程+信号量练习实例。问题:程序要求用多线程+共享内存+信号量,实现将一个文件中的内容写入到另一个空白文件中(读与写的操作必须分在两个线程中完成),要求文件内容的大小要远大于所用共享内存的大小。分析:题目要求将一个文件(假设为in)中的数据存入到另一个空白文 件(假如为out)中,而且要求共享内存的大小要小于文件传输内容的大小,因此我们就需要利用有限大小的共享内存和文件指针将in中的数据循环多次倒入到 out文件中,而为了解决重复写入或是漏写等线程调度混乱造成的问题,需要在其中使用信号量加以同步控制。 1 /********************* 2 阅读全文
posted @ 2013-11-12 17:47 永久指针 阅读(1130) 评论(0) 推荐(0)
摘要:/*keyboardview.c*/#include #include #include #include #include #include int main (){ int keys_fd; int pipes[2]; struct input_event t; char buf[100]; int pid,pid1; int nread; int key_code; if(pipe(pipes) < 0) { printf("%s\n","pipes error!" ); exit(EXIT_FAILURE... 阅读全文
posted @ 2013-11-12 17:41 永久指针 阅读(336) 评论(0) 推荐(0)
摘要:argv and argcSince a C program is executed as if it is a function called by the OS, the OS can and does pass parameters to the program.There are two parameters. These two parameters fully specify the command line that was used to invoke the executing program. The first parameter is argc. It is an in 阅读全文
posted @ 2013-11-12 17:35 永久指针 阅读(340) 评论(0) 推荐(0)
摘要:/*生成矩阵,输入n,输出n*n矩阵,生成的格式如下,例如n=3: 0 0 0 0 0 1 0 1 0 ...... 1 1 1*/#include #include int main(){ int n; int i,j; scanf("%d",&n); int row = 1 >j)&1) { *(arr+i*n+n-j-1) = 1; } else { *(arr+i*n+n-j-1) = 0; }... 阅读全文
posted @ 2013-11-12 17:34 永久指针 阅读(512) 评论(0) 推荐(0)
摘要:1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 int main(int argc, char* argv[])10 {11 int running = 1;12 int shid;13 int semid;14 int value;15 int read_num;16 FILE *stream;17 char* sharem=NULL;18 struct sembuf sem_b;19 sem_b.sem_n... 阅读全文
posted @ 2013-11-12 17:30 永久指针 阅读(946) 评论(0) 推荐(0)