|
I've done a simple version of eval for you. You can modify it to suite your needs.
int eval(char *s)
{
int res=0;
char *a;
if((a=strchr(s,'+'))==NULL)
if((a=strchr(s,'-'))==NULL)
if((a=strchr(s,'*'))==NULL)
if((a=strchr(s,'/'))==NULL)
if(a==NULL)
return 0;
char *b=a+1;
int v2=atoi(b);
switch (a[0])
{
case '+' :
res=atoi(s)+v2;
break;
case '-':
res=atoi(s)-v2;
break;
case '*':
res=atoi(s)*v2;
break;
case '/':
res=atoi(s)/v2;
break;
}
return res;
}
Regards
Nish
Sonork ID 100.9786 voidmain
|
|
|
|
|
there will be three source files: main,eval and ch2int.
OUCH!!!
If your teacher said that, then you are right as to his being a moron. He prolly took it out of some old C text book that was written by a similar moronic author.
Three source files for a program to evaluate a simple mathematic expression!! and what's more! Even their names are specified. One of those names is ch2int which prolly means write a source file to convert char to int [which is auto-done by C-compilers as far as I know]
Like Christian said, GO COMPLAIN ABOUT THAT IDIOT
Nish
Sonork ID 100.9786 voidmain
|
|
|
|
|
the file is a console app.
|
|
|
|
|
I find myself needing to read/write all of the class member variables to and from an ini file. The ini file contains data for other classes as well.
I'm looking for a cool way do this. I need a member variable of whatever type, a name for that variable in the ini file, and read/write routines that work off of some kind of list structure. Any ideas?
Todd Smith
|
|
|
|
|
How about Get/WritePrivateProfileStruct()?
---
It may be that your sole purpose in life is simply to serve as a warning to others.
|
|
|
|
|
That doesn't really work for what I need.
So far what I've come up with is to use a #define and create a .inc file which I will include in a .h and a .cpp file. The .inc will do different things when it's in the .h vs. in the .cpp. When I include the .inc in a .h file it will declare the member variables. When I include the .inc in a .cpp file it will create code to add the member variables along with a name to a map or something. I haven't worked out the details yet.
Todd Smith
|
|
|
|
|
If you're using MFC and your object is derived from CObject you can use CArchive with CMemFile, then save the contents of CMemFile to .ini using CWinApp::[Get|Set]ProfileBinary.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
What you're after is called reflection, the ability to access in runtime to the structure of a class. Efforts devoted to achieve some sort of reflection in C++ are usually very far-fetched (look for OpenC++ and Meta Object Protocol (MOP), for instance). Nevertheless, it is possible that some kind of library exists fith more modest objectives that suits your needs.
Good luck
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
How can I drag icon in CListCtrl ? Just like Windows Desktop!
|
|
|
|
|
See MFCDRAG and maybe LISTHDR samples in MSDN. Maybe it will help.
|
|
|
|
|
Does anyone know how to detect if a item was checked or unchecked in a CTreeCtrl? I took a look at the CTreeCtrl and it seems that there are no messages for this kind of thing.
|
|
|
|
|
Sorry - no special notification for that. You'll have to handle WM_LBUTTONDOWN and use CTreelCtrl::HitTest. If returned flags have TVHT_ONITEMSTATEICON bit set, it was a click on checkbox.
Tomasz Sowinski -- http://www.shooltz.com
|
|
|
|
|
I have an assignment about writing 2 dimensional arrays class Array2D, but I don't know the syntax for using int *iptr and int **array2D to dynamically allocate memory. The following is my program. The class interface and main driver given as it is. I couldn't really find the error on my class Array2D implementation part. Please help me out.
#include <iostream>
using namespace std;
#include <iomanip>
#include <cstdlib>
#include <cassert>
#include "PA_02.h"
//Output the array in row & column format (table format)
//Overloaded output operator for class Array2D
ostream &operator<<( ostream &out, const Array2D &obj )
{
for ( int i = 0; i < obj.rows; i++ )
{
for ( int j = 0; j < obj.cols; j++ )
out <<setw(10)<< obj.array2d[i][i];
="" out="" <<="" "\n"="" <<endl;
="" }
=""
="" return="" out;
="" input="" the="" entire="" array="" contents
="" overloaded="" operator="" for="" class="" array2d
="" istream="" &operator="">>( istream &in, Array2D &obj )
{
cout<<"Enter array values, one row at a time:\n"<<endl;
for="" (="" int="" i="0;" <="" obj.rows;="" i++="" )
="" {
="" cout<<"enter="" column="" values="" row="" "<<="" <<":\n"<<endl;
="" j="0;" obj.cols;="" j++="" cout="" <<"\nenter="" value="" ("="" <<="" <<"="" ,"="" <<")="" :";=""
="" in="">> obj.array2D[i][j];;
}
}
cout<<"\n\nInput complete\n"<<endl;
return="" in;
="" }
="" initialization="" and="" termination="" methods
=""
="" dynamically="" create="" an="" initialized="" array,="" contents="" will="" be="" to="" value
="" array2d::array2d(="" int="" r,="" c,="" value="" ):rows(r),="" cols(c)
="" {
="" i,="" j;="" iptr="new" int[="" r="" ];
="" assert(="" !="0);
" array2d="&iptr;
" for(="" i="0;" i<r;="" i++="" )
="" j="0;" j<c;="" j++="" array2d[i]="new" int[j];
="" );
="" }
="" }=""
="" initialize="" array="" equals="" 0
="" for="" (="" <="" r;="" c;="" array2d[i][j]="value;
" }
="" the="" copy="" constructor
="" const="" &rhs="" )="" :="" rows(="" rhs.rows),=""
cols(rhs.cols)
="" j;
="" rows="" rows;="" j<cols;="" rhs="" into="" object
="" cols;="" assignment="" operator="" (performs="" assignment)
="" &array2d::operator="(" j,="" i;
="" if="" check="" self="" assignment
="" delete="" []="" iptr;
="" cols="" array2d;
="" i<rows;="" *this;
="" clean="" up="" (e.g.="" allocated="" memory)
="" array2d::~array2d(="" void="" cout<<"\ndestructor="" called"<<endl;
="" =set="" get="" methods.="" explicitly="" whether="" index="" is="" within="" bounds="" of="" array
="" set="" item="" in="" at="" location(="" row,="" col="" ).
="" returns="" false="" out-of-bounds,="" else="" true
="" bool="" array2d::set(="" &new_item,="" if(="" row="">0 && col>0 )
{
for( int i=0; i<row; i++="" )
="" {
="" for(="" int="" j="0;" j<col;="" j++="" array2d[i][j]="new_item;
" }
="" return="" true;
="" else
="" false;
=""
="" }
="" get="" an="" item="" in="" the="" array="" at="" location="" (="" row,="" col="" ).
="" returns="" false="" if="" index="" is="" out-of-bounds,="" else="" true.
="" bool="" array2d::get(="" &item,="" )="" const
="" if(="" row="">0 && row<=rows )
{
if( col>0 && col<=cols )
{
for( int i=0; i<row; i++="" )
="" {
="" for(="" int="" j="0;" j<col;="" j++="" array2d[i][j]="item;
"
="" }
="" return="" true;
="" else
="" false;
="" }
="" returns="" the="" length="" of="" array
="" array2d::size(="" void="" )="" const
="" (="" sizeof(="" array2d[rows][cols]="" );
="" number="" rows
="" array2d::getrow(="" rows;
="" columns
="" array2d::getcol(="" cols;
="" compare="" this="" array="" with="" <rhs=""> for equality. Two arrays are equal if
//their
//rows, and cols are equal and all the elements are equal
bool Array2D::operator==( const Array2D &rhs ) const
{
if ( rows != rhs.rows && cols != rhs.cols )
return false; //arrays of different size
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
if ( array2D[i][j] != rhs.array2D[i][j] )
return false; //arrays are not equal
}
return true;
}
//Compare this array with <rhs> for inequality
bool Array2D::operator!=( const Array2D &rhs ) const
{
if ( rows == rhs.rows && cols == rhs.cols )
return false;
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ )
if ( array2D[i][j] == rhs.array2D[i][j] )
return false;
}
return true;
}
//Overload () to allow use of ( i, j ) notation ( leftvalue )
int &Array2D::operator()( int i, int j )
{
if ( i >= rows || j >= cols )
cout<<"out of bounds"<<endl;
="" return="" array2d[i][j];
="" }
="" overload="" ()="" to="" allow="" use="" of="" (="" i,="" j="" )="" notation="" rightvalue="" )
="" const="" int="" &array2d::operator()(="" const
="" {
="" if="" i="">= rows || j >= cols )
cout<<"out of bounds"<<endl;
return="" array2d[i][j];
="" }
="" utility="" functions
="" returns="" true="" if="" <index=""> is within range, else false
bool Array2D::in_range( int r, int c ) const
{
if ( r < 0 || r >= rows && c < 0 || c >= cols )
return false;
else
return true;
}
//Perform dynamic memory allocation
void Array2D::allocate( void )
{
rows = ( rows>0 ? rows );
cols = ( cols>0 ? cols );
iptr = new int[ rows ];
assert( iptr != 0 );
array2D = &iptr;
for( int i=0; i<rows; i++="" )
="" {
="" for(="" int="" j="0;" j<cols;="" j++="" array2d[i]="new" int[="" ];
="" assert(="" !="0" );
="" }
=""
="" }
--------------------------------------------------------------------------------
#include="" <iostream="">#include <cassert>
#include "PA_02.h"
#include "a2D.cpp"
using namespace std;
int main()
{
Array2D a1( 2, 3, 5 );
Array2D a2 = a1;
Array2D a3, a5( 4, 2, 3 );
Array2D a4( a3 );
//Test the << operator
cout<<"\nPrinting a1 ...\n"<<a1;
cout<<"\nprinting="" a2="" ...\n"<<a2;
="" a3="" ...\n"<<a3;
="" a4="" ...\n"<<a4;
="" a5="" ...\n"<<a5;
="" test="" the="">> operator
cin>>a3;
cout<<"\nPrinting a3 ...\n"<
|
|
|
|
|
You could save a lot of time by replacing the element of your matrix in a vector : let say you have a mxn matrix you just allocate a vector of size mn.
you can access the elements easily e.g. array[i*m+j]
Easier, faster, cleaner.
ps: by the way you have a bug in your << operator. [i][i] should be [i][j]
|
|
|
|
|
I am running a multiproject application and in one of the projects, there is a Process created using CreateProcess. This process uses an executable created in another project of this same application to perform certain tasks.
I am under the impression that if I add "DEBUG_PROCESS" as one of the options under CreateProcess, I should be able to debug into the Process (i.e. Go F11 and right into the actual file).
However, what I seem to get is a program that sort of hangs when it gets to WaitForSingleObject statement.
Any ideas of how to debug into a Process? What am I doing wrong?
thanks
|
|
|
|
|
Hello, the codegurus around the world.;)
I have the same problem to debug some project.
In this case, I wrote the following.
#ifndef _DEBUG
WaitForSingleObject (...);
#endif
This approach will not solve the basic problem, but
we can debug the different EXE created by CreateProcess.
So, in the debug mode, we can teminate the main exe process by debug menu,
otherwise, we will get some messy situation since we never call WaitForSingleObject.
Have a nice day!
-Masaaki Onishi-
|
|
|
|
|
I am using MFC for a XP program, and have various controls on the dialog box.
When I add my .manifest file, all the controls are displayed as they should be (flat, hot tracking etc), but if I add the style "ES_PASSWORD" to an Edit box, I don't get the '*' character or the XP black circle, instead I get a small black rectangle.
Help.
Joe.
|
|
|
|
|
Hello, the codegurus around the world.;)
I think that this is normal since MFST seems to change the password
word from * to the black circle on XP.
Windows application depends on the core DLL of Windows, so
as long as we use DLL of XP, I think that we can't change this.
Have a nice day!
-Masaaki Onishi-
|
|
|
|
|
A while back I posted this question here but no one replied. Let me try again...
I have a console app which prompts the user for a user id and password. The problem is that I don't know how to prevent the password from being displayed as the user types it. I'm using good old cin to retrieve it, like this:
cout << "Password: ";
char szPassword[80];
cin.getline(szPassword, sizeof(szPassword));
Is there a way to hide what the user types or perhaps mask it somehow?
Thanks,
Alvaro
|
|
|
|
|
char c = 0;
while (c!=' ')
{
if(kbhit())
{
c=getch();
printf("*");
}
}
-c
POKE 808,234
|
|
|
|
|
Thanks for the quick answer Chris. Now, to throw a monkey wrench into the solution: it has to compile and run on both Windows and UNIX. Is there an equivalent to kbhit on UNIX?
Thanks again,
Alvaro
|
|
|
|
|
Alvaro
In windows you can do this :-
while((c=_getch())!='\n')
{
printf("*");
s[i++]=c;
}
s[i]='\0';
For Linux/Unix you'll need to write your own _getch() function.
You can do this with not too much difficulty
(1)First open /dev/tty
(2)use ioctl to change keyboard device parameters [there is some flag to prevent echoing]
(3)Then read one character
(4)use ioctl to set echo back
(5)Now close /dev/tty
I am sorry I couldnt actually put the code here, but if you have access to a unix box you can lookup ioctl and /dev/tty in the man pages and figure out a way.
Regards
Nish
Sonork ID 100.9786 voidmain
|
|
|
|
|
Unix/Linux does haven't a getch()!
I'd have thought with all those open source programmers they could get round to doing one of the most basic input functions. I'd have though getch() would have been part of the standard c library.
Michael
|
|
|
|
|
Thats cause in Linux they dont take passwords from the keyboard. They put it in a file and put the file in /etc and chown it to root and chmod it to 000
Nish
Sonork ID 100.9786 voidmain
|
|
|
|
|
Nish,
Thanks for your reply. I got it working on Windows, which by the way requires me to look for '\r' instead of '\n'. Now I'm trying UNIX but I'm lost:
(1)First open /dev/tty how? with fopen?
(2)use ioctl to change keyboard device parameters [there is some flag to prevent echoing] could not find such flag.
(3)Then read one character how? with fgetc?
(4)use ioctl to set echo back again, I don't know how
(5)Now close /dev/tty I imagine with fclose, but then what, repeat for the next character?
Thanks for your help,
Alvaro
|
|
|
|