C语言学习笔记[第15天]

五、 运算号、表达式和语句

5. 类型转换
/* convert.c -- 自动类型转换 */
#include <stdio.h>
int main(void)
{
    char ch;
    int i;
    float fl;
    
    fl = i = ch = 'c';										/* 第9行  */
    printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl);		/* 第10行 */
    ch = ch + 1;											/* 第11行 */
    i =  fl + 2 * ch;										/* 第12行 */
    fl = 2.0 * ch + i;										/* 第13行 */
    printf("ch = %c, i = %d, fl = %2.2f\n", ch, i, fl);		/* 第14行 */
    ch = 1107;												/* 第15行 */
    printf("Now ch = %c\n", ch);							/* 第16行 */
    ch =  80.89;											/* 第17行 */
    printf("Now ch = %c\n", ch);							/* 第18行 */
    
    return 0;
}
//ch = c, i = 99, fl = 99.00
//ch = d, i = 299, fl = 499.00
//Now ch = S
//Now ch = P
5.1 强制类型转换运算符
mice = 1.6 +  1.7;
mice = (int)1.6 + (int)1.7;
6. 带参数的函数
/* pound.c -- 定义一个带一个参数的函数 */
#include  <stdio.h>
void pound(int  n);// ANSI 函数原型声明 
int main(void)
{
    int times = 5;
    char ch  ='!';		// ASCII 码是33
    float f = 6.0;
    
    pound(times);		// int 类型的参数
    pound(ch);			// 和pound((int)ch);相同
    pound(f);			// 和pound((int)f);相同
    
    return 0;
}

void pound(int n)		// ANSI 风格函数头
{						// 表明该函数接受一个int类型的参数
    while (n-- > 0)
        printf("#");
    printf("\n");
}
//#####
//#################################
//######
7. 示例程序
// running.c -- A usefull program for runners
#include <stdio.h>
const int S_PER_M =  60;				// 1 分钟的秒数
const int S_PER_H =  3600;				// 1 小时的秒数
const double M_PER_K = 0.62137;			// 1 公里的英里数
int main(void)
{
    double distk, distm;	// 跑过的距离(分别以公里和英里为单位)
    double rate;			// 平均速度(以英里/小时为单位)
    int min, sec;			// 跑步用时(以分钟和秒为单位)
    int time;				// 跑步用时(以秒为单位)
    double mtime;			// 跑1英里需要的时间 ,以秒为单位
    int mmin, msec;			// 跑1英里需要的时间 ,以分钟和秒为单位
	
    printf("This program converts your time for a metric race\n");
    printf("to a time for running a mile and to your average\n");
    printf("speed in miles per hour.\n");
    printf("Please enter, in kilometers, the distance run.\n");
    scanf("%lf", &distk);		// %If表示读取一个doublele类型的值
    printf("Next enter the time in minutes and seconds.\n");
    printf("Begin by entering the minutes.\n");
    scanf("%d", &min);
    printf("Now enter the seconds.\n");
    scanf("%d", &sec);
    
    time = S_PER_M * min + sec;		// 把时间转换成秒
    distm = M_PER_K * distk;		// 把公里转换成英里
    rate = distm / time * S_PER_H;	// 英里/秒*秒/小时 = 英里/小时
    mtime = (double) time / S_PER_M;// 时间/距离 = 跑1英里所用的时间
    mmin = (int) mtime / S_PER_M;	// 求出分钟数
    msec = (int) mtime % S_PER_M;	// 求出剩余的秒数
    
    printf("You ran %1.2f km (%1.2f miles) in %d min, %d sec.\n",
          distk, distm, min, sec);
    printf("That pace corresponds to running a mile in %d min,",
          mmin);
    printf("%d sec.\nYour average speed was %1.2f mph.\n", msec,
          rate);
    
    return 0;
}
//This program converts your time for a metric race
//to a time for running a mile and to your average
//speed in miles per hour.
//Please enter, in kilometers, the distance run.
//10.0
//Next enter the time in minutes and seconds.
//Begin by entering the minutes.
//36
//Now enter the seconds.
//23
//You ran 10.00 km (6.21 miles) in 36 min, 23 sec.
//That pace corresponds to running a mile in 0 min,36 sec.
//Your average speed was 10.25 mph.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值