Skip to content

Instantly share code, notes, and snippets.

@Vivek-abstract
Last active April 14, 2018 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vivek-abstract/07e7c93c4a696be9b8fe272fe554698a to your computer and use it in GitHub Desktop.
Save Vivek-abstract/07e7c93c4a696be9b8fe272fe554698a to your computer and use it in GitHub Desktop.
import java.util.*;
public class COCompileTimeEvaluation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of expressions");
int n = sc.nextInt();
sc.nextLine();
System.out.println("Enter the expression: ");
String prog[] = new String[100];
for(int i = 0; i < n; i++) {
prog[i] = sc.nextLine();
}
sc.close();
for(int i = 0; i < n; i++) {
if(prog[i].contains("=")) {
String tokens[] = prog[i].split("=");
String replaceThis = tokens[0];
String replaceBy = tokens[1];
for(int j = i+1; j < n; j++) {
prog[j] = prog[j].replace(replaceThis, replaceBy);
}
}
}
System.out.println("\nAfter performing Compile Time Evaluation:");
for(int i = 0; i < n; i++) {
System.out.println(prog[i]);
}
}
}
/*
OUPUT:
Enter number of expressions
3
Enter the expression:
pi=3.14259
area = pi * r * r;
circumference = 2 * pi * r;
After performing Compile Time Evaluation:
pi=3.14259
area = 3.14259 * r * r;
circumference = 2 * 3.14259 * r;
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment