(编译环境 Microsoft Visual Studio 2019)
1.
/*1.*/
#include<stdio.h>
void critic(int* p);
int main(void)
{
int units;
printf("How many pounds to a firkin of butter?\n");
scanf_s("%d", &units);
while (units != 56)
critic(&units);
printf("You must have looked it up!\n");
return 0;
}
void critic(int* p)
{
printf("No luck,my friend.Try again.\n");
scanf_s("%d", p);
}
2.
/*2.*/
//pe12-2a.h
void set_mode(int n);
void get_info(void);
void show_info(void);
//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_s("%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_s("%d", &mode);
}
printf("Done.\n");
return 0;
}
//pe12-2a.c
#include<stdio.h>
static int mod;
static float distance;
static float fuel;
void set_mode(int n)
{
static int last = 0;
if (n > 1)
{
printf("Invalid mode specified. Mode %s used.\n",
last == 1 ? "1(US)" : "0(metric)");
mod = last;
}
else
{
mod = n;
last = mod;
}
}
void get_info(void)
{
if (mod == 0)
{
printf("Enter distance traveled in kilometers: ");
while (!scanf_s("%f", &distance))
{
while (getchar() != '\n')
continue;
printf("Please enter digit: ");
}
printf("Enter fuel consumed in liters: ");
while (!scanf_s("%f", &fuel))
{
while (getchar() != '\n')
continue;
printf("Please enter digit: ");
}
}
else
{
printf("Enter distance traveled in miles: ");
while (!scanf_s("%f", &distance))
{
while (getchar() != '\n')
continue;
printf("Please enter digit: ");
}
printf("Enter fuel consumed in gallons: ");
while (!scanf_s("%f", &fuel))
{
while (getchar() != '\n')
continue;
printf("Please enter digit: ");
}
}
}
void show_info(void)
{
if (mod == 0)
printf("Fuel consumption is %.2f liters per 100km.\n",
fuel / (distance / 100));
else
printf("Fuel consumption is %.1f miles per gallon.\n",
distance / fuel);
}
3.
/*3.*/
//pe12-3a.h
void set_mode(int n,int *mod,int *last);
void get_info(int mod, float* fuel, float* distance);
void show_info(int mod,float fuel,float distance);
//pe12-3b.c
#include<stdio.h>
#include "pe12-2a.h"
int main(void)
{
int mode;
int mod, last;
float distance, fuel;
printf("Enter 0 for metric mode,1 for US mode: ");
scanf_s("%d", &mode);
while (mode >= 0)
{
set_mode(mode, &mod, &last);
get_info(mod, &fuel, &distance);
show_info(mod, fuel, distance);
printf("Enter 0 for metric mode, 1 for US mode");
printf(" (-1 to quit): ");
scanf_s("%d", &mode);
}
printf("Done.\n");
return 0;
}
//pe12-3a.c
#include<stdio.h>
void set_mode(int n,int *mod,int *last)
{
if (n > 1)
{
printf("Invalid mode specified. Mode %s used.\n",
*last == 1 ? "1(US)" : "0(metric)");
*mod = *last;
}
else
{
*mod = n;
*last = n;
}
}
void get_info(int mod,float *fuel,float *distance)
{
if (mod == 0)
{
printf("Enter distance traveled in kilometers: ");
while (!scanf_s("%f", distance))
{
while (getchar() != '\n')
continue;
printf("Please enter digit: ");
}
printf("Enter fuel consumed in liters: ");
while (!scanf_s("%f", fuel))
{
while (getchar() != '\n')
continue;
printf("Please enter digit: ");
}
}
else
{
printf("Enter distance traveled in miles: ");
while (!scanf_s("%f", distance))
{
while (getchar() != '\n')
continue;
printf("Please enter digit: ");
}
printf("Enter fuel consumed in gallons: ");
while (!scanf_s("%f", fuel))
{
while (getchar() != '\n')
continue;
printf("Please enter digit: ");
}
}
}
void show_info(int mod,float fuel,float distance)
{
if (mod == 0)
printf("Fuel consumption is %.2f liters per 100km.\n",
fuel / (distance / 100));
else
printf("Fuel consumption is %.1f miles per gallon.\n",
distance / fuel);
}
4.
/*4.*/
#include<stdio.h>
int coun = 0;
void count(void);
int main(void)
{
int n;
printf("Please enter the number of cycle: ");
scanf_s("%d", &n);
for (; n > 0; n--)
count();
return 0;
}
void count(void)
{
extern int coun;
printf("Number of calls: %d\n", ++coun);
}
5.
/*5.*/
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define MAX 10
#define TIMES 100
int roll(int max);
void down(int* pt,int n);
int main(void)
{
int arr[TIMES];
int* p;
srand((unsigned int)time(0));
for (p = arr; p < arr + TIMES; p++)
* p = roll(MAX);
printf("Random number:\n\n");
down(arr, TIMES);
for (p = arr; p < arr + TIMES; p++)
{
printf("%3d ", *p);
if ((p - arr + 1) % 10 == 0)
putchar('\n');
}
return 0;
}
int roll(int max)
{
int roll;
roll = rand() % max + 1;
return roll;
}
void down(int* pt,int n)
{
int i, j;
int temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if (pt[i] < pt[j])
{
temp = pt[i];
pt[i] = pt[j];
pt[j] = temp;
}
}
6.
/*6.*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
#define TIMES 1000
#define N_SEED 10
int roll(int max);
void print(int *se, int ar[][MAX], int n);
int main(void)
{
int seed[N_SEED];
int res[N_SEED][MAX] = { 0 };
int s_count = 0;
int r_count;
while (s_count < N_SEED)
{
printf("Seed %d: ", s_count + 1);
while (!scanf_s("%d", &seed[s_count]))
{
while (getchar() != '\n')
continue;
printf("Please enter integer: ");
}
srand(seed[s_count]);
for (r_count = 0; r_count < TIMES; r_count++)
{
switch (roll(MAX))
{
case 1: res[s_count][0]++; break;
case 2: res[s_count][1]++; break;
case 3: res[s_count][2]++; break;
case 4: res[s_count][3]++; break;
case 5: res[s_count][4]++; break;
case 6: res[s_count][5]++; break;
case 7: res[s_count][6]++; break;
case 8: res[s_count][7]++; break;
case 9: res[s_count][8]++; break;
case 10: res[s_count][9]++; break;
default: break;
}
}
s_count++;
}
printf("\nResult: \n\n");
print(seed, res, N_SEED);
return 0;
}
int roll(int max)
{
int roll;
roll = rand() % max + 1;
return roll;
}
void print(int *se, int ar[][MAX], int n)
{
int i, j;
printf(" %-10s","Seed:");
for (i = 0; i < n; i++)
printf("%8d", se[i]);
printf("\n\n");
for (i = 0; i < n; i++)
{
printf(" \'%2d\'%-5s ", i + 1,":");
for (j = 0; j < MAX; j++)
printf("%8d", ar[i][j]);
putchar('\n');
}
}
7.
/*7.*/
//manydice.c
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int roll_n_dice(int dice,int sides);
int main(void)
{
int sets;
int dice, sides;
int status;
int roll;
int n;
_Bool re_input = 0;
srand((unsigned int)time(0));
printf("Enter the number of sets; enter q to stop: ");
while (scanf_s("%d", &sets))
{
printf("How many sides and how many dice? ");
while ((status = scanf_s("%d %d", &sides, &dice)) != 2 ||
sides < 2 || dice < 1)
{
if (status == EOF)
break;
else if (status != 2)
{
printf("Please enter integer: ");
while (getchar() != '\n')
continue;
}
else
{
printf("Requires at least one dice and two sides.\n");
printf("Re-input: ");
continue;
}
}
printf("Here are %d sets of %d %d-sides throws.\n",
sets, dice, sides);
for (n = 0; n < sets; n++)
{
roll = roll_n_dice(dice, sides);
if (roll < 0)
{
re_input = 1;
break;
}
printf(" %d", roll);
if ((n + 1) % 15 == 0)
putchar('\n');
}
printf(“\nHow many sets? Enter q to quit: ");
}
printf("\nbye\n");
return 0;
}
//diceroll.c
#include<stdio.h>
#include<stdlib.h>
static int rollem(int sides)
{
int roll;
roll = rand() % sides + 1;
return roll;
}
int roll_n_dice(int dice, int sides)
{
int d;
int total = 0;
for (d = 0; d < dice; d++)
total += rollem(sides);
return total;
}
8.
/*8.*/
//pe12-8a.c
#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_s("%d", &size) == 1 && size > 0)
{
printf("Enter the initialization value: ");
if(!scanf_s("%d", &value))
break;
pa = make_array(size, value);
if (pa)
{
show_array(pa, size);
free(pa);
}
printf("\nEnter the number of elements (<1 to quit): ");
}
printf("Done.\n");
return 0;
}
//pe12-8b.c
#include<stdio.h>
#include<stdlib.h>
int* make_array(int elem, int val)
{
int* p;
int* pt;
p = (int*)malloc(elem * sizeof(int));
for (pt = p; pt < p + elem; pt++)
* pt = val;
return p;
}
void show_array(const int st[], int n)
{
const int *p;
for (p = st; p < st + n; p++)
{
printf("%d ", *p);
if ((p - st + 1) % 8 == 0)
putchar('\n');
}
putchar('\n');
}
9.
/*9.*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
char temp[20];
char** ch;
char* p;
int words;
int i;
printf("How many words do you wish to enter? ");
while (!scanf_s("%d", &words))
{
while (getchar() != '\n')
continue;
printf("Please enter integer: ");
}
ch = (char **)malloc(words * sizeof(char*));
printf("Enter %d words now:\n", words);
for (i = 0; i < words; i++)
{
scanf_s("%s", temp, 20);
p = (char*)malloc(sizeof(temp));
strcpy_s(p, sizeof(temp), temp);
ch[i] = p;
}
printf("Here are your words:\n");
for (i = 0; i < words; i++)
{
puts(ch[i]);
free(ch[i]);
}
free(ch);
return 0;
}