我的程序有问题.请帮忙! [英] I have a problem with my program. Please help!

查看:57
本文介绍了我的程序有问题.请帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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;
}

}





首先,在这个程序中,我想问用户行数,它可以工作.然后,我想让它阅读句子,并在下面用单词来处理它们
例如我写(5输入x)当我的程序看到输入去那里做我为它写的工作但是它却不起作用!:confused:





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:

推荐答案

查看您以前所有的问题(我参与了回答),我只能得出一个结论:只有从根本上改变策略,您才能成功.看看您以前的问题和相关讨论:专家们在争论,但大多数人都认为您无路可走.

请听取良好的友好建议并执行以下操作:

1)暂时停止在CodeProject上提问:它对您不起作用.

2)从基本原理开始,获取一本很好的详细C ++手册;阅读全部.
也许其他人会推荐您一两个.

3)不仅要阅读,还要在每章中编写简单的示例代码.从比您最近的问题更简单的练习开始.

4)不要以为您理解了这段文字;您的问题表明您没有.

5)不要害怕你走得慢;如果尝试更快,它将变得更慢.您应该编写代码,使自己感到内心和自信,以了解您键入的每个字符.以缓慢的速度获得这种信心.

6)最好不要跳过页面,阅读所有主题.如果您对某些主题有疑问,请不要害怕返回.

7)时不时走出舒适区,但准备回去.

8)不要放弃,确保你到达终点.不要说这太高级了,我将跳过此步骤".不要说我永远不会用它":您还没有足够的经验来确定.跳过的主题很可能会在稍后打到您.

抱歉,对您来说太琐碎了.我觉得不是,即使您可能不同意.做到这一点:对您来说,这是我可以实际建议的最佳方法.当您掌握正确的能力并了解自己在做什么时,欢迎回来.

你是学生吗?替代地或另外地,您可以得到一些老师的帮助.与CodeProject帮助相比,他(她)将有更多现实的机会来帮助您.

祝你好运,
—SA
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


据我所知,您正在使用解析器.您可能会发现使用 Boost Spirit [ ^ ]库将简化您的工作.

这是使用Boost Spirit在C ++中有效的解析器:
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:
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是最终的解析器,这就是为计算器创建功能解析器所需的全部内容,没有比这容易的多了.

问候
Espen Harlinn



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


您不能像这样比较字符串,应该使用^ ]功能.
查找 [
You can''t compare strings like that, you should use the compare[^] function.
The find[^] function might be useful too.


这篇关于我的程序有问题.请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆