C Peimer 第12章编程习题

原创 2016年08月30日 18:49:37

今天工作量不太大,终于到了动态内存分配,各种变量类型还是挺让人头疼的。

#include <stdio.h>
#include <stdlib.h>
#include "pe12-2a.h"
#define LENGTH 10


int *make_array(int elem, int val);
void show_array(const int ar[], int n);
int main(void) {
	//1
	/*
	int units[1];
	void critic(int []);
	printf("how many?");
	scanf("%d", units);
	while (*units!=56)
	{
		critic(units);
	}
	printf("you must have a look%d",*units);
	*/

	//2
	/*
	extern int mode;
	printf("Enter 0 for meteric mode,1 for US mode:");
	scanf("%d", &mode);
	while (mode>=0)
	{
		set_mode(mode);
		get_info();
		show_info();
		printf("\nEnter 0 for metric mode,1 for US mode:");
		printf("-1 to quit:");
		scanf("%d",&mode);
	}
	printf("Done.\n");
	*/

	//3
	/*
	void get_info2(double[], int mode);
	void show_info2(double[], int n);
	int mode;
	double info[2];
	printf("Enter 0 for meteric mode,1 for US mode:");
	scanf("%d", &mode);
	while (mode >= 0)
	{
		get_info2(info,mode);
		show_info2(info, mode);
		printf("\nEnter 0 for metric mode,1 for US mode:");
		printf("-1 to quit:");
		scanf("%d", &mode);
	}
	printf("Done.\n");
	*/

	//4
	/*
	int times=0;
	int RunTimes(int times);
	for (int i = 0; i < 10; i++)
	{
		times = RunTimes(times);
	}
	printf("the function has already run %d times!" ,times);
	*/

	//5
	/*
	int randMat[10 * LENGTH];
	void RandNumber100(int[]);
	void SortNumberMy(int randMat[], int length);
	RandNumber100(randMat);
	for (int i = 0; i < 10*LENGTH; i++)
	{
		printf("%2d",randMat[i]);
	}
	SortNumberMy(randMat, 10 * LENGTH);
	printf("\n*********************\n");
	for (int i = 0; i < 10 * LENGTH; i++)
	{
		printf("%2d", randMat[i]);
	}
	*/

	//6
	/*
	void Rand1000Number();
	Rand1000Number();
	*/

	//7
	/*
	int sets;
	int sides;
	int dice;
	int occur;
	printf("enter the number of sets: enter 0 to stop.");
	scanf("%d",&sets);
	while (sets != 0)
	{
		printf("how many sides and dice?");
		scanf("%d%d", &sides, &dice);
		printf("here are %d sets of %d %d-sided throws:\n", sets, dice, sides);
		srand(time(NULL));
		for (int i = 0; i < sets; i++)
		{
			for (int j = 0; j < dice; j++)
			{
				occur = 1 + (rand() % sides);
				printf("%4d", occur);
			}
			printf("\n");
			
		}
		printf("\nenter the number of sets: enter 0 to stop.");
		scanf("%d", &sets);
	}
	*/

	//8
	int *pa;
	int size;
	int value;

	printf("Enter the number of elements:");
	scanf("%d",&size);
	while (size>0)
	{
		printf("Enter the initialization value:");
		scanf("%d",&value);
		pa = make_array(size, value);
		if (pa)
		{
			show_array(pa, size);
			free(pa);
		}
		printf("\nEnter the number of elements(size < 1 to quit):");
		scanf("%d", &size);
	}
	printf("Done!\n");

	return 0;
}

void show_array(const int ar[],int n) {
	for (int i = 0; i < n; i++)
	{
		printf("%2d", *(ar + i));
		if ((i + 1) % 8 == 0 && i > 0) {
			printf("\n");
		}
	}
}
int * make_array(int elem, int val)
{
	int *temp;
	temp = (int*)malloc(elem*sizeof(int));
	for (int i = 0; i < elem; i++)
	{
		*(temp + i) = val;
	}
	return temp;
}
void Rand1000Number() {
	int temp;
	int info[10] = {0};
	srand(time(NULL));
	for (int i = 0; i < 1000 * LENGTH; i++)
	{
		temp = 1 + (rand() % 10);
		for (int j = 0; j < 10; j++)
		{
			if (temp == j+1)
			{
				info[j] += 1;
			}
		}
	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d appears %d times\n", i, info[i]);
	}
}

void SortNumberMy(int randMat[],int length) {
	int temp;
	for (int i = 0; i < length; i++)
	{
		temp = randMat[i];
		for (int j = i + 1; j < length; j++)
		{
			if (temp < randMat[j])
			{
				temp = randMat[j];
				randMat[j] = randMat[i];
				randMat[i] = temp;
			}
		}

	}
}
void RandNumber100(int randMat[]) {
	for (int i = 0; i < 10 * LENGTH; i++)
	{
		*(randMat + i) = ((time() + rand()) % 9) + 1;
	}
}
int RunTimes(int times) {
	times++;
	return times;
}

void get_info2(double info[],int mode) {
	switch (mode)
	{
	case 0:
		printf("Enter distace:");
		scanf("%lf", info);
		printf("Enter liter:");
		scanf("%lf", (info+1));
		break;
	case 1:
		printf("Enter miles:");
		scanf("%lf", info);
		printf("Enter gallons:");
		scanf("%lf", (info + 1));
	default:
		printf("Invalid mode!");
		break;
	}
}
void show_info2(double info[], int mode) {
	double temp;
	double distance = *info;
	double liter = *(info + 1);
	char *units=NULL;
	switch (mode)
	{
	case 0:
		units = "liters per 100 km";
		break;
	case 1:
		units = "miles per gallon";
		break;
	default:
		break;
	}
	if (mode == 0)
	{
		temp = (liter / distance) * 100;
	}
	else if (mode == 1)
	{
		temp = distance / liter;
	}
	if (mode == 0 || mode == 1)
	{
		printf("fuel consumption is :%.2lf %s", temp, units);
	}
}

void critic(int number[]) {
	printf("try again:");
	scanf("%d",number);
}


在同目录下,另建一个文件,保存以下内容,以完成第2题:

#include <stdio.h>
#include "pe12-2a.h"
#define UNITS 40


double distance;
double liter;
int mode;
static const char *units;
void set_mode(int n) {
	switch (n)
	{
	case 0:
		units = "liters per 100 km";
		break;
	case 1:
		units = "miles per gallon";
		break;
	default:
		break;
	}

}

void get_info() {
	switch (mode)
	{
	case 0:
		printf("Enter distace:");
		scanf("%lf", &distance);
		printf("Enter liter:");
		scanf("%lf", &liter);
		break;
	case 1:
		printf("Enter miles:");
		scanf("%lf", &distance);
		printf("Enter gallons:");
		scanf("%lf", &liter);
	default:
		printf("Invalid mode!");
		break;
	}
	
}

void show_info() {
	double temp;
	if (mode == 0)
	{
		temp = (liter / distance) * 100;
	}
	else if (mode == 1)
	{
		temp = distance / liter;
	}
	if (mode == 0 || mode == 1)
	{
		printf("fuel consumption is :%.2lf %s", temp, units);
	}

}

再以下建立一个头文件pe12-2a.h,保存以下内容:

#pragma once

extern double distance;
extern double liter;
void set_mode(int);
void get_info();
void show_info();


相关文章推荐

C Peimer 第17章编程习题(部分)

高级数据结构,先前接触了许多类似的算法,有一些概念,但要说实现的话,其实对个人还是挺难的。本来这是一个机会,能够真正实现一下,以锻炼这点短处,可是每一小段程序都太费时间了,但实现出来收获却又不是特别的...

C Peimer 第13章编程习题

本来以为什么I\o,还不是手到擒来,结果就被人擒了。整整两天时间,都填这上头了。 #include #include #include #include #define BUFSIZE 10...

C Peimer 第14章编程习题

大一学struct时并没有学好,现在果然要了命。这11道题摆弄了整整三天,终于完成了,但还是磕磕绊绊,唉…… #include #include #include #include ...

编程珠玑之第12章习题1: 随机位问题

习题描述:C库函数rand()通常返回约15个随机位。使用该函数实现函数bigrand和bigrand和randint(l, u), 要求前者至少返回30个随机位,后者返回[l,u]范围内的一个随机整...

《编程珠玑》第12章习题

习题一:

c++primer plus 第12章习题

//第12章第1题 #ifndef STACK_H #define STACK_H #define _CRT_SECURE_NO_WARNINGS class Cow { private: char...

《linux c 编程一站式学习》课后部分习题解答(一)

1、假设变量x和n是两个正整数,我们知道x/n这个表达式的结果要取Floor,例如x是17,n是4,则结果是4。如果希望结果取Ceiling应该怎么写表达式呢?例如x是17,n是4,则结果是5;x是1...

《C++编程思想》(第二版)第3章 C++中的C(笔记、习题及答案)(二)

#include #include using namespace std; void stringRef(string& s) { s += " come blow"; } voi...

《快学Scala》习题详解 第12章 高阶函数

1 编写函数values(fun: (Int) => Int, low: Int, high: Int),该函数输出一个集合,对应给定区间内给定函数的输入和输出。比如,values(x => x * ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C Peimer 第12章编程习题
举报原因:
原因补充:

(最多只允许输入30个字)