C Primer Plus(第六版)第五章 运算符、表达式和语句

1、C用运算符(operator)表示算术运算。基本算术运算的运算符:=、+、-、*和/(C没有指数运算符。不过,C 的标准数学库提供了一个pow()函数用于指数运算。例如,pow(3.5, 2.2)返回3.5的2.2次幂)。

2、+和-运算符都被称为二元运算符(binary operator),即这些运算符需要两个运算对象才能完成操作。

3、使用sizeof运算符,如果编译器不支持%zd,请将其改成%u或%lu。

4、C有一个typedef机制(第14章再详细介绍),允许程序员为现有类型创建别名。例如,
typedef double real;这样,real就是double的别名。现在,可以声明一个real类型的变量:
real deal; // 使用typedef。编译器查看real时会发现,在typedef声明中real已成为double的别
名,于是把deal创建为double 类型的变量。

5、求模运算符(modulus operator)用于整数运算。求模运算符给出其左侧整数除以右侧整数的余数(remainder)。例如,13 % 5(读作“13求模5”)得3,因为13比5的两倍多3,即13除以5的余数是3。求模运算符只能用于整数,不能用于浮点数。

6、                  a_post = a++; // 后缀:先使用a的值后,再递增a
                        b_pre= ++b; // 前缀:先递增b,再使用b的值

一些简单的表达式语句以及运算,我就不多讲了,运算的优先级就跟小学数学运算差不多的。我下面直接就去做第五章的编程练习把。

/*第一题*/
#include <stdio.h>
#define min_per_hour 60      //一小时60分
//const int min_per_hour = 60;
int main(void)
{
    int min, hour, left;
    
    printf ("Convert minutes to hour and minutes!\n");
    printf ("Enter the number of minutes (<=0 to quit) :\n");
    scanf ("%d", &min);      //输入分钟数
    while (min > 0)
    {
        hour = min / min_per_hour;  //截断分钟数,获得小时数
        left = min % min_per_hour;   //剩下的分钟数
        printf ("%d minutes is %d hour, %d minutes.\n", min, hour, left);
        printf ("Enter next value (<=0 to quit) :\n");
        scanf ("%d", &min);
    }
    printf ("Done!\n");
    return 0;
}
/*输出结果*/
Convert minutes to hour and minutes!
Enter the number of minutes (<=0 to quit) :
120
120 minutes is 2 hour, 0 minutes.
Enter next value (<=0 to quit) :
90
90 minutes is 1 hour, 30 minutes.
Enter next value (<=0 to quit) :
-60
Done!
/*第二题*/
#include <stdio.h>
int main(void)
{
    int a;
    int b = 0;
    printf ("please input a number : ");
    scanf ("\n%d", &a);
    while ( b <= 10 )
    {
        printf ("%d ", a);
        b++;
        a++;
    }
    return 0;
}

/*输出结果*/
please input a number : 5
5 6 7 8 9 10 11 12 13 14 15 
/*第三题*/
#include <stdio.h>
#define day_per_week 7      //一小时60分
//const int day_per_week = 7;
int main(void)
{
    int day, week, left;
    
    printf ("Convert day to weeks and days!\n");
    printf ("Enter the number of days (<=0 to quit) :\n");
    scanf ("%d", &day);      //输入tian 数
    while (day > 0)
    {
        week = day / day_per_week ;  //截断分钟数,获得小时数
        left = day % day_per_week ;   //剩下的分钟数
        printf ("%d days is %d weeks, %d days.\n", day, week, left);
        printf ("Enter next value (<=0 to quit) :\n");
        scanf ("%d", &day);
    }
    printf ("Done!\n");
    return 0;
}

/*输出结果*/
Enter the number of days (<=0 to quit) :
14
14 days is 2 weeks, 0 days.
Enter next value (<=0 to quit) :
365
365 days is 52 weeks, 1 days.
/*第四题*/
#include <stdio.h>
const float feet_per_cm = 30.48; // 1 feet= 30.48cm
const float inch_per_cm = 2.54;    // 1 inch= 2.54cm
int main(void)
{
    float cm, feet, inch;
    printf ("Enter a height in centimeters: ");
    scanf ("\n%f", &cm); 
    while (cm > 0)
    {
        
        feet = cm / 30.48;
        inch = cm /2.54;
        printf ("%.1f cm = %1.0f feet, %.1f inches\n", cm, feet, inch); 
        printf ("Enter a height in centimeters (<=0 to quit) : ");
        scanf ("\n%f", &cm); 
    }
    printf ("bye\n");
    return 0;
}

/*输出结果*/
Enter a height in centimeters: 182
182.0 cm = 6 feet, 71.7 inches
Enter a height in centimeters (<=0 to quit) : 168.2
168.2 cm = 6 feet, 66.2 inches
Enter a height in centimeters (<=0 to quit) : 0
bye
/*第五题*/
#include<stdio.h>                                 
int main(void)
{
	int count, sum, n;
	sum = 0;
	count = 0;
	
	printf("How many days dou you want to work?\n");
	scanf("%d",&n);
	while(count++ < n)
		{
			sum = sum + count;
		}
	printf("sum = %d\n",sum);
	
	return 0;
} 

/*输出结果*/
How many days dou you want to work?
365
sum = 66795
/*第六题*/
#include<stdio.h>                                 
int main(void)
{
	int count, sum, n;
	sum = 0;
	count = 0;
	
	printf("How many days dou you want to work?\n");
	scanf("%d",&n);
	while(count++ < n)
		{
			sum = sum + count * count;
		}
	printf("sum = %d\n",sum);
	
	return 0;
}
/*输出结果*/
How many days dou you want to work?
30
sum = 9455
/*第七题*/
#include<stdio.h> 
void cubic(void);
int main(void)
{	
	printf("Enter a number in double type.\n");
	cubic();
	
	return 0;
}
void cubic(void)
{
	double number;
	scanf("%lf",&number);
	printf("The cube of this number is %f\n",number*number*number);
} 
/*输出结果*/
Enter a number in double type.
3.0
The cube of this number is 27.000000
/*第八题*/
#include<stdio.h> 
int main(void)
{
    int a, b, c;
    printf ("This program computes moduli.\n");
    
    printf ("Enter an integer to serve as the second operand: ");
    scanf ("\n%d", &a);
    
    printf ("Now enter the first operand: ");
    scanf ("\n%d", &b);
    while ( b > 0 )
    {
        c = b % a;
        printf ("%d %% %d is %d\n", b, a, c);
        printf ("Enter next number for first operand (<= 0 to quit): ");
        scanf ("\n%d", &b);
    }
    printf ("Done\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 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> 
void Temperatures(double Fah_tem );
int main(void)
{
    double Fah_tem;
    int i;
    printf("Please enter the Fahrenheit temperature: ");
    while ( scanf ("\n%lf", &Fah_tem) == 1 )
    {
        Temperatures( Fah_tem );
        printf("Please enter the next Fahrenheit temperature: ");
    }
    return 0;
}
void Temperatures(double Fah_tem )
{
    const double Cent_tem = 5.0/9.0 *(Fah_tem - 32.0);
    const double Kel_tem = Cent_tem + 273.16;
    printf ("The Fahrenheit temperature is : %0.2lf \n", Fah_tem);
    printf ("The Fahrenheit temperature is : %0.2lf \n", Cent_tem);
    printf ("The Fahrenheit temperature is : %0.2lf \n", Kel_tem);
}

/*输出演示结果*/
Please enter the Fahrenheit temperature: 100
The Fahrenheit temperature is : 100.00 
The Fahrenheit temperature is : 37.78 
The Fahrenheit temperature is : 310.94 
Please enter the next Fahrenheit temperature: -20
The Fahrenheit temperature is : -20.00 
The Fahrenheit temperature is : -28.89 
The Fahrenheit temperature is : 244.27 
Please enter the next Fahrenheit temperature: .?     //退出程序了

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值