当前位置:网站首页>C Primer Plus Chapter 12_ Storage categories, links, and memory management_ Codes and exercises
C Primer Plus Chapter 12_ Storage categories, links, and memory management_ Codes and exercises
2022-06-29 18:37:00 【Holding hands to listen to falling flowers】
Random number functions and static variables
/* r_drive0.c--- test rand0() function */
#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 -- Generate random number */
static unsigned long int next = 1; /* seeds */
unsigned int rand0(void)
{
/* Magic formula for generating pseudo-random numbers */
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;/* seeds */
int rand1(void)
{
/* Magic formula for generating pseudo-random numbers */
next = next * 1103515245 + 12345;
return (unsigned int)(next / 65536) % 32768;
}
void srand1(unsigned int seed)
{
next = seed;
}
Dice
/* diceroll.c-- Dice simulation program */
#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; // Count the number of function calls
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-- Compile together */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include "diceroll.h" /* by roll_n_dice() Provide prototypes , by roll_count Variables provide declarations */
int main(void)
{
int dice, roll;
int sides;
int status;
srand((unsigned int)time(0)); /* Random seeds */
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; /* Exit loop */
}
else {
printf("You should have entered an integer.");
printf(" Let's begin again.\n");
while (getchar() != '\n') {
continue; /* Handle wrong input */
}
printf("How many sides? Enter 0 to stop.\n");
continue; /* Enter the next iteration of the loop */
}
}
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); /* Use external variables */
printf("GOOD FORTUNE TO YOU!\n");
return 0;
}
Allocate memory :malloc() and free()
/* dym_arr.c --- Assign arrays dynamically */
#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 Now point to max Array of elements */
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;
}
边栏推荐
- Usage of BeanUtils property replication
- Travel card "star picking" hot search first! Stimulate the search volume of tourism products to rise
- Adobe Premiere foundation - time remapping (10)
- WBF:检测任务NMS后虑框新方式?
- Configure the local domain name through the hosts file
- Chapter 02_ MySQL data directory
- MySQL 企业级开发规范
- codeforces每日5题(均1700)-第二天
- Sd6.25 summary of intensive training
- Record that the server has been invaded by viruses: the SSH password has been changed, the login fails, the malicious program runs full of CPU, the jar package fails to start automatically, and you ha
猜你喜欢
Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
Adobe Premiere foundation - cool text flash (14)
Adobe Premiere基礎-聲音調整(音量矯正,降噪,電話音,音高換擋器,參數均衡器)(十八)
Apache InLong百万亿级数据流处理
Request header field XXXX is not allowed by access control allow headers in preflight response
JDBC knowledge
Adobe Premiere基础-不透明度(混合模式)(十二)
Adobe Premiere foundation - time remapping (10)
Cannot retrieve repository metadata 处理记录
Shandong University project training (VII) add navigation bar to select city
随机推荐
My first experience of remote office | community essay solicitation
什么是多范式编程语言,其中的“多范式”是什么意思?
数据仓库模型分层ODS、DWD、DWM实战
Error building sqlsession problem
数据分析基础--预测模型
Error building SqlSession问题
Adobe Premiere foundation - material nesting (animation of Tiktok ending avatar) (IX)
Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)
Goldfish rhca memoirs: do447 building advanced job workflow -- using fact cache to improve performance
Adobe Premiere基础-常用的视频特效(裁剪,黑白,剪辑速度,镜像,镜头光晕)(十五)
MySQL -connector/j driver download
Adobe Premiere Basics - general operations for editing material files (offline files, replacing materials, material labels and grouping, material enabling, convenient adjustment of opacity, project pa
Understanding of strong caching and negotiation caching
Adobe Premiere foundation - opacity (matte) (11)
Servlet学生管理系统(萌新练手版)
RocketMQ的tag过滤和sql过滤
如何将OAK相机当做网络摄像头使用?
Sword finger offer 34 Path DFS method for binary tree neutralization
shell教程之循环语句for,while,until用法
【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍