C 和 C++ 一些基础
位运算:
Part1:
- #include <iostream>
- using namespace std;
- int main(int argc, char *argv[])
- {
- //unsigned char per byte that contain 00000000 - 11111111
- unsigned char a=; // bin->0000 0010
- unsigned char b = ~a; //~(0000 0010) result:(1111 1101) or 253
- printf("%d\n",b);
- unsigned char c=; //0000 0011
- unsigned char d=; //0000 0010
- printf(" & %d\n",c&d); // c&d -> 0000 0010 : 2
- printf(" | %d\n",c|d); // c|d -> 0000 0011 : 3
- //十六进制->十进制 hexadecimal(hex)->base10
- int d_hex = 0xA3F;
- //A-..-F (10-15)
- //0xA3F -> A3F -> 10*16^2 + 3*16^1 + 15*16^0 = 2623
- printf("0xA3F : %d\n",d_hex); //
- int d_hex_zero = 0x000; //0*16^2 + 0*16^1 + 0*16^0 = 0
- printf("0x000 : %d\n",d_hex_zero);
- int d_hex_1 = 0x001; //0*16^2 + 0*16^1 + 1*16^0 = 1
- printf("0x001 : %d\n",d_hex_1);
- int d_hex_2 = 0x011; //0*16^2 + 1*16^1 + 1*16^0 = 17
- printf("0x001 : %d\n",d_hex_2);
- return ;
- }
Part2:
- #include <iostream>
- using namespace std;
- int main()
- {
- // int is 32 bit
- int a = ; //00000000 00000000 00000000 00001010 0*2^0 + 1*2^1 + 0*2^2 + 1*2^3 = 10
- int b = ; //00000000 00000000 00000000 00000101 1*2^0 + 0*2^1 + 1*2^2 + 0*2^3 = 5
- int c = a | b ; // 00000000 00000000 00000000 00001111 1+2+4+8 = 15
- cout << c <<endl;
- int d = c|0xFF; //hex: 0xFF = 15*16^1 + 15*16^0 = 255 bin:255-> 1111 1111
- cout << d <<endl;
- // ^
- int e = a ^ b; //00000000 00000000 00000000 00001111 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 = 15
- cout << e <<endl;
- // swap a b
- // a 00000000 00000000 00000000 00001010
- // b 00000000 00000000 00000000 00000101
- a = a ^ b ; // a->00000000 00000000 00000000 00001111
- b = b ^ a ; // b->00000000 00000000 00000000 00001010
- a = a ^ b ; // 00000000 00000000 00000000 00000101
- cout << "after swap a,b a:" << a << "\t" << "b:"<<b <<endl;
- return ;
- }
C语言的几个输入输出函数
#include <stdio.h>
getchar(),putchar()
scanf(),printf()
1->getchar()与scanf()唯一的区别是getchar()不会跳过'\n'或者空格,例如如下
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int ch;
- int a,b;
- printf("enter a char ,enter to quit\n");
- while((ch=getchar()) != '\n' )
- {
- if (scanf("%d %d",&a,&b)!=)
- break;
- printf("you enter the %c %d %d\n",ch,a,b);
- // getchar();//the ch a b enter complete will leave a '\n'
- while(getchar() != '\n') // GNU BOOK always use this
- {
- continue;
- }
- printf("PLEASE ENTER ANOTHER CHAR \n");
- }
- exit();
- }
如果没有while循环中的while(getchar() != '\n') ,那么程序输入一轮直接结束,原因是scanf()会把回车 放到输入队列。所以要剔除输入的回车
2->如何确定输入类型
scanf()返回值是其成功读入的项目个数。比如scanf("%d %d",&a,&b)==2
如下面代码,如果输入q,则state=0
- int main()
- {
- int input;
- int state = scanf("%d",&input);
- printf("state = %d,and input value is %d,",state,input);
- exit();
- }
输入输出大集合:
- char temp[];
- printf("Please enter your first name\n");
- // STDIN
- //scanf("%s",temp); // jump backspace
- //fscanf(stdin,"%s",temp); // jump backspace
- fgets(temp,,stdin); // do not jump backspace
- //gets(temp); // do not jump backspace
- // STDOUT
- //printf("You enter the strings -> : %s\n",temp);
- //fprintf(stdout,"You enter the strings -> : %s\n" ,temp);
- //fputs(temp,stdout);
- puts(temp);
- // CHAR putchar() getchar()
- //getc(FILE*) putc(char,FILE*)
- FILE *in;
- FILE *out;
- in = fopen("test.txt","r");
- if(in==NULL){return;}
- out = fopen("test.txt","w");
- if(out==NULL){ return;}
- int ch;
- while((ch=getc(in))!=EOF) // COPY DATA PER CHAR
- {
- putc(ch,out);
- }
宏定义:
- #include <stdio.h>
- #define PSQR(x) printf("Value squre of "#x" is %d \n",(x*x));
- #define XNAME(n) x##n
- #define SIZE "HOUDINI"
- #ifdef SIZE // if define the SIZE
- #define SIZE "MAYA"
- #endif
- #ifdef __GNUC__ //if define the __GUNC__
- #define HOUDINI 2
- #else
- #define HOUDINI 3
- #endif
- #ifndef __SIZEOFINT__ // becuase __SIZEOFINT__ do not define,that "if not define the __SIZEOFINT__"
- #define __SIZEOFINT__ sizeof(int) // so can arrive this code
- #endif
- #define NOFLOAT 1
- #if NOFLOAT == 1 //check NOFLOAT equal 1,then #include<float.h>
- #include <float.h>
- #elif NOFLOAT == 2
- #include <typeinfo.h>
- #elif NOFLOAT == 3
- #include <typeinfo.h>
- #else
- #include <stdarg.h>
- #endif
- #if defined(NOFLOAT) // if define NOFLOAT ------ same as #ifdef NOFLOAT
- #define A 2
- #endif
- int main(int argc, char *argv[])
- {
- printf("Hello World!\n");
- PSQR(*);
- char name[];
- fprintf(stdout,"enter the name\n");
- fgets(name,,stdin);
- int i=;
- return ;
- }
定义一个简单的宏:
- #define GARRSERT(EXP) \
- {\
- if(EXP != )\
- {\
- printf("Assert expression "#EXP " ERROR %s IN %d line \n",__FILE__,__LINE__);\
- exit();\
- }\
- }
GARRSERT(3>5);
strycpy strlen
- #include <stdio.h>
- #include <stddef.h>
- void gstrcpy(char *dst,char const *str);
- void gstrcpy2(char *dst, char const *str);
- size_t gstrlen(char *str);
- int main()
- {
- char *src = "houdini";
- char dst[]={'\0'};
- gstrcpy(dst,src);
- printf("get final str %s \n" , dst);
- return ;
- }
- void gstrcpy(char *dst, char const *str)
- {
- while( )
- {
- *dst++ = *str++;
- if(*dst != '\0')
- break;
- }
- }
- void gstrcpy2(char *dst, char const *str)
- {
- while((*dst++ = *str++) !='\0');
- }
- size_t gstrlen(char *str)
- {
- int length;
- for(length=;*str++ != '\0';length+=);
- return length;
- }
3,C++ 标准输入cin, getline(),cin.get() ;
- #include <iostream>
- #include <cstring>
- using namespace std;
- #define FLUSH_BUFFER()\
- while(cin.get()!='\n') \
- {\
- continue;\
- }\
- int main()
- {
- char info[];
- cout << "\n**use cin>>info\n";
- cin >> info; // read a word,and leave other to the buffer ,include '\n',but no whitespace
- cout << "==use cin>>info: " <<info << " It's Length: " <<strlen(info)<<endl;
- FLUSH_BUFFER();
- cout << "\n**cin.getline(info,100)\n";
- cin.getline(info,); // read a line,discard '\n'
- cout << "==use cin.getline(info,100):" << info <<endl;
- cout << "\n**cin.get(info,100)\n";
- cin.get(info, ); // read a line ,but leave the '\n' to buffer
- cout << "==use cin.get(info,100): " << info << std::endl;
- FLUSH_BUFFER();
- cout<< "\n**next use the String\n";
- string stuff;
- cin>>stuff; //read a word ,leave \n,but no whitespace
- cout << "==use cin>>stuff: " << stuff << std::endl;
- FLUSH_BUFFER();
- cout<< "\n**next use the getline(cin.stuff)\n";
- getline(cin,stuff); // read a line,discard '\n'
- cout << "==use getline(cin,stuff) " << stuff << std::endl;
- char fname[];
- string lname;
- cin >> fname; // if input size > 9 , could be a problem
- cin >> lname; // can read long long word
- // C++ Style :
- operator>>(cin,fname);
- return ;
- }
4,
(1)字符串分析,函数指针)
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <vector>
- #include <omp.h>
- #include <sstream>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- namespace PARSE_STRING
- {
- inline int start_with(string &input,string startwith)
- {
- if(startwith.size()>input.size())
- {
- return ;
- }
- int state = ;
- for(int i=;i<startwith.size();i++)
- {
- if(input[i]!=startwith[i])
- {
- state = -;
- }
- }
- if(state==)
- {
- return ;
- }
- }
- inline string int_to_str(int value)
- {
- char str[];
- sprintf(str,"%d",value);
- string final(str);
- return final;
- }
- inline string str_to_pad(string str,int pad)
- {
- string prefix="";
- string cnt_str = prefix + str;
- string final = cnt_str.substr(cnt_str.size()-pad,cnt_str.size());
- return final;
- }
- }
- namespace GLY_FUNCTION_POINTER
- {
- void test()
- {
- cout << "test with no arguments\n";
- }
- void test2(int a)
- {
- cout << "test with one arguments "<< a <<"\n";
- }
- void* test3()
- {
- cout << "test with no argumetens and return pointer"<<"\n";
- int a =;
- return (void*)&a;
- }
- void *test4(int b)
- {
- cout <<"test with one arguments and return pointer" << "\n";
- return (void*)&b;
- }
- void test_pointer_func()
- {
- typedef void (*pf)();
- pf _func_01 = test;
- _func_01();
- typedef void (*pf_one)(int);
- pf_one _func_02 = test2;
- _func_02();
- typedef void * (*pf_sce)();
- pf_sce _func_03 = test3;
- cout << *((int*)_func_03()) << endl;
- typedef void * (*pf_sce_one)(int);
- pf_sce_one _func_04 = test4;
- cout << *((int*)_func_04()) <<endl;
- }
- }
- void heap_test()
- {
- int ia[]={,,,,,,,,};
- vector<int> ivec(ia,ia+);
- for(int i=;i<ivec.size();i++)
- {
- cout << "i :" << i << " -> value:" << ivec[i] << endl;
- }
- make_heap(ivec.begin(),ivec.end());
- cout << "----\n";
- for(int i=;i<ivec.size();i++)
- {
- cout << "i :" << i << " -> value:" << ivec[i] << endl;
- }
- cout << "----\n";
- pop_heap(ivec.begin(),ivec.end());
- for(int i=;i<ivec.size();i++)
- {
- cout << "i :" << i << " -> value:" << ivec[i] << endl;
- }
- }
- void convert_data()
- {
- int a = ;
- void *data = (void*)&a;
- int b = *(int *)data;
- cout << b <<endl;
- }
- void reverse_the_container()
- {
- vector <int> aaa;
- aaa.push_back();
- aaa.push_back();
- aaa.push_back();
- //std::reverse(aaa.begin(),aaa.end()); // this is use the algorithm.h ,aaa will changed
- //cout << aaa[0] <<endl;
- for(vector<int>::const_reverse_iterator iter=aaa.rbegin();iter!=aaa.rend();++iter) // aaa do not change
- cout<<*iter<<endl;
- }
- void transfer_data(void *data,int length)
- {
- for(int i=;i<length;i++)
- {
- cout << ((int*)data)[i] << endl;
- }
- }
- struct myarray
- {
- vector <int> data;
- };
- void transfer_struct(void *data)
- {
- cout<<"transfer_struct "<<data<<endl;
- myarray rh_array = *(myarray*)data;
- for(int i=;i<rh_array.data.size();i++)
- {
- cout<< rh_array.data[i] << endl;
- }
- }
- template <typename T>
- struct gly_array
- {
- vector <T> data_array;
- };
- template <typename T>
- void transfer_struct_template(gly_array<T> rh_array)
- {
- for(int i=;i<rh_array.data_array.size();i++)
- {
- cout<< rh_array.data_array[i] << endl;
- }
- }
- int main()
- {
- myarray _array;
- _array.data.push_back();
- _array.data.push_back();
- cout<< "main "<<&_array<<endl;
- transfer_struct((void*)&_array);
- cout<< "template ";
- gly_array<int> int_array;
- int_array.data_array.push_back();
- int_array.data_array.push_back();
- int_array.data_array.push_back();
- cout << &int_array<<endl;
- transfer_struct_template(int_array);
- }
(2)point
- namespace test_struct_define
- {
- struct Point3d
- {
- float x;
- float y;
- float z;
- };
- #define XSET(P,xval,yval,zval)\
- {\
- P.x=xval;\
- P.y=yval;\
- P.z=zval;\
- }
- inline ostream& operator <<(ostream &os,const Point3d &pt)
- {
- os << "X:VALUE->"<<pt.x << " Y:VALUE->"<< pt.y << " Z:VALUE->" <<pt.z<<endl;
- return os;
- }
- int main()
- {
- Point3d pt;
- XSET(pt,,,);
- cout<< pt <<endl;
- }
- }
- template <typename T>
- class GLY_POINT_3d
- {
- public:
- GLY_POINT_3d(T x=0.0,T y=0.0,T z=0.0):_x(x),_y(y),_z(z)
- {
- }
- T x() const
- {
- return _x;
- }
- T y() const
- {
- return _y;
- }
- T z() const
- {
- return _z;
- }
- friend ostream&operator<<(ostream &os,const GLY_POINT_3d <T> &pt)
- {
- os<< pt.x()<< " "<< pt.y()<<" " << pt.z();
- return os;
- }
- T &operator [](int index)
- {
- assert(index<);
- if(index==)
- {
- return _x;
- }
- if(index==)
- {
- return _y;
- }
- if(index==)
- {
- return _z;
- }
- }
- private:
- T _x;
- T _y;
- T _z;
- };
- int main()
- {
- GLY_POINT_3d <int> pt(,,);
- cout << pt<<endl;
- cout << pt[] <<endl;
- cout << pt[] <<endl;
- cout << pt[] <<endl;
- }
C 和 C++ 一些基础的更多相关文章
- java基础集合经典训练题
第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...
- node-webkit 环境搭建与基础demo
首先去github上面下载(地址),具体更具自己的系统,我的是windows,这里只给出windows的做法 下载windows x64版本 下载之后解压,得到以下东西 为了方便,我们直接在这个目录中 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- Golang, 以17个简短代码片段,切底弄懂 channel 基础
(原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...
- [C#] C# 基础回顾 - 匿名方法
C# 基础回顾 - 匿名方法 目录 简介 匿名方法的参数使用范围 委托示例 简介 在 C# 2.0 之前的版本中,我们创建委托的唯一形式 -- 命名方法. 而 C# 2.0 -- 引进了匿名方法,在 ...
- HTTPS 互联网世界的安全基础
近一年公司在努力推进全站的 HTTPS 化,作为负责应用系统的我们,在配合这个趋势的过程中,顺便也就想去搞清楚 HTTP 后面的这个 S 到底是个什么含义?有什么作用?带来了哪些影响?毕竟以前也就只是 ...
- Swift与C#的基础语法比较
背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...
- .NetCore MVC中的路由(1)路由配置基础
.NetCore MVC中的路由(1)路由配置基础 0x00 路由在MVC中起到的作用 前段时间一直忙于别的事情,终于搞定了继续学习.NetCore.这次学习的主题是MVC中的路由.路由是所有MVC框 ...
- .NET基础拾遗(5)多线程开发基础
Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理基础 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开 ...
- .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]
方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...
随机推荐
- python自动化开发-[第十三天]-javascript
今日概要 1.javascript简单语法 1.javascript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名S ...
- 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 ...
- flask session
flask session工作机制: 把敏感数据经过加密后放入到‘session’中,然后在把'session'存放到cookie中,下次请求的时候,再从浏览器发送过来的cookie中读取sessio ...
- 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:/ ...
- Jquery 添加插件
原文:http://www.iteye.com/topic/545971 jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法 ...
- [Android] Android RxBus 用法学习总结
事件总线的好处在于方便组件之间的交互,RxBus不是一个库,而是使用RxJava实现事件总线的一种思想. rxbus和eventbus相比较: RxJava 主要做异步.网络的数据处理,强大之处就是对 ...
- 配置tomcat限制指定IP地址访问后端应用
1. 场景后端存在N个tomcat实例,前端通过nginx反向代理和负载均衡. tomcat1 tomcatN | | | ...
- Android RelativeLayout属性含义
a).第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android ...
- build script和all projects作用和区别
buildscript中的声明是gradle脚本自身需要使用的资源.可以声明的资源包括依赖项.第三方插件.maven仓库地址等.而在build.gradle文件中直接声明的依赖项.仓库地址等信息是项目 ...
- C# UpdatePanel加载完毕回调JS
如果 我们想UpdatePanel加载完成后做一些事情 需要使用js <script type="text/javascript"> //给ScriptManager的 ...