Click here to Skip to main content
16,018,158 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include<string.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <map>
using namespace std;
int jomleTokalame(string s)
{
    string::iterator i;
    int count = 0;
    for (i = s.begin(); i != s.end(); i++)
    {
        if(*i != ' ' && *i != '.')
        {
            count++;
        }
    }
    return count;
}
#define operand(x)(x>='a'&&x<='z' || x>='A'&&x<='Z' || x>='0'&&x<='9')
char infix[30],prefix[30],stack[30];
int top,i=0;
void init()
{
    top=-1;
}
void push(char x)
{
    stack[++top]=x;
}

char pop()
{
    return(stack[top--]);
}

int isp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?0:-1);
return y;
}

int icp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?4:-1);
return y;
}
void infixtoprefix()
{
    int j,l=0;
    char x,y;
    stack[++top]='\0';
    for (j=strlen(infix)-1; j>=0; j--)
        {
            x=infix[j];
            if (operand(x))
                prefix[l++]=x;
            else
                if (x=='(')
                    while ((y=pop())!=')')
                        prefix[l++]=y;
                else
                    {
                        while (isp(stack[top])>=icp(x))
                            prefix[l++]=pop();
                        push(x);
                    }
        }
    while (top>=0)
        prefix[l++]=pop();
}
int main()
{
 string matn[100][100];
 int location,n;
 string z1,z2;
 string var[100];
 int loc=0;
 //4taii
  string s;
 int i=0;
 int ram[100];
 //2taii
 int memory[100];
 int address=99;
 int flag=0;
  string a[50];
 int b[100];
 string str[100];
 cout<<"plz enter the number of lines:"<<endl;
    cin>>n;
    cin.ignore();
    for(i=0 ; i<n ; i++)
    {
        getline(cin, matn[loc][i]);
        cout<<jomleTokalame(matn[loc][i])<<endl;
    }
  for(int j=0;j<=jomleTokalame(matn[loc][i]);j++){
    cout<<str[j];

loop:
 if(matn[loc][i]=="input"){
  a[loc]=loc;
  b[loc] = atoi(matn[loc][0].c_str());
   memory[loc]=10;
   ram[loc]=memory[loc]*100+address;
   address--;
   cout<<'\n'<<ram[loc]<<'\n';
   loc++;
   var[address+1]=matn[loc-1][i+1];
   //location+=5;
   goto loop;
 }
 if(matn[loc][i]=="print"){
  a[loc]=loc;
  b[loc] = atoi(matn[loc][0].c_str());
   memory[loc]=11;
   for(int q=address;q<=99;q++)
    if(var[q]==matn[loc][i+1])
     ram[loc]=memory[loc]*100+q;
   cout<<'\n'<<ram[loc]<<'\n';
   loc++;
   //location+=5;
   //goto loop;
 }
  if(matn[loc][i]=="if"){
  for(int q=address;q<=99;q++){
   if(var[q]==matn[loc][i+1]){
    //locc[loc][0] = atoi(matn[loc][0].c_str());
    memory[loc]=20;
    ram[loc]=memory[loc]*100+q;
    cout<<ram[loc];
    z1=var[q];
    cout<<'\t'<<z1;
    //loc++;
    //goto loop;
   }
  }
  for(int q=address;q<=99;q++){
   if(var[q]==matn[loc][i+4]){
    //locc[loc][0] = atoi(matn[loc][0].c_str());
    memory[loc]=31;
    ram[loc]=memory[loc]*100+q;
    cout<<'\n'<<ram[loc];
    z2=var[q];
    cout<<z2;
    //loc++;
    //goto loop;
   }
  }
  ram[loc]=4200;
  //locc[loc][0] = atoi(matn[loc][0].c_str());
  cout<<'\n'<<ram[loc];
  loc++;
  goto loop;
   if(z1==z2){
    cout<<"zzzzzzzzzzz";
    //location = atoi(matn[loc][i+6].c_str());
    //goto loop;
   }
   //else
   // break;
 }
 if(matn[loc][i]=="rem"){
  cout<<"//";
  //inja bejaye 3 bayad tedade kalamte bade rem ro bezarim.
  for(int d=i+1;d<=3;d++)
   cout<<matn[loc][d]<<" ";
  //locc[loc][0] = atoi(matn[loc][0].c_str());
  loc++;
 }
 if(matn[loc][i]=="end"){
  ram[loc]=4300;
  cout<<ram[loc];
 }
 if(matn[loc][i]=="goto"){
  a[loc]=loc;
  b[loc] = atoi(matn[loc][0].c_str());
  location=atoi(matn[loc][2].c_str());
  //cout<<location;
  memory[loc]=40;
  for(int u=0;u<=loc;u++)
   if(location==b[u])
    ram[loc]=memory[loc]*100+b[u];
  cout<<'\n'<<ram[loc];
  //goto loop;

 }
 if(matn[loc][i]=="let"){
  for(int q=address;q<=99;q++){
   if(var[q]==matn[loc][i+1]){
    flag=1;
    memory[loc]=20;
    ram[loc]=memory[loc]*100+q;
   }
  }
  if(flag==0){
   address--;
   memory[loc]=20;
   ram[loc]=memory[loc]*100+address;
  }
  if(matn[loc][i+2]=="="){
       init();
  cout<<"Enter an infix expression :\n";
  scanf("%s",infix);
  infixtoprefix();
  strrev(prefix);
  printf("The resulting prefix expression is %s",prefix);

  }
 }
 return 0;
}

}




in this program at first i want to ask the user the number of lines and it works .then i want it read the sentences and mach them with words in belows
for example i write (5 input x)when my program see the input go there and do the work that i write for it but it dosnnot work!:confused:
Posted
Updated 14-Feb-11 9:43am
v3
Comments
CPallini 14-Feb-11 15:41pm    
Do you know about the wonderful tool called DEBUGGER?
mf_arian 14-Feb-11 15:47pm    
no
CPallini 15-Feb-11 3:06am    
As a developer you have to, see for instance http://en.wikipedia.org/wiki/Debugger
mf_arian 14-Feb-11 16:00pm    
my program doesnot have error
[no name] 14-Feb-11 16:02pm    
The problem is in the comparison of strings, it'll compile just fine but it won't do what you intended it to do. Have a look at my answer for details on how to make it work.

Looking at all your previous Questions (I participated in answering), I can make only one conclusion: you can succeed only if you radically change your tactics. Look at you previous Question and related discussion: experts are arguing but mostly agree that you're going nowhere.

Please, listen to a good friendly advice and do the following:

1) Stop asking questions here at CodeProject for a while: it doesn't work for you.

2) Get a good big detailed C++ manual starting from first principles; read it all.
Perhaps someone else will recommend you one or two.

3) Not just read, code simple samples on each chapter. Start from even more simple exercise than you recent question.

4) Don't assume you understand the text when it seems so to you; your questions show that you don't.

5) Don't afraid that you go slowly; if you try going faster, it will be even slower. You should code feeling piece of mind and confidence that you understand every single character you type. Gain this confidence at slow pace.

6) Better don't skip a page, read all topics. Don't be afraid to go back if you have a problem on some topics.

7) Go out of your comfort zone from time to time but be ready to go back.

8) Don't give up, make sure you reach the very end. Don't say "this is too advanced, I'll skip this step". Don't say "I'll never used it": you don't have enough experience yet to know for sure. Chances are, the skipped topic will hit you later.

Sorry if it looks way too trivial to you. I feel it is not, even if you may disagree. Just do it: for you, this is the best way I could practically advise. When you gain some ability to right and understand what are you doing — welcome back.

Are you a student? Alternatively or additionally, you could get some teacher's help. He or she would have more realistic opportunities to help you, in contrast to CodeProject help.

Good luck,
—SA
 
Share this answer
 
v2
Comments
mf_arian 14-Feb-11 16:15pm    
thank you
Sergey Alexandrovich Kryukov 14-Feb-11 16:34pm    
Thank you for understanding. This is really the way for you.
--SA
Espen Harlinn 14-Feb-11 16:27pm    
My 5, for the advice and the effort :)
Sergey Alexandrovich Kryukov 14-Feb-11 16:34pm    
Thank you, Espen.
At least OP's reaction wasn't negative :-)
--SA
CPallini 15-Feb-11 3:13am    
My 5.
From what I can see, you are working on a parser. You may find that using the Boost Spirit[^] library will simplify your work.

This is a valid parser in C++ using Boost Spirit:
C++
namespace qi = boost::spirit::qi;
typedef qi::rule<std::string::iterator> rule;

rule fact, term, expr;

fact = qi::int_ | '(' >> expr >> ')' ;
term = fact >> *(('*' >> fact) | ('/' >> fact)) ;
expr = term >> *(('+' >> term) | ('-' >> term)) ;


expr is the final parser, and thats about all you need to create a functional parser for a calculator, it doesn't get much easier than this.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Feb-11 16:16pm    
The usual 5 from me, no doubt...

Espen, did you see previous questions? My answers were quite welcome, but I'm about to give up. OP needs different approach. I tried to give radically different advice, please see.
You could contribute by recommending a book; I only know Bjarne Stroustrup book, not sure that are easy enough to read, considering OP's level.

What do you think?
--SA
Espen Harlinn 14-Feb-11 16:26pm    
I see what you mean, thanks SAKryukov!
Stefan_Lang 1-Jul-11 4:33am    
I totally agree with your approach. Unfortunately I also agree that Stroustrup is not an easy read, but still, it's a lot easier than many of the very good books such as 'Exceptional C' that are usually recommended. I'd say Stroustrups book, taken together with various online FAQs *can* be a good source for new programmers, e. g. the C++ FAQ Lite page at http://www.parashift.com/c++-faq-lite/
Espen Harlinn 1-Jul-11 9:05am    
I've found that Stanley B. Lippmans’ C++ Primer works well as an introduction to C++, and James O. Copliens’ Advanced C++ Programming Styles and Idioms (1991) taught me various ways to think outside the box.

Boost::spirit did much of the same in the context of modern C++.
Sergey Alexandrovich Kryukov 3-Jul-11 1:46am    
I also have something against what Stroustrup does, not just this book. Nevertheless, for anyone tightly working with C++ this book is nearly a must-have one.
--SA
You can't compare strings like that, you should use the compare[^] function.
The find[^] function might be useful too.
 
Share this answer
 
v2
Comments
mf_arian 14-Feb-11 15:55pm    
How ?
if i want that my program get the (30 input x) from a file what should i do?
[no name] 14-Feb-11 16:00pm    
How: if(matn[loc][i]=="input") should be changed to if(matn[loc][i].compare("input") == 0). Same thing for your other comparisons.

How to read from a file is an entirely different question. You'd read from a file stream instead of from cin, most of your code could remain unchanged.
Have a look here for information on file streams: http://www.cplusplus.com/reference/iostream/ifstream/
Philippe Mori 30-Jun-11 22:43pm    
Since he has a C++ string on the left side, it is valid to compare with a C string on the right side. But there might be a problem if lines are supposed to contains multiple words as he compare the whole line.
You should uses an inline function instead:
C++
#define operand(x)(x>='a'&&x<='z' || x>='A'&&x<='Z' || x>='0'&&x<='9')


You should initialize your variable when you declare them when possible. Also, you should indent and space your code to make it more lisible:
C++
int isp(char x)
{
  int y = (
    x == '(' ? 6 :
    x == '^' ? 4 : 
    x == '*' ? 2 :
    x == '/' ? 2 :
    x == '+' ? 1 :
    x == '-' ? 1 :
    x == ')' ? 0 : 
    -1);

  return y;
}


In fact since the function only return the value, you could replace int y = with return and remove the line with return y;. Using a variable might help if you would use a debugger as suggested as it would allows to inspect the variable.

Next if we continue until the following code:
C++
for(int j=0;j<=jomleTokalame(matn[loc][i]);j++){
  cout<<str[j];

we can see that there is a problem with this code. The main problem is that i is equal to the number of lines that were previously read. This whole loop works on empty string.

Incorrect indexes cause this part and most of the remainding code to essentially do nothing as it works on empty strings.

Also there is another issue with the following code:
C++
if(matn[loc][i]=="input")
{
   /* ... * /
}


It will only works as expected if the input contains one word per line and without extra spaces. If all words on a line should be processed or if the first word on a line should be processed, it won't works as he would compare the whole line with a word like input.
 
Share this answer
 
v4

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