Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ???
- MAAIN C
- ???
- #include <stdio.h>
- #include <stdlib.h>
- #include "lab_2_5.h"
- int main(int argc, char *argv[])
- {
- char* str = (char*)malloc(sizeof(char)*40);
- int argcheck = 0;
- for (int count = 1; count < argc; count++){
- if (!strcmp(argv[count], "-fin") && (count+1 < argc)){
- if (load(str, argv[count+1]) == 0) printf("---Wrong arguments---\n");
- else argcheck = 1;
- }
- }
- if (argcheck == 0){
- printf("---Enter tree---\n");
- free(str);
- str = dscan();
- }
- while(!check(str)){
- printf("---Enter tree---\n");
- free(str);
- str = dscan();
- }
- int* dig = turnnum(toint(turn(str)));
- STree *root = NULL;
- root = (STree*)malloc(sizeof(STree));
- root -> prev = (STree*)malloc(sizeof(STree));
- root -> value = dig[0];
- root -> right = NULL;
- root -> left = NULL;
- root -> prev -> right = NULL;
- root -> prev -> left = NULL;
- for (int count = 1; dig[count] >=0; count++){
- Insert(root, dig[count]);
- }
- argcheck = 0;
- char *filename;
- for (int count = 1; count < argc; count++){
- if (!strcmp(argv[count], "-fout") && (count+1 < argc)){
- if (count+1 >= argc) printf("---Wrong arguments---\n");
- else {
- argcheck = 1;
- filename = argv[count+1];
- }
- }
- }
- if (argcheck == 1){
- FILE *file = fopen(filename, "r");
- if (file == NULL) {
- system("Cls");
- printf("Cant open file\n");
- }
- else
- fPrintTree(root, filename);
- fclose(file);
- }
- else
- PrintTree(root);
- FreeTree(root);
- free(str);
- free(dig);
- return 0;
- }
- ???
- BRAACKET C
- ???
- #include "bracket.h"
- int br_check(char st[]){
- int i, k = 0;
- char* br = (char*)malloc(sizeof(char)*strlen(st));
- for (i = 0; i < strlen(st); i++){
- switch (st[i]){
- case '(':
- br[k] = '(';
- k++;
- break;
- case '[':
- br[k] = '[';
- k++;
- break;
- case '{':
- br[k] = '{';
- k++;
- break;
- case ')':
- if ((br[k-1] == '('))
- k--;
- else{
- free(br);
- return 0;
- }
- break;
- case ']':
- if ((br[k-1] == '['))
- k--;
- else{
- free(br);
- return 0;
- }
- break;
- case '}':
- if ((br[k-1] == '{'))
- k--;
- else{
- free(br);
- return 0;
- }
- break;
- }
- }
- if (k == 0){
- free(br);
- return 1;
- }
- else{
- free(br);
- return 0;
- }
- }
- int bracket(char* a)
- {
- int n = 0;
- int i = 0;
- for (i = 0; i < strlen(a); i++)
- if ((a[i] == '(') || (a[i] == '[') || (a[i] == '{') || (a[i] == ')') || (a[i] == ']') || (a[i] == '}'))
- n++;
- if (n == 0){
- printf("----------no bracket----------\n");
- return 0;
- }
- if (br_check(a) == 0){
- printf("----------wrong bracket----------\n");
- return 0;
- }
- return 1;
- }
- ???
- FILEWORK C
- ???
- #include "filework.h"
- int save(char *str, char *filename){
- int check = 1;
- FILE *filej = fopen(filename, "w");
- if (filej == NULL) {
- system("Cls");
- printf("Cant open file\n");
- check = 0;
- }
- else{
- fputs(str, filej);
- }
- fclose(filej);
- return check;
- }
- int load(char *str, char *filename){
- int check = 1;
- FILE *file = fopen(filename, "r");
- if (file == NULL) {
- system("Cls");
- printf("Cant open file\n");
- check = 0;
- }
- else{
- fgets(str, 40, file);
- }
- fclose(file);
- return check;
- }
- char* dscan()
- {
- char* str = (char*)malloc(sizeof(char));
- char sym = 0;
- int n = 1;
- while ((sym = getc(stdin)) != '\n'){
- str[n-1] = sym;
- str = (char*)realloc(str, ++n);
- str[n-1] = '\0';
- }
- return str;
- }
- ???
- LAB 2 5 C
- ???
- #include "lab_2_5.h"
- int to10 (char dig)
- {
- int k = 0;
- char kod[] = "0123456789";
- for (k = 0; k < 10; k++)
- if (kod[k] == dig)
- return k;
- return -1;
- }
- char* turn(char *str){
- int j = strlen(str)-1;
- char save;
- for (int i = 0; i < j; i++, j--){
- save = str[j];
- str[j] = str[i];
- str[i] = save;
- }
- return str;
- }
- int* turnnum(int *num){
- int j;
- for (j = 0; num[j] != -1; j++);
- j--;
- int save;
- for (int i = 0; i < j; i++, j--){
- save = num[j];
- num[j] = num[i];
- num[i] = save;
- }
- return num;
- }
- int* toint(char* str){
- int* nums = (int*)malloc(sizeof(int));
- int count = 1;
- int ncount = 0;
- int n = 0;
- nums[0] = 0;
- while ((str[n]) != '\0'){
- if (to10(str[n]) > 0){
- nums[count-1] += to10(str[n])*pow(10, ncount++);
- }
- else
- if ((n != 0) && (to10(str[n-1]) >= 0)){
- nums = (int*)realloc(nums, sizeof(int)*(++count));
- nums[count-1] = 0;
- ncount = 0;
- }
- n++;
- }
- nums = (int*)realloc(nums, sizeof(int)*(++count));
- nums[count-1] = -1;
- return nums;
- }
- void Insert(STree *root, int v){
- if (v > root->value)
- root->right == NULL ?
- (root->right = (STree*)malloc(sizeof(STree)),root->right->value = v, root->right->prev = root, root->right->right = root->right->left = NULL)
- : Insert(root->right, v);
- else
- root->left == NULL ?
- (root->left = (STree*)malloc(sizeof(STree)),root->left->value = v, root->left->prev = root, root->left->right = root->left->left = NULL)
- : Insert(root->left, v);
- }
- void PrintTree(STree *root){
- if (root){
- if (root->prev->right || root->prev->left)
- if (!(root->prev->right == root) || !root->prev->left) printf("(");
- printf("%d", root->value);
- PrintTree(root->left);
- if (root->right && root->left) printf(",");
- PrintTree(root->right);
- if (root->prev->right || root->prev->left)
- if (!root->prev->right || root->prev->right==root)
- printf(")");
- }
- }
- void fPrintTree(STree *root, char* filename){
- FILE *file = fopen(filename, "w");
- if (file == NULL) {
- system("Cls");
- printf("Cant open file\n");
- }
- else{
- if (root){
- if (root->prev->right || root->prev->left)
- if (!(root->prev->right == root) || !root->prev->left) fprintf(file, "(");
- fprintf(file, "%d", root->value);
- fPrintTree(root->left, filename);
- if (root->right && root->left) fprintf(file, ",");
- fPrintTree(root->right, filename);
- if (root->prev->right || root->prev->left)
- if (!root->prev->right || root->prev->right==root)
- fprintf(file, ")");
- }
- }
- fclose(file);
- }
- void FreeTree(STree *root){
- if (root){
- FreeTree(root->left);
- FreeTree(root->right);
- free(root);
- }
- }
- int check(char *str){
- int check = 0;
- check = bracket(str);
- if (to10(str[0]) < 0){
- printf("---No Head---\n");
- check = 0;
- }
- if (str[1] != '('){
- printf("---No first bracket---");
- check = 0;
- }
- for(int count = 0; count < strlen(str)-1; count++){
- if ((str[count] == ',' && str[count+1] == ',') ||
- (str[count] == ',' && str[count+1] == '(') ||
- (str[count] == ',' && str[count+1] == ')') ||
- (str[count] == '(' && str[count+1] == ',') ||
- (str[count] == '(' && str[count+1] == '(') ||
- (str[count] == '(' && str[count+1] == ')') ||
- (str[count] == ')' && str[count+1] == '(') ){
- printf("---Wrong punctuation---\n");
- check = 0;
- }
- }
- for(int count = 0; count < strlen(str); count++){
- if ((str[count] != ',') &&
- (str[count] != '(') &&
- (str[count] != ')') &&
- (to10(str[count]) < 0)){
- printf("---Wrong symbol # %d---\n", count);
- check = 0;
- }
- }
- return check;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement