Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int numberOfOperators(char *input);
- int numberOfOperations(char *input);
- int readFromFileAndWrite(char *textfileName,char *codeFileName);
- int readFromFileAndPrint(char *fileName);
- int readFromKeyboardAndSaveInFile(char *filename, char *input);
- int readFromeKeyboardAndOutput(char *input);
- int main()
- {
- int menu_options;
- int i=0;
- int n;
- printf("\nMenu with different options!\n");
- printf("\nPress 1) for reading a program from a fail and save the result in a file.\n");
- printf("\nPress 2) for reading a program from a fail and output the result on the screen.\n");
- printf("\nPress 3) for reading a program from the keyboard and output in a file entered from the user.\n");
- printf("\nPress 4) for reading a program from the keyboard and output on the screen.\n");
- scanf("%d", &n);
- while(i==0)
- {
- if(n==1)
- {
- printf("\nPress 1) for reading a program from a fail and save the result in a file.\n");
- char textFileName[50];
- char codeFileName[50];
- printf("Enter file name for txt file: ");
- scanf("%s", textFileName);
- printf("File name added \n");
- printf("Enter file name for code file: ");
- scanf("%s", codeFileName);
- printf("File name added \n");
- readFromFileAndWrite(textFileName, codeFileName);
- return;
- }
- if(n==2)
- {
- printf("\nPress 2) for reading a program from a fail and output the result on the screen.\n");
- char fileName[50];
- printf("Enter filename: ");
- scanf("%s", fileName);
- readFromFileAndPrint(fileName);
- return;
- }
- if(n==3)
- {
- printf("\nPress 3) for reading a program from the keyboard and output in a file entered from the user.\n");
- char fileName[50];
- char programInput[500];
- printf("File name: ");
- scanf("%s", fileName);
- printf("File name added \n");
- printf("Program input: ");
- scanf(" %s", programInput);
- printf("ProgramInputAdded \n");
- readFromKeyboardAndSaveInFile(fileName, programInput);
- return;
- }
- if(n==4)
- {
- printf("\nPress 4) for reading a program from the keyboard and output on the screen.\n");
- char programInput[500];
- scanf(" %s", programInput);
- readFromeKeyboardAndOutput(programInput);
- }
- else
- {
- printf("Please enter a valid option!");
- scanf("%d", &n);
- }
- }
- return 0;
- }
- int numberOfOperators(char *input) // number of operators
- {
- int operators=0;
- size_t inputlenght=strlen(input);
- while(*input<inputlenght)
- {
- if(*input == '>' || *input == '<' || *input == '==' || *input =='!=' || *input == '>=' || *input == '<=')
- {
- operators++;
- }
- input++;
- }
- return operators;
- }
- int numberOfOperations(char *input) // Брой входно/изходни операции
- {
- int operations=0;
- size_t inputlenght=strlen(input);
- while(*input<inputlenght)
- {
- if(*input == 'printf' || *input == 'scanf' || *input =='gets' || *input=='puts' || *input=='fgets' || *input=='fputs')
- {
- operations++;
- }
- input++;
- }
- return operations;
- }
- int readFromFileAndWrite(char *textfileName,char *codeFileName) // 1
- {
- FILE *fp;
- FILE *newfile;
- int operators=0;
- int operations=0;
- char newFile_name[1000];
- fp=fopen(textfileName,"r");
- newfile=fopen(codeFileName, "w");
- if(fp==NULL)
- {
- perror("Invalid directory for your file!");
- return 1;
- }
- while(fgets(newFile_name, 1000, newfile) != NULL)
- {
- fputs(newFile_name, newfile);
- operators=numberOfOperators(newFile_name);
- operations=numberOfOperations(newFile_name);
- }
- printf("\nThe number of operators is %d\n", operators);
- printf("\nThe number of operations is %d\n", operations);
- return 0;
- }
- int readFromFileAndPrint(char *fileName) // 2
- {
- FILE *fp;
- int operators=0;
- int operations=0;
- char buf[1000];
- fp=fopen(fileName, "r");
- if(fp==NULL)
- {
- perror("Invalid directory for your file!");
- return 1;
- }
- while(fgets(buf, 1000, fp) != NULL)
- {
- printf("%s", buf);
- operators = numberOfOperators(buf);
- operations=numberOfOperations(buf);
- }
- printf("\nThe number of operators is %d\n", operators);
- printf("\nThe number of operations is %d\n", operations);
- return 0;
- }
- int readFromKeyboardAndSaveInFile(char *filename, char *input) // 3
- {
- FILE *fp;
- fp=fopen(filename, "w");
- int result=fputs(input, fp);
- fclose(fp);
- printf("The name of the file is %s", filename);
- int operators=numberOfOperators(input);
- int operations=numberOfOperations(input);
- printf("\nThe number of operators is %d\n", operators);
- printf("\nThe number of operations is %d\n", operations);
- return 0;
- }
- int readFromeKeyboardAndOutput(char *input) // 4
- {
- printf("%s", input);
- int operators=numberOfOperators(input);
- int operations=numberOfOperations(input);
- printf("\nThe number of operators is %d\n", operators);
- printf("\nThe number of operations is %d\n", operations);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement