12.11 编程练习

这是一个编程练习,涉及创建一个程序,用户可以选择公制或英制模式,输入旅行距离和消耗的燃料,程序会计算并显示油耗。程序还包括错误处理机制,如果用户输入无效模式,会提示并默认选择。此外,还要求重新设计程序只使用自动变量,并编写生成并排序随机数以及模拟骰子投掷的程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.不使用全局变量,重写程序清单12.4中的程序。

#include <stdio.h>
void critic(int * units);
int main(void)
{
	int units;
	printf("How many pounds to a firkin of butter?\n");
	scanf("%d",&units);
	
	while(units!=56)
		critic(&units);
	printf("You must have looked it up!\n");
	return 0;
}
void critic(int * units)
{
	printf("No luck,chummy.Try it again.\n");
	scanf("%d",units);
}
2.在美国通常以营利没加仑来计算油耗,在欧洲以升每百公里来计算。下面是某程序的一部分,改程序让用户选择一个模式(公制的或美制的),然后收集数据来计算油耗。

#include <stdio.h>
#include "pe12-2a.h"
int main(void)
{
	int mode;
	
	printf("Enter 0 for metric mode,1 for US mode:");
	scanf("%d",&mode);
	
	while(mode>=0)
	{
		set_mode(mode);
		get_info();
		show_info();
		printf("Enter 0 for metric mode, 1 for US mode (-1 to quit):");
		scanf("%d",&mode);		
	}
	printf("Done.\n");
	return 0;
}
下面是一些输出示例:

Enter 0 for metric mode, 1 for US mode:0

Enter distance traveled in kilometers:600

Enter fuel consumed in liters:78.8

Fuel consumption is 13.13 liters per 100 km.

Enter 0 for metric mode, 1 for US mode (-1 to quit):1

Enter distance traveled in miles:434

Enter fuel consumed in gallons:12.7

Fuel consumption is 34.2 mies per gallon

Enter 0 for metric mode, 1 for US mode (-1 to quit):3

Invalid mode specified.Mode 1(US) used.

Enter distance traveled in miles:388

Enter fuel consumed in gallons:15.3

Fuel consumption is 25.4 miles per gallon.

Enter 0 for metric mode, 1 for US mode(-1 to quit):-1

Done.

如果用户键入了不正确的模式,程序向用户给出提示信息并选取最近接的模式。请提供一个头文件pe12-2a.h和源代码文件pe12-2a.c,来使程序可以运行。源代码文件应该定义3个具有文件作用域,内部链接的变量。一个代表模式,一个代表距离,一个代表消耗的燃料。函数get_info()根据模式设置提示输入相应的数据,并将用户的回答存入文件作用域变量。函数show_info()根据所选的模式计算并且显示连日奥消耗值。

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

void set_mode(int mode)
{
	MODE=mode;
}
void get_info(void)
{
	if(MODE>=1)	
	{
		if(MODE==1)
			printf("\nEnter distance traveled in miles:");
		else
		{
			printf("\NInvalid mode specified.Mode 1(US) used.");
			printf("\nEnter distance traveled in miles:");
			MODE=1;
		}
	}

	else 
	{
		MODE=0;
		printf("\nEnter distance traveled in kilometers:");
	}
	scanf("%lf",&distance);
	printf("\nEnter fuel consumed in liters:");
	scanf("%lf",&fuel);		
	
}

void show_info(void)
{	
	printf("\nMODE=%d distance=%.2f fuel=%.1f",MODE,distance,fuel);
	if(MODE == 1) 
	{
		printf("\nFuel consumption is %.1f miles per gallon.",distance/fuel);
	}
	if(MODE==0)
	{		
		printf("\nFuel consumption is %.2f liters per 100 km.",fuel/(distance/100));
	}
}


/*pe12-2a.h*/
static int MODE;
static double fuel;
static double distance;
void set_mode(int mode);
void get_info(void);
void show_info(void);

3.重新设计练习2中的程序,使他仅仅使用自动变量。程序提供相同的用户界面,也就是说,要提示屏幕输入模式等等,然而,您还必须给出一组不同的函数调用。

#include <stdio.h>
void get(int mode,float *fuel,float *distance);
void result(int mode,float fuel,float distance);
int main(void)
{
	int mode;
	float fuel;
	float distance;
	
	printf("Enter 0 for metric mode,1 for US mode:");
	scanf("%d",&mode);
	printf("\n");
	
	while(mode>=0)
	{
		get(mode,&fuel,&distance);
		result(mode,fuel,distance);
		printf("Enter 0 for metric mode, 1 for US mode (-1 to quit):");
		scanf("%d",&mode);		
		printf("\n");
	}
	printf("Done.\n");
	return 0;	
}
void get(int mode,float *fuel,float *distance)
{
	if(mode>1)	
		mode=2;
	switch (mode)
	{
		case 0:
			printf("Enter distance traveled in kilometers:");
			scanf("%f",distance);
			printf("\n");
			printf("Enter fuel consumed in liters:");
			scanf("%f",fuel);
			printf("\n");
			break;
		case 1:	
			printf("Enter distance traveled in miles:");
			scanf("%f",distance);
			printf("\n");
			printf("Enter fuel consumed in gallons:");
			scanf("%f",fuel);
			printf("\n");
			break;
		case 2:
			printf("Invalid mode specified.Mode 1(US) used.\n");
			printf("Enter distance traveled in miles:");
			scanf("%f",distance);
			printf("\n");
			printf("Enter fuel consumed in gallons:");
			scanf("%f",fuel);
			printf("\n");
			break;
	}
	
}
void result(int mode,float fuel,float distance)
{
	if(mode==0)
		printf("Fuel consumption is %.2f liters per 100 km.\n",fuel/(distance/100));
	else
		printf("Fuel consumption is %.1f miles per gallon.\n",distance/fuel);
}
4.编写一个函数,它返回函数自身被调用的次数。并在一个循环中测试之。

#include <stdio.h>
static int ct=0;
int count(void);
int main(void)
{
	int i;
	
	for(i=0;i<10;i++)
	{
	printf(" main() call count() for %d times.\n",count());
	}
	return 0;
}
int count(void)
{
	ct++;
	return ct;
}

5.编写产生100个1到10范围内的随机数的程序,并且以降序排序。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
void randomArray(int arr[],int size);
void sortArray(int arr[],int size);
void input(int arr[],int size);
int main(void)
{
	int arr[SIZE];
	randomArray(arr,SIZE);
	input(arr,SIZE);
	
	sortArray(arr,SIZE);
	input(arr,SIZE);
	return 0;
}

void randomArray(int arr[],int size)
{
	int i;
	srand((unsigned int)time(0));
	for(i=0;i<SIZE;i++)
	{
		arr[i]=rand()%SIZE+1;
	}	
}

void sortArray(int arr[],int size)
{
	int i,j,temp;
	for(i=0;i<size-1;i++)
	{
		for(j=i+1;j<size;j++)
		{
			if(arr[j]<arr[i])
			{
				temp=arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			}
		}
	}
}

void input(int arr[],int size)
{
	int i;
	for(i=0;i<size;i++)
	{
		printf("%-5d",arr[i]);
		if((i+1)%10==0)
			printf("\n");
	}
	printf("here end!\n");
}
6.编写一个产生1000个1到10范围内的随机数的程序。不必保存或打印数字,仅打印每个数被产生了多少次。让程序对10个不同的种子值进行计算。数字出现的次数相同吗?可以使用本章中的函数或ANSIC中的函数rand()和srand(),它们与我们的函数具有相同的形式。这是一个测试特定随机数发生器的随机性的方法。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10000
#define SCOPE 10
void randomArray(int arr[],int size,int seed);
void prob(int arr[],int size,int array[],int scope);

int main(void)
{
	int i,j;
	int arr[SIZE];
	int array[SCOPE+1]={0,};
	for(j=0;j<10;j++)
	{
		int arr[SIZE];
		int array[SCOPE+1]={0,};
		randomArray(arr,SIZE,j);
		prob(arr,SIZE,array,SCOPE+1);
		for(i=0;i<SCOPE+1;i++)
		{
			if(array[i])
				printf("%d出现了%d次\n",i,array[i]);
		}	
		printf("\n");	
	}

	return 0;
}

void randomArray(int arr[],int size,int seed)
{
	int i;
	srand(seed);
	for(i=0;i<SIZE;i++)
	{
		arr[i]=rand()%SCOPE+1;
	}	
}


void prob(int arr[],int size,int array[],int scope)
{
	int i;
	for(i=0;i<size;i++)
	{
		array[arr[i]]++;
	}
}


7.编写一个程序,该程序与我们在显示程序清单12.13的输出之后讨论的修改版程序具有相同的表现。也就是说,输出应该像下面这样

Ener the number of sets; enter q to quit.

18

How many sides and how many dice?

6 3

Here are 18 sets of 3 6-sided throws.

12 10 6 9 8 14 8 15 9 14 12 17 11 7 10

13 8 14

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rand0(int sides);
int rand_n(int dice,int sides);
int main(void)
{
	int sets,sides,dice,i;
	int *pst=NULL;
	printf("Enter the number of sets,enter q to quit.\n");
	srand(time(0));
	while(scanf("%d",&sets)==1 && sets>0)
	{
		printf("How many sides and how many dice?\n");
		scanf("%d %d",&sides,&dice);
		printf("Here are 18 sets of 3 6-sides throws.\n");
		pst=(int *)malloc(sets*sizeof(int));
		for(i=0;i<sets;i++)
		{
			pst[i]=rand_n(dice,sides);
			printf("%d ",pst[i]);
		}	
		printf("\n");
		free(pst);
		printf("Enter the number of sets,enter q to quit.\n");
	}
	printf("Done.\n");
}

int rand0(int sides)
{
	return rand()%sides+1;
}

int rand_n(int dice,int sides)
{
	int i;
	int total=0;
	for(i=0;i<dice;i++)
	{
		total=total+rand0(sides);
	}
	return total;
}

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 of elements:\n");
	scanf("%d",&size);
	while(size>0)
	{
		printf("Enter the initialization value:\n");
		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):\n");
		scanf("%d",&size);
	}
	printf("Done.\n");
	return 0;
}
给出函数make_array()和show_array的定义使得程序完整。函数make_array()接受两个参数。第一个是int数组的元素个数,第二个是要赋给每个元素的值。函数使用malloc()来创建一个适当大小的数组,把每个元素设定为指定的值,并且返回一个数组指针。函数show_array()以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 of elements:\n");
	scanf("%d",&size);
	while(size>0)
	{
		printf("Enter the initialization value:\n");
		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):\n");
		scanf("%d",&size);
	}
	printf("Done.\n");
	return 0;
}

int * make_array(int elem,int val)
{
	int *pst=NULL;
	int i;
	pst=(int *)malloc(elem*sizeof(int));
	for(i=0;i<elem;i++)
		pst[i]=val;
	return pst;
}
void show_array(const int ar[],int n)
{
	int i;
	for(i=0;i<n;i++)
	{
		printf("%d ",ar[i]);
		if((i+1)%8==0)
			printf("\n");
	}
	printf("\n");
}


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值