[code]while(condition){...} // loops everything between {} while the condition evaluates to true
do{...}while(condition); // same, but it's executed at least once
for(declaration; condition; increment){...} // declaration runs once before the loop, then the condition is checked for the first time and if it's false the loop breaks.
// Increment and condition run each subsequent iteration and if condition is false the loop breaks here too.
// Any of these parts can be empty here, an empty condition is always true in a C#-for-loop.[/code]
A loop condition is anything that evaluates to a bool, [i]true[/i] for example.
how would i implement that?
[code]struct list {
int sz;
int maxsz;
int data[1];
}
#define INITSZ 5
#define INCRSZ 5
#define LISTSZ(n) ((size_t)(sizeof(struct list) + (n-1)*sizeof(int)))
[/code]
I'm reviewing my data structures in C (This is a dynamic list structure.) I had a question, what does the very last #define macro do exactly? I've always used it because that's how I was taught, never really understood what it did exactly.
This is the functions I use the macro in:
[code]struct list *init_dlist(int num) {
struct list *new;
new = malloc(LISTSZ(num));
if ( new == NULL )
return(NULL);
new->sz = 0;
new->maxsz = num;
return(new);
}
[/code]
Where in main it would be called like [code]init_dlist(INITSZ);[/code]
[QUOTE=Relaxation;40779380][code]struct list {
int sz;
int maxsz;
int data[1];
}
#define INITSZ 5
#define INCRSZ 5
#define LISTSZ(n) ((size_t)(sizeof(struct list) + (n-1)*sizeof(int)))
[/code]
I'm reviewing my data structures in C (This is a dynamic list structure.) I had a question, what does the very last #define macro do exactly? I've always used it because that's how I was taught, never really understood what it did exactly.[/QUOTE]
That array with one element at the end of the struct is just something that makes it easier to access the data in the list using array operations. This means that, to store n elements, firstly you need to allocate memory the size of the data structure. But that's only one element there, missing n-1 elements. You'll then need memory for n-1 additional ints. What the macro does is calculate the size of the struct with room for n elements.
I need to figure out how to split a certain string into multiple parts with Java where if a user enters...
[quote]CLIENT>>> FETCH loremipsum.txt[/quote]
...it notices CLIENT>>> FETCH as a command to fetch the contents of a file and fetch the contents of loremipsum.txt. Do I need to begin the torturous lessons of regular expressions or what?
[QUOTE=xKarma;40779197]how would i implement that?[/QUOTE]
Choose one of the loops and put the part you want to loop where ... is :eng101:
Inside the loop you can also use [i]continue;[/i] to go directly to the next iteration and [i]break;[/i] to exit the loop. (This affects only the innermost iteration in C#.)
[sp]There's also goto but I have a feeling you should learn to use the usual loops first.[/sp]
So I'm working on a new system to manage my GMod servers, and I'm starting with the database parts. Like, I have a MySQL database that all my servers connect to, but the problem is with the current system I use (made it a few months back), it doesn't have a local database so if no connection to the MySQL database is made, then all the ranks, logs and such just aren't accessible.
So I was thinking of having each server have a "mirror image" of the MySQL database in their local scope, using SQLite. So the server would copy the entire global db into it's local db each time it's started and in case of connection failure it'd rely on that snapshot of the database temporarily until connection can be regained.
Wondering if there are any fundamental flaws with a system like this? The database itself is currently only a few mb with around 10k players logged, so it's relatively small. And I've never used SQLite before, are there any considerations that I have to do and what would be the best way to actually insert the global db into SQLite?
[QUOTE=Stonecycle;40779716]I need to figure out how to split a certain string into multiple parts with Java where if a user enters...
...it notices CLIENT>>> FETCH as a command to fetch the contents of a file and fetch the contents of loremipsum.txt. Do I need to begin the torturous lessons of regular expressions or what?[/QUOTE]
You can use regex, but for something as simple as this you can just parse the string part-by-part with few, if any, disadvantages.
[code]String command = "CLIENT>>> FETCH loremipsum.txt";
String[] commandParts = command.split(" ");
assert(commandParts.length == 0);
if(commandParts[0] == "CLIENT>>>")
{
if(commandParts.length < 2)
{
warning("Missing command for " + commandParts[0]);
return;
}
//can Java switch on Strings yet?
switch(commandParts[1])
{
case "FETCH":
if(commandParts.length != 3)
{
warning("Incorrect amount of parameters for CLIENT>>> FETCH file");
return;
}
fetch(commandParts[2]);
break;
default:
warning("Unknown command " + commandParts[1]);
break;
}
}
else if(commandParts[0].length() > 0)
{
warning("Unknown command-prefix " + commandParts[0]);
}[/code]
This is rather simplistic, for a better designed extensibily you'd probably use some sort of Map<String, CommandInterface> with the CommandInterface having a function to parse the rest of the string. They can register themselves via a static initialization block.
Someone, I believe in the WAYWO, recently posted a snipped containing code for similar functionality that seemed to make neat use of annotations for various stuff. Might have been C# though.
[editline]25th May 2013[/editline]
There: [url]http://facepunch.com/showthread.php?t=1260790&p=40765075&viewfull=1#post40765075[/url]
[QUOTE=ZeekyHBomb;40780544]You can use regex, but for something as simple as this you can just parse the string part-by-part with few, if any, disadvantages.
This is rather simplistic, for a better designed extensibily you'd probably use some sort of Map<String, CommandInterface> with the CommandInterface having a function to parse the rest of the string. They can register themselves via a static initialization block.
Someone, I believe in the WAYWO, recently posted a snipped containing code for similar functionality that seemed to make neat use of annotations for various stuff. Might have been C# though.
[editline]25th May 2013[/editline]
There: [url]http://facepunch.com/showthread.php?t=1260790&p=40765075&viewfull=1#post40765075[/url][/QUOTE]
The snippet wasn't really necessary but the guidance to split() the command into a String[] was very useful. And yes, Java can use switches on Strings. I can sense it's going to be very useful for the rest of the summer semester.
[QUOTE=ZeekyHBomb;40780544][...]
Someone, I believe in the WAYWO, recently posted a snipped containing code for similar functionality that seemed to make neat use of annotations for various stuff. Might have been C# though.
[editline]25th May 2013[/editline]
There: [url]http://facepunch.com/showthread.php?t=1260790&p=40765075&viewfull=1#post40765075[/url][/QUOTE]
I don't think that works in Java, maybe it's possible with anonymous classes.
It's missing the compilation feature though, so it's likely inefficient to dynamically bind the parsing methods.
I may fix the missing parts and upload the project eventually, but it's not difficult to make once you are used to reflection.
I have never encountered something like this before, but hopefully some of you have.
My problem is that I have a list with commands that look like this:
opcode operand operand
so for example
CPY IO R1
R1, IO and other registers can be replaced with aliases like this:
$a: R1
$b: IO
CPY $b $a
Anyway, in my parser I have successfully put the aliases and their respective values in a dictionary. Now I want to replace all aliases with the actual values.. how the hell do I do that in C#?
[QUOTE=ArgvCompany;40779435]That array with one element at the end of the struct is just something that makes it easier to access the data in the list using array operations. This means that, to store n elements, firstly you need to allocate memory the size of the data structure. But that's only one element there, missing n-1 elements. You'll then need memory for n-1 additional ints. What the macro does is calculate the size of the struct with room for n elements.[/QUOTE]
Adding to this, it's not uncommon to define something like
[cpp]#ifndef __GNUC__
#define MINIMUM_ARRAY (1)
#else
#define MINIMUM_ARRAY (0)
#endif[/cpp]
Because compilers differ in what's the smallest array they allow. Prior to C99 it was illegal but not uncommon for compilers to support zero-length arrays.
So you would then write that LISTSZ macro as
[cpp]#define LISTSZ(n) (sizeof(struct list) + (n - MINIMUM_ARRAY) * sizeof(int))[/cpp]
In C99 you could just do
[cpp]struct list {
size_t size;
int data[];
};[/cpp]
[editline]26th May 2013[/editline]
Of course it makes little difference unless you actually have a need for potentially zero-length arrays.
[QUOTE=FlashStock;40782688]I have never encountered something like this before, but hopefully some of you have.
My problem is that I have a list with commands that look like this:
opcode operand operand
so for example
CPY IO R1
R1, IO and other registers can be replaced with aliases like this:
$a: R1
$b: IO
CPY $b $a
Anyway, in my parser I have successfully put the aliases and their respective values in a dictionary. Now I want to replace all aliases with the actual values.. how the hell do I do that in C#?[/QUOTE]
Loop the commands, then
if (command.Operand1[0] = '$') command.Operand1 = aliases[command.Operand1];
if (command.Operand2[0] = '$') command.Operand2 = aliases[command.Operand2];
unless your commands are immutable, then you have to recreate them.
So I'm following this guy's tutorial with LOVE2D:
[media]http://www.youtube.com/watch?v=E1MGLTdafu0[/media]
But when I get to the end with trying to check if it works, I get this error:
[t]http://puu.sh/31eSd.png[/t]
[code]function love.load()
require("entities")
ents.Startup()
love.graphics.setBackgroundColor( 102, 150, 255 )
xCloud = 0
imageCloud = love.graphics.newImage("textures/cloud.png")
imageEnemy_1 = love.graphics.newImage( "textures/enemy_1.png" )
imageEnemy_2 = love.graphics.newImage( "textures/enemy_2.png" )
end
function love.draw()
local x = love.mouse.getX( )
local y = love.mouse.getY( )
love.graphics.setColor( 128, 128, 128, 255 )
love.graphics.rectangle( "fill", 0, 540, 1280, 720 )
love.graphics.setColor( 255, 255, 255, 200 )
love.graphics.draw( imageCloud, 0 + xCloud, 180, 0, 1, 1, 0, 0 )
love.graphics.setColor( 255, 0, 64, 180 )
love.graphics.circle( "fill", x, y - 5, 20 )
end
function love.update(dt)
-- print("Delta Time: " .. dt) [Same as draw, but it updates something.]
xCloud = xCloud + 72*dt
if xCloud >= (720 * 2) then
print("Loop!")
xCloud = (0 - 360)
end
end
function love.focus(bool)
end
function love.keypressed( key, unicode )
-- print("Key Pressed: " .. key .. " " .. unicode)
end
function love.keyreleased( key, unicode )
end
function love.mousepressed( x, y, button )
-- print("Location (Pressed): " .. x .. " (X), " .. y .. " (Y), with " .. button)
end
function love.mousereleased( x, y, button )
-- print("Location (Released): " .. x .. " (X), " .. y .. " (Y)")
end
function love.quit()
end[/code]
The -- comments are there for reference.
I do [b]not[/b] see any unexpected symbols after ')' on Line 2. I'm using Notepad++. Any idea how to fix this?
What do you have in entities.lua? The error should be on the first line of it - perhaps [CODE]ents = {)[/CODE] instead of [CODE]ents = {}[/CODE]
[QUOTE=evil-tedoz;40770876]Look at this, a tutorial about what you're searching for. [URL="http://www.codeproject.com/Articles/18062/Detecting-USB-Drive-Removal-in-a-C-Program"]Link.[/URL][/QUOTE]
Got it working, thanks a bunch!
[QUOTE=Metroid48;40785687]What do you have in entities.lua? The error should be on the first line of it - perhaps [CODE]ents = {)[/CODE] instead of [CODE]ents = {}[/CODE][/QUOTE]
[code]ents = ()
ents.objects = ()
ents.objpatch = "entities/"
local register = ()
local id = 0
function ents.StartUp()
end
function ents.Create(name, x, y)
if not x then
x = 0
end
if not y then
y = 0
end
if register[name] then
id = id + 1
local ent = register[name]()
ent:load()
ent:setPos(x, y)
ent.id = id
ents.objects[#ents.objects + 1] = ent
return ents.objects[#ents.objects]
else
print ("Error: Entity " .. name .. "does not exist. Wanker.")
return false;
end
end[/code]
Tried using what you posted on the first line, still the same problem.
[sup]Don't mind the print, the Sniper tried helping.[/sup]
[QUOTE=AbeX300;40789210][code]ents = ()
ents.objects = ()
ents.objpatch = "entities/"
local register = ()
[/code]
[/QUOTE]
Use {} on lines 1, 2, and 4. The font in the video is a bit hard to make out, but {} is the syntax for making a new table.
[QUOTE=Metroid48;40790383]Use {} on lines 1, 2, and 4. The font in the video is a bit hard to make out, but {} is the syntax for making a new table.[/QUOTE]
Works now! Thanks for your help!
What code do I need to count in base4? Example:
[code]
000
001
002
003
010
011
012
013
020
...
[/code]
(I only need three digits at a time)
How does a loop that produces this look?
I don't really understand the issue. Why do you need to do this? In which language?
[QUOTE=esalaka;40794034]I don't really understand the issue. Why do you need to do this? In which language?[/QUOTE]
I'm trying to generate urls for images used in a map that uses Microsoft Bings Map System. Bings Map defines tile positions on the map using quadkeys, hence the need for getting sequences of numbers in base4. see: [url]http://msdn.microsoft.com/en-us/library/bb259689.aspx[/url]
[QUOTE=Armandur;40794276]I'm trying to generate urls for images used in a map that uses Microsoft Bings Map System. Bings Map defines tile positions on the map using quadkeys, hence the need for getting sequences of numbers in base4. see: [url]http://msdn.microsoft.com/en-us/library/bb259689.aspx[/url][/QUOTE]
From your link:
[cpp] /// <summary>
/// Converts tile XY coordinates into a QuadKey at a specified level of detail.
/// </summary>
/// <param name="tileX">Tile X coordinate.</param>
/// <param name="tileY">Tile Y coordinate.</param>
/// <param name="levelOfDetail">Level of detail, from 1 (lowest detail)
/// to 23 (highest detail).</param>
/// <returns>A string containing the QuadKey.</returns>
public static string TileXYToQuadKey(int tileX, int tileY, int levelOfDetail)
{
StringBuilder quadKey = new StringBuilder();
for (int i = levelOfDetail; i > 0; i--)
{
char digit = '0';
int mask = 1 << (i - 1);
if ((tileX & mask) != 0)
{
digit++;
}
if ((tileY & mask) != 0)
{
digit++;
digit++;
}
quadKey.Append(digit);
}
return quadKey.ToString();
} [/cpp]
-Snip-..
Fixed it, stupid mistake.
[QUOTE=Armandur;40794276]I'm trying to generate urls for images used in a map that uses Microsoft Bings Map System. Bings Map defines tile positions on the map using quadkeys, hence the need for getting sequences of numbers in base4. see: [url]http://msdn.microsoft.com/en-us/library/bb259689.aspx[/url][/QUOTE]
I'm not sure which programming language you wanted it in but it's not that hard so, in C# it's this.
[code]
int iBase = 4;
int[] numbers = new int[3] { 0, 0, 0 };
public Form1()
{
InitializeComponent();
for (int i = 0; i < 100; i++)
{
if (!addOne())
{
break;
}
}
}
private bool addOne()
{
numbers[numbers.Length - 1]++;
for (int i = numbers.Length - 1; i >= 0; --i)
{
if (numbers[i] == iBase)
{
numbers[i] = 0;
if (i != 0)
{
numbers[i - 1]++;
}
else
{
//The number became too big, arrghh.
MessageBox.Show("Went over the maximum, oops.");
return false;
}
}
}
//Console.WriteLine(" " + numbers[0] + numbers[1] + numbers[2]);
return true;
}
[/code]
Which outputs:
[url]https://dl.dropboxusercontent.com/u/22510918/output.txt[/url]
(You can change iBase to anything under 10 and above 1 and it'll work. Above 10 requires some more math and matching letters and such.)
Can anyone tell me why this is crashing my program? C
[code]int load_hit_list(dyn_array *hit_list)
{
int i, result = 0;
FILE *input;
input = fopen("hits.txt", "r");
if (input == NULL)
{printf("Loading Failed\n"); return 0;}
printf("test1\n");
fscanf(input, " %d", hit_list->no_of_hits);
printf("test2\n");
printf("%d\n", hit_list->no_of_hits);
hit_list->hits = (target *) realloc(hit_list->hits,sizeof(target)*hit_list->no_of_hits);
printf("test\n");
for (int i = 0; i < hit_list->no_of_hits; ++i)
{
fscanf(input," %[^\n]", hit_list->hits[i].name.str);
fscanf(input, " %d", hit_list->hits[i].bounty);
fscanf(input, " %lf", hit_list->hits[i].difficulty);
result += 1;
}
return result;
fclose(input);
}[/code]
Working in Java on Windows 7 64-bit, using the Slick2D library.
After running my game in the background for a while it sometimes crashes back to Eclipse with this long error:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6a4c1fd8, pid=5204, tid=5672
It doesn't crash like usual, taking me back to my code with a line reference. Instead this error appears in the console (along with a load of other information) and a log is dumped in the workspace, with no indication of where in my code the problem is. I'm having trouble reproducing it, too. Here's a few lines from the log which looked like they might be relevant:
[code]
# Problematic frame:
# C [msvcr100.dll+0x1fd8]
...
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
...
Current thread (0x002db000): JavaThread "main" [_thread_in_native, id=912, stack(0x01dc0000,0x01e10000)]
...
siginfo: ExceptionCode=0xc0000005, writing address 0x00000000
...
Registers:
EAX=0x05000500, EBX=0x01e0f08c, ECX=0x00000001, EDX=0x00000002
ESP=0x01e0f008, EBP=0x01e0f010, ESI=0x1c2b323a, EDI=0x00000000
EIP=0x683f1fd8, EFLAGS=0x00010297
...
Stack: [0x01dc0000,0x01e10000], sp=0x01e0f008, free space=316k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [msvcr100.dll+0x1fd8]
C [fontmanager.dll+0xb464]
C [fontmanager.dll+0x1086a]
C [fontmanager.dll+0x1032a]
C [fontmanager.dll+0x112b6]
[/code]
Any help would be much appreciated.
Incoming dreadful c++ template question:
I can't figure out why I can't use enable_if on a copy constructor, and possibly other contructors but I haven't tried yet.
[code]template <typename type>
class Class
{
public:
template <typename ttype = type,
typename std::enable_if<std::is_trivial<ttype>::value, int>::type = 0>
class(const class<type> &Class)
{
//code
}
template <typename ttype = type,
typename std::enable_if<!std::is_trivial<ttype>::value, int>::type = 0>
class(const class<type> &Class)
{
//code
}
};[/code]
The error is basically that the copy constructor is implicitly deleted (the class also has a move constructor), I am guessing that means that neither copy constructor is found. It even doesn't work if I replace it with one enable_if<true>. I've tried some other ways of doing this that i've found and get the same error. This is the same way i've successfully used enable_if on regular member functions of this class:
[cpp]template <typename ttype = type,
typename std::enable_if<std::is_trivial<ttype>::value, int>::type = 0>
void function();
template <typename ttype = type,
typename std::enable_if<!std::is_trivial<ttype>::value, int>::type = 0>
void function();[/cpp]
I am currently using mingw64 gcc 4.8.
Anybody know what's up?
Your ctor and parameter types are class instead of Class, but I'm going to assume that's done correctly in your actual code.
After I change this it works fine for me though; here's the complete code I compiled with clang 3.2 and gcc 4.8:
[code]#include <type_traits>
template <typename type>
class Class
{
public:
Class(){}
template <typename ttype = type,
typename std::enable_if<std::is_trivial<ttype>::value, int>::type = 0>
Class(const Class<type> &Class)
{
//code
}
template <typename ttype = type,
typename std::enable_if<!std::is_trivial<ttype>::value, int>::type = 0>
Class(const Class<type> &Class)
{
//code
}
};
int main()
{
Class<int> a, b(a);
}[/code]
Works for me on mingw64 gcc 4.8
Sorry, you need to Log In to post a reply to this thread.