Poj 3428 Formatting function

本文介绍了一个简单的文本格式化函数的实现,该函数能够处理字符串、整数及百分号的格式化,并能检测常见的格式错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2275 Accepted: 313

Description

Text formatting functions (Format, sprintf, etc) are very common between programming languages. Your task is to implement a simplified one.

Formatting function receives format string and several arguments. Argument values are processed one by one and inserted into format string at positions designated by format specifiers. The following format specifiers must be implemented:

* %s — argument as a string
* %d — argument as an integer value, without any leading zeroes
* %% — literal '%' character

Formatting error is generated if:

* number of arguments required by format specifiers is not equal to the number of actually supplied arguments,
* argument for %d specifier contains non-digit character.
* '%' character in format string is not followed by either '%', 's' or 'd'.

Input

Input file contains format string followed by arguments, one per line.

Constraints

Number of arguments is not greater than 1000, length of each line is not greater than 50000 characters.

Output

Output file must contain a single string — either formatted text or ERROR in case of any formatting error.

Sample Input

Sample Input 1
%d x %d %s %d
2
02
=
4
Sample Input 2
%s
Sample Input 3
%d
2a

Sample Output

Sample Output 1
2 x 2 = 4
Sample Output 2
ERROR
Sample Output 3
ERROR

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.



#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

char format[50050],  // format string
	 argument[1001][50050];

bool isDigit(char c)
{
	return '0'<=c && c<='9';
}


bool judgeInt( char args[])
{
	if(args[0]=='\0' || args[0]=='\n' ) 
		return false;
	for( int i=0;args[i] && args[i]!='\n' ;i++)
		if( isDigit(args[i])==false) return false;
	return true; 
}

int calPara( char format[] )
{
	int k = 0;
	for( int i=0; format[i] && format[i]!='\n' ;i++){
		if( format[i]=='%' ){
			i++;
			if( format[i]=='d' && judgeInt(argument[k]))  k++;
			else if( format[i]=='s' ) k++;
			else if( format[i]=='%' ) continue;
			else return -1;
		} 
	}
	return k; 
}

void printInt(char args[])
{
	int idx = 0;
	while( args[idx]=='0' )  idx++;
	if( args[idx]=='\n' || args[idx]=='\0' ){
		putchar('0');
		return ;
	}
	while( args[idx] &&  args[idx]!='\n' )    putchar(args[idx++]);
}

void printStr(char args[])
{
	for( int i=0;args[i] && args[i]!='\n' ;i++)
		putchar( args[i] );
}

void output(char format[],char args[][50050])
{
	int k = 0;
	for( int i=0; format[i] && format[i]!='\n' ;i++){
		if( format[i]=='%' ){
			i++;
			if( format[i]=='d' )    printInt(argument[k++]); 
			else if( format[i]=='s' ) printStr(argument[k++]);
			else if( format[i]=='%' ) putchar('%');
		} 
		else putchar( format[i] );
	}
}

int main(int argc, const char *argv[])
{
	gets(format);

	int nPara = 0;
	while(gets(argument[nPara]))  nPara++;

	if( calPara(format) != nPara )  printf("%s","ERROR");
	else    output(format,argument);	

	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值