Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- int op (char a){
- char oper[] = {'+', '-','*', '/', '(', ')'};
- for (int i = 0; i < 6; i++)
- if (a == oper[i])
- return i;
- return 0;
- }
- //((x+0))*(3*x)-x*(x-5)
- char* delete_scobe(char* str, char x){
- char res[100]="";
- bool used[100]={false};
- int prior, k=0,sc=0;
- for (int i = 0; i < strlen(str); i++){
- if(str[i] == 'x'){
- res[k] = str[i];
- k++;
- continue;
- }
- if ((int)str[i] - 48 >= 0){
- res[k] = str[i];
- k++;
- continue;
- }
- if (str[i] == '('){
- sc++;
- for (int j=i+1; j < strlen(str); j++){
- if (op(str[j]) <= 1){
- if (str[j] != 'x' && ((int)str[j] - 48 < 0 || (int)str[j] - 48 > 9) && !used[j]){
- if (i == 0){
- sc--;
- break;
- }
- used[j]=true;
- res[k]=str[i];
- k++;
- break;
- }
- }else if (str[j] == ')'){
- sc--;
- break;
- }
- if (str[j] =='*' || str[j] == '/'){
- j=strlen(str);
- if (sc > 0)
- sc--;
- }
- }
- continue;
- }
- if (str[i] == ')' && sc > 0){
- sc--;
- res[k]=str[i];
- k++;
- continue;
- }else{
- if (str[i] == ')' && !sc)
- continue;
- res[k]=str[i];
- k++;
- continue;
- }
- }
- for (int i=0; i < strlen(res); i++)
- if (res[i] == 'x')
- res[i] = x;
- return res;
- }
- int main(){
- char *str = "((x*5)+(x*3*(x-5)))-4*(x*5)";
- char x = '5';
- //cin >> str;
- //cin >> x;
- str = delete_scobe(str, x);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement