#include #include #include #include #include using namespace std; int result(int, int); void main1(); void main2(); const int k = 2; struct Point { int x, y; }; void main1() { int z(0), b(50);//调用构造函数,等同于z = 50 Point a; cout << "输入两个整数(以空格区分):"; cin >> a.x >> a.y; z = (a.x + a.y) * k; z = result(z, b); cout << "计算结果如下:" << endl; cout << "((" << a.x << "+" << a.y << ")*" << k << ")+" << b << "=" << z << endl; } int result(int a, int b) { return a + b; } void main2() { double *pDouble = new double[3]; for (int i = 0; i < 3; i++) { cout << "请输入一个数字" << endl; cin >> *(pDouble + i); } for (int j = 0; j < 3; j++) { cout << *(pDouble + j) << " "; } delete pDouble; } void constTest() { int *p1 = new int(5); int *&p2 = p1; //是指针的引用 // int * * p3 = &p1; const int y = 58; //常量,不能改变值 const int *p = &y;//指向常量的指针 // * p = 2; cout << *p << endl; cout << p << endl; cout << &p << endl; cout << y << endl; cout << &y << endl; } //1.6 bool compare(int a, int b) { return a > b; } void coutArray() { double a[4] = {1.1, 4.4, 3.3, 2.2}, b[4]; // 数组复制到流中输出 p1:开始位置,p2: 结束位置 p3:输出流 cout << "数组a" << endl; copy(a, a + 4, ostream_iterator(cout, " ")); cout << endl; cout << "逆向输出数组a" << endl; reverse_copy(a, a + 4, ostream_iterator(cout, " ")); cout << endl; //复制到数组b p1:开始位置,p2: 结束位置 p3,数组开始 copy(a, a + 4, b); //输出数组b cout << "数组a" << endl; copy(b, b + 4, ostream_iterator(cout, " ")); cout << endl; /* for (int i = 0; i < 4; ++i) { cout << b[i] << endl; }*/ //对数组a进行排序 sort(a, a + 4); //默认升序排序 // sort(a,a+4,compare) 降序排序 ,自己实现 // sort(a, a + 4, greater()); // less<数据类型>()//从小到大排序 greater<数据类型>()//从大到小排序 cout << "升序输出a" << endl; copy(a, a + 4, ostream_iterator(cout, " ")); cout << endl; cout << "逆向copy数组a到b" << endl; reverse_copy(a, a + 4, b); copy(b, b + 4, ostream_iterator(cout, " ")); cout << endl; double a1[4] = {1.1, 3.3, 2.2, 4.4}; double *x = find(a1, a1 + 4, 4.4); //x的地址超出a1地址范围就是没有找到 cout << (x == a1 + 3) << endl; if (x == a1 + 4) { cout << "没有找到值为4.4的元素" << endl; } else { cout << "有值为" << *x << "的数组元素" << endl; } delete x; //double i 每加一,内存地址增加8个字节 double类型8个字节,64位 for (int i = 0; i < 4; ++i) { cout << "a1+" << i << " = " << a1 + i << " value = " << a1[i] << endl; } cout << "x = " << x << " *x = " << *x << endl; // char char1[] = "ab"; char char2[] = {'a', 'b'}; //todo 两者有区别,前者可直接cout输出值,后者会有乱码,不知道是什么。 //todo 2016-07-17 01:00 ,发现是因为char2[] 没有 \0 不会截止, char1长度为3 cout << char2 << endl; // cout << sizeof(int) << endl; //sizeof 计算内存中占用字节数 //通过sizeof 可计算数组长度 /* for(int i=0;i< sizeof(char1)/ sizeof(char1[0]);i++){ cout< 头文件中,获取float 最大值,或者 min =0.0f, 当i=0时,把输入的值赋给min float min = numeric_limits::max(); cout << "请输入15个Float型数字" << endl; for (int i = 0; i < 15; ++i) { cin >> *(pfloat + i); sum += *(pfloat + i); if (min > *(pfloat + i)) { min = *(pfloat + i); } } cout << "15个float 数字中最小的是:" << min << ", 15个数字的和是:" << sum << endl; // int length = sizeof(pfloat)/ sizeof(pfloat[0]); } /** * 找出数组中4的位置,在将数组a复制给数组b,然后将数组a的内容反转在找出4的位置, * 最后分别输出数组a和b的内容 */ void expercises4_4() { int a[] = {1, 2, 3, 4, 5, 6, 7, 8}, b[8]; int *p = find(a, a + 8, 4); if (p == a + 8) { cout << "没有找到值为4的数组元素" << endl; } else { cout << "找到了值为" << *p << "的数组元素" << endl; } copy(a, a + 8, b); reverse(a, a + 8); p = find(a, a + 8, 4); if (p == a + 8) { cout << "没有找到值为4的数组元素" << endl; } else { cout << "找到了值为" << *p << "的数组元素" << endl; } copy(a, a + 8, ostream_iterator(cout)); //ostream_iterator 在 cout<(cout)); } int main() { // int num;//不初始化,值是不确定的, static 时默认值是0 // cout << num << endl; //constTest(); //1.6 //coutArray(); //1.9 //charCopy(); //1.10 //coutOperFun(); // expercises4_2(); // expercises4_3(); expercises4_4(); return 0; }