关闭

C Peimer 第12章编程习题

标签: C语言编程visual studio
106人阅读 评论(0) 收藏 举报
分类:

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

#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();


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5476次
    • 积分:262
    • 等级:
    • 排名:千里之外
    • 原创:22篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档