当前位置:网站首页>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;
}

边栏推荐
- Shell tutorial circular statements for, while, until usage
- PostgreSQL database system table
- Mysql database daily backup and scheduled cleanup script
- mysql -connector/j驱动下载
- Anaconda安装并配置jupyter notebook远程
- MySql存储过程循环的使用分析详解
- 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
- About microservices
- Sd6.24 summary of intensive training
- C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
猜你喜欢
随机推荐
Source code installation mavros
Encryption and decryption of 535 tinyurl
Sister Juan takes you to learn database -- 5-day dash day4
面试题 10.10. 数字流的秩
Adobe Premiere基础-炫酷文字快闪(十四)
codeforces每日5题(均1700)-第二天
Understanding of strong caching and negotiation caching
BeanUtils属性复制的用法
MySQL数据库每日备份并定时清理脚本
WBF: new method of NMS post filter frame for detection task?
Adobe Premiere基础-不透明度(混合模式)(十二)
报错Failed to allocate graph: MYRIAD device is not opened.
Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)
/usr/bin/ld: warning: **libmysqlclient. so. 20**, needed by //usr/
JWT login authentication
Programmer Resource Recommendation Guide
Data-* attribute usage
第八届“互联网+”大赛 | 云原生赛道邀你来挑战
剑指 Offer 34. 二叉树中和为某一值的路径-dfs法
SD6.23集训总结









