c++ 67,68,69

67

#define ERR 5
#define OK 6
#include <stdio.h>
  
int status;
double result,sig,scale;
int sign(int c)/*处理数的符号函数*/
{
	if(c=='-')/*若为负号,置负数标记*/
		sig=-sig;
}

int integer(int c)/*转换整数部分,转换一位整数位*/
{
	result=result*10.0+c-'0';
}

int decimal(int c)/*转换小数部分,转换一位小数位*/
{
	result+=(c-'0')*scale;
	scale/=10.0;
}
/*状态表*/
int statbl[ ][4]={{   1,2,3,ERR},/*0*/
	        {ERR,2,3,ERR},/*1*/
	        {OK,2,4,OK},/*2*/
	        {ERR,4,ERR,ERR},/*3*/
	        {OK,4,OK,OK}};/*4*/
/*转换函数表*/
int(*funtbl[ ][4])( )={{sign,integer,NULL,NULL},
	                {NULL,integer,NULL,NULL},
	                {NULL,integer,NULL,NULL},		
	                {NULL,decimal,NULL,NULL},
                                    {NULL,decimal,NULL,NULL}};

int readreal(double *dp)
{
	int c,ckind;
	sig=1.0;
	result=0.0;
	scale=0.1;

	while((c=getchar( ))==' '||c=='\n'||c=='\t');/*跳过前导空白符*/
	status=0;/*置初始状态*/
	for(;;)
	{
		/*分类当前字符*/
		if(c=='+'||c=='-') ckind=0;/*数的符号字符*/
		else if(c>='0'&&c<='9') ckind=1;/*数字符*/
		else if(c=='.') ckind=2;/*小数点*/
		else ckind=3;/* 其它字符 */

		if(funtbl[status][ckind])/* 如有转换函数 */
			(*funtbl[status][ckind])(c);/* 执行相应的函数 */
		status=statbl[status][ckind];/*设置新的状态*/
		if(status==ERR||status==OK)break;/* 结束:出错或成功 */
		c=getchar();
	}
	ungetc(c,stdin); /* 归还数德结束符 */
	if(status==OK)
	{
		*dp=result *sig;/* 读入数按指针参数赋给相应变量 */
		return 1;
	}
	return -1; /* 出错返回 */
}
main()
{
	double x;
	clrscr();
	printf("\nPlease input real numbers (use nonreal char to end input):\n");
	while(readreal(&x)==1)
		printf("The real number you input is: %f\n",x);
	printf("\nYou have inputted nonreal char.\n Press any key to quit...\n");	
	getch();
}

68

#define N 80
edit(char *s)
{
	int i,sp,w,inw,v,r;
	char buf[N],*str;
	for(inw=sp=w=i=0;s[i];i++)
	{
		if(s[i]==' ')
		{		/* 统计空白个数*/
			sp++;
			inw=0;	/* 置空白符状态*/
		}
		else if(!inw)
		{
			w++;	/* 统计单字个数*/
			inw=1;	/* 置单字状态*/
		}
	}
	if(w<=1)
		return;	/* 单字数不超过1, 不排版 */
	v=sp/(w-1);	/* 每个间隔平均空白符 */
	r=sp%(w-1);	/* 多余的空白符 */
	strcpy(buf,s);
	for(str=buf;;)
	{
		while(*str==' ')str++; /* 掠过空白符 */
		for(;*str&&*str!=' ';) /* 复制单字 */
			*s++=*str++;
		if(--w==0)
			return;		/* 全部单字复制完毕,返回 */
		for(i=0;i<v;i++)
			*s++=' ';	/* 插入间隔空白符 */
		if(r)
		{
			*s++=' ';	/* 插入一个多余空白符 */
			r--;
		}
	}
}
char buff[N];
main()		/* 用于测试edit函数 */
{
	clrscr();
	puts("This is a typeset program!\nPlease input a character line:\n");
	gets(buff);
	edit(buff);
	printf("\nThe character line after typeset is:\n\n%s\n",buff);
	puts("\n Press any key to quit...\n ");
	getch();
}

69

#define N 20
char w[N];
perm(int n, char *s)
{
	char s1[N];
	int i;
	if(n<1)
		printf("%s\n",w); /* 一个排列生成输出 */
	else
	{
		strcpy(s1,s);	/* 保存本层次可使用的字符 */
		for(i=0;*(s1+i);i++)	/* 依次选本层次可用字符 */
		{
			*(w+n-1)=*(s1+i);/* 将选用字符填入正在生成的字符排列中 */
			*(s1+i)=*s1;
			*s1=*(w+n-1);
			perm(n-1,s1+1);	 /* 递归 */
		}
	}
}
main()
{
	int n=2;
	char s[N];
	w[n]='\0';
	clrscr();
	printf("This is a char permutation program!\nPlease input a string:\n");
	scanf("%s",s);
	puts("\nPlease input the char number of permuted:\n");
	scanf("%d",&n);
	puts("The permuted chars are:\n");
	perm(n,s);
	puts("\nPress any key to quit...");
	getch();
}


猜你喜欢

转载自blog.csdn.net/sherryhaha123/article/details/72964885