这是林永吉的算法题仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.8 KiB

  1. // pch.cpp: 与预编译标头对应的源文件
  2. #include "pch.h"
  3. int CalcCount(int n, char(*str)[10], const char* FileName)
  4. {
  5. TCHAR chSectionNames[2048] = { 0 };//定义一个数组,保存读取节的名字
  6. char* pSectionName; //保存找到的某个节名字符串的首地址
  7. int i = 0; //i指向数组chSectionNames的某个位置,从0开始,顺序后移
  8. int j = 0; //j用来保存下一个节名字符串的首地址相对于当前i的位置偏移量
  9. int k = 0; //临时变量,用于给str数组赋值
  10. int m = 0; //临时变量,用于给str数组赋值
  11. int count = 0; //统计节的个数
  12. GetPrivateProfileSectionNames(chSectionNames, 2048, FileName);//获取文件中的节名,每个节名以"\0"分割,保存在chSectionNames中
  13. //要加头文件
  14. for (i = 0; i < 2048; i++, j++)//对chSectionName中保存的内容进行拆分
  15. {
  16. if (chSectionNames[0] == '\0')//如果第一个字符就是0,则说明ini中一个节也没有,
  17. {
  18. break;
  19. }
  20. if (chSectionNames[i] == '\0')
  21. {
  22. count++;
  23. pSectionName = &chSectionNames[i - j]; //找到一个0,则说明从这个字符往前,减掉j个偏移量,
  24. //就是一个节名的首地址
  25. j = -1; //找到一个节名后,j的值要还原,以统计下一个节名地址的偏移量
  26. //赋成-1是因为节名字符串的最后一个字符0是终止符,不能作为节名
  27. for (m = 0; m <= strlen(pSectionName); m++)//把找到的节名保存到str中
  28. {
  29. str[k][m] = *(pSectionName + m);
  30. }
  31. k++;//变量自加,用来保存下一个节名
  32. //在获取节名的时候可以获取该节中键的值,前提是我们知道该节中有哪些键。
  33. if (chSectionNames[i + 1] == 0)
  34. {
  35. break; //当两个相邻的字符都是0时,则所有的节名都已找到,循环终止
  36. }
  37. }
  38. }
  39. return count;//返回节名的个数
  40. }
  41. //***************************************************************************************************//
  42. //************切割字符串函数,返回值为int数组的首地址,通过传入int指针带回数组元素个数***************//
  43. //***************************************************************************************************//
  44. int* str_device(CString str, int* value_count)
  45. {
  46. char* token; //存放被切割后的第一个子串
  47. int* Section_devide = (int*)malloc(sizeof(int) * 200);//存放字符切割完成以后的数组元素值
  48. char Section_value[500] = { 0 };//存放nums转换成string类型的结果
  49. memset(Section_value, 0, sizeof(char) * 500);
  50. *value_count = 0;
  51. strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串
  52. //获得切割到的第一个字符串
  53. token = strtok(Section_value, ",");
  54. /* 继续获取其他的子字符串 */
  55. while (token != NULL) {
  56. Section_devide[*value_count] = (_ttoi)(token);//把切割得到的子串转为int,存到数组中去。
  57. token = strtok(NULL, ",");
  58. (*value_count)++; //记录存了多少个元素
  59. }
  60. return Section_devide; //返回数组首地址
  61. }
  62. int str_device2(CString str, char(*return_str)[50])
  63. {
  64. int value_count = 0;
  65. char* token; //存放被切割后的第一个子串
  66. char Section_value[500] = { 0 };//存放nums转换成string类型的结果
  67. memset(Section_value, 0, sizeof(char) * 500);
  68. strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串
  69. //获得切割到的第一个字符串
  70. token = strtok(Section_value, ",");
  71. /* 继续获取其他的子字符串 */
  72. while (token != NULL) {
  73. strcpy(return_str[value_count], token);
  74. token = strtok(NULL, ",");
  75. value_count++; //记录存了多少个元素
  76. }
  77. return value_count; //返回数组首地址
  78. }
  79. //字符串转bool
  80. bool CstrToBool(CString str)
  81. {
  82. if (str == "1") {
  83. return 1;
  84. }
  85. return 0;
  86. }