#include #define SPACE_WID 8 typedef struct { char char_p; /* 文字 */ int char_w; /* 文字幅(ポイント数) */ } CHARPROF; void print_string(int, char *[], CHARPROF *, int); int word_width(char *, CHARPROF *); int main(void) { int line_wid; char *string_list[]={"This","is","an","example", "of","main","function","for","the","problem", "6.",NULL}; CHARPROF c_list[]={{'6',8},{'.',6},{'T',12},{'a',10}, {'b',9},{'c',10},{'e',10},{'f',9},{'h',10},{'i',6}, {'l',6},{'m',12},{'n',10},{'o',10},{'p',10},{'r',9}, {'s',10},{'t',9},{'u',10},{'x',10}}; printf("1行の行幅をポイント数で入力してください:"); scanf("%d",&line_wid); print_string(line_wid, string_list, c_list, SPACE_WID); return 0; } void print_string(int line_w, char *str_list[], CHARPROF *char_list, int space_w) { int cur_w = 0, str_w, idx; for (idx = 0; str_list[idx] != NULL; idx++) { str_w = word_width(str_list[idx], char_list); cur_w += str_w; if (cur_w == str_w) /* 最初の単語? */ printf("%s", str_list[idx]); else { cur_w += space_w; if (cur_w <= line_w) printf(" %s", str_list[idx]); else { cur_w = str_w; printf("\n%s", str_list[idx]); } } } putchar('\n'); } int word_width(char *str, CHARPROF *char_list) { int print_w = 0, idx; while (*str != '\0') { for (idx = 0; *str != char_list[idx].char_p; idx++); print_w += char_list[idx].char_w; str++; } return print_w; }