C primer plus 第六版 第十二章 编程练习 个人解法

c语言小白的学习之路
时隔一个月,在学习c语言的漫漫长路上有前进了一小步

以下代码如有错误或建议,敬请斧正

12-1.

// 12-1  改写12.4.c 
#include <stdio.h>
int cirtic(void);

int main(void)
{
    
    
	int units;
	
	printf("How many pounds to firkin of butter?\n");
	scanf("%d", &units);
	while (units != 56)
		units = cirtic();
	printf("You must have look it up!");
	
	return 0;
}

int cirtic(void)
{
    
    
	int units;
	printf("No luck, my friends. Try again\n");
	scanf("%d",&units);
	
	return units;
}

12-2.

pe12 --2a.c

//pe12 --2a.c
#include <stdio.h>
#include "pe12-2a.h"

void set_mode(int n_mode)
{
    
    
	if (n_mode != 1 && n_mode != 0)
	{
    
    
		if (s_mode == 1)
		printf("Invalid mode specified, Mode 1(US) used.\n");
		else if (s_mode == 0)
		printf("Invalid mode specified, Mode 0(METRIC) used.\n");
	}
	else
		s_mode = n_mode;
}

void get_info(void)
{
    
    
	printf("Enter distance traveled in kilomteres: ");
	scanf("%f",&distance);
	printf("Enter fuel consumed in gallons: ");
	scanf("%f",&fuel);
}

void show_info(void)
{
    
    
	if (s_mode == 0)
	printf("Fuel consumption is %f liters per 100 km\n",fuel/distance*100);
	else if (s_mode == 1)
	printf("Fuel consumption is %f miles per gallon.\n",distance/fuel);
}

pe2a-2a.h

//pe2a-2a.h
void set_mode(int);
void get_info(void);
void show_info(void);

static int s_mode;
static float fuel;
static float distance;

12-3.
pe12-3a.c

// pe12-3a.c
#include <stdio.h>
#include "pe12-3a.h"

int set_mode(int new_mode,int last_mode)
{
    
    
	if (new_mode != 1 && new_mode != 0)
	{
    
    
		if (last_mode == 1)
		printf("Invalid mode specified, Mode 1(US) used.\n");
		else if (last_mode == 0)
		printf("Invalid mode specified, Mode 0(METRIC) used.\n");
		return last_mode;
	}
	else
		return new_mode;
}

float get_info_distance(int mode,float distance)
{
    
    
	if (mode == 0)
	printf("Enter distance traveled in kilomteres: ");
	else if (mode == 1)
	printf("Enter distance traveled in miles: ");
	
	scanf("%f",&distance);
	return distance;
}	
float get_info_fuel(int mode,float fuel)
{
    
    
	if (mode == 0)
	printf("Enter fuel consumed in liters: ");
	else if (mode == 1)
	printf("Enter fuel consumed in gallons: ");
	
	scanf("%f",&fuel);
	
	return fuel;
}

void show_info(int mode,float distance,float fuel)
{
    
    
	if (mode == 0)
	printf("Fuel consumption is %f liters per 100 km\n",fuel/distance*100);
	else if (mode == 1)
	printf("Fuel consumption is %f miles per gallon.\n",distance/fuel);
}

pe12-3a.h

//pe12-3a.h
int set_mode(int,int);
float get_info_distance(int,float);
float get_info_fuel(int,float);
void show_info(int,float,float);

pe12-3b.h

// pe12-3b.c
// 与pe12-3a.c 一起编译
#include <stdio.h>
#include "pe12-3a.h"
int main(void)
{
    
    
	int mode;    // mode = 方式
	int last_mode = 0;
	float distance;
	float fuel;
	
	printf("Enter 0 for metric(公制的) mode, 1 for US mode: ");
	scanf("%d",&mode);
	while(mode >= 0)
	{
    
    
		mode = set_mode(mode,last_mode);
		last_mode = mode;
		distance = get_info_distance(mode,distance);
		fuel = get_info_fuel(mode,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!");
	
	return 0;
}

12-4

// 12-4 
#include <stdio.h>
static int count;
int re_ct(void);

int main(void)
{
    
    
	int n = 5   // n可变; 
	int i = 1;
	for (; i <= n; i++)
	{
    
    
		count = re_ct();
	}
	printf("%d",count);
}

int re_ct(void)
{
    
    
	return ++count;
}

12-5

// 12-5 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_NUMBER 100 

static int array[MAX_NUMBER];

void rank(int *);			//排序 
void turn(int *,int,int);  //交换位置 

int main(void)
{
    
    
	int i = 0;
	int number[MAX_NUMBER];
	srand((unsigned int) time(0));
	for (; i < MAX_NUMBER; i++)
	{
    
    
		array[i] = rand() % 10 + 1;
	}
	rank(array);
	
	for (i = 0; i < MAX_NUMBER; i++)
	{
    
    
		if (array[i] != array[i-1] && i != 0)
		putchar('\n'); 
		printf("%d ",array[i]);
	}
	return 0;
}

void rank(int *random)
{
    
    
	int i,j;
	int MAX_mark;   // 记录最大值在原数组的位置 
	int max;
	
	for (i = 0; i < MAX_NUMBER;i++)
	{
    
    
		for (j = i,max = random[j],MAX_mark = j; j < MAX_NUMBER; j++)
		{
    
    
			if (max < random[j])
			{
    
    
				max = random[j];
				MAX_mark = j;
			}
		}
		turn(random,i,MAX_mark);
	}
}

void turn(int *random,int i,int MAX_mark)
{
    
    
	int temp;
	temp = random[i];
	random[i] = random[MAX_mark];
	random[MAX_mark] = temp;
}

12-6

// 12-6
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    
    
	int array[1000];
	int i = 0;
	int number[10];
	
	for (i = 0 ;i < 10; i++)
		number[i] = 0;
	
	srand((unsigned int) 1);
	
	for (i = 0;i < 1000; i++)
	{
    
    
		array[i] = rand() % 10 + 1;
		switch (array[i])
		{
    
    
			case 1:
				number[0]++;break;
			case 2:
				number[1]++;break;
			case 3:
				number[2]++;break;
			case 4:
				number[3]++;break;
			case 5:
				number[4]++;break;
			case 6:
				number[5]++;break;
			case 7:
				number[6]++;break;
			case 8:
				number[7]++;break;
			case 9:
				number[8]++;break;
			case 10:
				number[9]++;break;
		}
	}
	
	printf("1   2   3   4   5   6   7   8   9   10\n");
	for (i = 0; i < 10 ;i++)
	{
    
    
		printf("%-4d",number[i]);
	}
	
	return 0;
}

12-7

12-8

//pe12-8.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 if 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!");
	return 0;
}

int *make_array(int elem, int val)
{
    
    
	int *pd;
	int i = 0;
	
	pd = (int *)malloc(elem * sizeof(int));
	for (;i < elem; i++)
		pd[i] = val;
		
	return pd;
}

void show_array(const int ar[], int n)
{
    
    
	int i;
	for (i = 0;i < n; i++)
	{
    
    
		printf("%-5d",ar[i]);
		if (i % 7 == 0 && i != 0)
			putchar('\n');
		
	}
	printf("\n");
}

12-9

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    
    
	char **ptr;
	int n;
	int count;
	char arr[20];
	int i,j;
	
	printf("How many words do you wish to enter? ");
	scanf("%d",&n);
	getchar();
	
	ptr = (char **) malloc (n * sizeof(char *));
	printf("Enter %d words now: ",n);
	
	for (i = 0; i < n; i++)
	{
    
    
		while ((arr[count] = getchar()) != ' ' && arr[count] != '\n')
			count++;
		ptr[i] = (char *)malloc (count * sizeof(char));
		
		for (j = 0; j < count; j++)
		{
    
    
			ptr[i][j] = arr[j];
			printf("%c",ptr[i][j]); 
		}	
		printf("\n");
		count = 0;
	}
	// 此处省略free()函数,其实是忘记加了........
	
	return 0;
} 

总结:

  1. 排序中重置变量,很关键。
  2. 有关malloc的语法格式,12-9 中 的指向一个指向char类型指针的写法,百度和得知*(char **) malloc (n * sizeof(char *));*用于该表达.
  3. 随机数,time(0)作为种子时在循环内生成的数在一秒内使用同一个种子,故生成数相同。

欢迎留言,共同进步.

猜你喜欢

转载自blog.csdn.net/qq_45469783/article/details/105175602
今日推荐