《数据结构》实验四: 字符串和多维数组 实验
一..实验目的
巩固字符串和多维数组相关知识,学会运用灵活应用。
1.回顾字符串和多维数组的逻辑结构和存储操作特点,字符和数组的物理存储结构和常见操作。
2.学习运用字符串和和数组的知识来解决实际问题。
3.进一步巩固程序调试方法。
4.进一步巩固模板程序设计。
二.实验时间
准备时间为第9周到第10前半周,具体集中实验时间为10周周四。2个学时。
三..实验内容
1.从键盘输入一些文本,统计文本单词的个数。
2.写程序输出一个5阶幻方:每一行每一列每一个对角线和均相等。3. 自己设计一个字符加密算法,并设计程序验证:输入文本,解后加密并输出。在些基础上设计一个你算法的解密程序。
1.
//char_string.h
#ifndef CHAR_STRING_H_
#define CHAR_STRING_H_
#include <iostream>
#include <string>
using namespace std;
class Char_string{
public:
Char_string(){ //初始化
c_str = "";
c_str_num = 0;
// c_str_len = 0;
}
~Char_string(){}
void Input(); //输入文本
void Show(); //打印文本
void statistics(); //统计文本单词的个数
private:
string c_str; //文本
int c_str_num; //文本中单词的个数
//int c_str_len; //文本的长度
};
#endif
//char_string.cpp
#include <iostream>
#include "char_string.h"
using namespace std;
void Char_string::Input(){
cout << "\n请输入一个英语文本:";
getline(cin, c_str);
}
void Char_string::Show(){
cout << "\n你输入的英语文本为:";
cout << c_str << endl;
cout << "\n你输入的英语文本的单词个数为: " << c_str_num << endl;
}
void Char_string::statistics(){
int i = 0;
c_str_num = 1;
while(c_str[i] != '\0'){
if (c_str[i] == ' ' || c_str[i] == ','){
if (c_str[i + 1] != ' ' && c_str[i + 1] != ','
&& c_str[i + 1] != '.' && c_str[i + 1] != '\0'){
c_str_num++;
}
}
i++;
}
}
#include "char_string.h"
int main(){
Char_string cstr;
cstr.Input();
cstr.statistics();
cstr.Show();
return 0;
}
实验结果
2.
//magic_square.h
#ifndef MAGIC_SQUARE_H_
#define MAGIC_SQUARE_H_
#define m 5 //修改m,n来设置输出幻方的阶数
#define n 5
class Magic_square{
public:
Magic_square(); //初始化幻方
~Magic_square(){}
void Judge(); //判断设置的奇阶、双偶、单偶阶幻方并调用相关的函数
void OOD(); //奇阶幻方
void double_EVEN(); //双偶阶幻方
void single_EVEN(); //单偶阶幻方
void Show(); //输出幻方
private:
int Ms[m][n]; //幻方
};
#endif
//magic_square.cpp
#include <iostream>
#include <iomanip>
#include "magic_square.h"
using namespace std;
Magic_square::Magic_square(){
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
Ms[i][j] = 0;
}
void Magic_square::Judge(){
if (m % 2 == 0){
if (m % 4 == 0)
this->double_EVEN();
else
this->single_EVEN();
}
else{
this->OOD();
}
this->Show();
}
void Magic_square::OOD(){
int a = 0;
int b = n / 2;
Ms[a][b] = 1;
for (int i = 0; i < m * n - 1; i++){
if ((a - 1) < 0 || (b + 1) > n - 1){
if ((a - 1) < 0 && (b + 1) > n - 1){
a += 1;
Ms[a][b] = i + 2;
}
else if ((a - 1) < 0){
a = m - 1;
b += 1;
Ms[a][b] = i + 2;
}
else if ((b + 1) > n - 1){
a -= 1;
b = 0;
Ms[a][b] = i + 2;
}
}
else{
if (Ms[a - 1][b + 1] == 0){
a -= 1;
b += 1;
Ms[a][b] = i + 2;
}
else{
a += 1;
Ms[a][b] = i + 2;
}
}
}
}
void Magic_square::double_EVEN(){
int num = 1;
int temp = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++){
Ms[i][j] = num;
num++;
}
for (int i = 0; i < m / 2; i++){
for (int j = 0; j < m / 2; j++){
if ((i + j) % 2 == 0){
temp = Ms[i][j];
Ms[i][j] = Ms[m - 1 - i][n - 1 - j];
Ms[m - 1 - i][n - 1 - j] = temp;
}
}
for (int j = n - 1; j > m / 2 - 1; j--){
if ((i + j) % 2 != 0){
temp = Ms[i][j];
Ms[i][j] = Ms[m - 1 - i][n - 1 - j];
Ms[m - 1 - i][n - 1 - j] = temp;
}
}
}
}
void Magic_square::single_EVEN(){
//A
int i = 1;
int a = 0;
int b = n / 2 / 2;
int count = m / 4;
int temp = 0;
Ms[a][b] = 1;
for (int j = 0; j < ((m / 2) * (n / 2)) - 1; j++){
++i;
if ((a - 1) < 0 || (b + 1) > n / 2 - 1){
if ((a - 1) < 0 && (b + 1) > n / 2 - 1){
a += 1;
Ms[a][b] = i;
}
else if ((a - 1) < 0){
a = m / 2 - 1;
b += 1;
Ms[a][b] = i;
}
else if ((b + 1) > n / 2 - 1){
a -= 1;
b = 0;
Ms[a][b] = i;
}
}
else{
if (Ms[a - 1][b + 1] == 0){
a -= 1;
b += 1;
Ms[a][b] = i;
}
else{
a += 1;
Ms[a][b] = i;
}
}
}
//C
a = m / 2;
b = m / 2 + m / 2 / 2;
Ms[a][b] = ++i;
for (int j = 0; j < (m / 2) * (n / 2) - 1; j++){
i++;
if ((a - 1) < m / 2 || (b + 1) > n - 1){
if ((a - 1) < m / 2 && (b + 1) > n - 1){
a += 1;
Ms[a][b] = i;
}
else if ((a - 1) < m / 2){
a = m - 1;
b += 1;
Ms[a][b] = i;
}
else if ((b + 1) > n - 1){
a -= 1;
b = n / 2;
Ms[a][b] = i;
}
}
else{
if (Ms[a - 1][b + 1] == 0){
a -= 1;
b += 1;
Ms[a][b] = i;
}
else{
a += 1;
Ms[a][b] = i;
}
}
}
//D
a = 0;
b = n / 2 + n / 2 / 2;;
Ms[a][b] = ++i;
for (int j = 0; j < (m / 2) * (n / 2) - 1; j++){
i++;
if ((a - 1) < 0 || (b + 1) > n - 1){
if ((a - 1) < 0 && (b + 1) > n - 1){
a += 1;
Ms[a][b] = i;
}
else if ((a - 1) < 0){
a = m / 2 - 1;
b += 1;
Ms[a][b] = i;
}
else if ((b + 1) > n - 1){
a -= 1;
b = n / 2;
Ms[a][b] = i;
}
}
else{
if (Ms[a - 1][b + 1] == 0){
a -= 1;
b += 1;
Ms[a][b] = i;
}
else{
a += 1;
Ms[a][b] = i;
}
}
}
//B
a = m / 2;
b = n / 2 / 2;
Ms[a][b] = ++i;
for (int j = 0; j < (m / 2) * (n / 2) - 1; j++){
i++;
if ((a - 1) < m / 2 || (b + 1) > n / 2 - 1){
if ((a - 1) < m / 2 && (b + 1) > n / 2 - 1){
a += 1;
Ms[a][b] = i;
}
else if ((a - 1) < m / 2){
a = m - 1;
b += 1;
Ms[a][b] = i;
}
else if ((b + 1) > n / 2 - 1){
a -= 1;
b = 0;
Ms[a][b] = i;
}
}
else{
if (Ms[a - 1][b + 1] == 0){
a -= 1;
b += 1;
Ms[a][b] = i;
}
else{
a += 1;
Ms[a][b] = i;
}
}
}
for (int j = 0; j < count; j++){
static int c = 0;
for (int k = 0; k < m / 2; k++){
a = k;
temp = Ms[a][c];
Ms[a][c] = Ms[m / 2 + a][c];
Ms[m / 2 + a][c] = temp;
}
c++;
}
a = (m / 2 - 1) - m / 2 / 2;
b = n / 2 / 2;
temp = Ms[a][b];
Ms[a][b] = Ms[m / 2 + a][b];
Ms[m / 2 + a][b] = temp;
a = (m / 2 - 1) - m / 2 / 2;
b = 0;
temp = Ms[a][b];
Ms[a][b] = Ms[m / 2 + a][b];
Ms[m / 2 + a][b] = temp;
for (int j = 0; j < count - 1; j++){
static int c = n - 1;
for (int k = 0; k < m / 2; k++){
a = k;
const int c = n - 1;
for (int k = 0; k < n / 2; k++){
temp = Ms[a][c];
Ms[a][c] = Ms[m / 2 + a][c];
Ms[m / 2 + a][c] = temp;
}
}
c--;
}
}
void Magic_square::Show(){
cout << "\n" << m << "阶幻方为:\n" << endl;
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++)
cout << setiosflags(ios::left) << Ms[i][j] << '\t';
cout << "\n\n\n";
}
}
//magic_square_main.cpp
#include <iostream>
#include "magic_square.h"
using namespace std;
int main(){
Magic_square ms;
ms.Judge();
return 0;
}
实验结果
单阶幻方
双偶幻方
单偶幻方
原则上更多的阶是可以输出的,但是太麻烦了,懒得测试和校对,不出意外的话三阶以上的都应该可以输出。
至于第三题我觉得还需要时间。好了实验四就暂时告一段落了,如果测试发现有错的,告知我我试着再改。