Lesson23 PoEdu培训第二课 C语言篇(15) 函数3 数组与指针 随堂作业
文章类别: 培训作业 0 评论

Lesson23 PoEdu培训第二课 C语言篇(15) 函数3 数组与指针 随堂作业

文章类别: 培训作业 0 评论

老师未批改

1. 让用户分4次输入任意字符串, 将4个字符串集中到一个字符串

程序代码:

/*************************************************************************
    > 文件名: lesson23.hw.01.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-28 19:36:13
 ************************************************************************/

#include <stdio.h>

#define MAX_LEN 255

int main() {
    
    printf("请任意输入4个字符串:\n");
    char str1[MAX_LEN] = { '\0' };
    char str2[MAX_LEN] = { '\0' };
    char str3[MAX_LEN] = { '\0' };
    char str4[MAX_LEN] = { '\0' };
    char strAll[MAX_LEN * 4] = { '\0' };

    scanf_s("%s%s%s%s", str1, MAX_LEN, str2, MAX_LEN, str3, MAX_LEN, str4, MAX_LEN);

    sprintf_s(strAll, MAX_LEN * 4, "%s %s %s %s", str1, str2, str3, str4);

    printf("您输入的字符串已经拼接为一个, 结果为:\n[%s]\n", strAll);

    return 0;
}

运行效果:

Alt 运行效果

2. 让程序不停的读取, 一直遇到#号结束. 然后打印出读取到数据当中的空格数,字母数, 数字数

程序代码:

/*************************************************************************
    > 文件名: lesson23.hw.02.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-28 20:00:45
 ************************************************************************/

#include <stdio.h>

#define MAX_LEN 255

int main() {
    
    char str[MAX_LEN] = { '\0' };
    
    printf("请输入字符, 输入 # 结束输入!\n");
    for (int i = 0; i < MAX_LEN - 1; i++) {
        str[i] = getch();
        printf("%c", str[i]);
        if (str[i] == '#') {
            str[i] = '\0';
            printf("\n");
            break;
        }
    }

    int iCharNum = 0;
    int iSpaceNum = 0;
    int iNumNum = 0;

    for (int i = 0; i < MAX_LEN; i++) {
        if (str[i] == ' ') {
            // 空格
            iSpaceNum++;
        } else if (str[i] >= '0' && str[i] <= '9') {
            // 数字
            iNumNum++;
        } else if (str[i] >= 'a' && str[i] <= 'z'
                || str[i] >= 'A' && str[i] <= 'Z') {
            // 字母
            iCharNum++;
        }
    }

    printf("您输入的所有字符为:\n[%s]\n其中空格有[%d]个, 字母有[%d]个, 数字有[%d]个.\n",
            str, iSpaceNum, iCharNum, iNumNum);

    return 0;
}

运行效果:

Alt 运行效果

3. 让用户不停的输入数字, 直到输入0, 打印出用户输入的奇数个数即偶数个数, 并分别输出奇偶数的平均值

程序代码:

/*************************************************************************
    > 文件名: lesson23.hw.03.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-28 20:31:29
 ************************************************************************/

#include <stdio.h>

#define MAX_LEN 255

int main() {
    
    int iInputVal[MAX_LEN] = { 0 };
    
    for (int i = 0; i < MAX_LEN;) {
        printf("请输入数字, 输入 0 结束输入!\n");
        if (1 == scanf("%d", iInputVal + i)) {
            if (iInputVal[i] == 0) {
                break;
            }
            i++;
        } else {
            printf("非法输入!\n");
            char ch;
            while((ch = getchar()) != EOF && ch != '\n');
        }
    }

    int iOddNum = 0; // 奇数
    int iEnevNum = 0; // 偶数

    long long iOddSum = 0;
    long long iEnevSum = 0;

    for (int i = 0; i < MAX_LEN; i++) {
        if (iInputVal[i] == 0) {
            break;
        }
        if (iInputVal[i] % 2 == 0) {
            // 偶数
            iEnevNum++;
            iEnevSum += iInputVal[i];
        } else {
            // 偶数
            iOddNum++;
            iOddSum += iInputVal[i];
        }
    }

    printf("您输入的偶数有[%d]个, 奇数有[%d]个\n偶数平均值为[%.2lf], 奇数平均值为[%.2lf].\n",
            iEnevNum, iOddNum, 
            (double)iEnevSum / (double)iEnevNum, 
            (double)iOddSum / (double)iOddNum);

    return 0;
}

运行效果:

Alt 运行效果

4. 编写一个函数能够将字符的12345,转换成int型的12345

程序代码:

/*************************************************************************
    > 文件名: lesson23.hw.04.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-28 21:52:03
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>

int strToInt(char* str) {
    return atoi(str);
}

int main() {
    char* str = "123456";
    printf("字符串为[%s], 转换为数字为[%d]\n", str, strToInt(str));
    return 0;
}

运行效果:

Alt 运行效果

5. 编写一个函数将int型的12345转换成字符型的12345

程序代码:

/*************************************************************************
    > 文件名: lesson23.hw.05.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-28 22:12:09
 ************************************************************************/

#include <stdio.h>

#define MAX_LEN 255

char* intToStr(int num) {
    char* str = (char*)malloc(MAX_LEN);
    memset(str, 0x00, sizeof(str));
    sprintf_s(str, MAX_LEN, "%d", num);
    return str;
}

int main() {
    int iNum = 123456;
    printf("数字为[%d], 转换为字符串为[%s]\n", iNum, intToStr(iNum));
    return 0;
}

运行效果:

Alt 运行效果

6. 让程序不停的读取, 一直遇到#号结束. 将输入中的a替换成b, c替换成d其余的保持不变

程序代码:

/*************************************************************************
    > 文件名: lesson23.hw.06.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-28 22:36:32
 ************************************************************************/

#include <stdio.h>

#define MAX_LEN 255

int main() {
    
    char str[MAX_LEN] = { '\0' };
    
    printf("请输入字符, 输入 # 结束输入!\n");
    for (int i = 0; i < MAX_LEN - 1; i++) {
        str[i] = getch();
        printf("%c", str[i]);
        if (str[i] == '#') {
            str[i] = '\0';
            printf("\n");
            break;
        }
    }

    printf("您输入的所有字符为:\n[%s]\n", str);

    for (int i = 0; i < MAX_LEN; i++) {
        if (str[i] == '\0') {
            break;
        }
        if (str[i] == 'a') {
            str[i] = 'b';
        } else if (str[i] == 'c') {
            str[i] = 'd';
        }
    }

    printf("处理后的字符为:\n[%s]\n", str);

    return 0;
}

运行效果:

Alt 运行效果

7 .让用户输入 :

    These functions take the int equivalent of one character as parameter and return an int that can either be another character or a value representing a boolean value: an int value of 0 means false, and an int value different from 0 represents true.
    1).    统计这一段输入当中共有多少词
    2).    更换当中所有的空格为制表符
    3).    每一行只允许显示八个以内的单词超过八个换行到下一行
    4).    每个词首字母大写

程序代码:

/*************************************************************************
    > 文件名: lesson23.hw.07.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-28 22:49:05
 ************************************************************************/

#include <stdio.h>
#include <string.h>

#define MAX_LEN 255

int main() {
    
    char str[MAX_LEN] = "These functions take the int equivalent of one character as parameter and return an int that can either be another character or a value representing a boolean value: an int value of 0 means false, and an int value different from 0 represents true.";
    printf("已经为您输入好了语句:\n[%s]\n", str);

    int iWordNum = 0;

    for (int i = 0; i < MAX_LEN; i++) {
        if (str[i] == '\0') {
            break;
        }
        if (str[i] == ' ') {
            iWordNum++;
            // 将空格替换为制表符
            str[i] = '\t';
        }
    }
    printf("这段话中, 共有单词[%d]个\n", iWordNum + 1);

    printf("将空格替换为制表符后, 结果为:\n[%s]\n", str);

    printf("每行不多于8个输出单词!并且首字母转换为大写!\n");
    char* pStr = NULL;
    int i = 0;

    pStr = NULL;
    i = 0;

    pStr = strtok(str, "\t");
    while(pStr != NULL) {
        i++;
        char sTmp[MAX_LEN] = { '\0' };
        sprintf_s(sTmp, MAX_LEN, "%s", pStr);
        if (*sTmp >= 'a' && *sTmp <= 'z') {
            *sTmp -= 32;
        }
        printf("%s ", sTmp);
        if (i % 8 == 0) {
            printf("\n");
        }
        pStr = strtok(NULL, "\t");
    }

    printf("\n");

    return 0;
}

运行效果:

Alt 运行效果

8. 要求输出如下菜单

            主菜单
================================
         1. 输入功能 
          2. 按学号查找
         3. 打印输出
          0. 退出
================================
     请选择 (0~3):
如果选择1,则显示“请输入”;
选择2,则显示“请输入查找学生学号”;
选择3,显示“正在输出”;
选择0,显示“谢谢使用”;
选择其它则显示“输入错误” 

程序代码:

/*************************************************************************
    > 文件名: lesson23.hw.08.c
    > 作者: 花心胡萝卜
    > 邮箱: hxhlb@hxcarrot.com 
    > 创建时间: 2016-09-29 0:04:26
 ************************************************************************/

#include <stdio.h>

#define TRUE 1

void printEquals(int num) {
    for (int i = 0; i < num; i++) {
        printf("%s", "=");
    }
    printf("\n");
}

void printMenu() {
    printf("\t主菜单\n");
    printEquals(33);
    printf("\t1. 输入功能\n");
    printf("\t2. 按学号查找\n");
    printf("\t3. 打印输出\n");
    printf("\t0. 退出\n");
    printEquals(33);
}

int main() {
    
    printMenu();
    while (TRUE) {
        printf("\t请选择(0~3):");
        char ch = getch();
        printf("%c\n", ch);
        switch(ch) {
            case '0':
                printf("谢谢使用!\n");
                exit(0);
            case '1':
                printf("请输入\n");
                break;
            case '2':
                printf("请输入要查找的学号\n");
                break;
            case '3':
                printf("正在输出...\n");
                break;
            default:
                printf("输入错误!!!\n");
                break;
        }
    }

    return 0;
}

运行效果:

Alt 运行效果

如有错误,请提出指正!谢谢.

回复