C语言学习(三)运算符、表达式和语句

参考书:《C Primer Plus》第六版


  1. while循环,程序清单1
  2. 基本运算符有:赋值运算符=、加法运算符+、减法运算符-、乘法运算符*、除法运算符/,每个运算符都有自己的优先级。其它的运算符,比较常见的有:sizeof运算符、求模运算符%、递增运算符++、递减运算符--
  3. 一些基本概念:副作用:对数据对象或文件的修改;序列点:程序执行的点,在该点上,所有的副作用都在进入下一步之前发生。复合语句:用花括号括起来的一条或多条语句,也称为块。
  4. 基本的类型转化规则:1)当类型转换出现在表达式时,无论是unsigned还是signed的char和short都会被自动转换为int,如有必要会被转换为unsigned int。从较小类型转换为较大类型的这种转换称为升级;2)设计两种类型的运算,两个值会被分别转换成两种类型的更高级别;3)类型的级别从高到低依次是:long double、double、float、unsigned long long、long long、unsigned long、long、unsigned int、int;4)赋值表达式语句中,计算的结果会被转换成被赋值变量的类型;5)当作为函数参数传递时,char和short被转换成int,float被转换成double。
  5. 待赋值的值和目标类型不匹配时,规则如下:1)目标类型是无符号整型而待赋的值是整数时,额外的位将被忽略;2)如果目标类型是一个有符号整型而待赋的值是整数,结果因实现而异;3)如果目标类型是一个整型而待赋的值是浮点数,该行为是未定义的。
  6. 有些时候我们需要强制类型转换,此时在某个量的前面放置用圆括号括起来的类型名,圆括号和它括起来的类型名构成了强制类型转换运算符。

程序清单1

#include<stdio.h>
#define ADJUST 7.31

int main(void) {
	const double SCALE = 0.333;
	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 %15.2f inches\n", shoe, foot);
		shoe = shoe + 1.0;
	}

	return 0;
}

输出

shoe size (men's)   foot length
       3.0            8.31 inches
       4.0            8.64 inches
       5.0            8.97 inches
       6.0            9.31 inches
       7.0            9.64 inches
       8.0            9.97 inches
       9.0           10.31 inches
      10.0           10.64 inches
      11.0           10.97 inches
      12.0           11.31 inches
      13.0           11.64 inches
      14.0           11.97 inches
      15.0           12.30 inches
      16.0           12.64 inches
      17.0           12.97 inches
      18.0           13.30 inches

C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 6756)已退出,代码为 0。
按任意键关闭此窗口. . .

练习

  1. 编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间。使用#define 或const 创建一个表示60的符号常量或const常量。通过while循环让用户重复输入值,直到用户输入小于或等于0的值才停止循环。
  2. 编写一个程序,提示用户输入一个整数,然后打印从该数到比该数大10的所有整数。
  3. 编写一个程序,提示用户输入天数,然后将其转换为周数和天数。
  4. 编写一个程序,提示用户输入一个double类型的数,并打印该数的立方值。自己设计一个函数计算并打印立方值,main()函数将用户输入的值传递给该函数。
  5. 编写一个程序,显示求模运算符的结果。用户输入的第一个数是求模运算的第2个运算对象,第2个数是求模运算的第1个运算对象,输入非正数时程序结束。
  6. 编写一个程序,读取一个华氏温度,double类型的。将该值传递给一个用户自定义的函数Temperatures(),该函数计算摄氏温度和开氏温度,并以小数点后面两位数字的精度显示3种温度。使用不同的温标来表示3个温度值。

第一题:

#include<stdio.h>
#define unit 60

int main(void) {
	int minutes;
	printf("Enter a value: ");
	scanf_s("%d", &minutes);
	while (minutes>0)
	{
		printf("=>%d hours %d minutes\n", minutes / unit, minutes % unit);
		printf("Enter a value: ");
		scanf_s("%d", &minutes);
	}

	return 0;
}

输出

Enter a value: 123
=>2 hours 3 minutes
Enter a value: 234
=>3 hours 54 minutes
Enter a value: 342
=>5 hours 42 minutes
Enter a value: 0

C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 37584)已退出,代码为 0。
按任意键关闭此窗口. . .

第二题:

#include<stdio.h>

int main(void) {
	int value;
	printf("Enter a value:");
	scanf_s("%d", &value);
	int i = 0;
	while (i < 11) {
		printf("%d ", value + i);
		++i;
	}

	return 0;
}

输出

Enter a value:23
23 24 25 26 27 28 29 30 31 32 33
C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 46136)已退出,代码为 0。
按任意键关闭此窗口. . .

第三题:

#include<stdio.h>

int main(void) {
	int days;
	printf("Enter the number of day:");
	scanf_s("%d", &days);
	while (days > 0) {
	printf("%d days are %d weeks, %d days.", days, days / 7, days % 7);
	printf("Enter the number of day:");
	scanf_s("%d", &days);
	}
	

	return 0;
}

输出

Enter the number of day:18
18 days are 2 weeks, 4 days.Enter the number of day:22
22 days are 3 weeks, 1 days.Enter the number of day:0

C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 44228)已退出,代码为 0。
按任意键关闭此窗口. . .

第四题:

#include<stdio.h>

void cube(double in);
int main(void) {
	double dv;
	printf("Enter a value:");
	scanf_s("%lf", &dv);
	cube(dv);
	return 0;
}

void cube(double in) {
	printf("%f", in*in*in);
}

输出

Enter a value:0.44
0.085184
C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 46468)已退出,代码为 0。
按任意键关闭此窗口. . .

第五题:

#include<stdio.h>

int main(void) {
	int f1, f2;
	printf("This program computes moduli.\n");
	printf("Enter  an integer to serve as the second operand: ");
	scanf_s("%d", &f1);
	printf("Now enter the first operand: ");
	scanf_s("%d", &f2);
	printf("%d %% %d is %d\n", f2, f1, f2 % f1);
	f1 = f2;
	printf("Enter the next number for first operand (<= 0 to quit): ");
	scanf_s("%d", &f2);
	while (f2>0)
	{
		printf("%d %% %d is %d\n", f2, f1, f2 % f1);
		f1 = f2;
		printf("Enter the next number for first operand (<= 0 to quit): ");
		scanf_s("%d", &f2);
	}

	return 0;
}

输出

This program computes moduli.
Enter  an integer to serve as the second operand: 234
Now enter the first operand: 345
345 % 234 is 111
Enter the next number for first operand (<= 0 to quit): 233
233 % 345 is 233
Enter the next number for first operand (<= 0 to quit): 3222
3222 % 233 is 193
Enter the next number for first operand (<= 0 to quit): 0

C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 44828)已退出,代码为 0。
按任意键关闭此窗口. . .

第六题:

#include<stdio.h>

void Temperatures(double fahrenheit);
int main(void) {
	double feh;
	printf("Enter a temperature in Fahrenheit: ");
	
	while (scanf_s("%lf", &feh) != 0) {
		Temperatures(feh);
		printf("Enter a temperature in Fahrenheit: ");
	}
	return 0;
}
void Temperatures(double fehrenheit) {
	double const v1 = 5.0, v2 = 9.0, v3 = 32.0, v4 = 273.16;
	double celsius = v1 / v2 * (fehrenheit - v3);
	double kelvin = celsius - v4;
	printf("feherheit : %.2f, celsius: %.2f, kelvin: %.2f\n", fehrenheit, celsius, kelvin);
}

输出

Enter a temperature in Fahrenheit: 122
feherheit : 122.00, celsius: 50.00, kelvin: -223.16
Enter a temperature in Fahrenheit: 111
feherheit : 111.00, celsius: 43.89, kelvin: -229.27
Enter a temperature in Fahrenheit: 321
feherheit : 321.00, celsius: 160.56, kelvin: -112.60
Enter a temperature in Fahrenheit: 123.213
feherheit : 123.21, celsius: 50.67, kelvin: -222.49
Enter a temperature in Fahrenheit: p

C:\Users\xhh\Source\Repos\c_study_1\Debug\c_study_1.exe (进程 32584)已退出,代码为 0。
按任意键关闭此窗口. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值