计算机程序设计C++ MOOC(第4周编程作业)

本博客内容为中国大学生MOOC国家精品课程《计算机程序设计C++》作业记录,仅供参考,观者忌照搬照抄,欢迎交流批评指正!

(注:由于本人学习时,前八周的作业提交时间已过,因此这八周的作业代码只在自己的编译器上测试运行通过,在课程网站上还未测试,于下学期开课时,再另行测试,如您发现有明显错误,可留言评论)

##第4周编程作业

本周作业内容C++复制信息处理

  1. 恺撒加密
    在这里插入图片描述
#include<iostream>

using namespace std;

void encryption(char &a)
{
	if (a == 'X') { a = 'a'; return; }
	if (a == 'Y') { a = 'b'; return; }
	if (a == 'Z') { a = 'c'; return; }
	if (a == 'x') { a = 'A'; return; }
	if (a == 'y') { a = 'B'; return; }
	if (a == 'z') { a = 'C'; return; }
	a = a + 3;
	if (a >= 'A'&&a <= 'Z') { a = a - ('A' - 'a'); return; }
	else a = a + ('A' - 'a');
	return;
}

int main()
{
	char a[20];
	cin >> a;
	for (int i = 0; i < 20; i++)
	{
		if (a[i] == '\0') break;
		encryption(a[i]);
		cout << a[i];
	}
	return 0;
}
  1. 矩阵转置
    在这里插入图片描述
#include<iostream>

using namespace std;

void exchange(int &a, int &b)
{
	int temp = 0;
	temp = a;
	a = b;
	b = temp;
	return;
}

int main()
{
	int n, i, j;
	int a[5][5];
	cin >> n;
	if (n < 1 || n>5){ cout << "matrix order error"; return 0; }
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			cin >> a[i][j];
		}
	}

	for (i = 0; i < n; i++)
	{
		for (j = 0; j < i; j++)
		{
			exchange(a[i][j], a[j][i]);
		}
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n - 1; j++)
		{
			cout << a[i][j] << ' ';
		}
		cout << a[i][j] << endl;
	}
	return 0;
}
  1. 按点击率显示歌曲
    在这里插入图片描述
#include<iostream>

using namespace std;

struct song
{
	char name[50];
	char singer[20];
	int count;
};

void exchange(song &a, song&b)
{
	song temp;
	temp=a;
	a = b;
	b = temp;
}

int main()
{
	song a[5];
	for (int i = 0; i < 5; i++)
	{
		cin >> a[i].name >> a[i].singer >> a[i].count;
	}
	for (int i = 0; i < 5; i++)
	{
		for (int j = i+1; j < 5; j++)
		{
			if (a[i].count < a[j].count) exchange(a[i], a[j]);
		}
	}
	for (int i = 0; i < 5; i++)
	{
		cout << a[i].name <<' '<< a[i].singer <<' '<< a[i].count << endl;
	}
	return 0;
}
  1. 星期转换
    在这里插入图片描述
#include<iostream>

using namespace std;

int main()
{
	char *a[8] = { "","monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
	int n;
	cin >> n;
	if (n < 1 || n>7){ cout << "invalid"; return 0; }
	cout << a[n];
	return 0;
}
  1. 插入加密
    在这里插入图片描述
#include<iostream>
#include<list>

using namespace std;

int main()
{
	list<char>l;
	list<char>::iterator it;
	char a[30], b[5] = {'a','b','c','d','e'};
	cin >> a;//字符
	int interval;
	cin >> interval;//间隔

	int i = 0,j = 0;
	while (a[i]!='\0')
	{
		l.push_back(a[i]);
		i++;
		if (i%interval == 0)
		{
			l.push_back(b[j]); 
			j++; 
			if (j == 5)j = 0;
		}
	}
	if (i % interval!=0)l.push_back(b[j]);
	it = l.begin();
	while (!l.empty())
	{
		cout << *it;
		l.pop_front();
		it = l.begin();
	}
	return 0;
}

以上为第4周编程作业。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值