Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1.  
  2. functions.h
  3.  
  4. #ifndef FUNCTIONS_H
  5. #define FUNCTIONS_H
  6.  
  7. bool checkUnique(int,char,bool);
  8.  
  9. bool IntNumber(char,int,int);
  10.  
  11. void replace(int*, int,int);
  12.  
  13. void printNum(int, int);
  14.  
  15. #endif
  16.  
  17.  
  18. main.c
  19.  
  20. #include <stdio.h>
  21. #include <stdbool.h>
  22. #include <string.h>
  23. #include "functions.h"
  24.  
  25. int main (){
  26. FILE*in,*out;
  27. if (((out = freopen("out.txt", "w", stdout)) == NULL)
  28. || ((in = freopen("in.txt", "r", stdin)) == NULL))
  29. return 0;
  30. in = freopen("in.txt","r",stdin);
  31. out = freopen("out.txt","w",stdout);
  32. char enter [10];
  33. int num[10],length,transNum;
  34. bool verity;
  35. gets(enter);
  36. scanf("%d",&transNum);
  37. fclose(in);
  38. length = strlen(enter);
  39.  
  40. verity = IntNumber(enter,num,length);
  41. verity = checkUnique(length,enter,verity);
  42.  
  43. if (verity == false) {
  44. printf("bad input");
  45. return 0;
  46. }
  47.  
  48. bool check = true;
  49.  
  50. while (check == true){
  51. int TP = length-2;
  52. while (TP > -1 && num[TP] >= num[TP+1])
  53. TP--;
  54. if (TP == -1 || transNum == 0 ){
  55. check = false;
  56. break;
  57. }
  58. int min = length-1;
  59. while (num[TP] > num[min])
  60. min --;
  61. change(num,TP,min);
  62. int left = TP+1,right = length-1;
  63. while (left<right) {
  64. change(num, left, right);
  65. left++;
  66. right--;
  67. }
  68. printNum(num,length);
  69. transNum--;
  70. }
  71. fclose(out);
  72. return 0;
  73. }
  74.  
  75.  
  76. function.c
  77. #include <stdio.h>
  78. #include <stdbool.h>
  79. #include <string.h>
  80. #include "functions.h"
  81.  
  82. bool checkUnique(int length,char a[10],bool verity){
  83. int k = 0;
  84. for(int i = 0;i<length-1;i++)
  85. if (a[i]==a[i+1])
  86. k++;
  87. if (k==length-1 && length !=1)
  88. verity = false;
  89. return verity;
  90. }
  91.  
  92. bool IntNumber(char enter[10],int number[10],int length){
  93. if (length > 10||length < 1)
  94. return false;
  95. for (int i = 0;i<length;i++){
  96. number[i] = enter[i]-'0';
  97. if (number[i] < 0||number[i] > 9)
  98. return false;
  99. }
  100. return true;
  101. }
  102.  
  103. void change(int*num, int firstIndex,int secondIndex){
  104. int t = num[firstIndex];
  105. num[firstIndex] = num[secondIndex];
  106. num[secondIndex] = t;
  107. }
  108.  
  109. void printNum(int num[10],int length){
  110. FILE*out;
  111. out = freopen("out.txt","a",stdout);
  112. for (int i = 0;i < length;i++)
  113. printf("%d",num[i]);
  114. printf("\n");
  115. fclose(out);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement