C语言程序设计

从 大一下 开始写博客,记录自己的学习。

而作为一个强迫症(比如我)是不允许做事缺头少尾的。

想了想还是得大一所学的C语言也搬上来,虽然结构老师经常嘲讽

(也不算嘲讽,算是絮叨吧)我的C语言,但是我还是要厚着脸皮继续

学习的。永远保持一颗学徒的心。 加油!

上学期C语言的每次作业或者实验报告我就懒得再写了(太多了),就写一下期末

实训作业吧,有两个题。

1.做一个国际棋盘

例如:

现在看来真是简单,不知道上学期自己做的时候怎么还花了囊么多的时间.....

/*
*2018051604115 cjw 软工四班
*
*file:11_12.c
*------------------------
* This is DislayCheckerboard program
*/
#include <cstdio>
#include "cstring"


#define N 8

static char Getboard(int row, int column)//判断棋子的函数
{
    if ((row + column) % 2 == 0) return ' ';
    else if (row<3) return 'b';
    else if (row == 3 || row == 4) return '-';
    else return 'r';
}
static void InitCheckerboard(char board[N][N])//初始化棋盘的函数
{
    int i, j;
    for (i = 0; i<8; i++) {
        for (j = 0; j<8; j++) {
            board[i][j] = Getboard(i, j);
        }
    }
}

static void DislayCheckerboard(char board[N][N])//显示棋盘的函数
{

    for (int i = 0; i<8; i++) {
        for (int n = 0; n<8; n++) {
            printf("%c", board[i][n]);
        }
        printf("
");
    }
}
int main()
{
    char board[N][N];
    InitCheckerboard(board);
    DislayCheckerboard(board);
    return 0;
}

2. 猜单词hangman game

由一个玩家想出一个单词或短语,另一个玩家猜该单词或短语中的每一个字母,

第一个人抽走单词或短语,只留下相应数量的空白与下划线。

/*
*2018051604115 cjw 软工四班
*
*file:14_12.c
*------------------------
* This is hangman game program
*/
#include <stdio.h>
#include <ctype.h>
#include "strlib.h"
#include "simpio.h"
#include "random.h"
#include "string.h"

#define Chances 8
#define Letters 4
static void RandomWord(char word[])/*随机一个有Letters个字母的单词*/
{
    int i, x;
    for (i = 0; i<Letters; i++) {
        x = RandomInteger(65, 91);
        word[i] = x;
    }
}
static void HideWord(char result[])/*隐藏字母的函数*/
{
    for (int i = 0; i<Letters; i++) {
        result[i] = '-';
    }
}
static void replace(char ch, char result[], char word[])/*将‘-’替换成字母*/
{
    for (int i = 0; i<Letters; i++) {
        if (ch == word[i]) {//判断是否匹配并替换
            result[i] = ch;
        }
    }
}

static void GuessWord(char result[], char word[])/*用户猜单词*/
{
    int i = Chances;
    char *sptr;
    char ch;
    while (i>0) {
        printf("The word now looks like this:%s
", result);
        printf("you have %d chances left.
", i);
        printf("your guess:");
        ch = getchar();
        getchar();
        sptr = strchr(word, ch);
        replace(ch, result, word);/*调用函数替换‘-’*/
        if (sptr == NULL) {//猜错
            printf("There is no %c's in the word.
", ch);
            i--;
        }
        else {//猜对
            printf("That guess is correct.
");
        }
        if(strcmp(result, word) == 0) {//判断是否全对
            printf("The word is:%s
", word);
            printf("you win!!!
");
            break;
        }
else if (i == 0) {//机会用完
    printf("The word:%s
", word);
    printf("you lose.
");
}
    }
}
int main()
{
    char letter[Letters + 1];
    char result[Letters + 1];
    letter[Letters] = '';
    result[Letters] = '';
    char *word;
    word = &letter[0];
    printf("Let's play hangman!I will pick a secret word.
on each turn, you guess a letter.If 
");
    printf("the letter is in the secrect word,I will
show you where it appears. If you make an
");
    printf("incorrect guess,part of your body gets strung
up on the scaffold. The object is to
");
    printf("guess the word before you are hanged.
");
    RandomWord(letter);
    HideWord(result);
    GuessWord(result, word);
    return 0;
}

这里运行结果图我懒得再去找了(好像保存深度系统里面了),现在用visual studio重新运行的

话,又不知道怎么弄随机库,就这样吧。

原文地址:https://www.cnblogs.com/cjwen/p/10743900.html