C语言练习11---螺旋打印

 

 程序是思考的产物,它是一个精神产品,在想清楚后进行编写,事半功倍!!!

#include <stdio.h>
#include <stdlib.h>

#define ROW 4  //行
#define COS 5  //列

void Show(int arr[][COS]); //打印
                       
int FmatState1(int arr[][COS], int count); //状态
int FmatState2(int arr[][COS], int count);
int FmatState3(int arr[][COS], int count);
int FmatState4(int arr[][COS], int count);
int FmatState5(int arr[][COS], int count);
int FmatState6(int arr[][COS], int count);

void Call(int arr[][COS], int count); //调用状态

enum States
{
	STATES1 = 1,
	STATES2,
	STATES3,
	STATES4,
	STATES5,
	STATES6
};


int main()
{
	int row = 0;
	int cos = 0;
	int count = 0;
	int arr[ROW][COS] = { 0 };
	Call(arr, count);
	system("pause");
	return 0;
}

void Show(int arr[][COS])
{
	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COS; j++)
		{
			printf("%2d   ", arr[i][j]);
		}
		printf("\n");
		printf("\n");
	}
}

int FmatState1(int arr[][COS], int count)
{
	printf("状态1\n");
	printf("\n");
	for (int j = 0; j < COS - 1; j++)
	{
		arr[0][j] = ++count;
	}
	return count;
}

int FmatState2(int arr[][COS], int count)
{
	printf("状态2\n");
	printf("\n");
	for (int i = 0; i < ROW; i++)
	{
		arr[i][COS - 1] = ++count;
	}
	return count;
}

int FmatState3(int arr[][COS], int count)
{
	printf("状态3\n");
	printf("\n");
	for (int j = COS-2; j>0; j--)
	{
		arr[ROW - 1][j] = ++count;
	}
	return count;
}

int FmatState4(int arr[][COS], int count)
{
	printf("状态4\n");
	printf("\n");
	for (int i = ROW - 1; i > 0; i--)
	{
		arr[i][0] = ++count;
	}
	return count;
}

int FmatState5(int arr[][COS], int count)
{
	printf("状态5\n");
	printf("\n");
	for (int j = 1; j < COS - 1; j++)
	{
		arr[1][j] = ++count;
	}
	return count;
}

int FmatState6(int arr[][COS], int count)
{
	printf("状态6\n");
	printf("\n");
	for (int j = COS - 2; j>0; j--)
	{
		arr[2][j] = ++count;
	}
	return count;
}

void Call(int arr[][COS],int count)
{
	int input = 0;
	printf("依次从1输入至6!,输入0退出!\n");
	do
	{
		scanf("%d", &input);
		printf("\n");
		switch (input)
		{
		case STATES1:
			count = FmatState1(arr, count);
			Show(arr);
			break;
		case STATES2:
			count = FmatState2(arr, count);
			Show(arr);
			break;
		case STATES3:
			count = FmatState3(arr, count);
			Show(arr);
			break;
		case STATES4:
			count = FmatState4(arr, count);
			Show(arr);
			break;
		case STATES5:
			count = FmatState5(arr, count);
			Show(arr);
			break;
		case STATES6:
			count = FmatState6(arr, count);
			Show(arr);
			break;
		default:
			if (input == 0)
			{
				printf("成功退出!!!\n");
				break;
			}
			printf("输入有误,请输入1——6内的数字或输入0退出!!!\n");
			break;
		}
	} while (input != 0);
}

看到这里,很多人都觉得太复杂了,其实这只是一个半成品,并没有突出精髓,只是机械的搬运而已~~~

面对评论区各位的意见,接下来我将写一个完成版的,为了帮助理解,先来个简单的,热热身~~~

查询一串字符中有多少个单词

思来想去,一个单词无非就两个状态,要么在单词里,要么在单词外,所以用状态法,来解决最好不过~~~

且看代码: 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

enum States
{
	BEGIN=1,
	INWORD ,
	OUTWORD 
};


char* Inword(char *cp)  //单词里
{
	while (isalpha(*cp)||*cp=='\''||*cp=='-')
	{
		cp++;
	}
	return cp;
}

char* Outword(char *cp)   //单词外
{
	while (!isalpha(*cp) && *cp!='\0')
	{
		cp++;
	}
	return cp;
}

int Word_Count(char *cp)
{
	int num = 0;
	int input =BEGIN;
	do
	{
		switch (input)
		{
		case BEGIN:
			if (isalpha(*cp))
			{
				input = INWORD;
				break;
			}
			else
			{
				input = OUTWORD;
				break;
			}

		case INWORD:

			cp= Inword(cp);
			input = OUTWORD;
			num++;
			break;

		case OUTWORD:

			cp = Outword(cp);
			input = INWORD;
			break;

		default:
			break;
		}

	} while(*cp!='\0');

	return num;
}

int main()
{ 
	char str[] = { " You are my good frined~~~~ I Love You My---baby~~~It's good thing "};
	int count = Word_Count(str);
	printf("%d\n", count);
	system("pause");
	return 0;
}

 

 现在你还觉得状态法复杂吗?

再来搞搞我们的螺旋打印 

这将是真正的螺旋打印

首先我们先画图分析分析

 

 

首先 来分析一下偶数X偶数的 螺旋打印

我们不难发现 此螺旋打印只有四种状态,分别是 打印顶行,打印右列, 打印底行 , 打印左列

然后循环就好

不难发现在进行下一圈打印时,以顶行为例,会打印在此行的下一行,并且左减1,右减1,每走一圈都会左右减1;

其他几个状态是同样的道理,不在赘述~~~

 如果为奇数X奇数,会在中间空一个,那我们特殊照顾一下就好了。

看代码:

#include <stdio.h>
#include <stdlib.h>

#define COS  6 
#define ROW  6

int count = 0;

enum
{
	BEGIN,
	TOP ,
	RIGHT,
	BOTTOM,
	LEFT
};

int top_i = 0;
int top_limup = ROW - 1;
int top_limlow = 0;

void Top(int (*arr)[ROW])  //顶行
{
	
	for (int j = top_limlow; j < top_limup; j++)
	{
		arr[top_i][j] = ++count;
	}
	
	top_i++;  //行数
	top_limup--; //列的上限
	top_limlow++;//列的下限

	if (top_limlow > top_limup)
	{
		return;
	}
	
}

int right_j = ROW - 1;
int right_limup = COS - 1;
int right_limlow = 0;

void Right(int(*arr)[ROW])  //右列
{
	for (int i = right_limlow; i < right_limup; i++)
	{
		arr[i][right_j] = ++count;
	}

	right_j--;  //列数
	right_limup--; //行的上限
	right_limlow++; //行的下限
	
	if (right_limlow > right_limup)
	{
		return;
	}
}

int Bottom_i = COS - 1;
int Bottom_limup = COS - 1;
int Bottom_limlow = 0;

void Bottom(int(*arr)[ROW])  //底行
{
	for (int j = Bottom_limup; j > Bottom_limlow; j--)
	{
		arr[Bottom_limup][j] = ++count;
	}
	
	Bottom_i--;  //行数
	Bottom_limup--; //列的上限
	Bottom_limlow++; //列的下限
	if (Bottom_limlow > Bottom_limup)
	{
		return;
	}

}

int left_j = 0;
int left_limup = COS - 1;
int left_limlow = 0;

void Left(int(*arr)[ROW])  //左列
{
	for (int i = left_limup; i > left_limlow; i--)
	{
		arr[i][left_j] = ++count;
	}

	left_j++;  //列数
	left_limup--; //行的上限
	left_limlow++; //行的下限
	if (left_limlow > left_limup)
	{
		return;
	}
}

void Print(int arr[][ROW])
{
	if (ROW % 2 != 0)
	{
		int mid = ROW / 2 + 1;
		arr[mid - 1][mid - 1] = ROW*COS;
	}
	for (int i = 0; i < COS; i++)
	{
		for (int j = 0; j < ROW; j++)
		{
			printf("%-4d ", arr[i][j]);
		}
		printf("\n");
	}
	printf("\n");
	
}

int main()
{
	int arr[COS][ROW] = { 0 };
	int input = BEGIN;
	int judge = 0;
   
	do
	{
		switch (input)
		{
		case BEGIN:
			if (ROW % 2 != 0)
			{
				judge = COS*ROW - 1;
			}
			else
			{
				judge = COS*ROW;
			}
			input = TOP;
			break;
		case TOP:
			Top(arr);
			input = RIGHT;
			break;
		case RIGHT:
			Right(arr);
			input = BOTTOM;
			break;
		case BOTTOM:
			Bottom(arr);
			input = LEFT;
			break;
		case LEFT:
			Left(arr);
			input = TOP;
			break;

		default:
			break;
		}

	} while (count < judge);
	Print(arr);
	
	system("pause");
	return 0;
}

偶数方阵和奇数方阵的螺旋打印统统解决

4X4 

 5X5

 

6X6

。 

抽出一个稍作分析,其余都是一样的,照猫画虎即可~~~

 我对顶行分析一下:

按照上图分析的规律,打印顶行时我们让行不变,确定列的起始位置和结束位置,来确保准确打印,第一圈时,行==0,列的起始位置==0,结束位置==2,所以顶行将会打印在0,1,2列,第一圈打印完,我们将 行+1 保存 ,列的起始位置 +1保存,列的结束位置 -1保存,以便于下一圈打印时,能准确获取打印的位置信息,一直往复即可。当列的起始位置<列的结束位置时,说明此状态以结束。其他状态,亦是同样的道理。

说点题外话, 我把这个思路想清楚后,kuangchi  kuangchi(拟声词,陕西方言)的将顶行的函数写完,也没去测试,就将其复制后,改写其他三个,整体完成后,自信地按下F5,弹出红色的窗口,此时心态还算好,但看了看代码,却不知从何开始修改,一直做改改,右改改,还是出问题,心态逐渐发生变化,最后只好一个函数一个函数的检查,才编写完成~~~真是心累,下次写完一小段,一定要去测试,不要堆起来,真的很难改~~~ 所以我很讨厌给别人写的代码找错误,特别还是那种不按标准格式写的,我是真想给编写者一刀,自己上完厕所不擦屁股,让我来? 自己写的东西都找不到问题所在,别人也很难找~~~

好习惯:

规范编写,写完一个函数就去测试~~~

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值