fork download
  1. /*
  2.  The memory of array should be obtained by malloc with only one cell initially.
  3.  Double size whenever insertion-on-full
  4.  Alloc double size, Copy the current data, and then Insert the new data
  5.  
  6.  Each record has three fields:
  7.  char name [16]
  8.  int age
  9.  enum {female, male} sex
  10.  
  11.  The input is like "Peter Wang", 35, female
  12.  Each line of the input is a record to be inserted
  13.  
  14.  Output:
  15.  After insertion insert record, name=XXX,age=NN,sex=male;
  16.  When insertion-to-full: output message "queue/stack full, allocate double size NN"
  17.  */
  18.  
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <iostream>
  22. #include <cstring>
  23. #include <cctype>
  24. #define N 1000
  25. using namespace std;
  26.  
  27. typedef struct {
  28. char name[N];
  29. int age;
  30. enum {female, male} sex;
  31. } DATA;
  32.  
  33. bool check(char *name, char *age, char *sex){
  34. int space=0, i;
  35. if(name == NULL || age == NULL || sex == NULL){ //是否有缺少資料
  36. cout<<"Wrong input format"<<endl;
  37. return false;
  38. }
  39.  
  40. for(i = 0; i < strlen(age); i++){ //age
  41. if(age[i] == ' ') {
  42. space++;
  43. }
  44. else if(!isdigit(age[i])){
  45. printf("The age entered is not a number.");
  46. return false;
  47. }
  48. }
  49. if(space == (int)strlen(age)){
  50. printf("Only spaces in age.");
  51. return false;
  52. }
  53.  
  54. for(i=0; i<strlen(sex); i++){ //sex
  55. if(!strcasecmp(sex+i, "female") && !strcasecmp(sex+i, "male"))
  56. break;
  57. if(i == strlen(sex)){
  58. cout<<"Wrong sex";
  59. return false;
  60. }
  61. if(!isalpha(sex[i]) && sex[i] != ' '){
  62. cout<<"Wrong sex";
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68.  
  69. void copy(char *name, char *age, char *sex, int now, DATA *record){
  70.  
  71. //name
  72. char str[N];
  73. int i, num=0; //num記錄新字串目前位置
  74. for(i=0; i<strlen(name); i++){
  75. if(name[i] != '"') //移除雙引號
  76. str[num++] = name[i];
  77. }
  78. str[num] = '\0';
  79. strcpy((record+now)->name, str);
  80.  
  81. //age
  82. int year = atoi(age);
  83. (record+now)->age = year;
  84.  
  85. //sex
  86. if(strcasecmp(sex, "female"))
  87. (record+now)->sex = DATA::female;
  88. else
  89. (record+now)->sex = DATA::male;
  90. }
  91.  
  92. int main(){
  93. ios::sync_with_stdio(0);
  94. char input[N];
  95. int capacity=1, now=0; //capacity表示現在容量, now表示現在有幾筆資料
  96. DATA *record = (DATA *)malloc(sizeof(DATA)*capacity); //利用malloc取記憶體
  97. while(gets(input) != NULL){
  98. if(now == capacity){ //空間已滿
  99. DATA *temp;
  100. temp = (DATA *)realloc(record, sizeof(DATA) * capacity * 2); //需要兩倍大, 使用realloc調整
  101. if(temp == NULL){ //沒有拿到空間
  102. cout<<"Malloc failed. Program ends now\n";
  103. return 0;
  104. }
  105. else //有拿到空間
  106. cout<<"queue/stack full, allocate double size "<<capacity * 2<<endl;
  107. free(record); //free舊資料
  108. record=temp;
  109. capacity *= 2;
  110. }
  111. char *name_start, *age_start, *sex_start;
  112. name_start = strtok(input, ",");
  113. age_start = strtok(NULL, ",");
  114. sex_start = strtok(NULL, "\n");
  115. if(!check(name_start, age_start, sex_start))
  116. continue;
  117. else
  118. copy(name_start, age_start, sex_start, now, record);
  119. //cout<<(record)->name<<endl;
  120. now++;
  121. }
  122. return 0;
  123. }
Runtime error #stdin #stdout 0s 3464KB
stdin
"Peter Wang", 35, female
"apple", 35, female
"Apple Chang", 10, male
"AAAAABBBBBCCCCCD", 10, male
stdout
queue/stack full, allocate double size 2