Day 001. C语言程序设计案例教程book1练习

Practice 1:

#include <stdio.h>
//从键盘输入一个字母,如果是大写字母,则输出对应的小写字母;
// 如果是小写字母,则输出对应的大写字母。 如果不是字母,则输出非法输入
int main() {
    char word;
    char nword;
    printf("请输入一个字母: ");
    scanf("%c", &word);

    if (word >= 65 && word <= 90){
        word = word + 32; //大写转小写
        printf("把您的输入转换为%c", word);
    }else if (word >= 91 && word <= 122){
        word = word -32;//小写转大写
        printf("把您的输入转换为%c", word);
    }else{
        printf("非法输入!");
    }
    return 0;
}
//请输入一个字母: A
//把您的输入转换为a

//请输入一个字母: f
//把您的输入转换为F

//请输入一个字母: /
//非法输入!

观察ASCII字符集, 大写字母A的编号是65, 大写字母Z的编号是90.

小写字母a的编号是97, 小写字母z的编号是122;

因此大写字母与小写字母的编号相差32。

ascii-codes-table

Practice 2:

#include <stdio.h>
//continue的用法
int main() {
    int i;
    for (int i = 0; i < 10; i++) {
        if (i==8) continue;
        printf("%d\t", i);
    }
    return 0;
}
//0    1  2  3  4  5  6  7  9
//越过了 8 因为continue的功能

Practice 3:

#include <stdio.h>
//十进制数通过不同的输出转换为八进制,十六进制
int main() {
    int i;
    printf("请输入一个十进制整数: ");
    scanf("%d", &i);

    printf("十进制数: %d\n", i);
    printf("八进制数: %o\n", i); //小写字母o,不是数字0.
    printf("十六进制数: %x\n", i);
    return 0;
}
//请输入一个十进制整数: 12345678
//十进制数: 12345678
//八进制数: 57060516
//十六进制数: bc614e

Practice 4:

#include <stdio.h>
//猜大小
int main() {
    int object = 38;
    int guess;
    int counter = 1;

    printf("猜数游戏: 目标整数在1~100之间\n");

    while (1) {
        printf("请猜测一个整数: ");
        scanf("%d", &guess);
        printf("你猜测的整数是%d\n", guess);

        if (guess < 1 || guess > 100){
            printf("超出区间[1,100]!");
        }else if (guess > object) {
            printf("偏大了!\n");
        } else if (guess < object) {
            printf("偏小了!\n");
        } else {
            printf("猜对啦!一共猜测%d次!\b", counter);
            break;
        }
        counter++;
    }
    return 0;
}
//猜数游戏: 目标整数在1~100之间
//请猜测一个整数: 43
//你猜测的整数是43
//偏大了!
//请猜测一个整数: 40
//你猜测的整数是40
//偏大了!
//请猜测一个整数: 35
//你猜测的整数是35
//偏小了!
//请猜测一个整数: 38
//你猜测的整数是38
//猜对啦!一共猜测4次

Practice 5:

/*
 * 5. Fibonacci斐波那契数列, 又称黄金分割数列:
 * 该数列的第一项是0, 第二项是1, 从第三项起的每一项都是前两项之和。
 *
 * 分析:
 * 从键盘输入一个整数n,然后输出斐波那契数列中数值大小不超过n的项到屏幕中。
 * 数字用\t分开
 * 数字小于1,输出非法数据
 */
#include <stdio.h>

int main() {
    long long int n;
    int n1=0, n2=1, nextInt;
    printf("输入一个整数: \n");
    scanf("%lld",&n);

    printf("斐波那契数列:%d\t%d\t", n1, n2);//先输出前两个固定不变的
    nextInt = n1 + n2;

    while (nextInt <= n){
        printf("%d\t", nextInt);
        n1 = n2; // 第二个数字赋值给第一个数字
        n2 = nextInt; // 把两数之和赋值给第二个数字
        nextInt = n1 + n2; // nextInt是两数之和
    }
    return 0;
}
//输入一个整数: 
//233
//斐波那契数列:0 1  1  2  3  5  8  13 21 34 55 89 144    233    
posted @ 2021-03-07 15:41  NTE701  阅读(221)  评论(0)    收藏  举报
1