"C Primer Plus" (sixth edition) answers (5.11)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43219615/article/details/99937324

To use the "C Primer Plus" C beginners learning to prepare.

  1. Write a program, converts the time indicated by the time indicated by minutes to hours and minutes. Use #define or const Creates a symbol or const constant 60. By allowing users to re-enter the while loop value.
#include <stdio.h>
#define MIN_PER_H 60
int main(int argc, char const **argv) {
	int min; 
	int hour;
	
	printf("请输入分钟:");
	scanf("%d", &min);
	
	while(min >= 0) {
		//整数的除法取整 
		hour = min / MIN_PER_H;
		
		printf("%d分钟是%d小时%d分钟.\n", min, hour, min - hour * MIN_PER_H);
		
		printf("请输入分钟:");
		scanf("%d", &min);
	}
	
	return 0;
} 
  1. Write a program that prompts the user for an integer, and the number of prints from the ratio to the number of all integers is 10 (e.g., a user input 5, the print all integer of 5 to 15, including 5, and 15). With a space, tab or newline separated between values ​​required for printing.
#include <stdio.h>
#define MIN_PER_H 60
int main(int argc, char const **argv) {
	int a;
	
	printf("请输入一个整数:");
	scanf("%d", &a);
	
	int i = 0;
	while(i <= 10) {
		printf("%d ", a + i);
		i++;
	}
	
	return 0;
} 
  1. Write a program that prompts the user to enter the number of days, and then converted into weeks and days. For example, a user input 18, is converted to 4 weeks 2 days. Display Results in the following format.
    18 days are 2 weeks, 4 days
    . By repeated while loop allows the user enter the number of days, when the user inputs a non-positive, the cycle ends.
#include <stdio.h>
#define DAYS_PER_WEEK 7
int main(int argc, char const **argv) {
	int days;
	int weeks;
	
	printf("请输入天数(小于等于0退出):");
	scanf("%d", &days);
	
	while(days > 0) {
		weeks = days / DAYS_PER_WEEK;//周数 
		int r = days - weeks * DAYS_PER_WEEK;//余数 
		
		/*使用三目运算符为了输出更人性化 比如天数大于1输出days 否则输出day*/ 
		printf("%d %s are %d %s, %d %s.\n", days, days > 1? "days":"day", weeks, weeks > 1? "weeks":"week", r, r > 1? "days":"day");
		
		printf("请输入天数(小于等于0退出):");
		scanf("%d", &days);
	}

	return 0;
} 
  1. Write a program that prompts the user to enter the height (unit: cm), and are in centimeters and inches displays this value, allowing the fractional part. The program allows users to repeatedly enter the height, to know the user enters a non-positive.
    Which sample output:
    the Enter A height in centimeters: 182
    182.0 cm & lt =. 5 feet ', 11.7 Inchs
    the Enter A height in centimeters (<= 0 to quit): 168.7
    168.7 cm & lt =. 5 feet', 6.4 Inchs
    the Enter A height in centimeters (<= to quit 0): 0
    BYE
#include <stdio.h>
#define INCH_PER_CEN 2.54
#define FT_PER_IN 12
int main(int argc, char const **argv) {
	float f;
	float inches;
	int feets;
	
	printf("Enter a height in centimeters:");
	scanf("%f", &f);
	 
	//至少循环了一次 可以使用do while
	do {
		inches = f / INCH_PER_CEN;//英寸 
		feets = (int)(inches / FT_PER_IN);//英尺 
		float r = inches - feets * FT_PER_IN;//余数 
		printf("%.1f cm = %d feet, %.1f inches\n", f, feets, r);
		printf("Enter a height in centimeters(<=0 to quit):");
		scanf("%f", &f);
	} while(f > 0);

	printf("bye");
	return 0;
} 
  1. Modify the program addemup.c (Listing 5.13), you can think addemup.c is to calculate how much of the program in 20 days (assuming that the first day to earn $ 1, the next day to earn $ 2 ...). Modify the program so that it can be calculated (i.e., with a variable read instead of 20) interact with a user based on user input.
#include<stdio.h>
int main(void)
{
	int count=0, sum=0;
	int n;
	
	printf("Enter the supper limit:");
	scanf("%d",&n);
	
	//++下一次使用变量时才加一 
	while(count++ < n)
	{
		sum = sum + count*count;
	}
	
	printf("The total is %d$", sum);
	
	return 0;
} 
  1. 5 modify the program so that it can calculate the sum of squares (considered the first day to earn $ 1, the next day to earn $ 4 ... think are good). C is not square function, but may be used in place of n * n square.
#include<stdio.h>
int main(void)
{
	int count = 0, sum = 0;
	int n;
	
	printf("Enter the supper limit:\n");
	scanf("%d",&n);
	
	while(count++ < n)
	{
		sum = sum + count*count;
	}
	printf("The total is %d$", sum);
	
	return 0;
} 
  1. Write a program that prompts the user for a double in number, and the number of prints cube. Own design and print a cubic function values. main () function to change the user input value passed to the function.
#include <stdio.h>

/*计算立方的函数*/
double cube(double d) {
	return d * d * d;
} 

int main(int argc, char const **argv) {
	double d;
	
	printf("Input a num:");
	scanf("%lf", &d);
	
	printf("The cube of %lf is %lf.\n", d, cube(d)); 

	return 0;
} 
  1. Write a program that displays the results of the modulo operation. The first user input as integer modulo operator of two comparison objects, the number remains constant during operation. Behind the user input is a first comparison objects. When a user enters a non-positive, the program ends. Which sample output:
    This Program COMPUTES moduli.
    The Enter A Integer to Sever AS The SECOND the operand: 256
    Now Enter The First the operand: 438
    438% 256 IS 182
    the Enter The Next Number for First the operand (<= 0 to quit):. 1234567
    256 IS 135% 1234567
    the Enter at The First for the Next Number The operand (<= 0 to quit):? 0
    Done
#include<stdio.h>
int main(void)
{
	int s_operand, f_operand;
	
	printf("This program computes moduli.\n");
	
	printf("Enter a integer to sever as the second operand:");
	scanf("%d", &s_operand);
	
	printf("Now enter the first operand:");
	scanf("%d", &f_operand);
	
	printf("%d %% %d is %d\n",f_operand, s_operand, f_operand%s_operand);
	
	printf("Enter the next number for first operand.(<=0 to quit):");
	scanf("%d", &f_operand);
	
	while(f_operand > 0)
	{
		printf("%d %% %d is %d\n", f_operand, s_operand, f_operand%s_operand);
		printf("Enter the next number for first operand?(<=0 to quit):");
		scanf("%d", &f_operand);
	}
	printf("Done");
	
	return 0;
 } 
  1. Write a program that requires users to enter a Fahrenheit temperature. Type of program should be read as a double value of temperature, and this value as a parameter to a function of a user-defined Temperatures (). This function computes Celsius and Kelvin temperature, and with a precision of two decimal places after the decimal point display 3 temperature. The following is converted into Fahrenheit Celsius formula:
    Celsius = 5.0 / 9.0 * (Fahrenheit - 32.0)
    Kelvin = Celsius + 273.16

    function using Temperature () const create a variable temperature used in the conversion. In the main () function using a temperature cycle allows the user to repeat input, when the user inputs other non-digital, loop ends. scanf () function returns the number of read data, the read digital Returns 1 if, if the read q 0. == operator can use the scanf () return value and returns a comparison, if the two tests equal.
#include <stdio.h>
void Temperature(double);
int main(void)
{
	double temperature;
	
	printf("请输入华氏温度(q退出):");
	while(scanf("%lf",&temperature) == 1)
	{
		Temperature(temperature);
		printf("请输入华氏温度(q退出):\n");
	}
	
	return 0;
}
void Temperature(double temperature)
{
	printf("%0.2lf华氏温度 = %0.2lf摄氏温度 = %0.2lf开式温度\n", temperature, 5.0 / 9.0 * (temperature - 32.0), 5.0 / 9.0 * (temperature - 32) + 273.16);
}

Guess you like

Origin blog.csdn.net/weixin_43219615/article/details/99937324