C++语言程序设计 (郑莉)第六章 数组、指针和字符串




cpp 复制代码
#include <iostream>
using namespace std;

int main() {
	int a[10], b[10];
	for (int i = 0; i < 10; i++) {
		a[i] = 2 * i - 1;
		b[10 - i - 1] = a[i];
	}
	for (const auto& e : a) {
		cout << e << " ";
	}
	cout << endl;
	for (int i = 0; i < 10; i++) {
		cout << b[i] << " ";
	}
	cout << endl;
	return 0;
}







cpp 复制代码
#include <iostream>
using namespace std;

void rowSum(int a[][4], int nRow) {
	for (int i = 0; i < nRow; i++) {
		for (int j = 1; j < 4; j++) {
			a[i][0] += a[i][j];
		}
	}
}

int main() {
	int table[3][4] = { {1,2,3,4},{2,3,4,5},{3,4,5,6} };
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 4; j++) {
			cout << table[i][j] << " ";
		}
		cout << endl;
	}
	rowSum(table, 3);
	for (int i = 0; i < 3; i++) {
		cout << "Sum of row" << i << "is" << table[i][0] << endl;
	}
	return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;

void rowSum(const int a[][4], int nRow,int s[]) {
	for (int i = 0; i < nRow; i++) {
		for (int j = 0; j < 4; j++) {
			s[i] += a[i][j];
		}
	}
}

int main() {
	int table[3][4] = { {1,2,3,4},{2,3,4,5},{3,4,5,6} };
	int sum[3] = {0};
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 4; j++) {
			cout << table[i][j] << " ";
		}
		cout << endl;
	}
	rowSum(table, 3, sum);
	for (int i = 0; i < 3; i++) {
		cout << "Sum of row" << i << "is" << sum[i] << endl;
	}
	return 0;
}





cpp 复制代码
#pragma once
#ifndef _POINT_H
#define _POINT_H

class Point {
public:
	Point();
	Point(int x, int y);
	~Point();
	void move(int newX, int newY);
	int getX()const {
		return x;
	}
	int getY()const {
		return y;
	}
	static void showCount();
private:
	int x, y;
};
#endif
cpp 复制代码
#include <iostream>
#include "Point.h"
using namespace std;

Point::Point() :x(0), y(0) {
	cout << "Default Constructor called" << endl;
}

Point::Point(int x, int y) : x(x), y(y) {
	cout << "Constructor called" << endl;
}

Point::~Point() {
	cout << "Destructor called" << endl;
}

void Point::move(int newX, int newY) {
	cout << "Moving the point to (" << newX << "," << newY << ")" << endl;
	x = newX;
	y = newY;
}
cpp 复制代码
#include <iostream>
#include "Point.h"

using namespace std;
int main() {
	cout << "Entering main..." << endl;
	Point a[2];
	for (int i = 0; i < 2; i++) {
		a[i].move(i + 10, i + 20);
	}
	cout << "Exiting main..." << endl;
	return 0;
}

















cpp 复制代码
#include <iostream>
using namespace std;
int main() {
	int a[10] = { 1,2,3,4,5,6,7,8,9,0 };
	for (int i = 0; i < 10; i++) {
		cout << a[i] << " ";
	}
	cout << endl;
	return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;
int main() {
	int a[10] = { 1,2,3,4,5,6,7,8,9,0 };
	for (int i = 0; i < 10; i++) {
		cout << *(a+i) << " ";
	}
	cout << endl;
	return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;
int main() {
	int a[10] = { 1,2,3,4,5,6,7,8,9,0 };
	for (int* p = a; p < (a + 10); p++) {
		cout << *p << " ";
	}
	cout << endl;
	return 0;
}







cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
inline void test(const char* title, bool value) {
	cout << title << "returns " << (value ? "true" : false) << endl;
}

int main() {
	string s1 = "DEF";
	cout << "s1 is " << s1 << endl;
	string s2;
	cout << "Please enter s2:";
	cin >> s2;
	cout << "length of s2:" << s2.length() << endl;

	test("s1<=\"ABC\"",s1<="ABC");
	test("\"DEF\"<=s1", "DEF" <= s1);
	s2 += s1;
	cout << "s2=s2+s1:" << s2 << endl;
	cout << "length of s2:" << s2.length() << endl;
	return 0;
}


cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
	for (int i = 0; i < 2; i++) {
		string city, state;
		getline(cin, city, ',');
		getline(cin, state);
		cout << "City:" << city << "State:" << state << endl;
	}
	return 0;
}


cpp 复制代码
#include <iostream>
using namespace std;
int main() {
	int line1[] = { 1,0,0 };
	int line2[] = { 0,1,0 };
	int line3[] = { 0,0,1 };
	int* pLine[3] = { line1,line2,line3 };
	cout << "Matrix test:" << endl;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout << pLine[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}




cpp 复制代码
#include <iostream>
using namespace std;

void splitFloat(float x, int* intPart, float* fracPart) {
	*intPart = static_cast<int>(x);
	*fracPart = x - *intPart;
}

int main() {
	cout << "Enter 3 float point numbers:" << endl;
	for (int i = 0; i < 3; i++) {
		float x, f;
		int n;
		cin >> x;
		splitFloat(x, &n, &f);
		cout << "Integer Part=" << n << "Fraction Part=" << f << endl;
	}
	return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;

const int N = 6;
void print(const int* p, int n);

int main() {
	int array[N];
	for (int i = 0; i < N; i++) {
		cin >> array[i];
	}
	print(array, N);
	return 0;
}

void print(const int* p, int n) {
	cout << "{" << *p;
	for (int i = 1; i < n; i++) {
		cout << "," << *(p + i);
	}
	cout << "}" << endl;
}





cpp 复制代码
#include <iostream>
using namespace std;

int main() {
	int array[10];
	int* search(int* a, int num);
	for (int i = 0; i < 10; i++) {
		cin >> array[i];
	}
	int* zeroptr = search(array, 10);
	return 0;
}

int* search(int* a, int num) {
	for (int i = 0; i < num; i++) {
		if (a[i] == 0) {
			return &a[i];
		}
	}
	return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;
int main() {
	int* newintvar();
	int* intptr = newintvar();
	*intptr = 5;
	delete intptr;
	return 0;
}

int* newintvar() {
	int* p = new int();
	return p;
}




cpp 复制代码
#include <iostream>
using namespace std;
int compute(int a, int b, int(*func)(int, int)) {
	return func(a, b);
}

int max(int a, int b) {
	return (a > b) ? a : b;
}

int min(int a, int b) {
	return (a < b) ? a : b;
}

int sum(int a, int b) {
	return a + b;
}

int main() {
	int a, b, res;
	cout << "请输入整数a";
	cin >> a;
	cout << "请输入整数b";
	cin >> b;
	res = compute(a, b, &max);
	cout << "Max of " << a << "and" << b << "is" << res << endl;
	res = compute(a, b, &min);
	cout << "Min of " << a << "and" << b << "is" << res << endl;
	res = compute(a, b, &sum);
	cout << "Sum of " << a << "and" << b << "is" << res << endl;
}


cpp 复制代码
#include <iostream>
using namespace std;

class Point {
public:
	Point(int x=0,int y=0):x(x),y(y){}
	int getX() { return x; }
	int getY() { return y; }
private:
	int x, y;
};

int main() {
	Point a(4, 5);
	Point* p1 = &a;
	cout << p1->getX() << endl;
	cout << a.getX() << endl;
	return 0;
}








cpp 复制代码
#include <iostream>
using namespace std;

class Point {
public:
	Point() :x(0), y(0) {
		cout << "Default Constructor called" << endl;
	}
	Point(int x, int y):x(x),y(y) {
		cout << "Constructor called" << endl;
	}
	~Point() { cout << "Destructor called" << endl; }
	int getX() { return x; }
	int getY() { return y; }
	void move(int newX, int newY) {
		x = newX;
		y = newY;
	}
private:
	int x, y;
};

int main() {
	cout << "Step one" << endl;
	Point* ptr1 = new Point;
	delete ptr1;
	cout << "Step two" << endl;
	ptr1 = new Point(1, 2);
	delete ptr1;
	return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;

class Point {
public:
	Point() :x(0), y(0) {
		cout << "Default Constructor called" << endl;
	}
	Point(int x, int y) :x(x), y(y) {
		cout << "Constructor called" << endl;
	}
	~Point() { cout << "Destructor called" << endl; }
	int getX() { return x; }
	int getY() { return y; }
	void move(int newX, int newY) {
		x = newX;
		y = newY;
	}
private:
	int x, y;
};

int main() {
	Point* ptr = new Point[2];
	ptr[0].move(5, 10);
	ptr[1].move(15, 20);
	cout << "Deleting..." << endl;
	delete[] ptr;
	return 0;
}
cpp 复制代码
#include <iostream>
using namespace std;
int main() {
	int(*cp)[9][8] = new int[7][9][8];
	for (int i = 0; i < 7; i++) {
		for (int j = 0; j < 9; j++) {
			for (int k = 0; k < 8; k++) {
				*(*(*(cp + i) + j) + k) = (i * 100 + j * 10 + i);
			}
		}
	}
	for (int i = 0; i < 7; i++) {
		for (int j = 0; j < 9; j++) {
			for (int k = 0; k < 8; k++) {
				cout << cp[i][j][k] << " ";
			}
			cout << endl;
		}
		cout << endl;
	}
	delete[] cp;
	return 0;
}
cpp 复制代码
#include <iostream>
#include <cassert>
using namespace std;

#include <iostream>
using namespace std;

class Point {
public:
	Point() :x(0), y(0) {
		cout << "Default Constructor called" << endl;
	}
	Point(int x, int y) :x(x), y(y) {
		cout << "Constructor called" << endl;
	}
	~Point() { cout << "Destructor called" << endl; }
	int getX() { return x; }
	int getY() { return y; }
	void move(int newX, int newY) {
		x = newX;
		y = newY;
	}
private:
	int x, y;
};

class ArrayOfPoints {
public:
	ArrayOfPoints(int size):size(size) {
		points = new Point[size];
	}
	~ArrayOfPoints() {
		cout << "Deleting..." << endl;
		delete[] points;
	}
	Point& element(int index) {
		assert(index >= 0 && index < size);
		return points[index];
	}
private:
	Point* points;
	int size;
};

int main() {
	int count;
	cout << "Please enter the count of points";
	cin >> count;
	ArrayOfPoints points(count);
	points.element(0).move(5, 0);
	points.element(1).move(15, 20);
	return 0;
}






cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;
double average(const vector<double>& arr) {
	double sum = 0;
	for (unsigned i = 0; i < arr.size(); i++) {
		sum += arr[i];
	}
	return sum / arr.size();
}

int main() {
	unsigned n;
	cout << "n=";
	cin >> n;
	vector<double> arr(n);
	cout << "Please input" << n << "real numbers" << endl;
	for (unsigned i = 0; i < n; i++) {
		cin >> arr[i];
	}
	cout << "Average=" << average(arr) << endl;
	return 0;
}
cpp 复制代码
#include <iostream>
#include <vector>

int main() {
	std::vector<int> v = { 1,2,3 };
	for (auto i = v.begin(); i != v.end(); ++i) {
		std::cout << *i << std::endl;
	}
	for (auto e : v) {
		std::cout << e << std::endl;
	}
	return 0;
}


cpp 复制代码
#include <iostream>
#include <cassert>
using namespace std;

class Point {
public:
	Point() :x(0), y(0) {
		cout << "Default Constructor called" << endl;
	}
	Point(int x, int y) :x(x), y(y) {
		cout << "Constructor called" << endl;
	}
	~Point() { cout << "Destructor called" << endl; }
	int getX() { return x; }
	int getY() { return y; }
	void move(int newX, int newY) {
		x = newX;
		y = newY;
	}
private:
	int x, y;
};

class ArrayOfPoints {
public:
	ArrayOfPoints(int size) :size(size) {
		points = new Point[size];
	}
	~ArrayOfPoints() {
		cout << "Deleting..." << endl;
		delete[] points;
	}
	Point& element(int index) {
		assert(index >= 0 && index < size);
		return points[index];
	}
private:
	Point* points;
	int size;
};

int main() {
	int count;
	cout << "Please enter the count of points:";
	cin >> count;
	ArrayOfPoints pointsArray1(count);
	pointsArray1.element(0).move(5, 10);
	pointsArray1.element(1).move(15, 20);
	ArrayOfPoints pointsArray2(pointsArray1);
	cout << "Copy of pointsarary1" << endl;
	cout << "Point_0 of array2" << pointsArray2.element(0).getX() << "," << pointsArray2.element(0).getY() << endl;
	cout << "Point_1 of array2" << pointsArray2.element(1).getX() << "," << pointsArray2.element(1).getY() << endl;

	pointsArray1.element(0).move(25, 30);
	pointsArray1.element(1).move(35, 40);
	cout << "After the moving of pointsArray1:" << endl;
	cout << "Point_0 of array2" << pointsArray2.element(0).getX() << "," << pointsArray2.element(0).getY() << endl;
	cout << "Point_1 of array2" << pointsArray2.element(1).getX() << "," << pointsArray2.element(1).getY() << endl;
	return 0;
}
cpp 复制代码
#include <iostream>
#include <cassert>
using namespace std;
class Point {
public:
	Point() :x(0), y(0) {
		cout << "Default Constructor called" << endl;
	}
	Point(int x, int y) :x(x), y(y) {
		cout << "Constructor called" << endl;
	}
	~Point() { cout << "Destructor called" << endl; }
	int getX() { return x; }
	int getY() { return y; }
	void move(int newX, int newY) {
		x = newX;
		y = newY;
	}
private:
	int x, y;
};

class ArrayOfPoints {
public:
	ArrayOfPoints(int size) :size(size) {
		points = new Point[size];
	}
	ArrayOfPoints(const ArrayOfPoints& pointsArray);
	~ArrayOfPoints() {
		cout << "Deleting..." << endl;
		delete[] points;
	}
	Point& element(int index) {
		assert(index >= 0 && index < size);
		return points[index];
	}
private:
	Point* points;
	int size;
};

ArrayOfPoints::ArrayOfPoints(const ArrayOfPoints& v) {
	size = v.size;
	points = new Point[size];
	for (int i = 0; i < size; i++) {
		points[i] = v.points[i];
	}
}

int main() {
	int count;
	cout << "Please enter the count of points:";
	cin >> count;
	ArrayOfPoints pointsArray1(count);
	pointsArray1.element(0).move(5, 10);
	pointsArray1.element(1).move(15, 20);
	ArrayOfPoints pointsArray2(pointsArray1);
	cout << "Copy of pointsarary1" << endl;
	cout << "Point_0 of array2" << pointsArray2.element(0).getX() << "," << pointsArray2.element(0).getY() << endl;
	cout << "Point_1 of array2" << pointsArray2.element(1).getX() << "," << pointsArray2.element(1).getY() << endl;

	pointsArray1.element(0).move(25, 30);
	pointsArray1.element(1).move(35, 40);
	cout << "After the moving of pointsArray1:" << endl;
	cout << "Point_0 of array2" << pointsArray2.element(0).getX() << "," << pointsArray2.element(0).getY() << endl;
	cout << "Point_1 of array2" << pointsArray2.element(1).getX() << "," << pointsArray2.element(1).getY() << endl;
	return 0;
}


相关推荐
禹曦a9 小时前
Java实战:Spring Boot 构建电商订单管理系统RESTful API
java·开发语言·spring boot·后端·restful
superman超哥9 小时前
精确大小迭代器(ExactSizeIterator):Rust性能优化的隐藏利器
开发语言·后端·rust·编程语言·rust性能优化·精确大小迭代器
芒克芒克9 小时前
虚拟机类加载机制
java·开发语言·jvm
陌路209 小时前
C++28 STL容器--array
开发语言·c++
im_AMBER9 小时前
Leetcode 100 在链表中插入最大公约数
数据结构·c++·笔记·学习·算法·leetcode·链表
FPGAI9 小时前
Python之函数
开发语言·python
csbysj20209 小时前
NumPy Ndarray 对象
开发语言
Z1Jxxx9 小时前
删除字符串2
开发语言·c++·算法
小CC吃豆子9 小时前
Qt的信号与槽机制
开发语言·数据库·qt