main.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX_SPLIT (100)
  4. #define MAX_SIZE_LINE 1024
  5. /**
  6. * 输入分割坐标对, 按行分割内容
  7. */
  8. int getSplit(int *split);
  9. int splitLine(char *base, char *after_split, int *split, int split_len, int line_len);
  10. int getLine(char *line);
  11. int main() {
  12. int split[MAX_SPLIT];
  13. int c_split;
  14. if ((c_split = getSplit(split)) == 0)
  15. return 1;
  16. while (1){
  17. char new[MAX_SIZE_LINE] = "Hello Wolrd";
  18. char split_new[MAX_SIZE_LINE] = "";
  19. int line_len = getLine(new);
  20. if (line_len == 0)
  21. break;
  22. splitLine(new, split_new, split, c_split, line_len);
  23. printf("split: %s\n", split_new);
  24. }
  25. return 0;
  26. }
  27. int getSplit(int *split){
  28. int c_split;
  29. printf("Enter the split number: ");
  30. for (c_split=0; c_split < MAX_SPLIT; c_split++){
  31. int tmp = 0;
  32. if (!scanf("%d", &tmp) || tmp < 0)
  33. break;
  34. split[c_split] = tmp;
  35. }
  36. if (c_split == 0 || c_split % 2 != 0)
  37. return 0;
  38. while (getc(stdin) != '\n');
  39. return c_split;
  40. }
  41. int splitLine(char *base, char *after_split, int *split, int split_len, int line_len) {
  42. int c_split = 0;
  43. int stop = 0;
  44. for (int i=0;i < split_len;i += 2){
  45. int a = split[i];
  46. int b = split[i + 1];
  47. int size = b - a;
  48. if (b > line_len)
  49. b = line_len;
  50. if (a > line_len)
  51. a = line_len;
  52. if (a > b){
  53. int tmp = a;
  54. a = b;
  55. b = tmp;
  56. }
  57. if (c_split + size > MAX_SIZE_LINE) {
  58. size = MAX_SIZE_LINE - c_split;
  59. stop = 1;
  60. }
  61. strncpy(after_split, base + a, size);
  62. after_split += size;
  63. c_split += size;
  64. if (stop)
  65. break;
  66. }
  67. return 0;
  68. }
  69. int getLine(char *line){
  70. int len;
  71. printf("Enter the split Line: ");
  72. for (len=0;len < MAX_SIZE_LINE;len++){
  73. char tmp = getc(stdin);
  74. if (tmp == '\n')
  75. break;
  76. line[len] = tmp;
  77. }
  78. line[len] = '\0';
  79. return len;
  80. }