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

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

##第4周编程作业

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

  1. 恺撒加密
    计算机程序设计C++ MOOC(第4周编程作业)
#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. 矩阵转置
    计算机程序设计C++ MOOC(第4周编程作业)
#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 1; }
	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. 按点击率显示歌曲
    计算机程序设计C++ MOOC(第4周编程作业)
#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. 星期转换
    计算机程序设计C++ MOOC(第4周编程作业)
#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 1; }
	cout << a[n];
	return 0;
}
  1. 插入加密
    计算机程序设计C++ MOOC(第4周编程作业)
#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周编程作业。

相关文章:

  • 2021-07-20
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2021-10-15
  • 2021-11-19
  • 2022-12-23
猜你喜欢
  • 2021-06-17
  • 2021-12-24
  • 2021-07-10
  • 2021-11-18
  • 2021-12-27
  • 2021-08-02
  • 2021-04-29
相关资源
相似解决方案