1. 程式人生 > >C primer plus 程式設計練習 12.9

C primer plus 程式設計練習 12.9

2.

#include <stdio.h>
#include <stdlib.h>

int mode = 0; //計算模式
int distance;  //行程距離
double fuel;   //燃油量
//設定計算模式
void set_mode(int n);
//獲取使用者輸入資訊(里程和燃料)
void get_info(void);
//顯示計算結果
void show_info(void);

int main()
{
    int mode = 0;

    printf("請選擇計算模式:輸入0選擇美製,輸入1選擇英制\n");
    scanf("%d",&mode);
    while(mode>= 0)
    {
        set_mode(mode);
        get_info();
        show_info();
        printf("請選擇計算模式:輸入0選擇美製,輸入1選擇英制\n");
        scanf("%d",&mode);
    }
    printf("再見!\n");
    return 0;
}
void set_mode(int n)
{
    if(n == 0 || n == 1)
        mode = n;
    else
           printf("輸入的計算模式無效,自動選擇模式%d(%s)\n",mode,mode == 0 ? "美製" : "英制");
}
//獲取使用者輸入資訊(里程和燃料)
void get_info(void)
{
    printf("請輸入里程(%s)\n",mode == 0 ? "千米" : "英里");
    while(scanf("%d",&distance) != 1)
    {
        printf("輸入錯誤,請重新輸入:\n");
        while(getchar() != '\n')
            continue;
    }
    printf("請輸入消耗的燃料量(%s)\n",mode == 0 ? "升" : "加侖");
    while(scanf("%lf",&fuel) != 1)
    {
        printf("輸入錯誤,請重新輸入:\n");
        while(getchar() != '\n')
            continue;
    }
}
//顯示計算結果
void show_info(void)
{
    double result;
    if(mode == 0)
        result = 100 * fuel / distance;
    else
        result = distance / fuel;
    printf("油耗的計算結果為:%.2lf %s每%s\n",result,mode == 0 ? "升" : "英里",mode == 0 ? "100千米" : "加侖");
}

6.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int arr[10] = {0};
	for (int i = 0;i < 1000 ;i++ )
	{
		arr[rand() % 10]++;
	}
	printf("測試結果為:\n");
	for (int i = 0; i < 10;i++ )
	{
		printf("%d 出現次數:%d\n",i + 1,arr[i]);
	}
	return 0;
}

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("輸入元素數量:");
	while (scanf("%d",&size) == 1 && size > 0)
	{
		printf("輸入要賦給元素的值:");
		scanf("%d",&value);
		pa = make_array(size,value);
		if (pa)
		{
			show_array(pa,size);
			free(pa);
		}
		printf("輸入元素數量( < 1退出):");
	}
        puts("再見!");
        return 0;
}
int * make_array(int elem,int val)
{
	int * pm;
	pm = (int *) calloc(elem,sizeof(int));
	if (pm)
	{
		for (int i = 0; i < elem ;i++ )
			*(pm + i) = val;
	}
	return pm;
}
void show_array(const int ar[],int n)
{
	int i;
	for (i = 0;i < n ;i++ )
	{
		printf("%4d",ar[i]);
		if (i % 10 == 9)
			putchar('\n');
	}
	if (i % 10 != 0)
	    putchar('\n');
}

9.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int n;
	printf("How many words do you wish to enter? ");
	while (scanf("%d",&n) == 1 && n > 1)
	{
		int i = 0;
		char (* ptr)[20];
	        ptr = (char (*)[20]) malloc(n * 20 * sizeof(char));
		printf("Enter %d words now: \n",n);
		while (i < n)
		{
			scanf("%s",&ptr[i]);
			i++;
		}
		printf("Here are your words:\n");
                for (i = 0;i < n ;i++ )
			puts(ptr[i]);
		free(ptr);
        printf("How many words do you wish to enter? \n");
	}
        printf("Done!\n");
	return 0;
}