Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
2.65/5 (3 votes)
See more:
Project: Write program in c Language that receives codes from a file and analyzed, so that puts the correct codes in a file and wrong codes in another file
Structures available & Grammar
1-Declaring a Variable var i;
2- i =i+1;
3- for(i=0;i<4;i++){//your code };
4- if(i<3){ //your code };
5- while(i<3){//your code };
6- /*multi line comment */
7- Variable one Character
_________________________________
if code in file is :
C++
var i,x=2;
for{i=0;);i<3;i++}{
      iff(i==2){
            x=x+2;
          }  

wrong codes in file is:
C++
LINE2 ;)
LINE3 f
LINE6 }

correct codes :
C++
for{i=0;i<3;i++}{
      if(i==2){
            x=x+2;
          };
       };


What are the steps to write this program
Posted
Updated 31-Dec-14 23:59pm
v7
Comments
OriginalGriff 29-Dec-14 11:15am    
And?
What is the problem?
Where are you stuck?
What have you tried?
Whet help do you need?
Mortadda jafar 29-Dec-14 11:19am    
how write this program?
PIEBALDconsult 29-Dec-14 11:29am    
We won't do your homework for you. Try it yourself. If you can't even begin; then please consider a different career path.
Mortadda jafar 29-Dec-14 11:32am    
Thank you, but I've tried a lot
I just want algorithm
PIEBALDconsult 29-Dec-14 11:34am    
Use Improve question to add the particular part of your code that is giving you trouble.

"how write this program?"

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think!

Start by breaking it down into smaller tasks: read the file, analyse the file, output the right and wrong data. Then break those down until the tasks you get are manageable. The "analyse" task is going to be the complex one, so "dummy it" to start with, and make sure the others are working fine.
Then start on the analysis. Look at what is legal, and start checking if what you have received matches. Think about how you would do it manually, or even do it with a pencil and paper! Once you work that out, the "computerised" version will pretty much write itself.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Comments
Mortadda jafar 31-Dec-14 8:36am    
Thank you .. I've written code but in some conditions Broken
OriginalGriff 31-Dec-14 8:40am    
So start with the debugger and work out exactly what is going on.
Mortadda jafar 1-Jan-15 12:55pm    
Hello
I wrote code .. and I know that a long and bad
One problem in the code are explained below:
------The original code(input file)---
if(>2){
if(q>3){
var ;
};
var f;
};
------The correct code(output file)-----
if(f>2){ f(The user entered)
if(q>3){
var d; d (The user entered)
var f; The problem with this line and the next line where it should all of them instead of the other
};
};
OriginalGriff 2-Jan-15 6:22am    
Sorry, but that doesn't make a lot of sense - are you trying to say you are parsing C# code, and trying to prompt the user to correct it?
If so, how far have you got, and what problem(s) are you meeting?
Just showing examples, doesn't help that much, as I can't see your screen so I don't have any context for them.
C++
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
FILE *file, *fw, *fc;
int j = 0, i, n = 0;
char ch, matrix[500], temp[500];
int test_if(int i);
int test_while(int i);
int test_for(int i);
int test_var(int i);
int test_variable(int i);

void copy() {
    i = 0;
    memset(matrix, '\0', 500);
    file = fopen("code.txt", "r+");
    while ((ch = getc(file)) != EOF) {
        matrix[i] = ch;
        i++;
    }
    fclose(file);

}

void commend() {
    for (i = 0; i < strlen(matrix); i++) {
        if (matrix[i] == '/' && matrix[i + 1] == '*') {
            do {
                i++;
            } while (matrix[i] != '/' && matrix[i + 1] != '*' && i < (strlen(matrix) - 1));
            i += 3;
        }
        strncat(temp, matrix + i, 1);
    }
    memset(matrix, '\0', 500);
    strcpy(matrix, temp);
}

int test(int q) {
    for (i = q; i < strlen(matrix); i++) {
        while (matrix[i] == ' ' || matrix[i] == '\n') {
            i++;
        }
        if ((matrix[i] != 'v' && matrix[i + 1] != 'a' && matrix[i + 2] != 'r')&&(matrix[i] != 'i' && matrix[i + 1] != 'f')&&(matrix[i] != 'f' && matrix[i + 1] != 'o' && matrix[i + 2] != 'r') &&(matrix[i] != 'w' && matrix[i + 1] != 'h' && matrix[i + 2] != 'i' && matrix[i + 3] != 'l' && matrix[i + 4] != 'e') && isalpha(matrix[i])) {
            i = test_variable(i);
            return i;
        } else {
            switch (matrix[i]) {
                case 'i':
                    i = test_if(i);
                    return i;
                    break;
                case 'w':
                    i = test_while(i);
                    return i;
                    break;
                case 'v':
                    i = test_var(i);
                    return i;
                    break;
                default:
                    return i;
                    break;
            }
        }
    }
}

int main(int argc, char **argv) {
    fc = fopen("fc.txt", "w+");
    fw = fopen("fw.txt", "w+");
    copy();
    commend();
    test(0);
    fclose(fc);
    fclose(fw);
}

int test_if(int i) {
    int j;
    char var = '\0', sim = '\0', x, y, l_par = '\0', r_par = '\0', op, num, ll_par = '\0', rr_par = '\0', z;
    if (matrix[i] == 'i' && matrix[i + 1] == 'f') {
        for (j = i + 2; j < strlen(matrix); j++) {
            if (matrix[j] == ' ')
                continue;
            l_par = matrix[j];
            break;
        }
        if (l_par != '(')
            j = i + 1;
        for (i = j + 1; i < strlen(matrix); i++) {
            if (matrix[i] == ' ' || matrix[i] == '>' || matrix[i] == '<' || matrix[i] == '=' || matrix[i] == ')' || matrix[i] == '{' || isdigit(matrix[i])) {
                if (matrix[i] == '>' || matrix[i] == '<' || matrix[i] == '=' || matrix[i] == ')' || matrix[i] == '{' || isdigit(matrix[i]))
                    break;
                continue;
            }
            var = matrix[i];
            break;
        }
        if (matrix[i] == '>' || matrix[i] == '<' || matrix[i] == '=') {
            i = i - 1;
        }
        if (isdigit(matrix[i])) {
            goto after_op;
            j = j - 1;
        }
        if (matrix[i] == ')') {
            goto after_op_and_number;
            i = j - 1;
        }
        if (matrix[i] == '{') {
            goto after_op_number_rpar;
            j = j - 1;
        }
        for (j = i + 1; j < strlen(matrix); j++) {
            if (matrix[j] == ' ')
                continue;
            op = matrix[j];
            break;
        }
after_op:
        for (i = j + 1; i < strlen(matrix); i++) {
            if (matrix[i] == ' ')
                continue;
            num = matrix[i];
            break;
        }
after_op_and_number:
        for (j = i + 1; j < strlen(matrix); j++) {
            if (matrix[j] == ' ')
                continue;
            r_par = matrix[j];
            break;
        }
after_op_number_rpar:
        for (i = j + 1; i < strlen(matrix); i++) {
            if (matrix[i] == ' ')
                continue;
            ll_par = matrix[i];
            break;
        }
        if (isalpha(var) && (op == '>' || op == '<' || op == '=') && isdigit(num)) // var op num
            fprintf(fc, "if(%c%c%c){\n", var, op, num);
        else if (isalpha(var) && (op == '>' || op == '<' || op == '=')) { // var op
            printf("enter number for if \n");
            scanf("%c", &x);
            fprintf(fc, "if(%c%c%c){\n", var, op, x);
            fprintf(fw, "error in if\n");
        } else if (isalpha(var) && isdigit(num)) { //var num                                            
            printf("enter op for if\n");
            scanf("%c", &x);
            fprintf(fc, "if(%c%c%c){\n", var, x, num);
            fprintf(fw, "error in if\n");
        } else if ((op == '>' || op == '<' || op == '=') && isdigit(num)) { //op  num
            printf("enter Variable for if \n");
            scanf("%c", &x);
            fprintf(fc, "if(%c%c%c){\n", x, op, num);
            fprintf(fw, "error in if\n");
        } else if ((op == '>' || op == '<' || op == '=')) { //op  
            printf("enter var for if.. \n");
            scanf("%c", &x);
            printf("enter number for if.... \n");
            scanf("%c", &y);
            scanf("%c", &y);
            fprintf(fc, "if(%c%c%c){\n", x, op, y);
            fprintf(fw, "error in if\n");
        } else if (isdigit(num)) { //num
            printf("enter var for if.. \n");
            scanf("%c", &x);
            printf("enter op for if.... \n");
            scanf("%c", &y);
            scanf("%c", &y);
            fprintf(fc, "if(%c%c%c){\n", x, y, num);
            fprintf(fw, "error in if\n");
        } else if (isalpha(var)) { //var
            printf("enter op for if.... \n");
            scanf("%c", &x);
            printf("enter number for if.. \n");
            scanf("%c", &y);
            scanf("%c", &y);
            fprintf(fc, "if(%c%c%c){\n", var, x, y);
            fprintf(fw, "error in if\n");
        } else {
            printf("enter var for if.... \n");
            scanf("%c", &x);
            printf("enter op for if.. \n");
            scanf("%c", &y);
            scanf("%c", &y);
            printf("enter number for if.. \n");
            scanf("%c", &z);
            scanf("%c", &z);
            fprintf(fc, "if(%c%c%c){\n", x, y, z);
            fprintf(fw, "error in if\n");
        }
        for (j = i; j < strlen(matrix); j++) {
            if (matrix[j] == ' ' || matrix[j] == '\n' || matrix[j] != '}') {
                j = test(j);
                continue;
            }
            rr_par = matrix[j];
            break;
        }
        for (i = j + 1; i < strlen(matrix); i++) {
            if (matrix[i] == ' ' || matrix[i] == '\n' || matrix[i] != ';')
                continue;
            sim = matrix[i];
            break;
        }

        fprintf(fc, "};\n");
    }
    return i;
}

int test_var(int i) {
    int j, l, t;
    char var = '\0', sem = '\0', x;
    if (matrix[i] == 'v' && matrix[i + 1] == 'a' && matrix[i + 2] == 'r') {
        for (j = i + 3; j < strlen(matrix); j++) {
            if (matrix[j] == ' ' || matrix[j] == ';') {
                if (matrix[j] == ';')
                    break;
                continue;
            }
            var = matrix[j];
            break;
        }
        if (matrix[j] == ';')
            j--;
        t = j;
        for (l = j + 1; l < strlen(matrix); l++) {
            if (matrix[l] == ' ')
                continue;
            sem = matrix[l];
            break;
        }
        if (sem != ';') {
            l = i + 3;
            var = '\0';
        }
        if ((sem == ';') && isalpha(var))
            fprintf(fc, "var %c;\n", var);
        else if ((sem == ';')) {
            fprintf(fw, "var ;\n");
            printf("variable don't found please enter your character to correct\n");
            x = getchar();
            scanf("%c", &x);
            fprintf(fc, "var %c;\n", x);
        } else if (isalpha(var)) {
            fprintf(fw, "var %c\n", var);
            printf("one ; in this variable don't found press any key to correct this error\n");
            getch();
            fprintf(fc, "var %c;\n", var);
        } else {
            fprintf(fw, "var\n");
            printf("; and character don't found\n");
            printf("pleas enter your variable\n");
            x = getchar();
            scanf("%c", &x);
            fprintf(fc, "var %c;\n", x);
        }
    }
    return l;
}

int test_while(int i) {
    int j;
    char var = '\0', sim = '\0', x, y, l_par = '\0', r_par = '\0', op, num, ll_par = '\0', rr_par = '\0', z;
    if (matrix[i] == 'w' && matrix[i + 1] == 'h' && matrix[i + 2] == 'i' && matrix[i + 3] == 'l' && matrix[i + 4] == 'e') {
        for (j = i + 5; j < strlen(matrix); j++) {
            if (matrix[j] == ' ')
                continue;
            l_par = matrix[j];
            break;
        }
        if (l_par != '(')
            j = i + 2;
        for (i = j + 1; i < strlen(matrix); i++) {
            if (matrix[i] == ' ' || matrix[i] == '>' || matrix[i] == '<' || matrix[i] == '=' || matrix[i] == ')' || matrix[i] == '{' || isdigit(matrix[i])) {
                if (matrix[i] == '>' || matrix[i] == '<' || matrix[i] == '=' || matrix[i] == ')' || matrix[i] == '{' || isdigit(matrix[i]))
                    break;
                continue;
            }
            var = matrix[i];
            break;
        }
        if (matrix[i] == '>' || matrix[i] == '<' || matrix[i] == '=') {
            i = i - 1;
        }
        if (isdigit(matrix[i])) {
            goto after_op;
            j = j - 1;
        }
        if (matrix[i] == ')') {
            goto after_op_and_number;
            i = j - 1;
        }
        if (matrix[i] == '{') {
            goto after_op_number_rpar;
            j = j - 1;
        }
        for (j = i + 1; j < strlen(matrix); j++) {
            if (matrix[j] == ' ')
                continue;
            op = matrix[j];
            break;
        }
after_op:
        for (i = j + 1; i < strlen(matrix); i++) {
            if (matrix[i] == ' ')
                continue;
            num = matrix[i];
            break;
        }
after_op_and_number:
        for (j = i + 1; j < strlen(matrix); j++) {
            if (matrix[j] == ' ')
                continue;
            r_par = matrix[j];
            break;
        }
after_op_number_rpar:
        for (i = j + 1; i < strlen(matrix); i++) {
            if (matrix[i] == ' ')
                continue;
            ll_par = matrix[i];
            break;
        }
        if (isalpha(var) && (op == '>' || op == '<' || op == '=') && isdigit(num)) // var op num
            fprintf(fc, "while(%c%c%c){\n", var, op, num);
        else if (isalpha(var) && (op == '>' || op == '<' || op == '=')) { // var op
            printf("enter number for while \n");
            scanf("%c", &x);
            fprintf(fc, "while(%c%c%c){\n", var, op, x);
            fprintf(fw, "error in while\n");
        } else if (isalpha(var) && isdigit(num)) { //var num                                            
            printf("enter op for while\n");
            scanf("%c", &x);
            fprintf(fc, "while(%c%c%c){\n", var, x, num);
            fprintf(fw, "error in while\n");
        } else if ((op == '>' || op == '<' || op == '=') && isdigit(num)) { //op  num
            printf("enter Variable for while \n");
            scanf("%c", &x);
            fprintf(fc, "while(%c%c%c){\n", x, op, num);
            fprintf(fw, "error in while\n");
        } else if ((op == '>' || op == '<' || op == '=')) { //op  
            printf("enter var for while.. \n");
            scanf("%c", &x);
            printf("enter number for while.... \n");
            scanf("%c", &y);
            scanf("%c", &y);
            fprintf(fc, "while(%c%c%c){\n", x, op, y);
            fprintf(fw, "error in while\n");
        } else if (isdigit(num)) { //num
            printf("enter var for while.. \n");
            scanf("%c", &x);
            printf("enter op for while.. \n");
            scanf("%c", &y);
            scanf("%c", &y);
            fprintf(fc, "while(%c%c%c){\n", x, y, num);
            fprintf(fw, "error in while\n");
        } else if (isalpha(var)) { //var
            printf("enter op for while.. \n");
            scanf("%c", &x);
            printf("enter number for while.. \n");
            scanf("%c", &y);
            scanf("%c", &y);
            fprintf(fc, "while(%c%c%c){\n", var, x, y);
            fprintf(fw, "error in while\n");
        } else {
            printf("enter var for while.... \n");
            scanf("%c", &x);
            printf("enter op for while.. \n");
            scanf("%c", &y);
            scanf("%c", &y);
            printf("enter number for while.. \n");
            scanf("%c", &z);
            scanf("%c", &z);
            fprintf(fc, "while(%c%c%c){\n", x, y, z);
            fprintf(fw, "error in while\n");
        }
        for (j = i + 1; j < strlen(matrix); j++) {
            if (matrix[j] == ' ' || matrix[j] == '\n' || matrix[j] != '}') {
                j = test(j);
                continue;
            }
            rr_par = matrix[j];
            break;
        }
        for (i = j + 1; i < strlen(matrix); i++) {
            if (matrix[i] == ' ' || matrix[i] != ';')
                continue;
            sim = matrix[i];
            break;
        }

        fprintf(fc, "};\n");
    }
    return i;
}

int test_variable(int i) {
    int j;
    printf("[%d] %c\n", i, matrix[i]);
    char t, var = '\0', op = '\0', var1 = '\0', op1 = '\0', num = '\0', x, y, sem = '\0';
    if (isalpha(matrix[i])) {
        var = matrix[i];
        for (j = i + 1; j < strlen(matrix); j++) { //i  =   i+1  || 1  +  i  // var i;
            if (matrix[j] == ' ')
                continue;
            op = matrix[j];
            break;
        }
        if (op != '=')
            return i;
        for (i = j + 1; i < strlen(matrix); i++) {
            if (matrix[i] == ' ')
                continue;
            num = matrix[i];
            break;
        }
        if (num == ';') {
            sem = num;
            num = '\0';
            goto aftersem;
        }
        if ((num == '+' || num == '-' || num == '*' || num == '/')) {
            op1 = num;
            num = '\0';
            j = i;
            goto afterop1;
        }
        for (j = i + 1; j < strlen(matrix); j++) {
            if (matrix[j] == ' ')
                continue;
            op1 = matrix[j];
            break;
        }
        if (op1 == ';') {
            sem = op1;
            op1 = '\0';
            i = j;
            goto aftersem;
        }
afterop1:
        for (i = j + 1; i <= strlen(matrix); i++) {
            if (matrix[i] == ' ') {
                continue;
            }
            var1 = matrix[i];
            break;
        }

        if (var1 == ';') {
            sem = var1;
            var1 = '\0';
            goto aftersem;
        }
        for (j = i + 1; j <= strlen(matrix); j++) {
            if (matrix[j] == ' ') {
                continue;
            }
            sem = matrix[j];
            break;
        }// i=i+3
aftersem:
        if (isalpha(num) || isdigit(var1)) {
            t = num;
            num = var1;
            var1 = t;
        }
        if ((var == var1)&&(op1 == '+' || op1 == '-' || op1 == '*' || op1 == '/') && isdigit(num) && sem == ';')
            fprintf(fc, "%c %c %c %c %c;", var, op, var, op1, num);
        else if ((op1 == '+' && op1 == '-' && op1 == '*' && op1 == '/') && isdigit(num) && sem == ';') {
            fprintf(fw, "%c %c %c %c %c %c\n", var, op, var1, op1, num, sem);
            fprintf(fc, "%c %c %c %c %c;", var, op, var, op1, num);
        } else if ((op1 == '+' || op1 == '-' || op1 == '*' || op1 == '/') && isdigit(num)) {
            fprintf(fw, "%c %c %c %c %c %c\n", var, op, num, op1, var1, sem);
            fprintf(fc, "%c %c %c %c %c;\n", var, op, var, op1, num);
        } else if (isdigit(num)) {
            fprintf(fw, "%c %c %c %c %c %c\n", var, op, num, op1, var1, sem);
            printf("enter op for varible \n");
            scanf("%c", &x);
            fprintf(fc, "%c %c %c %c %c;\n", var, op, var, x, num);
        } else if ((op1 == '+' || op1 == '-' || op1 == '*' || op1 == '/')) {
            fprintf(fw, "%c %c %c %c %c %c\n", var, op, num, op1, var1, sem);
            printf("enter number for variable\n");
            scanf("%c", &x);
            fprintf(fc, "%c %c %c %c %c;\n", var, op, var, op1, x);
        } else {
            fprintf(fw, "%c %c %c %c %c %c\n", var, op, num, op1, var1, sem);
            printf("enter number for variable \n");
            scanf("%c", &x);
            printf("enter op for varible \n");
            scanf("%c", &y);
            fprintf(fc, "%c %c %c %c %c;\n", var, op, var1, y, x);
        }
    }
    i = j;
    return i;
}</string.h></ctype.h></stdlib.h></stdio.h>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900