|
pohcb_sonic wrote: I did use the debugger to step through...
And what did you notice about Cols as soon as "Danny" was read into stud ? Go back and re-read the questions in my first post. They give big clues as to what is wrong.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I was wondering if anyone can help me convert my Graph Code I have in Java to C++. I have going at it for a week or so now and am having trouble. Any help would be appreciated. I will post all parts of the code that is involved for this code.
Also, I will post the assignment I have and if it's easier to just ignore everything I have and do it all from scratch then I will post what is needed and said in the assignment.
import java.io.*;
public class VNode {
private VNode next;
private int vertex;
public VNode() {
next = null;
vertex = 0;
}
public VNode (int a, VNode n) {
a = 0;
next = n;
}
public VNode getNext() {
return next;
}
public int getVertex() {
return vertex;
}
public void setNext(VNode ND) {
next = ND;
}
public void setItem(int IT) {
vertex = IT;
}
}
public class Graph {
private int [] ppath;
private VNode [] Vertices;
private boolean [] visited;
private boolean setVisited = false;
private VNode a;
private VNode previous;
private VNode b;
private VNode c;
private VNode prev;
//Sets up a graph with n vertices 0,1,...,n-1
public Graph(int n) {
Vertices = new VNode[n];
for (int i = 0; i < Vertices.length; i++) {
Vertices[i] = null;
}
}
//Inserts edge(i,j). Check bounds.
public void insert(int i, int j) {
if(Vertices[j] == null) {
Vertices[j] = new VNode(i, null);
}
else if(i < Vertices[j].getVertex()) {
a = Vertices[j];
Vertices[j] = new VNode(i, a);
}
else {
a = Vertices[j];
previous = null;
while(a != null) {
if(i < a.getVertex()) {
previous.setNext(new VNode(i,a));
break;
}
previous = a;
a = a.getNext();
}
if(a == null) {
previous.setNext(new VNode(i, null));
}
}
if(Vertices[i] == null) {
Vertices[i] = new VNode(j, null);
}
else if (j < Vertices[i].getVertex()) {
b = Vertices[i];
Vertices[i] = new VNode(j, b);
}
else {
c = Vertices[i];
prev = null;
while(c != null) {
if(j < c.getVertex()) {
prev.setNext(new VNode(j, c));
break;
}
prev = c;
c = c.getNext();
}
if (c == null) {
prev.setNext(new VNode(j, null));
}
}
}
//Returns a string representing a graph using set notation.
public String toString() {
String v = "V = {";
String e = "E = {";
int vertCount = 0;
//Prints out the vertices
for (int i = 0; i < Vertices.length; i++) {
v += vertCount;
if (vertCount == Vertices.length-1) {
v += "}";
}
else {
v += ", ";
}
vertCount++;
}
System.out.println(v);
//Prints out the edges
/*int edgeCount = 0;
for (VNode n = Vertices[0]; n != null; n = n.getNext()) {
while (n.getNext() != null) {
if (edgeCount == Vertices.length-1) {
e += "}";
}
else {
e += ", ";
}
}
edgeCount++;
} */
return (v + e);
}
//Returns a string representing the graph as an adjacency list.
public String getAdjList() {
String x = "";
for (int i = 0; i < Vertices.length; i++) {
VNode adj = Vertices[i];
System.out.println(adj);
x = i + ": ";
while (adj != null) {
if (adj.getNext() == null) {
x += adj.getVertex() ;
}
else {
x += ", ";
}
adj = adj.getNext();
}
}
return x;
}
//Performs a recursive depth first search starting at k.
//Print nodes in dfs order. Use least numbered vertex scheme.
public void dfs(int k) {
if(!setVisited) {
visited = new boolean[Vertices.length];
for (int i = 0; i < visited.length; i++) {
visited[i] = false;
}
setVisited = true;
}
depthFirstSearch(k);
}
//From dfs. Actually does the searching and printing.
//The dfs just initialized the visited array.
public void depthFirstSearch(int k) {
System.out.println("K = ");
visited[k] = true;
for (VNode n = Vertices[k]; n != null; n = n.getNext()) {
if(!visited[n.getVertex()]) {
visited[n.getVertex()] = true;
System.out.print(n.getVertex());
depthFirstSearch(n.getVertex());
}
}
}
//Performs a breadth first search using a queue.
//Prints nodes in bfs order. Use least numbered vertex scheme.
public void bfs(int k) {
visited = new boolean[Vertices.length];
Queue q = new Queue();
for (int i = 0; i < Vertices.length; i++) {
visited[k] = true;
}
q.enqueue(k);
while(!q.isEmpty()) {
int w = (Integer)q.dequeue();
System.out.println("The value of w = " + w);
for (VNode n = Vertices[w]; n != null; n = n.getNext()) {
if (!visited[n.getVertex()]) {
visited[n.getVertex()] = true;
q.enqueue(n.getVertex());
}
}
}
}
//Returns the shortest path between r and s.
//If no such path exists, return null.
public int[] shortestPath(int r, int s) {
visited = new boolean[Vertices.length];
Queue q = new Queue();
ppath = new int[Vertices.length];
for (int i = 0; i < Vertices.length; i++) {
visited[r] = true;
}
//Initializes all entries in ppath to -2.
for (int a = 0; a < ppath.length; a++) {
ppath[a] = -2;
}
q.enqueue(r);
ppath[r] = -1;
while(!q.isEmpty()) {
int w = (Integer)q.dequeue();
System.out.println("The value of w = " + w);
for (VNode n = Vertices[w]; n != null; n = n.getNext()) {
if (!visited[n.getVertex()]) {
visited[n.getVertex()] = true;
ppath[n.getVertex()] = w;
q.enqueue(n.getVertex());
}
}
}
int y = 0;
int z = s;
return null;
}
}//end of Graph Class
So that is the java code.
Here is the assignment asked.
//struct Edge { int v1, int v2, int weight; }
//weight of -1 = no weight
void initGraph(int n, bool directed);
//may be a constructor - intialize a graph with n verticies
//the graph will be directed if directed is set to true
void addEdge(Edge edge)
//add an edge to the graph
int getAdjacentList(int v, List& adjList){
//puts in adjlist the vertices adjacent to v
//returns # vertices in adjlist
}
int getEdgeList(List& edgeList){
//puts in edgeList the list of edges in the graph
//for undirected graph, only 1 copy of the edge is needed
//returns # edges in edgeList
}
bool isPath(int start, int end, List& path){
//returns true if there is a path from start to end
//use Depth-First search algorithm (a private method)
//puts in path the list of vertices on the path from
// start to end (if there is a path)
}
Thanks for all and any help.
|
|
|
|
|
NxtGr8One wrote: I will post all parts of the code that is involved for this code.
[sarcastic]As opposed to just one piece at a time? Thanks.[/sarcastic]
NxtGr8One wrote: public class VNode {
private VNode next;
private int vertex;
public VNode() {
next = null;
vertex = 0;
}
Something like:
class VNode
{
private:
VNode *next;
int vertex;
public:
VNode()
{
next = NULL;
vertex = 0;
}
VNode( int a, VNode *n )
{
a = 0;
next = n;
}
VNode* getNext( void )
{
return next;
}
}; I trust you can attempt the rest.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Is it possible to have a C++ program work only if it is within a certain directory, say.. C:\Program Files\Random Folder ?
If so, any tips as to what code I would use?
|
|
|
|
|
Use _getcwd() or GetCurrentDirectory() and check the results.
PS. The comment below to use GetModuleFileName(NULL, ...) is the right answer. You can then compare the string using lstrcmpi(), the Win32 CompareString() function or something similar.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
modified on Tuesday, April 21, 2009 11:18 AM
|
|
|
|
|
#include <direct.h> // for getcwd
#include <stdlib.h>// for MAX_PATH
#include <windows.h>
#include <iostream> // for cout and cin
using namespace std;
// function to return the current working directory
// this is generally the application path
void GetCurrentPath(char* buffer)
{
getcwd(buffer, _MAX_PATH);
}
int main()
{
// _MAX_PATH is the maximum length allowed for a path
char CurrentPath[_MAX_PATH];
// use the function to get the path
GetCurrentPath(CurrentPath);
// display the path for demo purposes only
char temp[_MAX_PATH];
char temp1[_MAX_PATH]={"C:\\Program Files"};
if(CurrentPath==temp1){
cout << CurrentPath << endl;
cout << temp1 << endl;}
cout << "Press Enter to continue";
cin.getline(temp,_MAX_PATH);
return 0;
}
Thats what I have, and it outputs the current path and what temp1 holds when the if statement is gone, but no matter what I cannot get the program to check if its in the right directory. Any more tips?
|
|
|
|
|
gamefreak2291 wrote: if(CurrentPath==temp1){
You should be using some sort of string compare routine here (e.g., strcmp() ).
Have you used the debugger to step through the code?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Thanks again, opened up my book, went right to str functions found strcmp and here is the working code:
#include <direct.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
using namespace std;
void GetCurrentPath(char* buffer)
{
getcwd(buffer, _MAX_PATH);
}
int main()
{
char CurrentPath[_MAX_PATH];
GetCurrentPath(CurrentPath);
char temp[_MAX_PATH];
char temp1[_MAX_PATH]={"C:\\Program Files\\uTorrent"};
if(strcmp(CurrentPath, temp1)==0){
cout << CurrentPath << endl;
cout << temp1 << endl;}
cout << "Press Enter to continue";
cin.getline(temp,_MAX_PATH);
return 0;
}</iostream></windows.h></stdlib.h></direct.h>
|
|
|
|
|
Use GetModuleFileName().
The other response is not going to work in all cases (GetCurrentDirectory) since your program could be run from a shortcut on the desktop, and that shortcut could specify a different working directory.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
I've been trying to read up on some options of what I might be able to use. Perhaps, it can be better explained if I tell you what I would like to do.
I was hoping for a bit oriented class that not only provided storage on a per bit basis but also had the capability to provide an actual value. If you have binary data say in the form of messages in various formats, some of the binary data may use a single bit or a group of bits. In other words, a single bit is really a bit flag or boolean true or false, while a group of bits is a value. Of course, the groups of bits and bit flags may actually be intermixed say in a 16-bit word.
I've got 16-bit words (comprised of bit flags and bit field groups). It would be kind of nice to group the bits into bit fields and flags, e.g. say a 16-bit field comprised of three 4-bit fields, a 2-bit field and two single bit flags. I would like to make the contents of these bit fields somewhat human readable by decoding the binary in the same left to right order as they are stored in binary. We typically do this by using structures. This works fine for decoding but I would like to do something different.
If I could wrap these bit fields and flags into groups within an array or individual objects, I then would have the capability to name or label each object, bit field or group, and perform comparisons on similar data. In other words what may be a 4-bit field in one message may be a 3-bit field in another type of message, by writing a conversion routine between the two types, I still can make a similar comparison and not an exact comparison.
I'd have to design my own class but if I inherit from one of the previously mentioned classes, which one do you think would be best? For now, I have fixed data sizes but may need more flexible and unknown (until runtime) data sizes in the future. I'm not sure if I would want to wrap all of the bitfields or groups of bits that comprise a single message into a single object that would have a message type as a data member but I think I would like to.
|
|
|
|
|
JohnnyG wrote: if I inherit from one of the previously mentioned classes, which one do you think would be best?
JohnnyG wrote: may need more flexible and unknown (until runtime) data sizes
I'd alter each of your fields to be a method (rather than a data member) and extract it using bit-field extraction.
For instance, say we have a field 'A':
class MyBitFields
{
public:
MyBitFields(uint16_t compositeValue) : value_(compositeValue)
{
}
void SetA(unsigned int offset, unsigned int size) { aOffset_ = offset; aSize_ = size; }
unsigned int getA() { return (value_>>aOffset_) & ((1<<aSize_)-1); }
private:
uint16_t value_;
uint8_t aOffset_;
uint8_t aSize_;
};
?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
That's an interesting idea and may be exactly what I need since I am not intending to store every binary data value read from a file into the MyBitFields class permanently but rather to help me decode and perhaps to compare. I also could have a data member inside of MyBitFields and even have conversion modifiers inside of the class.
Additionally, I could also create an array of MyBitFields, one array per message
Thanks for the info!
|
|
|
|
|
I've noticed that sprintf() does some interesting things with the numbers, for example:
double : 8.38750000 sprintf: 8.387
double : 9.23750000 sprintf: 9.238
The final sprintf() was achieved with code like this:
double d = 9.23750000;
sprintf( tmp, "%0.3f", d );
In the case of 8.3875, no rounding appears to taking place. In the case of 9.2375, we get rounding! Is this normal? Is there a way to prevent this?
Bah humbug, it helps to actually examine the damn number more closely. Never mind!
Thanks to everyone who answered below!
modified on Monday, April 20, 2009 4:41 PM
|
|
|
|
|
You have got different (1 up, 1 down) rounding there, Jim - but I suspect it might be bankers rounding[^]?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi,
Both your numbers look very regular to humans, using decimal notation.
None of these numbers get represented exactly in floating-point, so it happens one of them is slightly less, the other slightly more than the values you would hope. Of course it is the internal value that gets rounded, not the one you had in mind.
Check by printing more decimals, e.g. "%0.8f"
|
|
|
|
|
Follow me into the IEEE journey (i.e. mere experimenal evidence of Luc's argument), check out the hexadecimal representation of the numbers:
40 20 c6 66 66 66 66 66
40 22 79 99 99 99 99 9a
From [^] we know that:
sign, for both number is 0 , hence they are positive (what surprise!).
Exponent is 0x402 for both numbers, i.e. 1026 , i.e. 3 , since the IEEE standard biases the exponents of 1023 .
Now the important part, the fraction:
First number has 0x0c66666666666 * 2^-52 , i.e. 0.048437499999999911182158029987477 (windows calculator is great!). following the IEEE prescription we add the implicit 1 obtaining 1.048437499999999911182158029987477 .
Second number has 0x279999999999a * 2^-52 , i.e. 0.15468750000000008881784197001252 . Adding the implicit 1 makes 1.15468750000000008881784197001252 .
Finally we get 1.048437499999999911182158029987477 * 2^3 = 8.3874999999999992894572642398998 (first number), 1.15468750000000008881784197001252 * 2^3 = 9.2375000000000007105427357601002 hence follows the 'strange printf behaviour'.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I am trying to build an application with OpenGL in a MFC dialog
can any one help me in drawing text in the openGL window..
any help would be appreciated
|
|
|
|
|
Google[^] brings this[^] up - helps?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
sorry i dint get it
|
|
|
|
|
i've seen this video dint help much
thanks anyways
|
|
|
|
|
What about the "Text Version" link?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I've just fixed a bug in one of our internal programs that was failing to release a COM interface (IXMLDOMNode returned by IXMLDOMDocument::createNote if you're interested) which resulted in a memory leak. The leak was occuring 192 times every 15 seconds the program was running and, given that it's a background task designed to run continously, you can seen the number of leaks quickly rises to a huge number (over 46000 per hour).
What I need to know is, should a Windows 2008 Terminal Server be able to handle that kind of problem and what resouce limits could the server have been hitting? The private bytes count was only up to 200MB which is nothing when the machine has 14GB. We've had some strange slow downs on that particular server (to the point where it needs to be restarted) and I'm trying to work out if this particular program could have been the cause.
|
|
|
|
|
I have integrated a close button on my Windows form written in Visual C++, and when the user clicks that button, it deletes the generated batch files from the program and then exits the application. The problem lies in the fact that the API also has that little red x up in the corner... How can I force the API to "clean" the subdirectory and delete the batch files that were generated before the program closes just concerning the built in close feature on all Windows applications? (By the close feature, I am referring to the X symbol in the upper right hand corner of the form)
|
|
|
|
|
turkmeistr1 wrote: The problem lies in the fact that the API also has that little red x up in the corner...
An API is a function or a set of functions. What do they have to do with a UI component?
turkmeistr1 wrote: How can I force the API to "clean" the subdirectory and delete the batch files that were generated before the program closes...
From where are you doing this clean-up?
Are you using MFC?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Sorry, I'm new to Visual C++ and am not familiar with terminology. Basically, I have a UI and I want it to execute the same code for this button (code shown below) when the user clicks the close button in the upper right hand corner that is built into the form.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if (File::Exists("cmd\\getmac.bat"))
File::Delete("cmd\\getmac.bat");
if (File::Exists("cmd\\showip.bat"))
File::Delete("cmd\\showip.bat");
if (File::Exists("cmd\\tracert.bat"))
File::Delete("cmd\\tracert.bat");
if (File::Exists("cmd\\reset.bat"))
File::Delete("cmd\\reset.bat");
if (File::Exists("cmd\\restart.bat"))
File::Delete("cmd\\restart.bat");
if (File::Exists("cmd\\ping.bat"))
File::Delete("cmd\\ping.bat");
Application::Exit();
}
|
|
|
|
|