文档章节

程序设计基础(C)第06讲例程

树人大学数字媒体吴凡
 树人大学数字媒体吴凡
发布于 10/21 23:28
字数 1013
阅读 35
收藏 0

1summing.c

/* summing.c -- 根据用户键入的整数求和 */
#include <stdio.h>
int main(void)
{
    long num;
    long sum = 0L;      /* 把sum 初始化为0   */
    int status;
    
    printf("Please enter an integer to be summed ");
    printf("(q to quit): ");
    status = scanf("%ld", &num);
    while (status == 1) /* == means "is equal to"   */
    {
        sum = sum + num;
        printf("Please enter next integer (q to quit): ");
        status = scanf("%ld", &num);
    }
    printf("Those integers sum to %ld.\n", sum);

    return 0;
}

2 when.c

// when.c -- 何时退出循环
#include <stdio.h>
int main(void)
{
    int n = 5;
    
    while (n < 7)                    // line 7
    {
        printf("n = %d\n", n);
        n++;                         // line 10
        printf("Now n = %d\n", n);   // line 11
    }
    printf("The loop has finished.\n");
    
    return 0;
}

3 while1.c

/* while1.c -- 注意花括号的使用    */
/* 糟糕的代码创建了一个死循环 */
#include <stdio.h>
int main(void)
{
    int n = 0;
    
    while (n < 3)
        printf("n is %d\n", n);
    n++;
    printf("That's all this program does\n");
    
    return 0;
}

4 while2.c

/* while2.c -- 注意分号的位置 */
#include <stdio.h>
int main(void)
{
    int n = 0;
    
    while (n++ < 3);             /* line 7 */
    printf("n is %d\n", n);  /* line 8 */
    printf("That's all this program does.\n");
    
    return 0;
}

5 cmpflt.c

// cmpflt.c -- 浮点数比较
#include <math.h>
#include <stdio.h>
int main(void)
{
    const double ANSWER = 3.14159;
    double response;
    
    printf("What is the value of pi?\n");
    scanf("%lf", &response);
    while (fabs(response - ANSWER) > 0.0001)
    {
        printf("Try again!\n");
        scanf("%lf", &response);
    }
    printf("Close enough!\n");
    
    return 0;
}

6 t_and_f.c

/* t_and_f.c -- C中的真和假的值 */
#include <stdio.h>
int main(void)
{
    int true_val, false_val;
    
    true_val = (10 > 2);    // value of a true relationship
    false_val = (10 == 2);  // value of a false relationship 
    printf("true = %d; false = %d \n", true_val, false_val);
    
    return 0;
}

7 truth.c

// truth.c -- 哪些值为真
#include <stdio.h>
int main(void)
{
    int n = 3;
    
    while (n)
        printf("%2d is true\n", n--);
    printf("%2d is false\n", n);
    
    n = -3;
    while (n)
        printf("%2d is true\n", n++);
    printf("%2d is false\n", n);
    
    return 0;
}

8 trouble.c

// trouble.c -- 这里误用了=导致了死循环
#include <stdio.h>
int main(void)
{
    long num;
    long sum = 0L;
    int status;
    
    printf("Please enter an integer to be summed ");
    printf("(q to quit): ");
    status = scanf("%ld", &num);
    while (status = 1)
    {
        sum = sum + num;
        printf("Please enter next integer (q to quit): ");
        status = scanf("%ld", &num);
    }
    printf("Those integers sum to %ld.\n", sum);
    
    return 0;
}

9 boolean.c

// boolean.c -- 使用 _Bool类型的变量
#include <stdio.h>
int main(void)
{
    long num;
    long sum = 0L;
    _Bool input_is_good;
    
    printf("Please enter an integer to be summed ");
    printf("(q to quit): ");
    input_is_good = (scanf("%ld", &num) == 1);
    while (input_is_good)
    {
        sum = sum + num;
        printf("Please enter next integer (q to quit): ");
        input_is_good = (scanf("%ld", &num) == 1);
    }
    printf("Those integers sum to %ld.\n", sum);
    
    return 0;
}

10 sweetie1.c

// sweetie1.c --  一个计数循环
#include <stdio.h>
int main(void)
{
    const int NUMBER = 22;
    int count = 1;                     // i初始化
    
    while (count <= NUMBER)            // 测试
    {
        printf("Be my Valentine!\n");  // 行为
        count++;                       // 更新计数
    }
    
    return 0;
}

11 sweetie2.c

// sweetie2.c -- 使用for循环的计数循环
#include <stdio.h>
int main(void)
{
    const int NUMBER = 22;
    int count;
    
    for (count = 1; count <= NUMBER; count++)
        printf("Be my Valentine!\n");
    
    return 0;
}

12 for_cube.c

/* for_cube.c -- 使用for循环创建一个立方表 */
#include <stdio.h>
int main(void)
{
    int num;
    
    printf("    n   n cubed\n");
    for (num = 1; num <= 6; num++)
        printf("%5d %5d\n", num, num*num*num);
    
    return 0;
}

13 postage.c

// postage.c --邮资
#include <stdio.h>
int main(void)
{
    const int FIRST_OZ = 46; // 2013 邮资
    const int NEXT_OZ = 20;  // 2013 邮资
    int ounces, cost;
    
    printf(" ounces  cost\n");
    for (ounces=1, cost=FIRST_OZ; ounces <= 16; ounces++,cost += NEXT_OZ)
        printf("%5d   $%4.2f\n", ounces, cost/100.0);
    
    return 0;
}

14 zeno.c

/* zeno.c -- 求序列的和*/
#include <stdio.h>

int main(void)
{
    int t_ct;       // 项计数
    double time, power_of_2;
    int limit;
    
    printf("Enter the number of terms you want: ");
    scanf("%d", &limit);
    for (time=0, power_of_2=1, t_ct=1; t_ct <= limit;t_ct++, power_of_2 *= 2.0)
    {
        time += 1.0/power_of_2;
        printf("time = %f when terms = %d.\n", time, t_ct);
    }
    
    return 0;
}

15 do_while.c

/* do_while.c -- 出口条件循环 */
#include <stdio.h>
int main(void)
{
    const int secret_code = 13;
    int code_entered;
    
    do
    {
        printf("To enter the triskaidekaphobia therapy club,\n");
        printf("please enter the secret code number: ");
        scanf("%d", &code_entered);
    } while (code_entered != secret_code);
    printf("Congratulations! You are cured!\n");
    
    return 0;
}

16 entry.c

/* entry.c -- 出口条件循环 */
#include <stdio.h>
int main(void)
{
    const int secret_code = 13;
    int code_entered;
    
    printf("To enter the triskaidekaphobia therapy club,\n");
    printf("please enter the secret code number: ");
    scanf("%d", &code_entered);
    while (code_entered != secret_code)
    {
        printf("To enter the triskaidekaphobia therapy club,\n");
        printf("please enter the secret code number: ");
        scanf("%d", &code_entered);
    }
    printf("Congratulations! You are cured!\n");
    
    return 0;
}

17 rows1.c

/* rows1.c -- u使用嵌套循环 */
#include <stdio.h>
#define ROWS  6
#define CHARS 10
int main(void)
{
    int row;
    char ch;
    
    for (row = 0; row < ROWS; row++)          /* line 10 */
    {
        for (ch = 'A'; ch < ('A' + CHARS); ch++)  /* line 12 */
            printf("%c", ch);
        printf("\n");
    }
    
    return 0;
}

18 rows2.c

// rows2.c -- 依赖外部循环的嵌套循环
#include <stdio.h>
int main(void)
{
    const int ROWS = 6;
    const int CHARS = 6;
    int row;
    char ch;
    
    for (row = 0; row < ROWS; row++)
    {
        for (ch = ('A' + row);  ch < ('A' + CHARS); ch++)
            printf("%c", ch);
        printf("\n");
    }
    
    return 0;
}

© 著作权归作者所有

树人大学数字媒体吴凡

树人大学数字媒体吴凡

粉丝 25
博文 11
码字总数 7610
作品 0
杭州
私信 提问
清华大学视频课件:基于Linux的C++(自主模式)

基于Linux的C++(自主模式) 课程简介 Linux操作系统开源的特性使得其获得越来越重要的地位,而Linux系统编程也向C++程序设计者提出了更高的要求。本课程由C/C++语言的共性与特性出发,在深入...

dragonscroll
2017/11/20
0
0
数据结构课程主页-2016级

  新学期,再度起程!   翻转的数据结构课程再度迎来新的一批同学。   前两年,资源建设基本完备,课堂方案逐渐完善,同学们对新型的学习方式设计给予了肯定(参见2014级问卷调查和201...

sxhelijian
2017/08/30
0
0
【计算机类】大学生计算机专业电子书汇总

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 https://blog.csdn.net/dengjin20104042056/article/details/95334387 00. 目录 文章目录 本...

沧海一笑-dj
07/10
0
0
精品C语言学习资料-教你成为编程高手

亲爱的Down友: 您好!51CTO下载中心为梦想增值,诚邀您的关注! 51CTO下载中心致力于让down友分享技术幵让技术变现,让广大技术爱好者便捷、实惠 的获取优质学习资源。 为了方便大家的学习,...

51CTO下载
2013/12/13
0
0
C语言书籍资料汇总

我汇总出自己收藏的C语言方面的书籍资料,方便后期使用,或许你也用的到。 以下内容,有链接的都可以下载。 一、书籍 元老级别的书籍: C程序设计语言.pdf (c语言之父) C Primer plus 第5...

BjarneCpp
2017/11/06
0
0

没有更多内容

加载失败,请刷新页面

加载更多

JS前端MD5加密

Bootstrap官网获得md5 js地址:https://www.bootcdn.cn/blueimp-md5/ <!--MD5加密--><script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.js"></script> 使用方法: md5(pwd)......

被毒打的程序猿_先瑞
23分钟前
4
0
BigDecimal 去后面无用的0的方法

BigDecimal a=new BigDecimal("0.1000"); System.out.println(a.stripTrailingZeros().toPlainString());...

xiaodong16
今天
5
0
JAVA--高级基础开发

[集合版双色球] 十二、双色球规则:双色球每注投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1—33中选择;蓝色球号码从1—16中选择;请随机生成一注双色球号码。(要求同色号码...

李文杰-yaya
昨天
20
0
聊聊rocketmq broker的CONSUMER_SEND_MSG_BACK

序 本文主要研究一下rocketmq broker的CONSUMER_SEND_MSG_BACK CONSUMER_SEND_MSG_BACK rocketmq/common/src/main/java/org/apache/rocketmq/common/protocol/RequestCode.java public class......

go4it
昨天
4
0
API常见接口(下)

system类 StringBuilder和StringBuffer 包装类 1.System类 (java.lang包中) 提供了大量的静态方法,可以获取与系统相关的信息或系统级操作。 常用方法: public static long currentTimeMi...

Firefly-
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部