c语言课下作业(第七次「数组」)

#include <stdio.h>
#include <string.h>
#define MAXSIZE 10

void homeworkOne();   //编程实现任意输入10个整数,将这10个字的数字降序排列输出(选择,冒泡,插入)
void homeworkTwo();   //凯撒密码 加密,解密 ,介绍凯撒密码https://blog.csdn.net/qq_36134761/article/details/80385862
void homeworkThree(); //编程实现任意输入5个手机号码,最后输出手机号码时第四位到第七位用*表示

int *cho_sort(int a[]);
int *pao_sort(int a[]);
int *cha_sort(int a[]);

int main()
{
    int no, retry;
    do
    {
        printf("请选择你要执行的任务[输入1-3 按回车结束]:");
        scanf("%d", &no);
        switch (no)
        {
        case 1:
            homeworkOne();
            break;
        case 2:
            homeworkTwo();
            break;
        case 3:
            homeworkThree();
            break;
        default:
            printf("请输入1-5选择任务");
            break;
        }
        //选择是否再次运行
        printf("是否继续选择任务执行[yes --> 0], [no --> 9]");
        scanf("%d", &retry);
    } while (retry == 0);
}

void homeworkOne()
{ //编程实现任意输入10个整数,将这10个字的数字降序排列输出(选择,冒泡,插入)
    int i, j;
    int retry;
    int a[MAXSIZE];

    do
    {
        puts("请输入10个整数。");

        for (i = 0; i < MAXSIZE; i++)
        {
            printf("请输入第%d个整数:", i + 1);
            scanf("%d", &a[i]);
        }
        do
        {
            puts("请选择排序方式【1.选择】【2.冒泡】【3.插入】");
            scanf("%d", &j);
            if (j < 1 || j > 3)
                printf("\a请在1-3中选择");
        } while (j < 1 || j > 3);

        putchar('{');
        for (i = 0; i < MAXSIZE; i++)
            printf("%d", j == 1 ? cha_sort(a)[i] : j == 2 ? pao_sort(a)[i]
                                                          : cha_sort(a)[i]);
        putchar('}');

        printf("是否再次选择排序方式【0-不尝试】【1-尝试】:");
        scanf(" %d ", &retry);
    } while (retry == 1);
}

int *cho_sort(int a[])
{ //选择排序(记录位置换位置)
    int i, j, location, t;

    for (i = 0; i < MAXSIZE; i++)
    {
        location = i;
        for (j = i + 1; j <= MAXSIZE; j++) /*查找最小值的位置*/
            if (a[location] > a[j])
                location = j;
        if (location != i)
        { //如果最小值不是i,就和j换位置
            t = a[i];
            a[i] = a[location];
            a[location] = t;
        }
    }

    return a;
}

int *pao_sort(int a[])
{ //冒泡排序 (几趟,相互交换位置)
    int i, j, t;

    for (i = 1; i < MAXSIZE; i++)
    { //有几趟(至少得有一趟)
        for (j = 0; j < MAXSIZE - i; j++)
            if (a[j] > a[j + 1])
            {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
    }

    return a;
}

int *cha_sort(int a[])
{ //插入排序
    int i, j, t, p;

    for (i = 1; i < MAXSIZE; i++)
    {
        t = a[i]; //保存插入的数字,插入的数字至少从第二位开始(i=1);
        for (j = 0; j < MAXSIZE; j++)
            if (a[j] > t)
                break; //找到第一个比a[i]大的数,退出
        if (j < i)     //如果第一个比a[i]大的数他的位置j在i前面,则换位置
        {
            for (p = i; p > j; p--)
            { //从第i个开始,逐步往前移动,原来的第i个位置腾出来
                a[p] = a[p - 1];
            }
            a[p] = t; //a[p]为原来的位置i,腾出来放插入的数字
        }
    }

    return a;
}

void homeworkTwo() //凯撒密码加密解密
{
#define NUM 100
    char strI[NUM];
    int i, m, numB, num; //numB加密偏移量,num解密偏移量

    puts("请输入要加密的字符串。");
    scanf("%s", &*strI);
    numB = 3;
    for (i = 0; i < strlen(strI); i++)
    { //加密过程
        if (strI[i] >= 'A' && strI[i] <= 'Z')
            strI[i] = ((strI[i] - 'A') + numB) % 26 + 'A';
        else if (strI[i] >= 'a' && strI[i] <= 'z')
            strI[i] = ((strI[i] - 'a') + numB) % 26 + 'a';
    }

    puts("加密完成。");
    printf("%s", strI);

    puts("是否解密【1——是】【0——否】");
    scanf("%d", &m);

    do
    {
        num = 26 - numB;
        for (i = 0; i < strlen(strI); i++)
        { //解密过程
            if (strI[i] >= 'A' && strI[i] <= 'Z')
                strI[i] = ((strI[i] - 'A') + num) % 26 + 'A';
            else if (strI[i] >= 'a' && strI[i] <= 'z')
                strI[i] = ((strI[i] - 'a') + num) % 26 + 'a';
        }
        if(m != 1)
            puts("请重新输入正确的选项。");
    }while (m != 1);

    m = 0;

    puts("解密完成。");
    printf("%s", strI);
}

void homeworkThree()
{
    #define PHONE_NUM 2

	char ph[PHONE_NUM][12];
	int i, j = 0;

	for (i = 0; i < PHONE_NUM; i++)
	{
		printf("请输入第%d个手机号码:", i + 1);
		gets(ph[i]);
		if (strlen(ph[i]) == 11 && ph[i][1] == 1 && ph[i][2] == 3
        || ph[i][2] == 4 || ph[i][2] == 5 || ph[i][2] == 7 || ph[i][2] == 8)
		{
            for (j = 3; j < 7; j++)
			    ph[i][j] = '*';	
			
		}else{
            puts("手机号码有误,重新输入。");
			i--;
        }
	}

	puts("号码部分隐藏后的效果是。");

	for(i = 0; i < PHONE_NUM; i++)
		puts(ph[i]);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值