位运算:

Part1:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. //unsigned char per byte that contain 00000000 - 11111111
  8. unsigned char a=; // bin->0000 0010
  9. unsigned char b = ~a; //~(0000 0010) result:(1111 1101) or 253
  10. printf("%d\n",b);
  11.  
  12. unsigned char c=; //0000 0011
  13. unsigned char d=; //0000 0010
  14. printf(" & %d\n",c&d); // c&d -> 0000 0010 : 2
  15.  
  16. printf(" | %d\n",c|d); // c|d -> 0000 0011 : 3
  17.  
  18. //十六进制->十进制 hexadecimal(hex)->base10
  19. int d_hex = 0xA3F;
  20. //A-..-F (10-15)
  21. //0xA3F -> A3F -> 10*16^2 + 3*16^1 + 15*16^0 = 2623
  22. printf("0xA3F : %d\n",d_hex); //
  23.  
  24. int d_hex_zero = 0x000; //0*16^2 + 0*16^1 + 0*16^0 = 0
  25. printf("0x000 : %d\n",d_hex_zero);
  26.  
  27. int d_hex_1 = 0x001; //0*16^2 + 0*16^1 + 1*16^0 = 1
  28. printf("0x001 : %d\n",d_hex_1);
  29.  
  30. int d_hex_2 = 0x011; //0*16^2 + 1*16^1 + 1*16^0 = 17
  31. printf("0x001 : %d\n",d_hex_2);
  32.  
  33. return ;
  34. }

Part2:

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. // int is 32 bit
  8. int a = ; //00000000 00000000 00000000 00001010 0*2^0 + 1*2^1 + 0*2^2 + 1*2^3 = 10
  9. int b = ; //00000000 00000000 00000000 00000101 1*2^0 + 0*2^1 + 1*2^2 + 0*2^3 = 5
  10. int c = a | b ; // 00000000 00000000 00000000 00001111 1+2+4+8 = 15
  11. cout << c <<endl;
  12. int d = c|0xFF; //hex: 0xFF = 15*16^1 + 15*16^0 = 255 bin:255-> 1111 1111
  13. cout << d <<endl;
  14.  
  15. // ^
  16. int e = a ^ b; //00000000 00000000 00000000 00001111 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 = 15
  17. cout << e <<endl;
  18.  
  19. // swap a b
  20. // a 00000000 00000000 00000000 00001010
  21. // b 00000000 00000000 00000000 00000101
  22.  
  23. a = a ^ b ; // a->00000000 00000000 00000000 00001111
  24. b = b ^ a ; // b->00000000 00000000 00000000 00001010
  25. a = a ^ b ; // 00000000 00000000 00000000 00000101
  26.  
  27. cout << "after swap a,b a:" << a << "\t" << "b:"<<b <<endl;
  28. return ;
  29. }

C语言的几个输入输出函数

#include <stdio.h>

getchar(),putchar()

scanf(),printf()

1->getchar()与scanf()唯一的区别是getchar()不会跳过'\n'或者空格,例如如下

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. int ch;
  7. int a,b;
  8. printf("enter a char ,enter to quit\n");
  9. while((ch=getchar()) != '\n' )
  10. {
  11. if (scanf("%d %d",&a,&b)!=)
  12. break;
  13. printf("you enter the %c %d %d\n",ch,a,b);
  14. // getchar();//the ch a b enter complete will leave a '\n'
  15. while(getchar() != '\n') // GNU BOOK always use this
  16. {
  17. continue;
  18. }
  19. printf("PLEASE ENTER ANOTHER CHAR \n");
  20. }
  21.  
  22. exit();
  23. }

如果没有while循环中的while(getchar() != '\n') ,那么程序输入一轮直接结束,原因是scanf()会把回车 放到输入队列。所以要剔除输入的回车

2->如何确定输入类型

scanf()返回值是其成功读入的项目个数。比如scanf("%d %d",&a,&b)==2

如下面代码,如果输入q,则state=0

  1. int main()
  2. {
  3. int input;
  4. int state = scanf("%d",&input);
  5. printf("state = %d,and input value is %d,",state,input);
  6. exit();
  7. }

输入输出大集合:

  1. char temp[];
  2. printf("Please enter your first name\n");
  3. // STDIN
  4. //scanf("%s",temp); // jump backspace
  5. //fscanf(stdin,"%s",temp); // jump backspace
  6. fgets(temp,,stdin); // do not jump backspace
  7. //gets(temp); // do not jump backspace
  8.  
  9. // STDOUT
  10. //printf("You enter the strings -> : %s\n",temp);
  11. //fprintf(stdout,"You enter the strings -> : %s\n" ,temp);
  12. //fputs(temp,stdout);
  13. puts(temp);
  14.  
  15. // CHAR putchar() getchar()
  16.  
  17. //getc(FILE*) putc(char,FILE*)
  18. FILE *in;
  19. FILE *out;
  20. in = fopen("test.txt","r");
  21. if(in==NULL){return;}
  22. out = fopen("test.txt","w");
  23. if(out==NULL){ return;}
  24. int ch;
  25. while((ch=getc(in))!=EOF) // COPY DATA PER CHAR
  26. {
  27. putc(ch,out);
  28. }

宏定义:

  1. #include <stdio.h>
  2. #define PSQR(x) printf("Value squre of "#x" is %d \n",(x*x));
  3. #define XNAME(n) x##n
  4.  
  5. #define SIZE "HOUDINI"
  6.  
  7. #ifdef SIZE // if define the SIZE
  8. #define SIZE "MAYA"
  9. #endif
  10.  
  11. #ifdef __GNUC__ //if define the __GUNC__
  12. #define HOUDINI 2
  13. #else
  14. #define HOUDINI 3
  15. #endif
  16.  
  17. #ifndef __SIZEOFINT__ // becuase __SIZEOFINT__ do not define,that "if not define the __SIZEOFINT__"
  18. #define __SIZEOFINT__ sizeof(int) // so can arrive this code
  19. #endif
  20.  
  21. #define NOFLOAT 1
  22.  
  23. #if NOFLOAT == 1 //check NOFLOAT equal 1,then #include<float.h>
  24. #include <float.h>
  25. #elif NOFLOAT == 2
  26. #include <typeinfo.h>
  27. #elif NOFLOAT == 3
  28. #include <typeinfo.h>
  29. #else
  30. #include <stdarg.h>
  31. #endif
  32.  
  33. #if defined(NOFLOAT) // if define NOFLOAT ------ same as #ifdef NOFLOAT
  34. #define A 2
  35. #endif
  36.  
  37. int main(int argc, char *argv[])
  38. {
  39. printf("Hello World!\n");
  40. PSQR(*);
  41.  
  42. char name[];
  43. fprintf(stdout,"enter the name\n");
  44. fgets(name,,stdin);
  45.  
  46. int i=;
  47.  
  48. return ;
  49. }

定义一个简单的宏:

  1. #define GARRSERT(EXP) \
  2. {\
  3. if(EXP != )\
  4. {\
  5. printf("Assert expression "#EXP " ERROR %s IN %d line \n",__FILE__,__LINE__);\
  6. exit();\
  7. }\
  8. }

GARRSERT(3>5);

strycpy strlen

  1. #include <stdio.h>
  2. #include <stddef.h>
  3. void gstrcpy(char *dst,char const *str);
  4. void gstrcpy2(char *dst, char const *str);
  5. size_t gstrlen(char *str);
  6. int main()
  7. {
  8. char *src = "houdini";
  9. char dst[]={'\0'};
  10. gstrcpy(dst,src);
  11. printf("get final str %s \n" , dst);
  12. return ;
  13. }
  14.  
  15. void gstrcpy(char *dst, char const *str)
  16. {
  17. while( )
  18. {
  19. *dst++ = *str++;
  20. if(*dst != '\0')
  21. break;
  22. }
  23. }
  24. void gstrcpy2(char *dst, char const *str)
  25. {
  26. while((*dst++ = *str++) !='\0');
  27. }
  28. size_t gstrlen(char *str)
  29. {
  30. int length;
  31. for(length=;*str++ != '\0';length+=);
  32. return length;
  33. }

3,C++ 标准输入cin, getline(),cin.get() ;

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. #define FLUSH_BUFFER()\
  6. while(cin.get()!='\n') \
  7. {\
  8. continue;\
  9. }\
  10.  
  11. int main()
  12. {
  13.  
  14. char info[];
  15.  
  16. cout << "\n**use cin>>info\n";
  17. cin >> info; // read a word,and leave other to the buffer ,include '\n',but no whitespace
  18. cout << "==use cin>>info: " <<info << " It's Length: " <<strlen(info)<<endl;
  19.  
  20. FLUSH_BUFFER();
  21.  
  22. cout << "\n**cin.getline(info,100)\n";
  23. cin.getline(info,); // read a line,discard '\n'
  24. cout << "==use cin.getline(info,100):" << info <<endl;
  25.  
  26. cout << "\n**cin.get(info,100)\n";
  27. cin.get(info, ); // read a line ,but leave the '\n' to buffer
  28. cout << "==use cin.get(info,100): " << info << std::endl;
  29.  
  30. FLUSH_BUFFER();
  31.  
  32. cout<< "\n**next use the String\n";
  33. string stuff;
  34. cin>>stuff; //read a word ,leave \n,but no whitespace
  35. cout << "==use cin>>stuff: " << stuff << std::endl;
  36.  
  37. FLUSH_BUFFER();
  38.  
  39. cout<< "\n**next use the getline(cin.stuff)\n";
  40. getline(cin,stuff); // read a line,discard '\n'
  41. cout << "==use getline(cin,stuff) " << stuff << std::endl;
  42.  
  43. char fname[];
  44. string lname;
  45. cin >> fname; // if input size > 9 , could be a problem
  46. cin >> lname; // can read long long word
  47.  
  48. // C++ Style :
  49. operator>>(cin,fname);
  50.  
  51. return ;
  52. }

4,

(1)字符串分析,函数指针)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <omp.h>
  6. #include <sstream>
  7. #include <string.h>
  8. #include <algorithm>
  9. using namespace std;
  10.  
  11. namespace PARSE_STRING
  12. {
  13.  
  14. inline int start_with(string &input,string startwith)
  15. {
  16. if(startwith.size()>input.size())
  17. {
  18. return ;
  19. }
  20.  
  21. int state = ;
  22. for(int i=;i<startwith.size();i++)
  23. {
  24. if(input[i]!=startwith[i])
  25. {
  26. state = -;
  27. }
  28. }
  29. if(state==)
  30. {
  31. return ;
  32. }
  33. }
  34.  
  35. inline string int_to_str(int value)
  36. {
  37. char str[];
  38. sprintf(str,"%d",value);
  39. string final(str);
  40. return final;
  41. }
  42.  
  43. inline string str_to_pad(string str,int pad)
  44. {
  45. string prefix="";
  46. string cnt_str = prefix + str;
  47. string final = cnt_str.substr(cnt_str.size()-pad,cnt_str.size());
  48. return final;
  49. }
  50. }
  51. namespace GLY_FUNCTION_POINTER
  52. {
  53. void test()
  54. {
  55. cout << "test with no arguments\n";
  56. }
  57. void test2(int a)
  58. {
  59. cout << "test with one arguments "<< a <<"\n";
  60. }
  61. void* test3()
  62. {
  63. cout << "test with no argumetens and return pointer"<<"\n";
  64. int a =;
  65. return (void*)&a;
  66. }
  67. void *test4(int b)
  68. {
  69. cout <<"test with one arguments and return pointer" << "\n";
  70. return (void*)&b;
  71. }
  72.  
  73. void test_pointer_func()
  74. {
  75. typedef void (*pf)();
  76. pf _func_01 = test;
  77. _func_01();
  78.  
  79. typedef void (*pf_one)(int);
  80. pf_one _func_02 = test2;
  81. _func_02();
  82.  
  83. typedef void * (*pf_sce)();
  84. pf_sce _func_03 = test3;
  85. cout << *((int*)_func_03()) << endl;
  86.  
  87. typedef void * (*pf_sce_one)(int);
  88. pf_sce_one _func_04 = test4;
  89. cout << *((int*)_func_04()) <<endl;
  90. }
  91.  
  92. }
  93.  
  94. void heap_test()
  95. {
  96. int ia[]={,,,,,,,,};
  97. vector<int> ivec(ia,ia+);
  98. for(int i=;i<ivec.size();i++)
  99. {
  100. cout << "i :" << i << " -> value:" << ivec[i] << endl;
  101. }
  102. make_heap(ivec.begin(),ivec.end());
  103. cout << "----\n";
  104. for(int i=;i<ivec.size();i++)
  105. {
  106. cout << "i :" << i << " -> value:" << ivec[i] << endl;
  107. }
  108. cout << "----\n";
  109. pop_heap(ivec.begin(),ivec.end());
  110. for(int i=;i<ivec.size();i++)
  111. {
  112. cout << "i :" << i << " -> value:" << ivec[i] << endl;
  113. }
  114. }
  115.  
  116. void convert_data()
  117. {
  118.  
  119. int a = ;
  120. void *data = (void*)&a;
  121. int b = *(int *)data;
  122. cout << b <<endl;
  123.  
  124. }
  125.  
  126. void reverse_the_container()
  127. {
  128. vector <int> aaa;
  129. aaa.push_back();
  130. aaa.push_back();
  131. aaa.push_back();
  132.  
  133. //std::reverse(aaa.begin(),aaa.end()); // this is use the algorithm.h ,aaa will changed
  134. //cout << aaa[0] <<endl;
  135.  
  136. for(vector<int>::const_reverse_iterator iter=aaa.rbegin();iter!=aaa.rend();++iter) // aaa do not change
  137. cout<<*iter<<endl;
  138.  
  139. }
  140. void transfer_data(void *data,int length)
  141. {
  142.  
  143. for(int i=;i<length;i++)
  144. {
  145. cout << ((int*)data)[i] << endl;
  146. }
  147. }
  148.  
  149. struct myarray
  150. {
  151. vector <int> data;
  152. };
  153.  
  154. void transfer_struct(void *data)
  155. {
  156. cout<<"transfer_struct "<<data<<endl;
  157. myarray rh_array = *(myarray*)data;
  158. for(int i=;i<rh_array.data.size();i++)
  159. {
  160. cout<< rh_array.data[i] << endl;
  161. }
  162. }
  163.  
  164. template <typename T>
  165. struct gly_array
  166. {
  167. vector <T> data_array;
  168. };
  169.  
  170. template <typename T>
  171. void transfer_struct_template(gly_array<T> rh_array)
  172. {
  173. for(int i=;i<rh_array.data_array.size();i++)
  174. {
  175. cout<< rh_array.data_array[i] << endl;
  176. }
  177. }
  178.  
  179. int main()
  180. {
  181. myarray _array;
  182. _array.data.push_back();
  183. _array.data.push_back();
  184. cout<< "main "<<&_array<<endl;
  185. transfer_struct((void*)&_array);
  186.  
  187. cout<< "template ";
  188. gly_array<int> int_array;
  189. int_array.data_array.push_back();
  190. int_array.data_array.push_back();
  191. int_array.data_array.push_back();
  192. cout << &int_array<<endl;
  193. transfer_struct_template(int_array);
  194.  
  195. }

(2)point

  1. namespace test_struct_define
  2. {
  3. struct Point3d
  4. {
  5. float x;
  6. float y;
  7. float z;
  8. };
  9.  
  10. #define XSET(P,xval,yval,zval)\
  11. {\
  12. P.x=xval;\
  13. P.y=yval;\
  14. P.z=zval;\
  15. }
  16.  
  17. inline ostream& operator <<(ostream &os,const Point3d &pt)
  18. {
  19. os << "X:VALUE->"<<pt.x << " Y:VALUE->"<< pt.y << " Z:VALUE->" <<pt.z<<endl;
  20. return os;
  21. }
  22.  
  23. int main()
  24. {
  25. Point3d pt;
  26. XSET(pt,,,);
  27. cout<< pt <<endl;
  28. }
  29.  
  30. }
  31.  
  32. template <typename T>
  33. class GLY_POINT_3d
  34. {
  35. public:
  36. GLY_POINT_3d(T x=0.0,T y=0.0,T z=0.0):_x(x),_y(y),_z(z)
  37. {
  38. }
  39.  
  40. T x() const
  41. {
  42. return _x;
  43. }
  44. T y() const
  45. {
  46. return _y;
  47. }
  48. T z() const
  49. {
  50. return _z;
  51. }
  52.  
  53. friend ostream&operator<<(ostream &os,const GLY_POINT_3d <T> &pt)
  54. {
  55. os<< pt.x()<< " "<< pt.y()<<" " << pt.z();
  56. return os;
  57. }
  58.  
  59. T &operator [](int index)
  60. {
  61. assert(index<);
  62. if(index==)
  63. {
  64. return _x;
  65. }
  66. if(index==)
  67. {
  68. return _y;
  69. }
  70. if(index==)
  71. {
  72. return _z;
  73. }
  74. }
  75. private:
  76. T _x;
  77. T _y;
  78. T _z;
  79. };
  80. int main()
  81. {
  82. GLY_POINT_3d <int> pt(,,);
  83. cout << pt<<endl;
  84. cout << pt[] <<endl;
  85. cout << pt[] <<endl;
  86. cout << pt[] <<endl;
  87.  
  88. }

C 和 C++ 一些基础的更多相关文章

  1. java基础集合经典训练题

    第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...

  2. node-webkit 环境搭建与基础demo

    首先去github上面下载(地址),具体更具自己的系统,我的是windows,这里只给出windows的做法 下载windows x64版本 下载之后解压,得到以下东西 为了方便,我们直接在这个目录中 ...

  3. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  4. Golang, 以17个简短代码片段,切底弄懂 channel 基础

    (原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...

  5. [C#] C# 基础回顾 - 匿名方法

    C# 基础回顾 - 匿名方法 目录 简介 匿名方法的参数使用范围 委托示例 简介 在 C# 2.0 之前的版本中,我们创建委托的唯一形式 -- 命名方法. 而 C# 2.0 -- 引进了匿名方法,在 ...

  6. HTTPS 互联网世界的安全基础

    近一年公司在努力推进全站的 HTTPS 化,作为负责应用系统的我们,在配合这个趋势的过程中,顺便也就想去搞清楚 HTTP 后面的这个 S 到底是个什么含义?有什么作用?带来了哪些影响?毕竟以前也就只是 ...

  7. Swift与C#的基础语法比较

    背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...

  8. .NetCore MVC中的路由(1)路由配置基础

    .NetCore MVC中的路由(1)路由配置基础 0x00 路由在MVC中起到的作用 前段时间一直忙于别的事情,终于搞定了继续学习.NetCore.这次学习的主题是MVC中的路由.路由是所有MVC框 ...

  9. .NET基础拾遗(5)多线程开发基础

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理基础 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开 ...

  10. .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]

    方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...

随机推荐

  1. python自动化开发-[第十三天]-javascript

    今日概要 1.javascript简单语法 1.javascript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名S ...

  2. centos6.7不联网的情况下安装配置本地yum源

    1  cd / 2 mkdir -p /app/ios 3  cd /opt     mkdir ios 4   把下载好的centos-6.7-x86_64-bin-dvd1.iso  上传到 /o ...

  3. flask session

    flask session工作机制: 把敏感数据经过加密后放入到‘session’中,然后在把'session'存放到cookie中,下次请求的时候,再从浏览器发送过来的cookie中读取sessio ...

  4. Hadoop问题:Input path does not exist: hdfs://Master:9000/user/hadoop/input

    问题描述: org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input path does not exist: hdfs:/ ...

  5. Jquery 添加插件

    原文:http://www.iteye.com/topic/545971 jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法 ...

  6. [Android] Android RxBus 用法学习总结

    事件总线的好处在于方便组件之间的交互,RxBus不是一个库,而是使用RxJava实现事件总线的一种思想. rxbus和eventbus相比较: RxJava 主要做异步.网络的数据处理,强大之处就是对 ...

  7. 配置tomcat限制指定IP地址访问后端应用

    1. 场景后端存在N个tomcat实例,前端通过nginx反向代理和负载均衡. tomcat1      tomcatN         |                 |        |    ...

  8. Android RelativeLayout属性含义

    a).第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android ...

  9. build script和all projects作用和区别

    buildscript中的声明是gradle脚本自身需要使用的资源.可以声明的资源包括依赖项.第三方插件.maven仓库地址等.而在build.gradle文件中直接声明的依赖项.仓库地址等信息是项目 ...

  10. C# UpdatePanel加载完毕回调JS

    如果 我们想UpdatePanel加载完成后做一些事情 需要使用js <script type="text/javascript"> //给ScriptManager的 ...