编程练习答案是我学习过程中所敲,作为一个学渣,答案是以完成习题和易理解为导向,很少有一些五彩缤纷的算法。程序仅作为交流,如有错误与不足还请指出。 所用软件:VS2019
题号
12.9.112.9.212.9.312.9.412.9.512.9.612.9.712.9.812.9.9
12.9.1
#include <stdio.h>
void critic(int * p
);
int main(void)
{
int units
= 0;
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
);
}
12.9.2
#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;
}
#ifndef PE12_2A_H
#define PE12_2A_H
void set_mode(int mode
);
void get_info(void);
void show_info(void);
#endif
#include "pe12-2a.h"
#include "stdio.h"
int mode
= 0;
int distance
= 0;
double fuel
= 0;
void set_mode(int m_mode
)
{
if (m_mode
== 0 || m_mode
== 1)
mode
= m_mode
;
else
{
printf("Invalid mode specified.Mode %d used.\n",mode
);
}
}
void get_info(void)
{
if (mode
== 0)
{
printf("Enter distance traveled in kilometers : ");
scanf_s("%d", &distance
);
printf("Enter fuel consumed in liters : ");
scanf_s("%lf", &fuel
);
}
else
{
printf("Enter distance traveled in miles : ");
scanf_s("%d", &distance
);
printf("Enter fuel consumed in gallons :");
scanf_s("%lf", &fuel
);
}
}
void show_info(void)
{
if (mode
== 0)
{
printf("Fuel consumption is %.2lf liters per 100 km.\n", fuel
/ (double)distance
* 100.0);
}
else
printf("Fuel consumption is %.1lf miles per gallon.\n", distance
/ fuel
);
}
12.9.3
#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)
{
get_set_show(mode
);
printf("Enter 0 for metric mode, 1 for US mode");
printf(" (-1 to quit): ");
scanf_s("%d", &mode
);
}
printf("Done.\n");
return 0;
}
#ifndef PE12_2A_H
#define PE12_2A_H
void get_set_show(int mode
);
#endif
#include "pe12-2a.h"
#include "stdio.h"
void get_set_show(int mode
)
{
static now_mode
= 0;
int distance
;
double fuel
;
if (mode
== 0 || mode
== 1)
now_mode
= mode
;
else
printf("Invalid mode specified.Mode %d used.\n", now_mode
);
if (now_mode
== 0)
{
printf("Enter distance traveled in kilometers : ");
scanf_s("%d", &distance
);
printf("Enter fuel consumed in liters : ");
scanf_s("%lf", &fuel
);
printf("Fuel consumption is %.2lf liters per 100 km.\n", fuel
/ (double)distance
* 100.0);
}
else
{
printf("Enter distance traveled in miles : ");
scanf_s("%d", &distance
);
printf("Enter fuel consumed in gallons :");
scanf_s("%lf", &fuel
);
printf("Fuel consumption is %.1lf miles per gallon.\n", distance
/ fuel
);
}
}
12.9.4
#include "stdio.h"
int test(void);
int main(void)
{
int num
= 0;
for (int i
= 0;i
< 50;i
++)
num
= test();
printf("该函数被调用了%d次", num
);
return 0;
}
int test(void)
{
static int num
= 0;
num
++;
printf("这是该函数第%d次被调用\n",num
);
return num
;
}
12.9.5
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int temp
;
int num
[100];
for (int i
= 0;i
< 100;i
++)
num
[i
] = rand() % 10 + 1;
for(int i
= 0;i
< 100;i
++)
for (int j
= 0;j
< 100 - i
- 1;j
++)
{
if (num
[j
+1] > num
[j
])
{
temp
= num
[j
];
num
[j
] = num
[j
+1];
num
[j
+ 1] = temp
;
}
}
for (int i
= 0;i
< 100;i
++)
{
printf("%d\n", num
[i
]);
}
return 0;
}
12.9.6
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
int count
[10][10] = {0};
int num
[10][1000];
for(int i
= 0;i
< 10;i
++)
for (int j
= 0;j
< 1000;j
++)
num
[i
][j
] = rand() % 10 + 1;
for (int i
= 0;i
< 10;i
++)
{
for (int j
= 0;j
< 1000;j
++)
{
switch (num
[i
][j
])
{
case 1:count
[i
][0] += 1;break;
case 2:count
[i
][1] += 1;break;
case 3:count
[i
][2] += 1;break;
case 4:count
[i
][3] += 1;break;
case 5:count
[i
][4] += 1;break;
case 6:count
[i
][5] += 1;break;
case 7:count
[i
][6] += 1;break;
case 8:count
[i
][7] += 1;break;
case 9:count
[i
][8] += 1;break;
case 10:count
[i
][9] += 1;break;
default:exit(EXIT_FAILURE
);
}
}
}
for (int i
= 0;i
< 10;i
++)
{
for (int j
= 1;j
< 11;j
++)
{
printf("第%d组%d出现的次数为%d\n", i
, j
, count
[i
][j
]);
}
}
return 0;
}
12.9.7
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "diceroll.h"
int main(void)
{
int sets
;
int sides
, dice
;
srand((unsigned int)time(0));
printf("Enter the number of sets; enter q to stop :");
while (scanf_s("%d", &sets
) == 1 && sets
!= 'q' )
{
printf("How many sides and how many dice?");
scanf_s("%d%d", &sides
,&dice
);
printf("Here are %d sets of %d %d-sided throws.\n", sets
, sides
, dice
);
for (int i
= 0;i
< sets
;i
++)
{
printf("%d ", roll_n_dice(sides
, dice
));
}
printf("How many sets? Enter q to stop:");
}
printf("done!\n");
return 0;
}
#ifndef _DICEROLL_H
#define _DICEROLL_H
extern int roll_count
;
int roll_n_dice(int dice
, int sides
);
#endif
#include "diceroll.h"
#include <stdio.h>
#include <stdlib.h>
int roll_count
= 0;
static int rollem(int sides
)
{
int roll
;
roll
= rand() % sides
+ 1;
++roll_count
;
return roll
;
}
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
;
}
12.9.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_s("%d", &size
) == 1 && size
> 0)
{
printf("Enter the initialization value: ");
scanf_s("%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* elem_ptr
= (int*)malloc(sizeof(int) * elem
);
for (int i
= 0;i
< elem
;i
++)
{
elem_ptr
[i
] = val
;
}
return elem_ptr
;
}
void show_array(const int ar
[], int n
)
{
for (int i
= 0;i
< n
;i
++)
{
if (i
% 8 == 0 && i
!= 0)
printf("\n");
printf("%d", ar
[i
]);
}
printf("\n");
}
12.9.9
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int main(void)
{
int word_num
= 0;
printf("How many words do you wish to enter ?\n");
scanf_s("%d", &word_num
);
char** num_ptr
= (char **)malloc(word_num
* sizeof(char*));
printf("Enter %d words now :\n",word_num
);
for (int i
= 0;i
< word_num
;i
++)
{
char word_temp
[100];
scanf("%s", word_temp
);
char* word_ptr
= (char *)malloc(sizeof(char) * strlen(word_temp
) + 1);
for (int j
= 0;j
< sizeof(char) * strlen(word_temp
) + 1;j
++)
{
word_ptr
[j
] = word_temp
[j
];
}
num_ptr
[i
] = word_ptr
;
}
printf("Here are your words:\n");
for (int i
= 0;i
< word_num
;i
++)
{
printf("%s\n", num_ptr
[i
]);
}
return 0;
}