CSP考试 2015年09月第3题 C语言实现





#include <stdio.h>
#include <string.h>
main()
{
	int m;
	int n;
	
	//m=11;
	//n=2;
	scanf("%d",&m);
	scanf("%d",&n);
	//printf("%d\n",m);
	//printf("%d\n",n);
	char a[m][100];
	/* 
	char* a[m];
	a[0]="<!DOCTYPE html>";
	a[1]="<html>";
	a[2]="<head>";
	//{{一定是成对出现 {{}}中允许有空格 
	a[3]="<title>User {{ name }}</title>";
	a[4]="</head>";
	a[5]="<body>";
	a[6]="<h1>{{ name }}</h1>";
	//<p>Email: <a href="mailto:{{ email }}">{{ email }}</a></p>
	a[7]="<p>Email: <a href=\"mailto:{{ email }}\">{{ email }}</a></p>";
	a[8]="<p>Address: {{ address }}</p>";
	a[9]="</body>";
	a[10]="</html>";*/

	int i;
	int j;
	for(i=0;i<m;i++)
	{
		for(j=0;j<100;j++)
		{
			a[i][j]='\0';
		}
	}
	char tian;
	scanf("%c",&tian);
	for(i=0;i<m;i++)
	{
		for(j=0;j<100;j++)
		{
			scanf("%c",&tian);
			if(tian!='\n')
			{
				a[i][j]=tian;
			}
			else if(tian=='\n')
			{
				break;
			}
		}
	}
	//printf("0   %s\n",a[0]);
	for(i=0;i<m;i++)
	{
		//printf("%s\n",a[i]);
	}	
	
	/* 
	char* b[n];
	b[0]="{{ name }}";
	b[1]="{{ email }}";
	char* c[n];
	c[0]="David Beckham";
	c[1]="david@beckham.com";*/
	char b[n][100];
	char c[n][100];
	char d[n][100]; 
	
	for(i=0;i<n;i++)
	{
		
		for(j=0;j<100;j++)
		{
			b[i][j]='\0';
			c[i][j]='\0';
			d[i][j]='\0';
		}
		b[i][0]='{';
		b[i][1]='{';
		b[i][2]=' ';
	}
	char t='\0';
	for(i=0;i<n;i++)
	{
		for(j=0;j<100;j++)
		{
			scanf("%c",&t);
			if(t!='\n')
			{
				d[i][j]=t;
			}else if(t=='\n')
			{
				break;
			}
		} 
	}
	char* bu1="{{ ";
	char* bu2=" }}";
	for(i=0;i<n;i++)
	{
		int temp=-1;
		for(j=0;j<100;j++)
		{
			if(d[i][j]==' ')
			{
				temp=j;
				strncat(b[i],d[i],temp);
				strcat(b[i],bu2);
				strncpy(c[i],d[i]+temp+2,strlen(d[i])-temp-3);
				break;
			}
		}
	}
	
	for(i=0;i<m;i++) 
	{
		int j;
		char hello[100];
		for(j=0;j<100;j++)
		{
			hello[j]='\0';
		}
		strcpy(hello,a[i]);
		//printf("%d\n",i);
		//printf("%s\n",hello);
		
		while(1)
		{
			if(strchr(hello,'{')==NULL)
			{

				break;
			}

			int st1=strchr(hello,'{')-hello;
			//printf("%d\n",st1);
			int st2=strchr(hello,'}')-hello;
			//printf("%d\n",st2);
			char cut1[100];
			char cut2[100];
			char cut3[100];
			int j;
			for(j=0;j<100;j++)
			{
				cut1[j]='\0';
				cut2[j]='\0';
				cut3[j]='\0';
			}
			strncpy(cut1,hello,st1);
			strncpy(cut2,hello+st1,st2-st1+2);
			strcpy(cut3,hello+st2+2);

			//printf("%s\n",cut1);
			//printf("%s\n",cut2);
			//printf("%s\n",cut3);
			char t[100];
			
			for(j=0;j<100;j++)
			{
				t[j]='\0';
			}
			strncpy(t,hello+st1,st2-st1+2);
			int temp=-1;
			for(j=0;j<n;j++)
			{
				if(!strcmp(t,b[j]))
				{
				temp=j;
				}
			}
			if(temp!=-1)
			{
				strcat(cut1,c[temp]);
			}
			strcat(cut1,cut3);
			//printf("%s\n",cut1);

			strcpy(hello,cut1);
		}
		printf("%s\n",hello);
		
	}
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值