C++

C到C++快速入门(2019版本)

Tutorial From C to C++ (2019)

Posted by xuepro on January 26, 2019
#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>  //标准输入输出函数

#include <cmath>

#include <cstring> //字符串处理函数

int main() {
	printf("hello\n");
	double x = 3.14;
	printf("%lf %lf\n", sqrt(x), sin(x));

	char s[10] = "hello";
	puts(s);
	char s2[16];
	strcpy(s2, "world");
	puts(s2);
	strcat(s2, "sdfsdf");
	puts(s2);
	printf("%d %d\n", strlen(s), strlen(s2));
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>  //标准输入输出函数

#include <cstring> //字符串处理函数

#include <malloc.h>

int main() {

# if 1

	char s[10];
	strcpy(s, "hello");
	puts(s);
#else

	char *s = (char *)malloc(12 * sizeof(char));
	strcpy(s, "hello world");
	puts(s);
  
#endif 

}

#include <iostream>  //C++标准输入输出流头文件

using namespace std;

int main() {
	cout << "hello world!"<< endl;
	cout << "https://a.hwdong.com" << endl;
	cout << 3+4 << endl;


	double radius;
	std::cin >> radius; //标准输入流对象cin 输入运算符>>
  
	cout << 3.14*radius*radius;

	std::cout << "  *\n";
	std::cout << " *  *\n";
	std::cout << " *  *\n";
	return 0;
}


#include <iostream>

using namespace std;

void help() {
	cout << "=========简单计算器=========\n";
	cout << "请输入:左运算数  运算符  右运算符\n";
}

int main() {
	while (1) {
		help();
		double a, b;
		char op;
		cin >> a >> op >> b;
		if (op == '+')
			cout << a + b << endl;
		//...补充你的代码
    
	}
}


#include <fstream>

#include <iostream>

#include <string>

using namespace std;

int main() {
	ofstream oF("test.txt");
	oF << 3.14 << " " << "hello world\n";
	oF.close();
	ifstream iF("test.txt");
	double d;
	string str;
	iF >> d >> str;
	cout<<d <<" "<< str<<endl;

	return 0;
}

#include <iostream>

using namespace std;

int main() {
	int a = 3, &r = a;
	cout << a << '\t' << r << endl;
	r = 5;
	cout << a << '\t' << r << endl;
	return 0;
}


#include <iostream>

using namespace std;

void swap(int x, int y) {
	cout << x << '\t' << y << endl;
	int t = x;
	x = y; 
	y = t;
	cout << x << '\t' << y << endl;
}

int main() {
	int a = 3, b = 4;
	cout << a << '\t' << b << endl;
	swap(a, b);
	cout << a << '\t' << b  << endl;
}


void swap(int *x, int *y) {
	int t = *x;
	*x = *y;
	*y = t;
}


#include <iostream>

using namespace std;

int main() {
	int a = 3, b = 4;
	cout << a << '\t' << b << endl;
	swap(&a, &b);
	cout << a << '\t' << b << endl;
}



void swap(int &x, int &y) {
	int t = x;
	x = y;
	y = t;
}


#include <iostream>

using namespace std;

int main() {
	int a = 3, b = 4;
	cout << a << '\t' << b << endl;
	swap(a, b);
	cout << a << '\t' << b << endl;
}


#include <iostream>

using namespace std;

void print(char ch, int n = 1) {
	for (int i = 0; i < n; i++)
		cout << ch;
}

int main() {
	print('*'); cout << endl;
	print('*',3); cout << endl;
	print('*',5); cout << endl;
}

#include <iostream>

using namespace std;

int add(int x,int y=2,int z=3) {
	return x + y + z;
}


int main() {
	cout << add(5)<<endl;
	cout << add(5,7) << endl;
	cout << add(5,7,9) << endl;
}


#include <iostream>

using namespace std;

int add(int x, int y = 2) {
	return x + y ;
}
double add(double x, double y = 2.0) {
	return x + y;
}

int main() {
	cout << add(5,3) << endl;
	cout << add(5.3, 7.8) << endl;	
	cout << add((double)5, 7.8) << endl;//歧义性
}
#include <iostream>

using namespace std;

int add(int x, int y) {
	return x + y;
}

double add(double x, double y ) {
	return x + y;
}

int main() {
	cout << add(5, 3) << endl;
	cout << add(5.3, 7.8) << endl;
//	cout << add("hello", "world") << endl;

}
#include <iostream>

#include <string>

using namespace std;

template<typename T>
T add(T x, T y) {
	return x + y;
}

int main() {

/*
	cout << add<int>(5, 3) << endl;
	cout << add<double>(5.3, 7.8) << endl;
	cout << add<int>(4, 6) << endl;
	cout << add<string>("hello", "world") << endl;
  
*/

	cout << add(5, 3) << endl;
	cout << add(5.3, 7.8) << endl;
	cout << add((double)5, 7.8) << endl; //歧义性
  

}

#include <iostream>

#include <string>

using namespace std;

int main() {
	string s = "hello", s2("world");
	//访问运算符.
  
	cout << s.size() << endl;
	string s3 = s.substr(1, 3);
	cout << s3<< endl;
	
	string s4 = s + " " + s2;
	cout << s4 << endl; //"hello world"
  
	
	s4[0] = 'H';
	s4[6] = 'X';
	cout << s4 << endl;
	
	int pos = s4.find("orl");
	cout << pos << endl;
	s4.insert(3, "ABCDE");
	cout << s4 << endl;

	for (int i = 0; i < s4.size(); i++)
		cout << s4[i] << "-";
	cout << "\n";

}
#include <iostream>

using std::cout;

int main() {
	int arr[] = { 10,20,30,40 }; //大小固定,以后不能添加更多int值
  
	for (int i = 0; i < 4; i++)
		cout << arr[i] << '\t';
	cout << '\n';
}
#include <iostream>

#include <vector>

using namespace std;

int main() {	
	vector<int> v = { 7, 5, 16, 8 };
	//push_back(),最后添加一个元素
  
	v.push_back(25);
	v.push_back(13);

	//成员函数size()、下标运算符[]
  
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << '\t';
	cout << '\n';

	v.pop_back();
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << '\t';
	cout << '\n';
	
	v.resize(2);

	for (int i = 0; i < v.size(); i++)
		cout << v[i] << '\t';
	cout << '\n';
}

/*
指针就是地址,变量的指针就是变量的地址。
指针变量就是存储指针(地址)的变量。
*/

#include <iostream>

using namespace std;

int main() {
	int a=3;
	int *p = &a;   //取地址运算符&用于获得a的地址:&a
  
	cout << p << '\t' << &a << endl;
  
	//取内容运算符*用于获得指针指向的变量(内存块)
  
	cout << *p << '\t' << a << endl;     //*p就是a
  
	*p = 5;                              //即a = 5;
  
	cout << *p << '\t' << a << endl;
  

	int *q = p;             //q和p值相同,都是a的地址(指针)
  
	cout << *p << '\t' << *q << '\t' << a << endl;
	char *s = &a; //int *


}
/* 
用指针访问数组元素
*/
#include <iostream>

using namespace std;

int main() {
	int arr[] = { 10,20,30,40 };
	int *p = arr;//数组名就是数组第一个元素的地址,即arr等于&(arr[0])
  
	// p[i]就是*(p+i)
  
	cout << *(p + 2) << '\t' << p[2] << '\t' << arr[2] << endl;
	
	for (int *q = p + 4; p < q; p++)
		cout << *p << '\t';
	cout << '\n';
}

/*
malloc free realloc
  动态内存分配:new用于申请内存块、delete用于释放内存块
  T *p = new T;
  delete p;
  T *q  = new T[5];
  delete[] q;
*/

// 堆存储区

#include <iostream>

using namespace std;

int main() {
	int *p = new int; 
  
	*p = 3;
	cout << p << '\t' << *p << endl;	
	delete p;   //内存泄漏
  
	p = new int;
	*p = 5;
	cout << p << '\t' << *p << endl;
	delete p;
}

#include <iostream>

using namespace std;

int main() {
	int n = 4;
	int *p = new int[n];
	for (int i = 0; i < n; i++)
		p[i] = 2 * i + 1;

	for (int *q = p + n; p < q; p++)
		cout << *p << '\t';
	cout << '\n';

	char *s = (char *)p;
	char ch = 'A';
	int n2 = n * sizeof(int) / sizeof(char);
	for (int i = 0; i < n2; i++)
		s[i] = ch + i;

	for (char *r = s+n2; s < r; s++)
		cout << *s;
	cout << '\n';

	delete[] p;
}

/*
输入一组学生成绩(姓名和分数),输出:平均成绩、最高分和最低分。
当然,也要能输出所有学生信息

*/
#include <iostream>

#include <string>

#include <vector>

using namespace std;

struct student{
	string name;
	double score;
	void print();
};
void student::print() {
	cout << name << " " << score << endl;
}

int main() {

/*

	student stu;
	stu.name = "Li Ping";
	stu.score = 78.5;
	stu.print();
  */


	vector<student> students;	
	
	while (1) {
		student stu;
		cout << "请输入姓名 分数:\n";
		cin >> stu.name >> stu.score;
		if (stu.score < 0) break;
		students.push_back(stu);
	}
	for (int i = 0; i < students.size(); i++)
		students[i].print();

	double min = 100, max=0, average = 0;
	for (int i = 0; i < students.size(); i++) {
		if (students[i].score < min) min = students[i].score;
		if (students[i].score > max) max = students[i].score;
		average += students[i].score;
	}
	average /= students.size();
	cout << "平均分、最高分、最低分:" 
		<< average << " " << max << " " << min << endl;

}

/* 
    this指针: 成员函数实际上隐含一个this指针。
*/

#include <iostream>

#include <string>

using namespace std;

struct student {
	string name;
	double score;
	void print() {
		cout << this->name << " " << this->score << endl;
	}
};
int main() {
	student stu;
	stu.name = "Li Ping";
	stu.score = 78.5;
	stu.print();   // print(&stu);
  
}

/*
	struct和class区别:
	struct里的成员默认是public(公开的)
	class里的成员默认是private(私有的)
*/

#include <iostream>

#include <string>

using namespace std;

class student{
public: //接口

	void print() {
		cout << this->name << " " << this->score << endl;
	}
	string get_name() { return name; }
	double get_score() { return score; }
	void set_name(string n) { name = n; }
	void set_score(double s) { score = s; }
private:
	string name;
	double score;
};
int main() {
	student stu;
	
//	stu.name = "Li Ping";

//	stu.score = 78.5;

	stu.set_name("Li Ping");
	stu.set_score(78.5);
	stu.print();   // print(&stu);
  
	cout << stu.get_name() << " " << stu.get_score() << endl;
}
/*
构造函数: 函数名和类名相同且无返回类型的成员函数。
*/

#include <iostream>

#include <string>

using namespace std;

class student{
	string name;
	double score;
public:	
	student(string n,double s){ //不是默认构造函数
  
		name = n; score = s;
		cout << "构造函数\n";
	}
	void print() {
		cout << this->name << " " << this->score << endl;
	}
};


int main() {
	student stu("LiPing",80.5); //在创建一个类对象时会自动调用称为“构造函数”的成员函数
  
	stu.print();
	student students[3];

}

/* 运算符重载:针对用户定义类型重新定义运算符函数
*/

#include <iostream>

#include <string>

using namespace std;

class student {
	string name;
	double score;
public:
	student(string n, double s) { 
		name = n; score = s;		
	}
  
	//友元函数
  
	friend ostream& operator<<(ostream &o, student s);
	friend istream& operator>>(istream &in, student &s);
};

ostream& operator<<(ostream &o, student s) {
	cout << s.name << "," << s.score << endl;
	return o;
}
istream& operator>>(istream &in, student &s) {
	in >> s.name >> s.score;
	return in;
}

int main() {
	student stu("LiPing", 80.5);	
	cin >> stu; //operator>>(cin,stu)
  
	cout << stu; //operator<<(cout,stu)
  
}


#include <iostream>

#include <string>

using namespace std;

class Point{
	double x, y;
public:	
	double operator[](int i) const{ //const函数
  
		if (i == 0) return x;
		else if (i == 1) return y;
		else throw "下标非法!"; //抛出异常
    
	}
	double& operator[](int i) {
		if (i == 0) return x;
		else if (i == 1) return y;
		else throw "下标非法!"; //抛出异常
    
	}
	Point(double x_,double y_) {
		x = x_; y = y_;
	}
	Point operator+(const Point q) {
		return Point(this->x+q[0],this->y + q[1]);
	}

	//友元函数
  
	friend ostream & operator<<(ostream &o, Point p);
	friend istream & operator>>(istream &i, Point &p);
};

ostream & operator<<(ostream &o, Point p) {
	o <<p.x << " " << p.y<< endl;
	return o;
}
istream & operator>>(istream &i, Point &p) {
	i >> p.x >> p.y;
	return i;
}
/*
Point operator+(const Point p,const Point q) {
	return Point(p[0] + q[0], p[1] + q[1]);
}
*/

int main() {
	Point p(3.5, 4.8),q(2.0,3.0);	
  

//	cin >> p;

	cout << p;
	cout << p[0] << "-" << p[1] << endl; //p.operator[](0)
  
	p[0] = 3.45; p[1] = 5.67;
	cout << p;
  

	cout << p<<q;	
	Point s = p + q; //p.operator+(q)  vs  operator+(p,q)
  
	cout << s;
}


#include <iostream>

using namespace std;

class String {
	char *data; //C风格的字符串
  
	int n;
public:
	~String() {
		cout <<n<< " 析构函数!\n";
		if(data)
			delete[] data;
	}

	String(const String &s) { //硬拷贝
  
		cout << "拷贝构造函数!\n";	
		data = new char[s.n + 1];		
		n = s.n;		
		for (int i = 0; i < n; i++)
			data[i] = s.data[i];
		data[n] = '\0';
	}
  


	String(const char *s=0) {
		cout << "构造函数!\n";
		if (s == 0) {
			cout << "s==0\n";
			data = 0; n = 0; return;
		}
		const char *p = s;
		while (*p) p++;
		n = p - s;
		data = new char[n + 1];
		for (int i = 0; i < n; i++)
			data[i] = s[i];
		data[n] = '\0';
	}
  
	int size() { return n; }
  
	char operator[](int i)const {
		if (i<0 || i>=n ) throw "下标非法";
		return data[i];
	}
  
	char& operator[](int i) {
		if (i < 0 || i >= n) throw "下标非法";
		return data[i];
	}
};

ostream & operator<<(ostream &o, String s) {
	for (int i = 0; i < s.size(); i++)
		cout << s[i];
	return o;
}

void f() {
	String str,str2("hello world");
	str2[1] = 'E';
//	cout << str2 << endl;


	String s3 = str2; //拷贝构造函数
  
	cout << s3 << endl;
	s3[3] = 'L';
	cout << s3 << endl;
	cout << str2 << endl;


}


int main() {
	f();
}

/*类
   模拟vector<int>的类Vector
*/

#include <iostream>

#include <string>

using namespace std;

class student {
	string name;
	double score;
public:
	student(string n="no", double s=0) {
		name = n; score = s;
	}
	friend ostream& operator<<(ostream &o, student s);
};

ostream& operator<<(ostream &o, student s) {
	cout << s.name << "," << s.score << endl;
	return o;
}

//类模板

template<typename T>
class Vector {
	T *data;
	int capacity;
	int n;
public:
	Vector(int cap=3) {
		data = new T[cap];
		if (data == 0) {
			cap = 0; n = 0;
			return;
		}
		capacity = cap;
		n = 0;
	}
	void push_back(T e) {
		if (n == capacity) {//空间已经满
    
			cout << "增加容量!\n";
			T *p = new T[2 * capacity];
			if (p) {
				for (int i = 0; i < n; i++)
					p[i] = data[i];
				delete[] data;
				data = p;
				capacity = 2*capacity;
			}
			else {
				return;
			}
		}
		data[n] = e;
		n++;
	}
	T operator[](int i) const{
		if (i < 0 || i >= n) throw "下标非法!";
		return data[i];		
	}	
	int size() {
		return n;
	}
};


int main() {
	Vector<student> v;
	v.push_back(student("Li",45.7));
	v.push_back(student("Wang", 45.7));
	v.push_back(student("zhao", 45.7));

	for (int i = 0; i < v.size(); i++)
		cout << v[i] ;
	cout << endl;

	v.push_back(student("zhang", 45.7));
	v.push_back(student("Liu", 45.7));
	for (int i = 0; i < v.size(); i++)
		cout << v[i];
	cout << endl;
}

int main() {	
	Vector<int> v;
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);

	for(int  i = 0 ; i<v.size();i++)
		cout<<v[i]<<'\t';
	cout << endl;

	v.push_back(6);
	v.push_back(7);
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << '\t';
	cout << endl;
}
  

int main() {	
	Vector<string> v;
	v.push_back("hello");
	v.push_back("world");
	v.push_back("sdfasdf");

	for (int i = 0; i < v.size(); i++)
		cout << v[i] << '\t';
	cout << endl;

	v.push_back("ggg");
	v.push_back("hhh");
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << '\t';
	cout << endl;
  
}



支付宝打赏 微信打赏

您的打赏是对我最大的鼓励!