Poj 3428 Formatting function
来源:互联网 发布:正大数据恢复中心 深圳 编辑:程序博客网 时间:2024/06/03 21:23
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 %d202=4Sample Input 2%sSample Input 3%d2a
Sample Output
Sample Output 12 x 2 = 4Sample Output 2ERRORSample Output 3ERROR
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;}
- Poj 3428 Formatting function
- POJ 3428 Formatting function 可能会
- POJ 3428 Formatting function (文件输入)
- poj 1093 Formatting Text
- poj 1093 Formatting Text
- POJ 1896 Code Formatting
- poj 1093 Formatting Text (DP)
- POJ 1093 Formatting Text(DP)
- POJ 1093 Formatting Text 笔记
- Formatting
- Formatting Text - UVa 709 POJ 1093 dp
- poj - 1093 - Formatting Text(dp)
- POJ Function Run Fun
- Function Run Fun POJ
- uva 709 &poj 1093 - Formatting Text (dp之小白告一段落)
- formatting messagebox
- Formatting Types
- Formatting Types
- 界面编程之基本界面组件(6)AnalogClock 和 DigitalClock(时钟)
- pthread_create 简介
- 将文件字符编码方式从 windows 的 gb18030 转 linux 下的 utf-8
- 工具目录
- SVN插件在MyEclipse8.0如何安装使用?
- Poj 3428 Formatting function
- scanf()函数用法小结
- C语言学习笔记
- C++编译不通过问题排查
- Git repo 常用命令
- 四巨头GoF的《设计模式》中23种设计模式的UML图 - 3 of 4
- vcl消息
- IBM “New Power”五大新计划
- POJ 1580 Code