第五章 运算符、表达式和语句

  • 程序清单5.1,shoes1.c:

/* shoes1.c -- 把鞋码转换成英寸 */ 
#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 %15.2f inches\n", shoe, foot);
    
    return 0;    
}

输出结果:

  • 程序清单5.2,shoes2.c:

/* shoes2.c -- 计算多个不同鞋码对应的脚长 */ 
#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; 
    }
    printf("If the shoe fits, wear it.\n");
    
    return 0;
} 

输出结果:

  • 程序清单5.3,golf.c:

/* golf.c -- 高尔夫锦标赛记分卡 */ 
#include <stdio.h>

int main(void)
{
    int jane, tarzan, cheeta;
    
    cheeta = tarzan = jane = 68;
    printf("                  cheeta  tarzan    jane\n");
    printf("First round score %4d %8d %8d\n", cheeta, tarzan, jane);
    
    return 0;
}

输出结果:

  • 程序清单5.4,squares.c:

/* squares.c -- 计算 1~20 的平方 */ 
#include <stdio.h>

int main(void)
{
    int num = 1;
    
    while (num < 21)
    {
        printf("%4d %6d\n", num, num * num);
        num = num + 1;
    }
    
    return 0;
}

输出结果:

  • 程序清单5.5,wheat.c:

/* wheat.c -- 指数增长 */ 
#include <stdio.h>
#define SQUARES 64 // 棋盘中的方格数
 
int main(void)
{
    const double CROP = 2E16; // 世界小麦年产谷粒数 
    double current, total;
    int count = 1;
    
    printf("square   grains       total        ");
    printf("fraction of \n");
    printf("         added        grains       ");
    printf("world total\n");
    total = current = 1.0; /* 从 1 颗谷粒开始 */ 
    printf("%4d %13.2e %12.2e %12.2e\n", count, current,
            total, total / CROP);
    while (count < SQUARES)
    {
        count = count + 1;
        current = 2.0 * current; /* 下一个方格谷粒翻倍 */ 
        total = total + current; /* 更新总数 */ 
        printf("%4d %13.2e %12.2e %12.2e\n", count, current,
                total, total / CROP);
    }
    printf("That's all.\n");
    
    return 0;
} 

输出结果:

  • 程序清单5.6,divide.c:

/* divide.c -- 演示除法 */ 
#include <stdio.h>

int main(void)
{
    printf("integer division: 5/4 is %d \n", 5 / 4);
    printf("integer division: 6/3 is %d \n", 6 / 3);
    printf("integer division: 7/4 is %d \n", 7 / 4);
    printf("floating dividion: 7./4. is %1.2f \n", 7. / 4.);
    printf("mixed division:   7./4 is %1.2f \n", 7. / 4);
    
    return 0;
}

输出结果:

  • 程序清单5.7,rules.c:

/* rules.c -- 优先级测试*/ 
#include <stdio.h>

int main(void)
{
    int top, score;
    
    top = score = -(2 + 5) * 6 + (4 + 3 * (2 + 3));
    printf("top = %d, score = %d\n", top, score);
    
    return 0;
}

输出结果:

  • 程序清单5.8,sizeof.c:

// sizeof.c -- 使用 sizeof 运算符
// 使用 C99 新增的 %zd 转换说明 -- 如果编译器不支持 %zd,请将其改成%u 或 %lu 
#include <stdio.h>

int main(void)
{
    int n = 0;
    size_t intsize;
    
    intsize = sizeof(int);
    printf("n = %d, n has %zd bytes; all ints have %zd bytes.\n", n, sizeof n, intsize);
    
    return 0;
}

输出结果:

  • 程序清单5.9,min_sec.c:

// min_sec.c -- 把秒数转换成分和秒 
#include <stdio.h>
#define SEC_PER_MIN 60 //一分钟 60 秒
 
int main(void)
{
    int sec, min, left;
    
    printf("Covert seconds to minutes and seconds!\n");
    printf("Enter the number of seconds (<=0 to quit):\n");
    scanf("%d", &sec); // 读取秒数 
    while (sec > 0)
    {
        min = sec / SEC_PER_MIN; // 截断分钟数 
        left = sec % SEC_PER_MIN; // 剩下的秒数 
        printf("%d seconds is %d minutes, %d seconds.\n", sec, min, left);
        printf("Enter next value (<=0 to quit):\n");
        scanf("%d", &sec);
    }
    printf("Done!\n");
    
    return 0;
}

输出结果:

  • 程序清单5.10,add_one.c:

/* add_one.c -- 递增:前缀和后缀 */ 
#include <stdio.h>

int main(void)
{
    int ultra = 0, super = 0;
    
    
    while (super < 5)
    {
        super++;
        ++ultra;
        printf("super = %d, ultra = %d \n", super, ultra);
    }
    
    return 0; 
} 

输出结果:

  • 程序清单5.11,post_pre.c:

/* post_pre.c -- 前缀和后缀 */ 
#include <stdio.h>

int main(void)
{
    int a = 1, b = 1;
    int a_post, pre_b;
    
    a_post = a++; // 后缀递增 
    pre_b = ++b; // 前缀递增 
    printf("a  a_post   b   pre_b \n");
    printf("%1d %5d %5d %5d\n", a, a_post, b, pre_b);
    
    return 0;
}

输出结果:

  • 程序清单5.12,bottles.c:

#include <stdio.h>
#define MAX 100

int main(void)
{
    int count = MAX + 1;
    
    while (--count > 0)
    {
        printf("%d bottles of spring water on the wall, "
                "%d bottles of spring water!\n", count, count);
        printf("Take one down and pass it around,\n");
        printf("%d bottles of spring water!\n\n", count - 1);
    }
    
    return 0;    
}

输出结果:

  • 程序清单5.13,addemup.c:

/* addemup.c -- 几种常见的语句 */ 
#include <stdio.h>

int main(void)                 /* 计算前 20 个整数的和 */ 
{
    int count, sum;            /* 声明                 */ 
    
    count = 0;                 /* 表达式语句           */ 
    sum = 0;                   /* 表达式语句           */ 
    while (count++ < 20)       /* 迭代语句             */ 
        sum = sum + count;
    printf("sum = %d\n", sum); /* 表达式语句           */ 
    
    return 0;                  /* 跳转语句             */ 
}

输出结果:

  • 程序清单5.14,convert.c:

/* convert.c -- 自动类型转换 */ 
#include <stdio.h>

int main(void)
{
    char ch;
    int i;
    float f1;
    
    f1 = i = ch = 'C';
    printf("ch = %c, i = %d, f1 = %2.2f\n", ch, i, f1);
    ch = ch + 1;
    i = f1 + 2 * ch;
    f1 = 2.0 * ch + i;
    printf("ch = %c, i = %d, f1 = %2.2f\n", ch, i, f1);
    ch = 1107;
    printf("Now ch = %c\n", ch);
    ch = 80.89;
    printf("Now ch = %c\n", ch);
    
    return 0;
}

输出结果:

  • 程序清单5.15,pound.c:

/* pound.c -- 定义一个带一个参数的函数 */ 
#include <stdio.h>

void pound(int n); // ANSI 函数原型声明 

int main(void)
{
    int times = 5;
    char ch = '!'; // ASCII 码是 53 
    float f = 6.0f;
    
    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");    
}

输出结果:

  • 程序清单5.16,running.c:

// running.c -- A useful 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); // %lf 表示读取一个 double 类型的值 
    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 / distm;  // 时间/距离 = 跑 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.\n Your average speed was %1.2f mph.\n", msec, rate);
    
    return 0;        
}

输出结果:

  • 编程练习

题目1,方法:计算并输出数值。示例代码5_1.c:

#include <stdio.h>
#define TIME_CHANGE 60

int main(void)
{
    int minutes, mhour, mminutes;
    
    printf("Please input the minutes you want to" 
            "change or input a number less than 0 to quit:___\b\b\b");
    scanf("%d", &minutes);
    while (minutes > 0)
    {
        mhour = minutes    / TIME_CHANGE;
        mminutes = minutes % TIME_CHANGE;
        printf("%d minutes convert to %d hours and %d mimutes.\n", minutes, mhour, mminutes);
        printf("Please input the minutes you want to" 
                "change or input a number less than 0 to quit:___\b\b\b");
        scanf("%d", &minutes);
    }
    
    return 0;
}

输出结果:

题目2,方法:打印指定的数字。示例代码5_2.c:

#include <stdio.h>
#define LEN 10

int main(void)
{
    int num;
    int i = 0;
    
    printf("Please input a integer: ");
    scanf("%d", &num);
    printf("%d ", num);
    while (++i < LEN + 1)
        printf("%d ", num + i);
    
    return 0 ;
}

输出结果:

题目3,方法:计算并输出数值。示例代码5_3.c:

#include <stdio.h>
#define WEEK_DAY 7

int main(void)
{
    int days, dweeks, ddays;
    
    printf("Please input the days you want to convert or input a number less than 0 to quit: ");
    scanf("%d", &days);
    while ( days > 0)
    {
        dweeks = days / WEEK_DAY;
        ddays = days % WEEK_DAY;
        printf("%d days equal to %d weeks and %d days.\n", days, dweeks, ddays);
        printf("Please input the days you want to convert or input a number less than 0 to quit: ");
        scanf("%d", &days);
    }
    printf("Done!\n");
    
    return 0;
}

输出结果:

题目4,方法:转换并输出指定数值。示例代码5_4.c:

#include <stdio.h>
#define CM_PER_INCH 2.54f
#define CM_PER_FEET 30.48f

int main(void)
{
    float height, inch;
    int feet;
    
    printf("Please input your height with centimetres or input a number "
            "less than or equals to 0 to quit: ");
    while ((scanf("%f", &height) == 1) && height > 0)
    {
        feet = height / CM_PER_FEET;
        inch = (height - feet * CM_PER_FEET) / CM_PER_INCH;
        printf("%.1f cm = %d feet, %.1f inches.\n", height, feet, inch);
        printf("Please input your height with centimetres or input a number "
                "less than or equals to 0 to quit: ");
    }
    printf("Done!\n");
    
    return 0;
}

输出结果:

题目5,输出数值并完成指定计算。示例代码5_5.c:

#include <stdio.h>

int main(void)
{
    int count, sum, num;
    
    count = 0;
    sum = 0;
    printf("Please input a integer: ");
    scanf("%d", &num);
    while (count++ < num)
        sum = sum + count;
    printf("sum = %d\n", sum);
    
    return 0;
}

输出结果:

题目6,方法:计算并输出。示例代码5_6.c:

#include <stdio.h>

int main(void)
{
    int count, sum, num;
    
    count = 0;
    sum = 0;
    printf("Please input a integer: ");
    scanf("%d", &num);
    while (count++ < num)
        sum = sum + count * count;
    printf("sum = %d\n", sum);
    
    return 0;
}

输出结果:

题目7,方法:输入并计算立方。示例代码:5_7.c:

#include <stdio.h>

void cube(double num);

int main(void)
{
    double d_num;
    
    printf("Please input a double number: ");
    scanf("%lf", &d_num);
    cube(d_num);
    
    return 0;
}

void cube(double num)
{
    double d_cube;
    d_cube = num * num * num;
    printf("The cube of %lf is %lf\n", num, d_cube);
}

输出结果:

题目8,方法:求模运算。示例代码5_8.c:

#include <stdio.h>

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

输出结果:

题目9,方法:温度转换。示例代码5_9.c:

#include <stdio.h>

void Temperatures(double temp);

int main(void)
{
    double d_f;
    
    printf("Please input a fahrenheit degree(q to quit): ");
    while (scanf("%lf", &d_f) == 1)
    {
        Temperatures(d_f);
        printf("Please input a fahrenheit degree again(q to quit): ");
    }
    printf("Done!");
    
    return 0;
}

void Temperatures(double temp)
{
    const double f_value = 32.0;
    const double k_value = 273.16;
    double c_t, k_t;
    
    c_t = 5.0 / 9.0 * (temp - f_value);
    k_t = c_t + k_value;
    printf("Fahrenheit degree is %.2lf.\n", temp);
    printf("Centigrade degree is %.2lf.\n", c_t);
    printf("Kelvin degree is %.2lf.\n", k_t); 
}

输出结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值