实验四

实验(1)

统计文本单词个数:

源代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<fstream>
using namespace std;


int Match(char str[])
{
 int count = 0;
 int state = 1;
 int i = 0;
 while (str[i] != '\0')
 {
  if (str[i] == ' ')
  {
   state = 1;
  }
  else if (state == 1)
  {
   state = 0;
   count++;
  }
  i++;
 }
 return count;
}
int main()
{
 cout << "读取文本:" << endl;
 ifstream file("D:\\words\\words.txt");
 if (file.fail()){ cout << "文件不存在!" << endl; }
 char T[255];
 file.get(T, 255);
 cout << "输入的内容是:" << T << endl;
 int Length = 0;
 Length = Match(T);
 cout << "字符串中单词的个数为:" << Length << endl;

 system("PAUSE");
 return 0;
}

 结果如下:

 


实验(2):5阶幻方

#include<iostream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
const int N = 5;
void square(int a[N][N], int n)
{
 int i, j, k;
 i = 0; j = n / 2;
 a[i][j] = 1;
 for (k = 2; k <= N*N; k++)
 {
  int itemp, jtemp;
  itemp = i; jtemp = j;
  i = i - 1;
  if (i<0)i = n - 1;
  j = j - 1;
  if (j<0)j = n - 1;
  if (a[i][j]>0)
  {
   i = itemp + 1;
   if (i == n)i = 0;
   j = jtemp;
  }
  a[i][j] = k;
 }
}
void show(int b[N][N])
{
 int i, j;
 for (i = 0; i<5; i++)
 {
  for (j = 0; j<5; j++)
   cout << b[i][j] << " ";
  cout << endl;
 }
}
void init(int c[N][N])
{
 int i, j;
 for (i = 0; i<5; i++)
 for (j = 0; j<5; j++)
 {
  c[i][j] = 0;
 }
}
int main()
{
 int a[N][N];
 init(a);
 square(a, 5);
 show(a);
 system("PAUSE");
 return 0;
}

结果截图如下:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值