c/c++学习01

F:/汇编/SG_CC++/p1.c
#include <stdio.h>
void main(){
	int a,b;
	int *p1,*p2;
	a=52;
	b=67;
	p1=&a;
	p2=&b;
	printf("%d",a);
	printf("%d",b);
	printf("%d",*p1);
	printf("%d",*p2);
}
/*
3:        int a,b;
4:        int *p1,*p2;
5:        a=52;
0040D728   mov         dword ptr [ebp-4],34h	//直接将52 mov 到 变量a对应的内存单元(4个字节)
6:        b=67;
0040D72F   mov         dword ptr [ebp-8],43h	//直接将67 mov 到 变量b对应的内存单元(4个字节)
7:        p1=&a;
0040D736   lea         eax,[ebp-4]		//先将变量a  lea即取其地址,放入eax
0040D739   mov         dword ptr [ebp-0Ch],eax	//再将eax即地址 mov到 变量p1对应的内存单元存放
8:        p2=&b;
0040D73C   lea         ecx,[ebp-8]		//先将变量b  lea即取其地址,放入ecx
0040D73F   mov         dword ptr [ebp-10h],ecx	//再将ecx即地址 mov到 变量p2对应的内存单元存放
9:        printf("%d",a);
0040D742   mov         edx,dword ptr [ebp-4]	
0040D745   push        edx					//取变量a的内存单元4个字节的值mov给了edx后,直接入栈了
0040D746   push        offset string "%d" (0042201c)
0040D74B   call        printf (00401080)
0040D750   add         esp,8
10:       printf("%d",b);
0040D753   mov         eax,dword ptr [ebp-8]
0040D756   push        eax	//取变量a的内存单元4个字节的值mov给了eax后,也直接入栈了
0040D757   push        offset string "%d" (0042201c)
0040D75C   call        printf (00401080)
0040D761   add         esp,8
11:       printf("%d",*p1);
0040D764   mov         ecx,dword ptr [ebp-0Ch]	//先取变量p1的内存单元4个字节的值mov给了ecx
0040D767   mov         edx,dword ptr [ecx]		//再从ecx存放的内存地址对应的内存单元取出4个字节,放到edx,最后才入栈
0040D769   push        edx
0040D76A   push        offset string "%d" (0042201c)
0040D76F   call        printf (00401080)
0040D774   add         esp,8		//调用者自己平衡栈
12:       printf("%d",*p2);
0040D777   mov         eax,dword ptr [ebp-10h]	//先从变量p2对应的内存单元取4个字节的值mov给了eax
0040D77A   mov         ecx,dword ptr [eax]		//再从eax存放的内存地址对应的内存单元取出4个字节,放到ecx,最后才入栈
0040D77C   push        ecx
0040D77D   push        offset string "%d" (0042201c)
0040D782   call        printf (00401080)
0040D787   add         esp,8
13:   }

*/

F:/汇编/SG_CC++/2.c
#include <stdio.h>
//对一个数组求和

//C函数声明,分号结尾
//int sumArr(int arr[],int len);
int sumArr(int *arr,int len);
int main(){
	int arr[]={5,2,6,7};
	int len=sizeof(arr)/sizeof(arr[0]);
	printf("sum is :%d\n",sumArr(arr,len));
	//结果是16
	printf("数组arr  size is :%d\n",sizeof(arr));
	return 0;
}

//int sumArr(int arr[],int len){
int sumArr(int *arr,int len){
	

	int sum=0;
	int i;
	//结果是4
	printf("数组首地址  size is :%d\n",sizeof(arr));
	for(i=0;i<len;i++){
		//sum+=arr[i];
		sum+=*arr++;  //指针每次移动四个字节 1个单位

	}
	return sum;
}

F:/汇编/SG_CC++/3.cpp
#include <iostream>
using namespace std;
//C++风格的数组求和
int sumArr(int *arr,int len);
int main(){
	int arr[]={5,2,6,7};
	int len=sizeof(arr)/sizeof(arr[0]);
	cout<<"sum is :"<<sumArr(arr,len)<<endl;
	return 0;
}
int sumArr(int *arr,int len){
	int sum=0;
	int i;
	for(i=0;i<len;i++){
		sum+=*arr++;
	}
	if(i==3){

	}
	return sum;
}

F:/汇编/SG_CC++/4.c
#include <stdio.h>
#include <stdlib.h>
/*
int main(){
	int i;
	int sum=0;
	char ch;
	while(0){		
	}
	for(i=0;i<XX;i++){
	}
	
	printf("sum is :%d\n",sum);
	system("pause");
	return 0;
}
*/

//C版本文件复制
int main(int argc,char *argv[]){
	FILE *in,*out;
	int ch;//因为getc(in)返回int类型
	if(argc!=3){
		fprintf(stderr,"sample:copyFile 1,doc 2.doc");
		exit(EXIT_FAILURE);
	}
	if((in=fopen(argv[1],"rb"))==NULL){
		fprintf(stderr,"open sourceFile:%s failure!",argv[1]);
		exit(EXIT_FAILURE);
	}
	if((out=fopen(argv[2],"wb"))==NULL){
		fprintf(stderr,"open destFile:%s failure!",argv[2]);
		exit(EXIT_FAILURE);
	}
	//健壮性完毕,这是开始文件copy,EOF原因:1读取错误,2文件结尾
	while((ch=getc(in))!=EOF){
		if(putc(ch,out)==EOF){
				break;
		}
	}
	if(ferror(in)){
		fprintf(stderr,"open sourceFile:%s failure!",argv[1]);
	}
	if(ferror(out)){
		fprintf(stderr,"open destFile:%s failure!",argv[2]);
	}
	
	
	printf("one file success copy\n");
	//system("pause");
	return 0;
}


/*
//对一串数字和空格,求和
int main(){
	int i;
	int sum=0;
	char ch;
	while(scanf("%d",&i)){
		sum+=i;
		while((ch=getchar())==' ');
		if(ch=='\n'){
			break;//退出循环
		}
		ungetc(ch,stdin);
		
	}
	
	printf("sum is :%d\n",sum);
	system("pause");
	
	return 0;
}
*/

F:/汇编/SG_CC++/5.cpp
#include <fstream>	//包含IO
#include <iostream>

//#include <math.h>
using namespace std;
/*
int main(){
	int res=0;
	cout<<"输入\n";
	if(0){
	}
	for(i=0;i<XXX;i++){
	}
	while(0){
	}
	cout<<endl;
	return 0;
}
*/
int main(){
	int arr[5]={1,2,3,4,5};
	char chArr[5]={'a','b','c','d','e'};
	int *p1=&arr[0];
	char *p2=&chArr[0];
	cout<<*p1+1<<endl;//先解引用,再加1
	cout<<*(p1+1)<<endl;//地址+sizeof(int)*1之后,再解引用
	return 0;
	
	
	for(int i=0;i<5;i++){
		cout<<*p1<<" at address:"<<p1<<endl;
		p1++;
	}
	for(i=0;i<5;i++){
		//cout<<*p2<<" at address:"<<reinterpret_cast<unsigned long>(p2)<<endl;
		//cout<<*p2<<" at address:"<<reinterpret_cast<unsigned int>(p2)<<endl;
		cout<<*p2<<" at address:"<<p2<<endl;
		p2++;
	}
	
	cout<<endl;
	return 0;
}
/*
//C++版本打开并写入文件
int main(){
	//ofstream out;
	//out.open("sg2.txt");
	ofstream out("sg2.txt",ios::app|ios::out);
	if(!out){
		cerr<<"open source file failure"<<endl;
		return 0; 
	}
	for(int i=0;i<10;i++){
		out<<i;
	}
	out<<"\n";
	out.close();
	system("pause");
	return 0;
}
*/
/*
//C++版本读取文件,显示在控制台
int main(){
	//ifstream in;
	//in.open("sg.txt");
	//合起来可以是
	ifstream in("sg.txt");
	in.open("sg.txt");
	//这儿总是打开失败!
	if(!in){
		cerr<<"open source file failure"<<endl;
		return 0; 
	}
	char ch;
	while(in>>ch){
		cout<<ch;
	}
	
	cout<<endl;
	in.close();
	return 0;
}
*/
/*
int main(){
	int width=4;
	char str[20];
	cout<<"输入\n";
	cin.width(5);//每次提取4个,自动补1个\0
	while(cin>>str){
		cout.width(width++);
		cout<<str<<endl;
		cin.width(5);
	}

	cout<<endl;
	return 0;
}
*/
/*
int main(){
	
	double res=sqrt(3);
	cout<<"输根号3\n";
	for(int i=0;i<3;i++){
		cout<<"this is :"<<i<<endl;
		cout.precision(i);	//精度,小数点也占一位
		cout<<res<<endl;
	}
	
	cout<<endl;
	return 0;
}
*/

/*
int main(){
	const int LEN=50;
	char buf[LEN];
	cout<<"输入字符串,我会截断输出\n";
	//输入123456789
	cin.read(buf,6);//读取6个字符到缓冲区
	cout<<cin.gcount()<<endl;	//6
	cout.write(buf,3);//123
	cout<<endl;
	return 0;
}

*/

/*
int main(){
	char ch;
	cout<<"输入字符串,我按原样输出\n";
	while(cin.peek()!='\n'){
		ch=cin.get();//从输入缓冲区,剪切1个字符
		cout<<ch;
	}
	cout<<endl;
}
*/






/*
int main(){
	char buf[20];
	//输入123456
	cin.ignore(3);//忽略123
	cin.getline(buf,4); //取出4-1个放到缓冲区,因为结尾\0
	cout<<buf<<endl;
	return 0;
}
*/




/*
//C++风格的输入字符串和空格  求和
int main(){
	int i;
	int sum=0;
	//每次从输入缓冲区,提取一个整数到i
	while(cin>>i){
		sum+=i;
		cout<<"i is :"<<i<<endl;
		cout<<"sum is :"<<sum<<endl;

		while(cin.peek()==' '){
			//peek 跳出来,检查后右放回去
			cin.get();
			//get直接剪切出来了
		}

		if(cin.peek()=='\n'){
			break;//回车退出循环
		}
	}
	//cout<<"sum is :"<<endl;
	return 0;
}
*/

F:/汇编/SG_CC++/c1.cpp
#include <fstream>	//包含IO
#include <iostream>
#include <string>
//#include <math.h>
class B{
	public:
		std::string text,name;
		std::ofstream out;
		B();//构造函数
		~B();//析构函数
		void inputText();
		void inputName();
		bool save();
};//千万不要忘记分号
B::B(void){
	out.open("sg.txt",std::ios::trunc);
}
B::~B(){
	out.close();
}
void B::inputText(){
	std::cout<<"put in an sentence:"<<std::endl;
	std::getline(std::cin,text);
}
void B::inputName(){
	std::cout<<"put in an name:"<<std::endl;
	std::getline(std::cin,name);
}
bool B::save(){
	if(out.is_open()){
		out<<text<<"|"<<name<<"\n";
		return true;
	}else{
		return false;
	}
}

int main(){
	B b1;//创建对象
	
	b1.inputText();
	
	b1.inputName();
	if(b1.save()){
		return 0;
	}else{
		return 1;
	}
	
}

F:/汇编/SG_CC++/c5.cpp
#include <iostream>
#include <string>
class Animal{
public :
	//void play();
	virtual void play();//virtual虚方法,告诉编译器,在运行时才绑定
};//千万不要忘记分号
void Animal::play(){//获取静态的私有的成员
	std::cout<<"animal like play games too..."<<std::endl;
}


//子类继承父类,C++中,冒号表示 继承
class Pig : public Animal{
public:
	void play();
};
void Pig::play(){//获取静态的私有的成员
	Animal::play();
	std::cout<<"猪喜欢拱白菜..."<<std::endl;
}

//子类继承父类,C++中,冒号表示 继承
class Cat : public Animal{
public:
	void play();
};
void Cat::play(){//获取静态的私有的成员
	Animal::play();
	std::cout<<"猫喜欢爬树..."<<std::endl;
}

int main(){
	//默认,编译时,编译器就将指针p c定性为Animal类型,调用方法时,就会调用Animal定义的方法
	//而父类将该方法声明为virtual时,会将指针p c在运行时,才绑定
	//这类似于JAVA中的父类引用指定子类对象的实例
	Animal *p=new Pig();
	Animal *c=new Cat();
	p->play();
	c->play();
	
	std::cout<<"hi"<<std::endl;
	return 0;
}

F:/汇编/SG_CC++/class2.cpp
#include <iostream>
class Animal{
public :
	void eat();
	Animal();//构造函数
	~Animal();//析构函数
};//千万不要忘记分号
Animal::Animal(void){
	std::cout<<"animal 构造函数"<<std::endl;
}
Animal::~Animal(){
	std::cout<<"animal 析构函数"<<std::endl;
}

class Pig:public Animal{
public:
	void climb();
	Pig();//构造函数
	~Pig();//析构函数
};//千万不要忘记分号
void Animal::eat(){
	std::cout<<"animal eating"<<std::endl;
}

void Pig::climb(){
	std::cout<<"pig climbing"<<std::endl;
}

Pig::Pig(void){
	std::cout<<"Pig 构造函数"<<std::endl;
}
Pig::~Pig(){
	std::cout<<"Pig 析构函数"<<std::endl;
}
int main(){
	//std::cout<<"hello world"<<std::endl;
	//Animal a1;
	//a1.eat();
	Pig p1;
	//p1.eat();
	//p1.climb();
	return 0;
}

F:/汇编/SG_CC++/class3.cpp
#include <iostream>
#include <string>
class Animal{
public :
	Animal(std::string _name);//构造函数
	~Animal();//析构函数
	static int getCount();//用于获取静态的计数;
public:
	std::string name;//成员属性:姓名
private:
	static int count;//静态成员:统计动物数量
};//千万不要忘记分号

int Animal::count=0;//静态成员初始化,关键,必不可少

int Animal::getCount(){//获取静态的私有的成员
	return count;
}

Animal::Animal(std::string _name){
	std::cout<<"构造--this :"<<this<<std::endl;
	this->name=_name;
	count++;
	std::cout<<"animal:"<<name<<" 出生!\n\n"<<std::endl;
}
Animal::~Animal(){
	std::cout<<"析构--this :"<<this<<std::endl;
	count--;
	std::cout<<"animal:"<<name<<" game over!\n\n"<<std::endl;
}


//子类继承父类,C++中,冒号表示 继承
class Pig : public Animal{
public:
	Pig(std::string _name);//子类带参构造函数声明
};

Pig::Pig(std::string _name):Animal(_name){
	//子类的带参构造函数实现,也是继承父类的
}


int main(){
	std::cout<<Animal::getCount()<<std::endl;//0
	Pig P1("小猪快跑");
	std::cout<<P1.name<<"'s address is :"<<&P1<<std::endl;//0
	std::cout<<Animal::getCount()<<std::endl;//1
	//下面这个括号,相当于一次入栈和出栈
	{
		Pig P2("小鸡快跑");
		std::cout<<Animal::getCount()<<std::endl;//2
	}
	std::cout<<Animal::getCount()<<std::endl;//1
	std::cout<<"hi"<<std::endl;
	return 0;
}

F:/汇编/SG_CC++/exception.cpp
#include <iostream>
#include <climits>
//带异常的函数声明
unsigned long Rec() throw const char *p;
int main(){
	std::cout<<"hello world"<<std::endl;
}

F:/汇编/SG_CC++/FuncPoint.cpp
//FuncPoint函数指针,是一个指向函数的指针,即变量里面存放的是函数的内存基址
#include<iostream>
int func1(int x,int y){
	return x*y;
}
int main(){
	int result;
	int (*p)(int a,int b);//括号代表是函数,前面代表是指针,指向函数的指针
	p=func1;//函数名等于就是函数的基址 指针变量p存放的就是该地址
	result=(*p)(5,6);//解引用之后,就相当于func1
	std::cout<<result<<std::endl;

	return 0;
}

F:/汇编/SG_CC++/hannuota.c
#include <stdio.h>
int count;
count=0;
//参数表示:将n个盘,从X针上,借助Y针,移动到Z上
void move(int n,char x,char y,char z){
	if(1==n){
		count++;
		printf("第%d步:将盘子从  %c  上,移动到  %c  上\n",count,x,z);
	}else{
		move(n-1,x,z,y);//将n-1个盘,从X针上,借助Z针,移动到Y上
		count++;
		printf("第%d步:将盘子从  %c  上,移动到  %c  上\n",count,x,z);
		move(n-1,y,x,z);//将n-1个盘,从Y针上,借助X针,移动到Z上
	}
}


int main(){
	int n;
	printf("请输入汉诺塔的层数: \n");
	scanf("%d",&n);
	move(n,'X','Y','Z');

}

F:/汇编/SG_CC++/model1.cpp
#include <iostream>
#include <string>
class Animal{
public :
	//void play();
	//virtual虚方法,告诉编译器,在运行时才绑定
	virtual void play(){
		std::cout<<"animal like play games too..."<<std::endl;
	}
};//千万不要忘记分号

//子类继承父类,C++中,冒号表示 继承
class Pig : public Animal{
public:
	void play(){
		Animal::play();
		std::cout<<"猪喜欢拱白菜..."<<std::endl;
	}
};

//子类继承父类,C++中,冒号表示 继承
class Cat : public Animal{
public:
	void play(){
		Animal::play();
		std::cout<<"猫喜欢爬树..."<<std::endl;
	}
};

int main(){
	//默认,编译时,编译器就将指针p c定性为Animal类型,调用方法时,就会调用Animal定义的方法
	//而父类将该方法声明为virtual时,会将指针p c在运行时,才绑定
	//这类似于JAVA中的父类引用指定子类对象的实例
	Animal *p=new Pig();
	Animal *c=new Cat();
	p->play();
	c->play();

	//下面效果一样
	Cat c2;
	Animal *a2=&c2;
	a2->play();
	std::cout<<"hi"<<std::endl;
	return 0;
}

F:/汇编/SG_CC++/MsgBox.c
#include <windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0) ;

     return 0 ;
}

F:/汇编/SG_CC++/Point1.cpp
#include<iostream>
int main(){
	int a,b;
	int *p1,*p2;
	a=52;
	b=67;
	p1=&a;
	p2=&b;
/*
		  a=52
00401578   mov         dword ptr [ebp-4],34h		//[ebp-4]是变量a
6:        b=67;
0040157F   mov         dword ptr [ebp-8],43h		//[ebp-8]是变量b
7:        p1=&a;
00401586   lea         eax,[ebp-4]			//lea 就是将[ebp-4]的地址放到eax
00401589   mov         dword ptr [ebp-0Ch],eax		//再将eax中的地址放到变量[ebp-0Ch]的内存单元存放
8:        p2=&b;
0040158C   lea         ecx,[ebp-8]		//lea 将[ebp-8]即变量b的地址放到ecx
0040158F   mov         dword ptr [ebp-10h],ecx		//再将地址放到变量[ebp-10h]的内存单元存放
*/
	std::cout<<*p1<<std::endl;
	std::cout<<*p2<<std::endl;

	return 0;
}

F:/汇编/SG_CC++/PointFunc.cpp
#include<iostream>
//函数返回内存,即C里面的指针函数,函数内部在堆里面通过new开辟一块内存空间,并将存放该内存空间基址的变量返回,该变量就是指针
int *func1(int value){
	//返回值是指向int类型的指针变量
	int *p=new int;
	//解引用对该新开辟的内存空间赋值
	*p=value;
	return p;
}
int main(){
	int *s=func1(5267);
	std::cout<<*s<<std::endl;
	//堆空间new出来的内存,只能通过delete释放 C中是free
	delete s;//将指针s存放的地址空间的值释放
	s=NULL;//s置空	
	return 0;
}

F:/汇编/SG_CC++/swap.cpp
#include<iostream>
//指针变量 存放地址
void swap(int *eax,int *ebx){
#if 0
	int edx=*eax;//解引用,相当于取地址的值 汇编中的mov edx, [eax]将eax里存放的地址的对应的内存中的4个字节数据,放入edx
	*eax=*ebx;
	*ebx=edx;
#endif
	*eax^=*ebx;
	*ebx^=*eax;
	*eax^=*ebx;
}
int main(){
	int a=52;
	int b=67;
	swap(&a,&b);//函数的参数是指针变量,存放的是地址,故需要地址,相当于汇编中的mov eax,0x00401000h;
	std::cout<<"a="<<a<<std::endl;
	std::cout<<"b="<<b<<std::endl;
	return 0;
}

F:/汇编/SG_CC++/T.cpp
#include <iostream>
#include <string>
template <class T>	//C++固定泛型使用声明
class Stack{
	public:
		Stack(unsigned int _size){
			//根据参数,初始化栈,用数组模拟
			this->size=_size;
			data=new T[_size];//data类似于数组名,存放的是数组的首地址
			sp=0;//空栈,栈顶指针为
		}
		~Stack(){
			delete []data;
		}
		void push(T element){
			if(sp>this->size){
				throw "full already!";
			}
			sp=sp+1;//指针先移动
			data[sp]=element;
		}
		T pop(){
			if(sp==0){
				throw "error";
			}
			T temp=data[sp];//先弹数据
			sp=sp-1;//指针下移一个单位
			return temp;
		}
	private:
		//成员私有化
		unsigned int size;
		unsigned int sp;
		T *data;//指向T类型的指针,相当于数组名,指针变量data存放的是T元素的地址(也是首地址)
};
int main(){
	//带参,实例化一个泛型类
	Stack<int> s(100);
	s.push(5);
	s.push(2);
	s.push(6);
	s.push(7);
	std::cout<<"pop: "<<s.pop()<<std::endl;
	std::cout<<"pop: "<<s.pop()<<std::endl;
	std::cout<<"pop: "<<s.pop()<<std::endl;
	std::cout<<"pop: "<<s.pop()<<std::endl;
	
	return 0;
}

F:/汇编/SG_CC++/T_overRiding.cpp
#include<iostream>
//使用泛型打印
template <typename T>		//typename也可以是class
void prin(T *start,T *end){
	while(start!=end){
		std::cout<<*start<<std::endl;//解引用
		start++;//指针下移
	}
}
int main(){
	int num[5]={1,2,3,4,5};
	prin(num,num+5);
	return 0;
}

F:/汇编/SG_CC++/vector.cpp
//C++STL标准模板库 vector
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>	//运算法则
int main(){
	//创建一个vector对象,<>里面明确参数类型
	std::vector<std::string> names;
	names.push_back("beyond");
	names.push_back("keke");
	names.push_back("coco");
	names.push_back("hashMap");
	//像使用数组一样,传统for遍历
	for(int i=0;i<names.size();i++){
		std::cout<<names[i]<<std::endl;
	}
	
	//使用algorithm(运算法则)头文件里面的排序sort完成对向量的字典排序
	std::sort(names.begin(),names.end());

	//使用容器的内部类_迭代器_(即指针),完成容器的遍历
	std::vector<std::string>::iterator it=names.begin();
	while(it!=names.end()){
		std::cout<<*it<<std::endl;
		it++;		//指针指向下一个
	}
	return 0;
}

F:/汇编/SG_CC++/_01.c
#include <stdio.h>
int main(){
	printf("hello world");
	return 0;
}

F:/汇编/SG_CC++/_2.c
#include <stdio.h>
//对一个数组求和

//C函数声明,分号结尾
int sumArr(int arr[],int len);
int main(){
	int arr[]={5,2,6,7};
	int len=sizeof(arr)/sizeof(arr[0]);
	printf("sum is :%d\n",sumArr(arr,len));
	return 0;
}
int sumArr(int arr[],int len){
	int sum=0;
	int i;
	for(i=0;i<len;i++){
		sum+=arr[i];
	}
	return sum;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值