C++初试(1)

java出身的我,不习惯使用其他的IDE所以使用了Eclipse来编写C++程序。需要先下载eclipse c++版本。然后下载MinGW。配置环境变量。然后就可以新建一个C++的项目了。

我学东西的步骤就是 ,先会用,然后再研究为什么这么用。所以我先写代码:

/*
 * first.cpp
 *
 *  Created on: 2014年10月27日
 *      Author: chuer
 */
#include <iostream>
#include <climits>
#include <cstring>
#include <string>
using namespace std;


//函数需要先声明  然后下面再定义
int aaa(int n, int m);
void bbb();
void test_array();
void test_string();
void test_struct();
void test_union();
void test_enum();

//结构体
struct people {
	string name;
	int age;
};

//共用体  (公用一段内存)
union num {
	int int_val;
	long long_val;
	double double_val;
};

//枚举
enum color {
	red, yellow = 6, blue, green, perpul
};

void test_param(int num, people p);
void test_address();
void test_point();
void test_point_param(int *p ,int &a);
int main() {
	//int b = aaa(5,3);
	//cout << b << endl;

//	bbb();

//	test_array();

//	test_string();

//	test_struct();

//	test_union();

//	test_enum();

//	int a = 1;
//	people p;
//	p.age = 1;
//	cout << "a=" << a << " p.age=" << p.age << endl;
//	test_param(a,p);
//	cout << "a=" << a << " p.age=" << p.age << endl;

//	test_address();

	test_point();

//	int a = 100;
//	int* p = &a;
//
//	cout << a << endl;
//	test_point_param(p,a);
//	cout << a << endl;
	return 0;
}

void test_point_param(int* p ,int& a){
	cout << "invoke test_point_param();" << endl;
	cout << p << endl;
	cout << &a << endl;

	a = 101;
	*p = 102;
}
//测试指针
void test_point() {
	int a = 100;
	int *b;

	b = &a;
	cout << "a=" << a << endl;
	cout << "&a=" << &a << endl;
	cout << "b=" << b << endl;
	cout << "&b=" << &b << endl;
	cout << "*b=" << *b << endl;

	*b = *b + 1;
	cout << "a=" << a << endl;
	cout << "&a=" << &a << endl;
	cout << "b=" << b << endl;
	cout << "&b=" << &b << endl;
	cout << "*b=" << *b << endl;


	//一定要配对的使用new 和 delete。否则会发生内存泄露
	int *c = new int(100);
	cout << "*c=" << *c <<endl;
	delete c;


	int *d = new int[2];
	d[0] = 1;
	d[1] = 3;

	cout << d << endl;
	cout << *(d+1) << endl;

	delete [] d;
}

//测试地址符号
void test_address() {
	int a = 100;
	int b = 200;

	cout << &a << endl;
	cout << &b << endl;
}
//测试参数传递
void test_param(int a, people p) {
	cout << "invoke test_param ()" << endl;
	a = 100;
	p.age = 100;
	cout << "a=" << a << " p.age=" << p.age << endl;
}

void test_enum() {
	color b;
	b = blue;
	cout << b << endl;

	switch (b) {
	case red:
		cout << red << endl;
		break;
	case yellow:
		cout << yellow << endl;
		break;
	case blue:
		cout << blue << endl;
		break;
	case green:
		cout << green << endl;
		break;
	case perpul:
		cout << perpul << endl;
		break;
	default:
		cout << "default" << endl;
	}
}

void test_union() {
	num n;
	n.int_val = 1;
	cout << n.int_val << endl;
	n.long_val = 100;
	cout << n.int_val << endl; //会把int_val的值替换,因为公用一段内存
	n.double_val = 111.2;
	cout << n.int_val << endl;
}

void test_struct() {

	people p;
	p.name = "aaa";
	p.age = 15;

	people p2 = { "bbb", 20 };
	cout << p.name << "  " << p.age << endl;
	cout << p2.name << "  " << p2.age << endl;

	people p3[2] = { { "ccc", 1 }, { "ddd", 2 } };
	cout << p3[0].name << "  " << p3[0].age << endl;
	cout << p3[1].name << "  " << p3[1].age << endl;
}

void test_string() {
	const int SIZE = 15;
	char name1[SIZE];
	cout << "what is your name?" << endl;
	cin >> name1;
	cout << strlen(name1) << sizeof(name1) << name1 << endl;

	char name2[SIZE] = "C++owboy";
	name2[3] = '\0';
	cout << name2 << endl;

	string str = "abcdedf";
	str[2] = '\0';
	cout << str << str.size() << endl;
}
void test_array() {
	int array[3];
	array[0] = 1;
	array[1] = 2;
	array[2] = 3;

	cout << array[0] << array[1] << array[2] << endl;
	int aaa[3] = { 4, 5, 6 };
	cout << aaa[0] << aaa[1] << aaa[2] << endl;

	char bbb[5] = { 'a', 'a', 'a', 'a', 'a' };
	char ccc[5] = { 'b', 'b', 'b', 'b', '\0' };

	cout << bbb << endl;
	cout << ccc << endl;
}

void bbb() {
	int n_int = INT_MAX;
	short n_short = SHRT_MAX;
	long n_long = LONG_MAX;

	cout << "int is " << sizeof(int) << " bytes" << endl;
	cout << "short is " << sizeof(short) << " bytes" << endl;
	cout << "long is " << sizeof(long) << " bytes" << endl;

	cout << "maximum values" << endl;

	cout << "int  :" << n_int << endl;
	cout << "short:" << n_short << endl;
	cout << "long :" << n_long << endl;
}

int aaa(int n, int m) {
	int result = 0;
	if (n <= 1) {
		result = 0;
	} else {
		result = (aaa(n - 1, m) + m) % n;
	}

	return result;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值