疑问:
cout << "Score" << static_cast<float>(numCorrect)/NUM_QUES*100 << "%";
使用static_cast<float>强制转换?
例6-1 数组的定义与使用
//例6-1 数组的定义与使用
#include <iostream>
using namespace std;
int main() {
int a[10], b[10];
for(int i = 0; i < 10; i++) {
a[i] = i * 2 - 1;
b[10 - i - 1] = a[i];
}
for(int i = 0; i < 10; i++) {
cout << "a[" << i << "] = " << a[i] << " ";
cout << "b[" << i << "] = " << b[i] << endl;
}
return 0;
}
例6-1-2 求 Fibonacci 数列的前 20 项
//例6-1-2 求 Fibonacci 数列的前 20 项
#include <iostream>
using namespace std;
int main() {
int f[20] = {1,1};
for(int i = 2;i<20;i++)
f[i] = f[i-2]+f[i-1];
for (int i=0;i<20;i++) {
if (i % 5 == 0) cout << endl;
cout.width(12);
cout << f[i];
}
return 0;
}
例6-1-3 循环从键盘读入若干组选择题答案,计算并输出每组答案的正确率,直到输入ctrl+z为止。 每组连续输入5个答案,每个答案可以是'a'..'d'。
#include <iostream>
using namespace std;
int main() {
const char key[] = {'a','c','b','a','d'};
const int NUM_QUES = 5;
char c;
int ques = 0,numCorrect = 0;
while(cin.get(c)){
if (c !='\n'){
if(c == key[ques]){
numCorrect++;cout << " ";
}
else
cout << "*";
ques++;
}else{
cout << "Score" << static_cast<float>(numCorrect)/NUM_QUES*100 << "%";
ques = 0; numCorrect = 0; cout << endl;
}
}
return 0;
}
二维数组
数组作为函数参数
- 数组元素作实参,与单个变量一样。
- 数组名作参数,形、实参数都应是数组名(实质上是地址,关于地址详见6.2),类型要一样,传送的是数组首地址。对形参数组的改变会直接影响到实参数组。
例 6-2 使用数组名作为函数参数
主函数中初始化一个二维数组,表示一个矩阵,并将每个元素都输出,然后调用子函数,
分别计算每一行的元素之和,将和直接存放在每行的第一个元素中,返回主函数之
后输出各行元素的和。
//例 6-2 使用数组名作为函数参数
#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;
}
对象数组
- 通过初始化列表赋值 Point a[2]={Point(1,2),Point(3,4)};
- 数组中每一个元素对象被创建时,系统都会调用类构造函数初始化该对象。
- 如果没有为数组元素指定显式初始值,数组元素便使用默认值初始化(调用默认构造函数)。
例 6-3 对象数组应用举例
//Point.h
#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 //_POINT_H
//Point.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;
}
//6-3.cpp
#include "Point.h"
#include <iostream>
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;
}