当前位置:网站首页>C Primer Plus 第12章_存储类别、链接和内存管理_代码和练习题
C Primer Plus 第12章_存储类别、链接和内存管理_代码和练习题
2022-06-29 18:10:00 【执手听落花】
随机数函数和静态变量
/* r_drive0.c---测试rand0()函数 */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
extern unsigned int rand0(void);
int main(void)
{
int count;
for (count = 0; count < 5; count++) {
printf("%d\n", rand0());
}
return 0;
}
/* rand0.c --生成随机数*/
static unsigned long int next = 1; /* 种子 */
unsigned int rand0(void)
{
/* 生成伪随机数的魔术公式 */
next = next * 1103515245 + 12345;
return (unsigned int)(next / 65536) % 32768;
}

/* r_drive1.c */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
extern int rand1(void);
extern void srand1(unsigned int x);
int main(void)
{
int count;
unsigned seed;
printf("Please enter your choice for seed.\n");
while (scanf("%u", &seed) == 1) {
srand1(seed);
for (count = 0; count < 5; count++) {
printf("%d\n", rand1());
}
printf("Please enter next seed (q to quit):\n");
}
printf("Done\n");
return 0;
}
/* s_and_r.c */
static unsigned long int next = 1;/* 种子 */
int rand1(void)
{
/* 生成伪随机数的魔术公式 */
next = next * 1103515245 + 12345;
return (unsigned int)(next / 65536) % 32768;
}
void srand1(unsigned int seed)
{
next = seed;
}

掷骰子
/* diceroll.c--掷骰子模拟程序*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include "diceroll.h"
int roll_count = 0;
static int rollem(int sides)
{
int roll;
roll = rand() % sides + 1;
++roll_count; // 计算函数调用次数
return roll;
}
int roll_n_dice(int dice, int sides)
{
int d;
int total = 0;
if (sides < 2) {
printf("Need at least 2 sides.\n");
return -2;
}
if (dice < 1) {
printf("Need at least 1 die.\n");
return -1;
}
for (d = 0; d < dice; d++) {
total += rollem(sides);
}
return total;
}
/* diceroll.h */
#pragma once
extern int roll_count;
int roll_n_dice(int dice, int sides);
/* manydice.c--一起编译 */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include "diceroll.h" /* 为roll_n_dice()提供原型,为roll_count变量提供声明 */
int main(void)
{
int dice, roll;
int sides;
int status;
srand((unsigned int)time(0)); /* 随机种子 */
printf("Enter the number of sides per die, 0 to stop.\n");
while (scanf("%d", &sides) == 1 && sides > 0) {
printf("How many dices?\n");
if (status = scanf("%d", &dice) != 1) {
if (status == EOF) {
break; /* 退出循环 */
}
else {
printf("You should have entered an integer.");
printf(" Let's begin again.\n");
while (getchar() != '\n') {
continue; /* 处理错误的输入 */
}
printf("How many sides? Enter 0 to stop.\n");
continue; /* 进入循环的下一次迭代 */
}
}
roll = roll_n_dice(dice, sides);
printf("You have rolled a %d using %d %d-sided dice.\n",
roll, dice, sides);
}
printf("The rollem() function was called %d times.\n",
roll_count); /* 使用外部变量 */
printf("GOOD FORTUNE TO YOU!\n");
return 0;
}
分配内存:malloc()和free()
/* dym_arr.c ---动态分配数组 */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int main(void)
{
double* ptd;
int max;
int number;
int i = 0;
puts("What is the maximum number of type double entries?");
if (scanf("%d", &max) != 1) {
puts("Number not correct entered -- bye.");
exit(EXIT_FAILURE);
}
ptd = (double*)malloc(max * sizeof(double));
if (ptd == NULL) {
puts("Memory allocation failed. Goodbye.");
exit(EXIT_FAILURE);
}
/* ptd现在指向有max个元素的数组 */
puts("Enter the values (q to quit):");
while (i < max && scanf("%lf", &ptd[i]) == 1) {
++i;
}
printf("Here are your %d entries:\n", number = i);
for (i = 0; i < number; i++) {
printf("%7.2f ", ptd[i]);
if (i % 7 == 6) {
putchar('\n');
}
}
if (i % 7 != 0) {
putchar('\n');
}
puts("Done.");
free(ptd);
return 0;
}

边栏推荐
- Request header field xxxx is not allowed by Access-Control-Allow-Headers in preflight response问题
- Yolov6+tensorrt+onnx: deployment based on win10+tensorrt8+yolov6+onnx
- shell教程之循环语句for,while,until用法
- jdbc认识上手
- 数据分析基础--预测模型
- /usr/bin/ld: warning: **libmysqlclient. so. 20**, needed by //usr/
- 行程卡“摘星”热搜第一!刺激旅游产品搜索量齐上涨
- 美法官裁定,被控掩盖黑客行为的Uber前安全主管必须面对欺诈指控
- Servlet学生管理系统(萌新练手版)
- 第02章_MySQL的数据目录
猜你喜欢

Application and practice of DDD in domestic hotel transaction -- Theory

Markdown knowledge comes gently

Adobe Premiere Foundation - réglage du son (correction du volume, réduction du bruit, tonalité téléphonique, changement de hauteur, égaliseur de paramètres) (XVIII)

AMAZING PANDAVERSE:META”无国界,来2.0新征程激活时髦属性

mysql -connector/j驱动下载

Apache InLong百万亿级数据流处理

行程卡“摘星”热搜第一!刺激旅游产品搜索量齐上涨

通过 hosts文件配置本地域名

Fluent's MSH grid learning

Proxmox VE Install 7.2
随机推荐
How to use idea?
Adobe Premiere foundation - opacity (mixed mode) (XII)
mysql — 清空表中数据
Sd6.22 summary of intensive training
Adobe Premiere foundation - batch material import sequence - variable speed and rewind (recall) - continuous action shot switching - subtitle requirements (13)
Adobe Premiere基础-不透明度(蒙版)(十一)
Sister Juan takes you to learn database -- 5-day sprint Day1
踩坑记:JSON.parse和JSON.stringify
POJ 1975 (transitive closure)
centos 7.5安装mysql 8.0.27----yum
通过 hosts文件配置本地域名
Shell基本语法--流程控制
Adobe Premiere Basics - common video effects (cropping, black and white, clip speed, mirroring, lens halo) (XV)
Adobe Premiere foundation - sound adjustment (volume correction, noise reduction, telephone tone, pitch shifter, parameter equalizer) (XVIII)
【TcaplusDB知识库】TcaplusDB系统用户组介绍
Proxmox VE Install 7.2
Weibo comments on high-performance and high availability architecture design (module 5 of architecture practice camp)
Application and practice of DDD in domestic hotel transaction -- Theory
Adobe Premiere Foundation - réglage du son (correction du volume, réduction du bruit, tonalité téléphonique, changement de hauteur, égaliseur de paramètres) (XVIII)
国内酒店交易DDD应用与实践——理论篇