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; }
上一篇:【NOIP模拟题】【二分】【倍增】【链表】【树规】


下一篇:mac忘记密码的解决办法