CPrimerPlus学习(五):while循环/运算符

while循环

//不循环
#include<stdio.h>
#define ADJUST 7.31 // 字符常量

int main(void)
{
	const double SCALE = 0.333;// const变量
	double shoe, foot;
	shoe = 9.0;
	foot = SCALE * shoe + ADJUST;
	printf("Shoe size (men's)   foot length\n");
	printf("%10.1f %16.2f inches\n", shoe, foot);
	return 0;
}
//循环
#include<stdio.h>
#define ADJUST 7.31

int main(void)
{
	const double SCALE = 0.333;// const变量
	double shoe, foot;
	printf("Shoe size (men's)   foot length\n");
	shoe = 3.0;
	while (shoe < 18.5)
	{
		foot = SCALE * shoe + ADJUST;
		printf("%10.1f %16.2f inches\n", shoe, foot);
		shoe = shoe + 1.0;
	}
	printf("If the shoe fits, wear it.\n");
	return 0;
}

基本运算符

1、= 表示赋值而不是相等
2、运算符优先级:
		括号
		先乘除后加减
		优先级相同时按出现顺序执行

其它运算符
-
1、sizeof
		以字节为单位返回运算对象的大小
		运算对象可以是具体的数据对象(如,变量名)或类型。
		如果运算对象是类型(如, float),则必须用圆括号将其括起来。
2、求模:%
		只能用于整数运算
		计算余数
3、递增递减
		++/--
4、(类型名) 
		强制类型转换运算符
		将其右侧的值转换成圆括号中指定的类型,如(float)9把整数9转换成浮点数9.0

typedef

	允许我们为现有类型创建别名
	例如:typedef double real;
	real就是double的别名
	可以声明real deal; //相当于double deal;

带参数的函数

void pound(int n)
如果函数不接受任何参数,函数头的圆括号中应该写上关键字void。 
由于该函数接受一个int 类型的参数,所以圆括号中包含一个int类型变量n的声明。

实参与形参:
我们可以说形参是变量,实参是函数调用提供的值,实参被赋给相应的形参。

在调用函数时可以:pound ((int)f); 
把浮点数f强制类型转换为正确的类型
注意:如果f的值太大,超过了int类型表示的范围,这样做也不行。

编程练习

1
编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间。
使用#define或const创建一个表示60的符号常量或const变量。
通过while循环让用户重复输入值,直到用户输入小于或等于0的值才停止循环。

#include<stdio.h>
#define M 60

int main(void)
{
	int minutes;
	int hour,min;

	printf("Enter minutes:");
	scanf("%d", &minutes);
	while (minutes > 0)
	{
		hour = minutes / M;
		min = minutes % M;
		printf("%d minutes = %d hours %d minutes\n",minutes,hour,min);
		printf("Enter minutes again:");
		scanf("%d", &minutes);
	}
	printf("Error\n");

	return 0;
}

2
编写一个程序,提示用户输入一个整数,然后打印从该数到比该数大10的所有整数
(例如,用户输入5,则打印5~15的所有整数,包括5和 15)。
要求打印的各值之间用一个空格、制表符或换行符分开。

#include<stdio.h>

int main(void)
{
	int num;
	int numadd;

	printf("Enter a number:");
	scanf("%d", &num);

	numadd = num + 10;

	while (num <= numadd)
	{
		printf("%d\n",num);
		num++;
	}
	printf("Over\n");

	return 0;
}

3
编写一个程序,提示用户输入天数,然后将其转换成周数和天数。
例如,用户输入18,则转换成2周4天。以下面的格式显示结果:
18 days are 2 weeks, 4 days.
通过while循环让用户重复输入天数,当用户输入一个非正值时(如0 或-20),循环结束。

#include<stdio.h>

int main(void)
{
	const int D = 7;
	int days;
	int week,day;

	printf("Enter days:");
	scanf("%d", &days);

	while (days > 0)
	{
		week = days / D;
		day = days % D;
		printf("%d days = %d weeks %d days\n",days,week,day);
		printf("Enter days again:");
		scanf("%d", &days);
	}
	printf("Error\n");

	return 0;
}

4
编写一个程序,提示用户输入一个身高(单位:厘米),并分别以厘米和英寸为单位显示该值,允许有小数部分。
程序应该能让用户重复输入身高,直到用户输入一个非正值。其输出示例如下:
Enter a height in centimeters: 182
182.0 cm = 5 feet, 11.7 inches
Enter a height in centimeters (<=0 to quit): 168.7
168.0 cm = 5 feet, 6.4 inches
Enter a height in centimeters (<=0 to quit): 0
bye

#include<stdio.h>
#define C 33.33

//1 feet = 33.33 cm
//与书中示例结果不一致

int main(void)
{
	float height;
	int feet;
	float feets,inch;

	printf("Enter height(in cm):");
	scanf("%f", &height);

	while (height > 0)
	{
		feets = height / C;
		feet = (int)feets;
		inch = height - feet * C;

		printf("%.2f cm = %d feet %.2f inches\n",height,feet,inch);
		printf("Enter height again(in cm):");
		scanf("%f", &height);
	}
	printf("Error\n");

	return 0;
}

5
修改程序addemup.c(程序清单5.13),
你可以认为addemup.c是计算20天里赚多少钱的程序
(假设第1天赚$1、第2天赚$2、第3天赚$3,以此类 推)。
修改程序,使其可以与用户交互,根据用户输入的数进行计算(即, 用读入的一个变量来代替20)。

#include <stdio.h> 

int main(void)    
{   
	int count, sum;               
	int n;      

	printf("Enter days: ");   
	scanf("%d", &n);   

	count = 0;                     
	sum = 0;           

	while (count++ < n)      
		sum = sum + count;    

	printf("sum = %d\n", sum);   

	return 0; 
} 

6
修改编程练习5的程序,使其能计算整数的平方和
(可以认为第1天赚 $1、第2天赚$4、第3天赚$9,以此类推,这看起来很不错)。
C没有平方函数,但是可以用n * n来表示n的平方。

#include <stdio.h> 

int main(void)    
{   
	int count, sum;               
	int n;      

	printf("Enter days: ");   
	scanf("%d", &n);   

	count = 0;                     
	sum = 0;           

	while (count++ < n)      
		sum = sum + count * count;    

	printf("sum = %d\n", sum);   

	return 0; 
} 

7
编写一个程序,提示用户输入一个double类型的数,并打印该数的立方值。
自己设计一个函数计算并打印立方值。
main()函数要把用户输入的值传递给该函数。

#include <stdio.h> 

void tri(double i)
{
	double t = i * i * i;
	printf("%lf^3 = %lf\n", i,t);
}

int main(void)    
{   
	double n;

	printf("Enter a number:");
	scanf("%lf", &n);

	tri(n);

	return 0; 
} 

8
编写一个程序,显示求模运算的结果。
把用户输入的第1个整数作为求模运算符的第2个运算对象,该数在运算过程中保持不变。
用户后面输入的数是第1个运算对象。
当用户输入一个非正值时,程序结束。其输出示例如下:
This program computes moduli.
Enter an integer to serve as the second operand: 256
Now enter the first operand: 438
438 % 256 is 182
Enter next number for first operand (<= 0 to quit): 1234567
1234567 % 256 is 135
Enter next number for first operand (<= 0 to quit): 0
Done

#include <stdio.h> 

int main(void)    
{   
	int a,b;
	int m;

	printf("This program computes moduli.\n");
	printf("Enter an integer to serve as the second operand:");
	scanf("%d", &a);
	const int c = a;
	printf("Now enter the first operand:");
	scanf("%d", &b);
	while (b > 0)
	{
		m = b % c;
		printf("%d %% %d = %d\n", b,c,m);

		printf("Now enter the first operand again:");
		scanf("%d", &b);
	}
	printf("error\n");

	return 0; 
} 

/*
This program computes moduli.
Enter an integer to serve as the second operand:256
Now enter the first operand:438
438 % 256 = 182
Now enter the first operand again:1234567
1234567 % 256 = 135
Now enter the first operand again:0
error
*/

9
编写一个程序,要求用户输入一个华氏温度。
程序应读取double类型的值作为温度值,并把该值作为参数传递给一个用户自定义的函数Temperatures()。
该函数计算摄氏温度和开氏温度,并以小数点后面两位数字的精度显示3种温度。
要使用不同的温标来表示这3个温度值。
下面是华氏温度转摄氏温度的公式:
摄氏温度 = 5.0 / 9.0 * (华氏温度 - 32.0)
开氏温标常用于科学研究,0表示绝对零,代表最低的温度。
下面是摄氏温度转开氏温度的公式:
开氏温度 = 摄氏温度 + 273.16
Temperatures()函数中用const创建温度转换中使用的变量。
在main()函数中使用一个循环让用户重复输入温度,当用户输入 q 或其他非数字时,循环结束。
scanf()函数返回读取数据的数量,所以如果读取数字则返回1,如果读取q则不返回1。
可以使用==运算符将scanf()的返回值和1作比较,测试两值是否相等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值