[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]

/prog/ - Programming

Programming board

Catalog

8chan Bitcoin address: 1NpQaXqmCBji6gfX8UgaQEmEstvVY7U32C
The next generation of Infinity is here (discussion) (contribute)
A message from @CodeMonkeyZ, 2ch lead developer: "How Hiroyuki Nishimura will sell 4chan data"
Name
Email
Subject
Comment *
File
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Embed
(replaces files and can be used instead)
Options
Password (For file and post deletion.)

Allowed file types:jpg, jpeg, gif, png, webm, mp4, pdf
Max filesize is 8 MB.
Max image dimensions are 10000 x 10000.
You may upload 1 per post.


File: 1436515746826.jpg (48.73 KB, 659x633, 659:633, 1433507912385.jpg)

d5c93c No.2801

There is no sticky here so I'll make a questions thread.

I made a program (for an excercise in a book) that draws an histogram of the length of words in a given text.

It works absolutely fine except for one little thing.

here's the full source code: http://pastebin.com/eFjEGA79

here's the relevant part:


while((c=getchar())!=EOF){
if (c!=' '&&c!='\t'&&c!='\n'&&c!='.'&&c!=',')
++teller;
else if(teller>=10){
++woord[10];
if (woord[10]>=maxteller)
maxteller=woord[10];
teller=0;
}
else{
++woord[teller];
if (woord[teller]>=maxteller)
maxteller=woord[teller];
teller=0;
}
}
++woord[teller];
if (woord[teller]>=maxteller)
maxteller=woord[teller];

When I was testing it I made a typing mistake and I hit backspace. Now this made me wonder if that does ++teller (teller is dutch for counter) since every keystroke except ' ' && '\t' && '\n' adds to it. In my reasoning backspace is part of ascii so getchar() 'gets' it and returns 8, which is the decimal integer for it, resulting in c having that value and c returng true to the first if.

tl;dr

hitting backspace to correct a typing mistake does not add to the word length although I didn't specify it do to so and I don't understand why.

d5c93c No.2802

wow I copy pasted this shit from

http://8ch.net/tech/res/181463+50.html

I didn't mean to make so many blank lines


e0c434 No.2815

Can anyone more skilled at maths than me figure out if this third last digit function can "break" (return an incorrect answer for an integer) :


function thirdLastDigit(num) {
if (isFinite(num) === true) {
return Math.floor(( ( (num - (num % 10) ) - (( (num - (num % 10)) % 100 ) / 10)) % 1000 ) / 100);
} else return NaN; }


e0c434 No.2816

>>2815

Oh, also, adjusting it. Change Math.floor to Math.trunc and negative numbers will work the same as the positive numbers.


22b842 No.2823

>>2801

The backspace happened before your program saw anything. Your input is buffered. You are not actually handling it character by character: you are receiving lines (or probably multiple-kilobyte blocks if input is piped from a file) at a time, and then are processing those lines character by character.

This is an optimization. Syscalls are slow, so you by default want to get a lot of I/O done per syscall.

You can use ioctl calls to change the terminal mode so that you are *actually* processing the user's backspaces, for example.


e0c434 No.2880

Posting a question here. I'm working on a CCG, no I'm not the guy from the other thread. Is there a better way than what I have in mind?

How I have it worked out in my head right now : of course I'll probably make it link to a file (.js or .json) describing each of the basic cards with methods based on how it acts. Then It uses two arrays of card IDs (one of the player deck and the other the enemy deck) to add objects to a "cards" array, attaching methods and properties based on data from the file. At the same time it fills in arrays of things like the player's hand, enemy's hand with shortcuts to those objects.

Each ally can have a single equip, the data is placed inside the "cards" objects as a single property, with a value linking to the appropriate card, otherwise left undefined.

My prototype will definately be in javascript.


ffc697 No.2884

>>2880

>Is there a better way than what I have in mind?

…maybe to not make your prototype in javascript?




[Return][Go to top][Catalog][Post a Reply]
Delete Post [ ]
[]
[ home / board list / faq / random / create / bans / search / manage / irc ] [ ]