我是靠谱客的博主 超帅背包,最近开发中收集的这篇文章主要介绍《C Primer Plus》(第6版)编程练习——第12章,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

第1题

#include <stdio.h>
void critic(int* u);
int main(void)
{
int units;
printf("How many pounds to a firkin of butter?n");
scanf("%d", &units);
while (units != 56)
critic(&units);
printf("You must have looked it up!n");
return 0;
}
void critic(int* u)
{
printf("No luck, my friend. Try again.n");
scanf("%d", u);
}
// or use a return value:
// units = critic(); 
// and have critic look like this:
/*
int critic(void)
{
int u;
printf("No luck, my friend. Try again.n");
scanf("%d", &u);
return u;
}
*/
// or have main() collect the next value for units

第2题

// pe12-2a.h
#pragma once
void set_mode(int m);
void get_info(void);
void show_info(void);
// pe12-2a.c
#include <stdio.h>
static int mod = 0;
static double dis = 0;
static double fue = 0;
void set_mode(int m)
{
if (m == 0 || m == 1)
mod = m;
else
{
printf("Invalid mode specified. ");
if (mod == 0)
printf("Mode 0(metric) used.n");
else
printf("Mode 1(US) used.n");
}
}
void get_info(void)
{
if (mod == 0)
{
printf("Enter distance traveled in kilometers: ");
scanf("%lf", &dis);
printf("Enter fuel consumed in liters: ");
scanf("%lf", &fue);
}
else
{
printf("Enter distance traveled in miles: ");
scanf("%lf", &dis);
printf("Enter fuel consumed in gallons: ");
scanf("%lf", &fue);
}
}
void show_info(void)
{
printf("Fuel consumption is ");
if (mod == 0)
printf("%.2f liters per 100 km.n", 100 * fue / dis);
else
printf("%.1f miles per gallon.n", dis / fue);
// pe12-2b.c
#include <stdio.h>
#include "pe12-2a.h"
int main(void)
{
int mode;
printf("Enter 0 for metric mode, 1 for US mode: ");
scanf("%d", &mode);
while (mode >= 0)
{
set_mode(mode);
get_info();
show_info();
printf("Enter 0 for metric mode, 1 for US mode");
printf(" (-1 to quit): ");
scanf("%d", &mode);
}
printf("Done.n");
return 0;
}

第3题

// pe12-3a.h
#pragma once
#define METRIC 0
#define US 1
#define USE_RECENT 2
void check_mode(int* pm);
void get_info(int mode, double* pd, double* pf);
void show_info(int mode, double distance, double fuel);
// pe12-3a.c
#include <stdio.h>
#include "pe12-3a.h"
void check_mode(int* pm)
{
if (*pm != METRIC && *pm != US)
{
printf("Invalid mode specified. Mode %dn", *pm);
printf("Previous mode will be used.n");
*pm = USE_RECENT;
}
}
void get_info(int mode, double* pd, double* pf)
{
if (mode == METRIC)
printf("Enter distance traveled in kilometers: ");
else
printf("Enter distance traveled in miles: ");
scanf("%lf", pd);
if (mode == METRIC)
printf("Enter fuel consumed in liters: ");
else
printf("Enter fuel consumed in gallons: ");
scanf("%lf", pf);
}
void show_info(int mode, double distance, double fuel)
{
printf("Fuel consumption is ");
if (mode == METRIC)
printf("%.2f liters per 100 km.n", 100 * fuel / distance);
else
printf("%.1f miles per gallon.n", distance / fuel);
}
// pe12-3b.c
#include <stdio.h>
#include "pe12-3a.h"
int main(void)
{
int mode;
int prev_mode = METRIC;
double distance, fuel;
printf("Enter 0 for metric mode, 1 for US mode: ");
scanf("%d", &mode);
while (mode >= 0)
{
check_mode(&mode);
if (mode == USE_RECENT)
mode = prev_mode;
prev_mode = mode;
get_info(mode, &distance, &fuel);
show_info(mode, distance, fuel);
printf("Enter 0 for metric mode, 1 for US mode");
printf(" (-1 to quit): ");
scanf("%d", &mode);
}
printf("Done.n");
return 0;
}

第4题

#include <stdio.h>
int func(void);
int main(void)
{
int i;
int ct = 0;
int loop;
printf("Enter the test numbers: ");
scanf("%d", &loop);
for (i = 0; i < loop; i++)
ct = func();
printf("The function has called %d times.n", ct);
return 0;
}
int func(void)
{
static int count = 0;
return ++count;
}

第5题

#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
void sort(int val[], int num);
void print(int val[], int num);
int main(void)
{
int num[SIZE];
int i;
unsigned int seed;
printf("This program can generate %d random numbers from 1 to 10n"
"and arrange them in descending order.n", SIZE);
printf("Please enter your choice for seed: ");
while (scanf("%u", &seed) == 1)
{
srand(seed);
for (i = 0; i < SIZE; i++)
num[i] = rand() % 10 + 1;
printf("Initial numbers:n");
print(num, SIZE);
sort(num, SIZE);
printf("Sorted numbers:n");
print(num, SIZE);
printf("nPlease enter next seed (q to quit): ");
}
printf("Bye.n");
return 0;
}
void sort(int val[], int num)
{
int i, j;
int temp;
for (i = 0; i < num - 1; i++)
for (j = i + 1; j < num; j++)
{
if (val[i] < val[j])
{
temp = val[i];
val[i] = val[j];
val[j] = temp;
}
}
}
void print(int val[], int num)
{
int i;
for (i = 0; i < num; i++)
{
printf("%2d ", val[i]);
if (i % 10 == 9)
printf("n");
}
if (i % 10 != 0)
// if last line not complete
printf("n");
}

第6题

#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000
int main(void)
{
unsigned int seed;
int arr[SIZE];
int times[10];
int count = 0;
int i, j;
while (count++ < 10)
{
printf("Enter your choice for seed: ");
scanf("%u", &seed);
srand(seed);
for (i = 0; i < SIZE; i++)
arr[i] = rand() % 10 + 1;
for (j = 0; j < 10; j++)
times[j] = 0;
for (i = 0; i < SIZE; i++)
for (j = 0; j < 10; j++)
{
if (arr[i] == j + 1)
times[j]++;
}
for (j = 0; j < 10; j++)
printf("No.%2d appeared %3d times.n", j + 1, times[j]);
}
printf("Bye.n");
return 0;
}

第7题

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rollem(int sides);
int main(void)
{
int d, dice;
int sides;
int s, sets;
int roll;
int status;
srand((unsigned int)time(0));
printf("Enter the number of sets; enter q to stop.n");
while (scanf("%d", &sets) == 1 && sets > 0)
{
printf("How many sides and how many dice?n");
if ((status = scanf("%d %d", &sides, &dice)) != 2)
{
if (status == EOF)
break;
else
{
printf("You should have entered tow integers. Let's begin again.n");
while (getchar() != 'n')
continue;
printf("Enter the number of sets; enter q to stop.n");
continue;
}
}
if (sides < 2 || dice < 1)
{
printf("Need at least 2 sides and at least 1 die.n");
printf("Enter the number of sets; enter q to stop.n");
continue;
}
printf("Here are %d sets of %d %d-sided throws.n", sets, dice, sides);
for (s = 0; s < sets; s++)
{
for (roll = 0, d = 0; d < dice; d++)
roll += rollem(sides);
printf("%d ", roll);
if (s % 15 == 14)
printf("n");
}
if (s % 15 != 0)
printf("n");
printf("How many sets? Enter q to stop.n");
}
printf("GOOD FORTUNE TO YOU!n");
return 0;
}
int rollem(int sides)
{
int roll;
roll = rand() % sides + 1;
return roll;
}

第8题

#include <stdio.h>
#include <stdlib.h>
int* make_array(int elem, int val);
void show_array(const int ar[], int n);
int main(void)
{
int* pa;
int size;
int value;
printf("Enter the number of elements: ");
while (scanf("%d", &size) == 1 && size > 0)
{
printf("Enter the initialization value: ");
scanf("%d", &value);
pa = make_array(size, value);
if (pa)
{
show_array(pa, size);
free(pa);
}
printf("Enter the number of elements (<1 to quit): ");
}
printf("Done.n");
return 0;
}
int* make_array(int elem, int val)
{
int* pt;
int i;
pt = (int*)malloc(elem * sizeof(int));
if (pt)
for (i = 0; i < elem; i++)
pt[i] = val;
return pt;
}
void show_array(const int ar[], int n)
{
int i;
printf("The array of %d elements is as follows:n", n);
for (i = 0; i < n; i++)
{
printf("%d ", ar[i]);
if (i % 8 == 7)
printf("n");
}
if (i % 8 != 0)
printf("n");
}

第9题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 30
int main(void)
{
char(*pw)[LEN];
char temp[LEN];
int size;
int i;
printf("How many words do you wish to enter? ");
while (scanf("%d", &size) == 1 && size > 0)
{
printf("Enter %d words now:n", size);
pw = (char(*)[LEN])malloc(size * LEN * sizeof(char));
if (pw == NULL)
{
printf("Memory allocation failed. Goodbye.n");
exit(EXIT_FAILURE);
}
for (i = 0; i < size; i++)
{
scanf("%s", temp);
strcpy(pw[i], temp);
}
while (getchar() != 'n')
continue;
// 处理多余的输入
printf("Here are your words:n");
for (i = 0; i < size; i++)
printf("%sn", pw[i]);
free(pw);
printf("Enter next number of words to enter (<1 to quit): ");
}
printf("Done.n");
return 0;
}

最后

以上就是超帅背包为你收集整理的《C Primer Plus》(第6版)编程练习——第12章的全部内容,希望文章能够帮你解决《C Primer Plus》(第6版)编程练习——第12章所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部