惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security

博客园 - 慢步前行

实验0 安装GLUT包及工程的创建与运行 实验2 二维图形几何变换 实验1 时间趋势可视化 《鲜活的数据-第2章 处理数据》有关代码 本博客基本不再更新,请移步至我的CSDN博客 WebGL绘制三角形 WebGL画点程序v3 WebGL画点程序v2 WebGL画点程序v1 三步实现修改hosts方式登录谷歌 Maya API编程快速入门 我的高拍仪自动阅卷系统 实验8 标准模板库STL 实验7 多态与模板 实验6 继承 实验5 运算符重载 实验4 类初步 实验3 文件操作 实验1 C++函数
实验2 C++数组与指针
慢步前行 · 2017-03-10 · via 博客园 - 慢步前行

一.实验目的:

    1. 掌握一维数组和二维数组的定义、赋值和输入输出的方法。

    2. 掌握字符数组和字符串函数的使用。

    3. 通过实验进一步掌握指针的概念,会定义和使用指针变量。

    4. 能正确使用数组的指针和指向数组的指针变量。

    5. 能正确使用字符串的指针和指向字符串的指针变量。

    6. 能正确使用引用型变量。

二.实验内容:

  1. 运行调试第5章编程示例5-3,5-4,5-5扑克发牌程序;完成练习题5.3.1,5.4.1, 5.5.1和7.5.2;
  2. 运行调试第6章编程示例6-3数组排序器;完成以下练习:
    1. 改进sort函数;
    2. 用vector改造程序,使其支持变长数组;
    3. 用char类型来改造程序具有更好输入方式,使其能一次性输入多个数组元素;
    4. 用string类型来改造程序具有更好输入方式,使其能一次性输入多个数组元素;

三.示例代码:

  1.第5章编程示例5-3扑克发牌程序:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;

int rand_0toN1(int n);
void draw_a_card();

char *suits[4] = {"hearts", "diamonds", "spades", "clubs"};
char *ranks[13] = {"ace", "two", "three", "four", "five",
"six", "seven", "eight", "nine",
"ten", "jack", "queen", "king" };

int main() {
int n, i;

srand(time(NULL)); // Set seed for random numbers.

while (1) {
cout << "Enter no. of cards to draw (0 to exit): ";
cin >> n;
if (n == 0)
break;
for (i = 1; i <= n; i++)
draw_a_card();
}
return 0;
}

// Draw-a-card function
// Performs one card-draw by getting a random 0-4 and a random
// 0-12. These are then used to index the string arrays, ranks
// and suits.
//
void draw_a_card() {
int r; // Random index (0 thru 12) into ranks array
int s; // Random index (0 thru 3) into suits array

r = rand_0toN1(13);
s = rand_0toN1(4);
cout << ranks[r] << " of " << suits[s] << endl;
}

// Random 0-to-N1 Function.
// Generate a random integer from 0 to N-1.
//
int rand_0toN1(int n) {
return rand() % n;
}

  2.第6章编程示例6-3数组排序器:

#include <iostream>
using namespace std;

void sort(int n);
void swap(int *p1, int *p2);

int a[10];

int main () {
int i;

for (i = 0; i < 10; i++) {
cout << "Enter array element #" << i << ": ";
cin >> a[i];
}
sort(10);

cout << "Here are all the array elements, sorted:" << endl;
for (i = 0; i < 10; i++)
cout << a[i] << " ";

cout << endl;
system("PAUSE");
return 0;
}

// Sort array function: sort array named a, having n elements.
// 
void sort (int n) {
int i, j, low;

for(i = 0; i < n - 1; i++) {

// This part of the loop finds the lowest
// element in the range i to n-1; the index
// is set to the variable named low.

low = i;
for (j = i + 1; j < n; j++)
if (a[j] < a[low])
low = j;

// This part of the loop performs a swap if
// needed.

if (i != low)
swap(&a[i], &a[low]);
}
}

// Swap function.
// Swap the values pointed to by p1 and p2.
//
void swap(int *p1, int *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}