Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

C++ Token Class Code Problem

$
0
0
I am currently in my second year for Game Software Development and we have been working with code in just about ever semester. I have had some experience with programming in C++ but the code that we have been working with has given me some problems, main because I haven't worked with if before and the "terms" used can just complicate things.

I'm currently working on an assignment with some signifcant code, however, the manner in which it is to be arranged in confuses me. The commented out code is the "Token Class" code given to us but I don't believe that it is correct.
This is my first post and tips/adive is greatly appreciated :)

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <fstream>
#include <string>
#define VARIABLE		0x1
#define COEF			0x2
#define MULTIPLYDIVIDE	0x4
#define ADDSUB			0x8
#define XOR				0x10
#define AND				0x20
#define OR				0x40
#define NOT				0x80
#define PARENOPEN		0x100
#define PARACLOSE		0x200
#define EQUALS			0x400
#define NOTEQUALS		0x800
#define LESSTHAN		0x1000
#define GREATERTHAN		0x2000
#define LESSTHANEQUAL	0x4000
#define GREATERTHANEQUAL	0x8000
#define STATEMENT		0X10000
#define ASSIGN			0x20000

using namespace std;

class cFile
{
private:
	ifstream myfile;
	string nextToken;

public:
	bool Open(const wchar_t* FileName)
	{
		myfile.open(FileName);
		return myfile.is_open();
	}

	void Close(const wchar_t* FileName)
	{
		myfile.close();
	}

	bool eof()
	{
		bool bReturn = myfile >> nextToken;
		return bReturn;
	}

	string GetLine()
	{
		 return nextToken;
	}
};

class cToken
{
public:
	float fValue;
	int iType;

	cToken(float Value, int Type)
	{
		fValue = Value;
		iType = Type;
	}

	cToken()
	{
		fValue = 0;
		iType = 0;
	}

	~cToken()
	{
	};

	int SizeOf()
	{
		return sizeof(cToken);
	}

	cToken & operator= (const cToken & other)
	{
		if (this != &other)
		{
			fValue = other.fValue;
			iType = other.iType;
		}
		return *this;
	}
};

cToken TokensIn[1000];
int iTokens = 0;


void main(void) //(01/25/2013)
{
	string line;
	cFile oFile;
	oFile.Open(L"list.txt");
	ifstream myfile ("list.txt");

	if (myfile.is_open())
	{
		while (myfile.good())
		{
			getline (myfile, line);
			cout << line << endl;
		}
		myfile.close();
	}

	char Line[] = "x= 3*467;" ;
	int i;
	
	for (i=0; Line[i]!=0;)
	{
			/*
                          cToken myToken, anotherToken;         // The sections commented out here
	                  myToken.fValue = 3;                   // are the "Token Code" given to us.
                          myToken.iType = VARIABLE;             // We are suppose to make them work with
                        */                                      // our code.

		if (Line[i] == ' ')
		{
			i++;
		}
		else if (Line[i] == 'x')
		{
			printf("Token x %i\n", i);
			i++;
		}
		else if (Line[i] == '=')
		{
			printf("Token =\n");
			i++;
		}
		else if (Line[i] >= '0' && Line[i] <= '9')
		{
			/*                                           //
                          anotherToken = myToken;                    //
	                  TokensIn[iTokens] = myToken;               //
			*/                                           //

			int Temp, Numb = 0;
			while (Line[i] >= '0' && Line[i] <= '9')
			{
				/*                                   //
                                  TokensIn[iTokens].fValue = 4;      //
	                          TokensIn[iTokens].iType = XOR;     //
                                */

				Temp = Line[i] - '0';
				Numb = 10*Numb + Temp;  // build the number one place at a time.
				i++;
				iTokens++;
			}
			printf("Token %i\n", Numb);
		}
		else
		{
			i++;
		}
	}
	system("pause");
}

/*

list.txt               // this section in comments here is a txt file that is included
{                      // and gets printed out where it is called

print 32
print 3 * 4
print 2 + 3 * 4
print 2 * ( 3 + 4 * 5 )
print 32/8
print 32 * 8 /  8 -30
print 3 * 4 * 5

}

*/


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>