高级语言程序设计作业 10/12

4.8编程练习

2

/* 4.8.2.c -- 打印名字 */

#include <stdio.h>

int main()
{
	printf("Please enter your name: ");
	char name[20];
	scanf("%s", name);
	
	printf("\"%s\"\n", name);
	printf("\"%20s\"\n", name);
	printf("\"%-20s\"\n", name);
	printf("%*s\n", strlen(name) + 3, name);
	return 0;
} 

4

/* 4.8.4.c -- 输出身高和姓名信息。 */

#include <stdio.h>

int main()
{
	float height;
	char name[20];
	
	printf("Please enter your height in feet: ");
	scanf("%f", &height);
	printf("Please enter your name: ");
	scanf("%s", name);
	
	printf("%s, you are %.3f feet tall.", name, height);
	
	return 0;
}

6

/* 4.8.6.c -- 打印姓名及长度。 */

#include <stdio.h>
#include <string.h>

int main()
{
	char first[20], last[20];
	
	printf("Please enter your firstname: ");
	scanf("%s", first);
	printf("Please enter your lastname: ");
	scanf("%s", last);
	
	int flen = strlen(first);
	int llen = strlen(last);
	printf("%s %s\n", first, last);
	printf("%*d %*d", flen, flen, llen, llen);
	printf("\n");
	
	printf("%s %s\n", first, last);
	printf("%-*d %-*d", flen, flen, llen, llen);
	return 0;
}

8

/* 4.8.6.c -- 旅行里程和耗油量。 */

#include <stdio.h>

#define LITRE_PER_GALLON 3.785
#define KM_PER_MILE 1.609

int main()
{
	double mile, gas;
	
	printf("Please enter your trip distance in mile: ");
	scanf("%lf", &mile);
	printf("Please enter the gas you consumed in gallon: ");
	scanf("%lf", &gas);
	
	double milePerGallon = mile / gas;
	printf("The distance per gallon of gas is %.1f miles.\n", milePerGallon);
	
	printf("convert to litre per one hundred km: %.1f\n", 1 / milePerGallon / LITRE_PER_GALLON * KM_PER_MILE / 100);
	return 0;
}

5.11编程练习

1

/* 5.11.1.c -- 把分钟显示成“小时+分钟”,直到用户输入小于等于零的时间 */

#include <stdio.h>

#define MIN_PER_HOUR 60

int main()
{
	int minutes;
	
	do
	{
		printf("Please enter minutes: ");
		scanf("%d", &minutes);
	
		if (minutes > 0)
		{
			int h = minutes / MIN_PER_HOUR;
			int m = minutes % MIN_PER_HOUR;
			printf("It's %02d:%02d.\n", h, m);
		}
		else
			break;
	}
	while(1);

	return 0;
}

2

/* 5.11.2.c -- 打印从某个数到比他大10的所有整数。 */

#include <stdio.h>

int main()
{
	int num;
	printf("Please enter an integer: ");
	scanf("%d", &num);
	
	for (int current = num; current <= num +10; current++)
	{
		printf("%d\n", current);
	}	
	return 0;
}

3

/* 5.11.3.c -- 将天数显示成“周数+天数” */

#include <stdio.h>

int main()
{
	int days;
	
	while (1)
	{
		printf("Please enter the number of days: ");
		scanf("%d", &days);
	
		if (days <= 0)
			break;
		int w = days / 7;
		int d = days % 7;
		printf("%d days are %d weks, %d days.\n", days, w, d);	
	}
	return 0;
}

4

/* 5.11.4.c -- 重复接收身高并改单位输出。 */

#include <stdio.h>

int main()
{
    /* 一厘米等于0.3937英寸,一英尺等于12英寸。 */
    double centi;
    
    while (1)
    {
       printf("Enter a height in centimeters: ");
       scanf("%lf", &centi);
    
       if(centi <= 0)
         break;
       double inches = centi * 0.3937;
       int f = ((int)inches) / 12;
       double i = inches - f * 12;
       printf("%.1f cm = %d feet, %.1f inches\n", centi, f, i);
    }
    return 0;
}

5

/* 5.11.5.c -- 前n个整数累加 */

#include <stdio.h>

int main()
{
  int current, sum, max;
  
  printf("Please enter a number: ");
  scanf("%d", &max);
  
  current = 0;
  sum = 0;
  while (current++ < max)
    sum += current;
  printf("sum = %d\n", sum);
  return 0;
}

6

/* 5.11.6.c -- 前n个整数平方和 */

#include <stdio.h>

int main()
{
  int current, sum, max;
  
  printf("Please enter a number: ");
  scanf("%d", &max);
  
  current = 0;
  sum = 0;
  while (current++ < max)
    sum += (current * current);
  printf("sum = %d\n", sum);
  return 0;
}

7

/* 5.11.7.c -- 立方 */

#include <stdio.h>

double cube(double value)
{
  return value * value * value;
}

int main()
{
    double num;
    printf("Enter a double number: ");
    scanf("%lf", &num);
    
    printf("It's cube is: %.2f", cube(num));
    return 0;
}

8

/* 5.11.8.c -- 求模运算 */

#include <stdio.h>

int main()
{
    int first, second;
    printf("This program computes moduli.\n");
    
    while (1)
    {
      printf("Enter an integer to serve as the second operand: ");
      scanf("%d", &second);
      printf("Now enter the first operand: ");
      scanf("%d", &first);
    
      if (first <= 0 || second <= 0)
        break;
        
      printf("%d %% %d is %d\n", first, second, first % second);
    }
    return 0;
}

9

/* 5.11.9.c -- 温标换算 */

#include <stdio.h>

void Temperatures(double fa)
{
  const double absZero = 273.16;
  double centigrade = 5.0 / 9.0 * (fa - 32.0);
  printf("in centigrade is: %.2f\n", centigrade);
  printf("in kelvin is: %.2f\n", centigrade + absZero);
}

int main()
{
    double fahrenheit;
    
    while (1)
    {
      printf("Enter a fahrenheit dregree: ");
      if (scanf("%lf", &fahrenheit))
        Temperatures(fahrenheit);
      else
        break;
    }
    
    return 0;
}

总结&感悟

  • Q:如何动态地指定打印宽度?平常指定一个固定宽度写法:printf("%5d", num)(右对齐、宽度5、打印十进制数),如果5不能在编译时知道,怎么办?
    A: printf("%*d", length, num):用*代替具体宽度,后面参数加一个长度length
posted @ 2024-10-12 16:18  _vertigo  阅读(14)  评论(0编辑  收藏  举报