Daily c++ problems

Pages: 12345
I'm glad someone started something like this, its really good to have a goal to accomplish.

on a side note, I completed the first 3 exercises easily, then I reached the roman numerals and found it a bit difficult, I'll have to try again later, the way I set it up just wasn't working, which is scary considering it was rated at easy :3
Hey guys, how are you getting on with the lasest problem? I am having trouble wrapping my head around it!

How do I take the text file information and read it into 7 unique objects of stockObject?

I'm lost! Thanks.
closed account (o3hC5Di1)
Hi there,

Some hints, as to not spoil the fun:

* You will need to read the file line by line, meaning you will be in a loop most likely.
* Every line should create a new stock object - so you will need an arbitrary number of stock objects. In such cases it's usually recommended to use an array, in this case an array of stock objects.

If you need any further help perhaps it's best to create a separate thread on the forum listing your specific problem with this task.

All the best,
NwN
closed account (j2NvC542)
Zephilinox, the roman numerals was an exercise in my book ("The C++ Programming Language" "Programming: Principles and Practice using C++", Bjarne Stroustrup) and I also was a little clueless. But I was having problems during the last days anyway, I don't know what's wrong ._. I completed it yesterday and it really isn't so complicated as it might seem.
Last edited on
I have that book too, but I never bothered to do any of the exercises :3 plus I don't think he gave any solutions.
closed account (o3hC5Di1)
@Need4Sleep - here's a possible answer for your last challenge (the one with the stock market file) for you to add to the answer list. That was a good exercise for me - thanks!

[Solution now in answer thread]

All the best,
NwN
Last edited on
updated, thanks for posting the stock problem NwN. Looks great
updated a tad late today, sorry.
It would be nice if you could give simple graphical problems. I find them very fun
Had a little downtime so I did the latest problem, anyone got a better way to do this?

http://pastebin.com/ts8U6YAR
Updated, apologize for missing yesterdays problem! Added you to the answer thread speakon, thanks for sharing. Today's problem might be a little lack - luster, i apologize as my life has been extremely busy lately.
http://pastebin.com/sW1HKzG6

does that actually work? where is it subtracting the numbers? where is the 3-roman chars in a row input check, valid roman numeral check, lowercase check/fix, why is it the solution for this problem if it doesn't solve it :/

this is my version of it, with help and inspiration from bool (user)

http://pastebin.com/82U3PqUf

Last edited on
Yeah, that solution's missing some key portions. Here's how I solved the Roman subtraction issue:

http://pastebin.com/b9KM3EvM
hmm, are you sure? try running that with DMMIV, DM = 500, MI = 1001, V = 5; 1506 would be the result from it (I think), but it should actually be 1504

actually, looking at it again it should give you

D > M, 1000 - 500 - 500
M > M, 1000
M > I, 1000
I > V, 5 - 1 - 1

0
1000
1000
3
2003

I think? can't compile it, it isn't a functional program, just a function

instead of the correct output, 1504
Last edited on
Assignment for the budding dev:

Build a Firefox extension that changes every text instance of "keyboard" to "leopard".

For reference, here's where the idea came from (http://xkcd.com/1031/) and the Jscript code that I have been working on (with help from other fans of the idea):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// ==UserScript==
// @name s/keyboard/leopard/g
// @version 3.1
// @description Replaces the word "keyboard" with "leopard".
// @match *://*/*
// @license MIT License
// @updateURL http://userscripts.org/scripts/source/128626.meta.js
// @downloadURL https://userscripts.org/scripts/source/128626.user.js
// ==/UserScript==

// The pattern to match
var keyboard_pattern = /(k)(e)(y)(b)o(ard)/ig

// which letters in "keyboard" get replaced with which in "leopard"
var leopard_subs = {
  'k': 'l', 'K': 'L',
  'y': 'o', 'Y': 'O',
  'b': 'p', 'B': 'P'
};

// construct "leopard" replacement for "keyboard" components
function replacement_leopard(match,k,e,y,b,ard){
    return leopard_subs[k] + e + leopard_subs[y] + leopard_subs[b] + ard
}

// Transform all instances of 'keyboard' in a string into 'leopard'
function leopardize(str) {
  return str.replace(keyboard_pattern, replacement_leopard)
}

// define the noteType value for TEXT_NODEs (the kind we want to replace)
// Some browsers may fail to make this value available - it's 3
var TEXT_NODE = Node.TEXT_NODE || 3

// Flag to signal that we're replacing text, so that change doesn't trigger
// another replacement (technically, that can't happen if all the instances
// of "keyboard" that would trigger a replacement have been replaced with
// "leopard", but it's still good practice)
var replacingContent = false

function replaceTextContent(node) {
  if (~node.textContent.search(keyboard_pattern)) {
    //flag that content is being replaced so the event it generates
    //won't trigger another replacement
    replacingContent = true
    node.textContent = leopardize(node.textContent)
    replacingContent = false
  }
}

function changeTextNodes(node) {
  var length, childNodes
  //If this is a text node, leopardize it
  if (node.nodeType == TEXT_NODE) {
    replaceTextContent(node)
  //If this is anything other than a text node, recurse any children
  } else {
    childNodes = node.childNodes
    length = childNodes.length
    for(var i=0; i<length; ++i){
      changeTextNodes(childNodes[i])
    }
  }
}

function insertion_listener(event) {
  //change any new text nodes in a node that is added to the body
  changeTextNodes(event.target)
}

function cdm_listener(event) {
  //avoid infinite loop by ignoring events triggered by replacement
  if(!replacingContent){
    replaceTextContent(event.target)
  }
}

changeTextNodes(document.body)
document.title = leopardize(document.title)
document.body.addEventListener ("DOMNodeInserted", insertion_listener, false)
document.body.addEventListener ("DOMCharacterDataModified", cdm_listener, false)

Sadly, it is not functional, so I would appreciate pointers and other good ideas for scripts to release onto annoying classmates' computers.
Last edited on
@zeph
It outputs to 1504. Typically, it would just be written as MDIV though, but it works that way too.

value initialized to D: 500;
D < M: value + (1000 - 500) -500 = 500;
M >= M: value + 1000 = 1500;
M >= I: value + 1 = 1501;
I < V = value + ( 5 - 1 ) - 1 = 1504.

Here's the whole prog: http://pastebin.com/QyHhPHtu
Last edited on
thanks, yeah it does work, nice job.
closed account (j2NvC542)
PadreDoom's solution is even simpler than mine.
I will post mine later, because now I am too lazy, sorry.
Bump.

don't let this die.
Here's my solution to 7/25, the airline ticket prog. In retrospect, I should have probably made Ticket and maybe even Plane a class and encapsulated some of the functions. Lesson learned.

http://pastebin.com/pzy82wqU
Pages: 12345