This is an easy problem from uva judge http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2478
Ad hoc, Output formatting.
How I did it
- Read the number of lines of the case.
- Read the strings and save it into a vector. (vec)
- Print the static header.
- Traverse vec and parse the data.
- Print the static footer.
Hints
- Careful with ‘\\’ ‘\” ‘\”‘
Input
1
\Y
3
(empty line)
(empty line)
(empty line)
Output
Case 1:
#include<string.h>
#include<stdio.h>
int main()
{
printf(“\\Y\n”);
printf(“\n”);
return 0;
}
Case 2:
#include<string.h>
#include<stdio.h>
int main()
{
printf(“\n”);
printf(“\n”);
printf(“\n”);
return 0;
}
Code
#include <iostream> #include <cctype> #include <cstdio> #include <vector> using namespace std; int caso = 1; void printHeader() { printf("Case %d:\n", caso++); printf("#include<string.h>\n"); printf("#include<stdio.h>\n"); printf("int main()\n"); printf("{\n"); } void printInput(string str) { printf("printf(\""); for(int c=0; c<str.size(); c++) { if(isalnum(str[c]) || isspace(str[c])) printf("%c",str[c]); else if(str[c] == '\"') printf("\\\""); else if(str[c] == '\'') printf("\\\'"); else if(str[c] == '\\') printf("\\\\"); } printf("\\n\");\n"); } void printFooter() { printf("printf(\"\\n\");\n"); printf("return 0;\n"); printf("}\n"); } int main() { int lines; string str; while(scanf("%d", &lines)) { if(!lines) break; vector<string> vec; getline(cin,str); for(int a=0; a<lines; a++) { getline(cin, str); vec.push_back(str); } printHeader(); for(int x=0; x<vec.size(); x++) printInput(vec[x]); printFooter(); } return 0; }