#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]);
}
c语言课下作业(第七次「数组」)
最新推荐文章于 2024-07-18 11:11:00 发布