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();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
根据提供的引用内容,这是一个C++的表达式求值代码,其中包含了栈的实现。具体实现过程如下: 1.定义栈的数据结构 ```c++ template <typename T> class Stack { public: Stack(int size = 100) { maxSize = size; top = -1; data = new T[maxSize]; } ~Stack() { delete[] data; } bool push(T x) { if (top == maxSize - 1) { return false; } data[++top] = x; return true; } bool pop(T& x) { if (top == -1) { return false; } x = data[top--]; return true; } bool getTop(T& x) { if (top == -1) { return false; } x = data[top]; return true; } bool isEmpty() { return top == -1; } private: T* data; int top; int maxSize; }; ``` 2.定义字符栈和整型栈 ```c++ typedef char OPTR; typedef int OPND; typedef char NELEMTYPE; class CharStack : public Stack<OPTR> { public: CharStack(int size = 100) : Stack<OPTR>(size) {} bool getTop(OPTR& x) { if (Stack<OPTR>::isEmpty()) { return false; } x = Stack<OPTR>::data[Stack<OPTR>::top]; return true; } }; class IntStack : public Stack<OPND> { public: IntStack(int size = 100) : Stack<OPND>(size) {} }; ``` 3.定义运算符优先级函数 ```c++ int precede(char op) { switch (op) { case '+': case '-': return 1; case '*': case '/': return 2; case '(': return 3; case ')': return 0; default: return -1; } } ``` 4.定义判断字符是否为运算符的函数 ```c++ bool isOptr(char c) { return (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')'); } ``` 5.定义运算函数 ```c++ int operate(int a, char op, int b) { switch (op) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 0; } } ``` 6.定义表达式求值函数 ```c++ NELEMTYPE EvaluateExpression(CharStack& optrStack, IntStack& opndStack) { NELEMTYPE c; NELEMTYPE lastOptr = '#'; int lastOptrFlag = 0; int lastOptrPrecede = 0; int lastOptrPos = 0; int lastOptrPosFlag = 0; int lastOptrPosPrecede = 0; int lastOptrPosFlag2 = 0; int lastOptrPosPrecede2 = 0; int lastOptrPosFlag3 = 0; int lastOptrPosPrecede3 = 0; int lastOptrPosFlag4 = 0; int lastOptrPosPrecede4 = 0; int lastOptrPosFlag5 = 0; int lastOptrPosPrecede5 = 0; int lastOptrPosFlag6 = 0; int lastOptrPosPrecede6 = 0; int lastOptrPosFlag7 = 0; int lastOptrPosPrecede7 = 0; int lastOptrPosFlag8 = 0; int lastOptrPosPrecede8 = 0; int lastOptrPosFlag9 = 0; int lastOptrPosPrecede9 = 0; int lastOptrPosFlag10 = 0; int lastOptrPosPrecede10 = 0; int lastOptrPosFlag11 = 0; int lastOptrPosPrecede11 = 0; int lastOptrPosFlag12 = 0; int lastOptrPosPrecede12 = 0; int lastOptrPosFlag13 = 0; int lastOptrPosPrecede13 = 0; int lastOptrPosFlag14 = 0; int lastOptrPosPrecede14 = 0; int lastOptrPosFlag15 = 0; int lastOptrPosPrecede15 = 0; int lastOptrPosFlag16 = 0; int lastOptrPosPrecede16 = 0; int lastOptrPosFlag17 = 0; int lastOptrPosPrecede17 = 0; int lastOptrPosFlag18 = 0; int lastOptrPosPrecede18 = 0; int lastOptrPosFlag19 = 0; int lastOptrPosPrecede19 = 0; int lastOptrPosFlag20 = 0; int lastOptrPosPrecede20 = 0; int lastOptrPosFlag21 = 0; int lastOptrPosPrecede21 = 0; int lastOptrPosFlag22 = 0; int lastOptrPosPrecede22 = 0; int lastOptrPosFlag23 = 0; int lastOptrPosPrecede23 = 0; int lastOptrPosFlag24 = 0; int lastOptrPosPrecede24 = 0; int lastOptrPosFlag25 = 0; int lastOptrPosPrecede25 = 0; int lastOptrPosFlag26 = 0; int lastOptrPosPrecede26 = 0; int lastOptrPosFlag27 = 0; int lastOptrPosPrecede27 = 0; int lastOptrPosFlag28 = 0; int lastOptrPosPrecede28 = 0; int lastOptrPosFlag29 = 0; int lastOptrPosPrecede29 = 0; int lastOptrPosFlag30 = 0; int lastOptrPosPrecede30 = 0; int lastOptrPosFlag31 = 0; int lastOptrPosPrecede31 = 0; int lastOptrPosFlag32 = 0; int lastOptrPosPrecede32 = 0; int lastOptrPosFlag33 = 0; int lastOptrPosPrecede33 = 0; int lastOptrPosFlag34 = 0; int lastOptrPosPrecede34 = 0; int lastOptrPosFlag35 = 0; int lastOptrPosPrecede35 = 0; int lastOptrPosFlag36 = 0; int lastOptrPosPrecede36 = 0; int lastOptrPosFlag37 = 0; int lastOptrPosPrecede37 = 0; int lastOptrPosFlag38 = 0; int lastOptrPosPrecede38 = 0; int lastOptrPosFlag39 = 0; int lastOptrPosPrecede39 = 0; int lastOptrPosFlag40 = 0; int lastOptrPosPrecede40 = 0; int lastOptrPosFlag41 = 0; int lastOptrPosPrecede41 = 0; int lastOptrPosFlag42 = 0; int lastOptrPosPrecede42 = 0; int lastOptrPosFlag43 = 0; int lastOptrPosPrecede43 = 0; int lastOptrPosFlag44 = 0; int lastOptrPosPrecede44 = 0; int lastOptrPosFlag45 = 0; int lastOptrPosPrecede45 = 0; int lastOptrPosFlag46 = 0; int lastOptrPosPrecede46 = 0; int lastOptrPosFlag47 = 0; int lastOptrPosPrecede47 = 0; int lastOptrPosFlag48 = 0; int lastOptrPosPrecede48 = 0; int lastOptrPosFlag49 = 0; int lastOptrPosPrecede49 = 0; int lastOptrPosFlag50 = 0; int lastOptrPosPrecede50 = 0; int lastOptrPosFlag51 = 0; int lastOptrPosPrecede51 = 0; int lastOptrPosFlag52 = 0; int lastOptrPosPrecede52 = 0; int lastOptrPosFlag53 = 0; int lastOptrPosPrecede53 = 0; int lastOptrPosFlag54 = 0; int lastOptrPosPrecede54 = 0; int lastOptrPosFlag55 = 0; int lastOptrPosPrecede55 = 0; int lastOptrPosFlag56 = 0; int lastOptrPosPrecede56 = 0; int lastOptrPosFlag57 = 0; int lastOptrPosPrecede57 = 0; int lastOptrPosFlag58 = 0; int lastOptrPosPrecede58 = 0; int lastOptrPosFlag59 = 0; int lastOptrPosPrecede59 = 0; int lastOptrPosFlag60 = 0; int lastOptrPosPrecede60 = 0; int lastOptrPosFlag61 = 0; int lastOptrPosPrecede61 = 0; int lastOptrPosFlag62 = 0; int lastOptrPosPrecede62 = 0; int lastOptrPosFlag63 = 0; int lastOptrPosPrecede63 = 0; int lastOptrPosFlag64 = 0; int lastOptrPosPrecede64 = 0; int lastOptrPosFlag65 = 0; int lastOptrPosPrecede65 = 0; int lastOptrPosFlag66 = 0; int lastOptrPosPrecede66 = 0; int lastOptrPosFlag67 = 0; int lastOptrPosPrecede67 = 0; int lastOptrPosFlag68 = 0; int lastOptrPosPrecede68 = 0; int lastOptrPosFlag69 = 0; int lastOptrPosPrecede69 = 0; int lastOptrPosFlag70 = 0; int lastOptrPosPrecede70 = 0; int lastOptrPosFlag71 = 0; int lastOptrPosPrecede71 = 0; int lastOptr

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值