一:题目
将不规范的若干行代码进行对齐。对齐按照最长字符串进行操作。见样例输入
(一)样例输入
??start:??integer;????//?begins?here stop:?integer;?//??ends?here ?s:??string; c:???char;?//?temp
注: ? 表示空格
(二)样例输出
start: integer; // begins here stop: integer; // ends here s: string; c: char; // temp
按照每列最长字符串空一格对齐
二:代码实现
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <iomanip> #include <vector> #include <string> #include <sstream> #define MAX_LINES 1000 #define MAX_LEN 180 using namespace std; int main() { FILE* fp = freopen("data5_1_h.in", "r", stdin); freopen("data5_1_h.out", "w", stdout); //收集代码信息 int n = 0; string code,single_c; vector<string> codes[MAX_LINES]; int code_max_len[MAX_LEN] = { 0 }; //进行信息获取 while (!feof(fp)) { getline(cin, code); stringstream ss(code); int i = 0; while (ss>>single_c) { codes[n].push_back(single_c); code_max_len[i] > single_c.length() ? i++ : code_max_len[i++] = single_c.length() + 1; } n++; } //进行信息打印 for (int i = 0; i <= n; i++) { int j = 0; for (vector<string>::iterator it = codes[i].begin(); it != codes[i].end(); it++,j++) { cout << setw(code_max_len[j])<<setiosflags(ios::left)<< *it; } cout << endl; } freopen("CON", "r", stdin); freopen("CON", "w", stdout); return 0; }
原文地址:https://www.cnblogs.com/ssyfj/p/11528906.html
时间: 2024-10-29 12:34:35