Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- functions.h
- #ifndef FUNCTIONS_H
- #define FUNCTIONS_H
- bool checkUnique(int,char,bool);
- bool IntNumber(char,int,int);
- void replace(int*, int,int);
- void printNum(int, int);
- #endif
- main.c
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include "functions.h"
- int main (){
- FILE*in,*out;
- if (((out = freopen("out.txt", "w", stdout)) == NULL)
- || ((in = freopen("in.txt", "r", stdin)) == NULL))
- return 0;
- in = freopen("in.txt","r",stdin);
- out = freopen("out.txt","w",stdout);
- char enter [10];
- int num[10],length,transNum;
- bool verity;
- gets(enter);
- scanf("%d",&transNum);
- fclose(in);
- length = strlen(enter);
- verity = IntNumber(enter,num,length);
- verity = checkUnique(length,enter,verity);
- if (verity == false) {
- printf("bad input");
- return 0;
- }
- bool check = true;
- while (check == true){
- int TP = length-2;
- while (TP > -1 && num[TP] >= num[TP+1])
- TP--;
- if (TP == -1 || transNum == 0 ){
- check = false;
- break;
- }
- int min = length-1;
- while (num[TP] > num[min])
- min --;
- change(num,TP,min);
- int left = TP+1,right = length-1;
- while (left<right) {
- change(num, left, right);
- left++;
- right--;
- }
- printNum(num,length);
- transNum--;
- }
- fclose(out);
- return 0;
- }
- function.c
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include "functions.h"
- bool checkUnique(int length,char a[10],bool verity){
- int k = 0;
- for(int i = 0;i<length-1;i++)
- if (a[i]==a[i+1])
- k++;
- if (k==length-1 && length !=1)
- verity = false;
- return verity;
- }
- bool IntNumber(char enter[10],int number[10],int length){
- if (length > 10||length < 1)
- return false;
- for (int i = 0;i<length;i++){
- number[i] = enter[i]-'0';
- if (number[i] < 0||number[i] > 9)
- return false;
- }
- return true;
- }
- void change(int*num, int firstIndex,int secondIndex){
- int t = num[firstIndex];
- num[firstIndex] = num[secondIndex];
- num[secondIndex] = t;
- }
- void printNum(int num[10],int length){
- FILE*out;
- out = freopen("out.txt","a",stdout);
- for (int i = 0;i < length;i++)
- printf("%d",num[i]);
- printf("\n");
- fclose(out);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement