View Full Version : Programming Assignment
BroTrevor
02-27-2006, 03:05 PM
JoeC,
What's the miserable programming assignment you are working on.
I started this new thread to ask you that since to ask you in the wish granted thread would have been taboo and off topic.
GIve us the details?
If then else?
Christian-Samurai
02-27-2006, 03:13 PM
Anything to do with programing is miserable lol UGH AND MORE UGH, oh incase you didnt guess I dont like programing :D
BroTrevor
02-27-2006, 04:45 PM
I spose I kinda guessed that, yeah.
Check out http://mylife-mytimes.blogspot.com/ - I've been ranting about it for the past couple posts.
Basically, I'm to construct a roman numeral calculator using the following functions:
int get_Data(ifstream&, char);
int roman_conversion(char);
void calc_Romans(int, int, char, int&);
The numeral system is not the traditional.familliar one we all know (or, in my case, don't know)
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
So, DXXXXVIII = 548
My input file to be evaluated is this:
MCCXXVI CV +
MCCXXVI MCCXXVI /
V I -
MDCLXVI III *
DL DXXXXVIII -
D L /
MDI CXI +
XXV IIII /
XI CII *
As you'll see from my grumblings on the blog, I've already subjected 20+ hours of my Spring break to this problem... and counting.
Do you know C++ BroTrevor?
Philippe
02-27-2006, 05:32 PM
My input file to be evaluated is this:
MCCXXVI CV +
MCCXXVI MCCXXVI /
V I -
MDCLXVI III *
DL DXXXXVIII -
D L /
MDI CXI +
XXV IIII /
XI CII *
Well, I know "romain" numbers but normally, we would write 4 as IV and similar for other number. Since that does not happen in your case, it is much simpler to parse the file.
OTOH, what is the meaning of space in a number like XI CII in the input above?
And does each line mean one computation? Does the line "D L /" means 500/50 and should give and answer of X (that is 10)? In that case does your program always have 2 numbers and one operation or it can be arbitrary complex?
Philippe
Philippe
02-27-2006, 08:18 PM
Basically, I'm to construct a roman numeral calculator using the following functions:
int get_Data(ifstream&, char);
int roman_conversion(char);
void calc_Romans(int, int, char, int&);
Well, I do not understand how the first function is intended to be used. What is the use of the second argument?
For roman_conversion et calc_Romans, a simple switch statement should do.
I tried it and I think the harder part of it is properly reading the stream. There would be 3 possibles way to read it.
1) Read each characters using get() member function. This will allows you to see white space.
2) Uses >> operator with a char as the target. The problem is that by default you won't see the end of your token as white space are skipped. In particular, you would not see the space between both numbers.
3) Uses >> operator with a string as the target. IMO, this is the best solution but it does requires that you already knows how to manipulate strings.
My solution uses a combination of 1) and 2) as it allows me to see white space when I want (the first two on each line) and skip the we I do not want (carriage return and/or new line).
Hope it will help.
Philippe
Does the line "D L /" means 500/50 and should give and answer of X (that is 10)? In that case does your program always have 2 numbers and one operation or it can be arbitrary complex?
D L / does mean 500/10. And yes, the program will be limited to numbers and one operation at a time.
I'm using the infile.get(notation) method --- (notation is a char). I We haven't covered manipulation of strings... seems like I would somehow have to break the string down into chars after reading it in order to assign a value to it based on the chars. :confused:
int get_Data(ifstream&, char);
this graps a char from the ifstream, inserts it into the variable 'notation', then calls the function int roman_conversion(char) to assess the value of the char.
What I'm working on now is making this function add up the chars before returning the int variable num back to main.
BroTrevor
02-27-2006, 10:53 PM
While I don't necessarily speak this dialect, can you post the code you have so far??
this truly has been riveting...I'm interested in seeing the solution, although at this point, I don't know if I will be much help. I'm a self taught guy...so I only know what I've had to learn so far...which has mostly been database and presentation of data.
Most of my bugs are database stuff.
I'll think about it Bro Trevor.
Anyhow, this semester, anyone who knows C++ is my friend. :)
Philippe
02-28-2006, 12:54 AM
What I'm working on now is making this function add up the chars before returning the int variable num back to main.
Well you would do something in the line of:
char notation;
int total = 0;
while (infile.get(notation) && notation != ' ')
{
total += roman_conversion(notation);
}
Now you are at the space and you can repeat same process for the second number and finally get the character that represent the operation.
You will then have the result after calling calc_Romans().
This should works for the first line. The next step is to add a loop to process other lines. At that point, you want to skip carriage return and/or new line before processing the next line.
One way would be to read characters until you have a valid one. If this is done outside of the get_Data function, then that character would be the last argument. In that case, you have to modify the previous loop so that it will process that character first. You can do this by replacing the while loop with a do while loop. If this is the solution you use then you should also read the first character of the file before calling the function for the first time.
Another way is the do it a the beginning of the function. In that case, it seems that the last argument would never be used...
I think I have given enough hints... I'm not supposed to do all your homework
Philippe
BroTrevor
02-28-2006, 10:06 AM
Phillipe seems to be your buddy today Joe!
i followed what he was doing, even tho it's a bit different of a dialect like I said.
OK, this is what I've got so far:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// Developer: Josiah Cornett
// function prototypes go here
int get_Data(ifstream& infile, char notation);
int roman_conversion(char notation);
void calc_Romans(int num2,int num3, char notation, int& num);
int main()
{
char notation;
string sentence;
int num, numfirst, numsecond;
ifstream infile;
infile.open("MP4data.txt");
if(!infile)
cout << "Data File Not Present.\n";
notation = 'Z';
num = numfirst = numsecond = 0;
infile.get(notation);
while (infile)
{
numfirst = get_Data(infile, notation);
//numfirst = num;
infile.get(notation);
numsecond = get_Data(infile, notation);
infile.get(notation);
//numsecond = num;
//calc_Romans(numfirst, numsecond, notation, num);
//cout << num << endl;
cout << " num1: " << numfirst << " num2: " << numsecond << endl;
infile.ignore(100, '/n');
}
return 0;
}
//Define functions
int get_Data(ifstream& infile, char notation)
{
int num;
num = 0;
while (notation != ' ')
{
num = num + roman_conversion(notation);
//num2 = num;
//infile.get(notation);
//num = roman_conversion(notation);
//num = num + num2;
infile.get(notation);
}
return num;
}
//
int roman_conversion(char notation)
{
switch (notation)
{
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
void calc_Romans(int numfirst,int numsecond, char notation, int& num)
{
switch (notation)
{
case '+': num = numfirst + numsecond;
break;
case '-': num = numfirst - numsecond;
break;
case '*': num = numfirst * numsecond;
break;
case '/': num = numfirst / numsecond;
break;
case '\n': num = numfirst = numsecond = 0;
break;
}
}
This is my input:
MCCXXVI CV +
MCCXXVI MCCXXVI /
V I -
MDCLXVI III *
DL DXXXXVIII -
D L /
MDI CXI +
XXV IIII /
XI CII *
This is my current output:
num1: 1226 num2: 0
num1: 0 num2: 3
num1: 0 num2: 0
Press any key to continue . . .
If anyone doesn't understand it all and would like me to comment it out, just ask. I've gotta go catch some breaky.
PianoMan79
02-28-2006, 12:32 PM
Sounds confusing! :confused: Pretty cool, though. Best of luck on your assignment.
Nick
Yeah, the problem right now is getting numfirst and numsecond to be assigned values >0. Those blanks are really messing me up, but I need them in order to tell me where the numeral stops. I could put
if (notation != 0)
return num;
in the function get_Data, but that doesn't really do anything. Even under the cases that I have gotten that to work, the program assignes a value to char ' ' based upon the ASCII values (namely, 32).
But even that doesn't explain my weird output. I'm only getting three lines of output when my input it 9 lines long. I inderstand why num1 is zero on the output in the first line, but after that I'm failing to trace through program to determine what's giving me those weird values.
I still don't know what field of engineering I want to go into, but I think I've successfully ruled out computer science & engineering.
Christian-Samurai
02-28-2006, 02:07 PM
Sounds confusing! :confused: Pretty cool, though. Best of luck on your assignment.
Nick
Programing isnt really confusing its just fustrating lol when you know what you are working on and what each element does it can go pretty well and you can write this nice long series of codes but then you make one little typo and you can spend hours trying to figure out what you did wrong lol right Joe lol
Unfortunately, I've probably got both problems. Since I'm learning it, I really don't know what I'm doing. AND I've probably got some typo causing a logical error somewhere :rolleyes:
Christian-Samurai
02-28-2006, 02:28 PM
well I know only the basics of a few versions of code but I know enough to hack it to what I need. I have rewritten some code for some of my games to make them more interesting to play but thats the extent of my main coding, I mostly do DHTML, CSS and things to do with web and try to stay away from getting to deep into the evil that is coding :D
Excuse me for posting in the fort, but try this:
#include<iostream.h>
#include<stdio.h>
int convert(char rom)
{
switch (rom)
{
case 'I': {return 1;}
case 'V': {return 5;}
case 'X': {return 10;}
case 'L': {return 50;}
case 'C': {return 100;}
case 'D': {return 500;}
case 'M': {return 1000;}
}
return 0;
}
int operation(int num1, int num2, char o)
{
float rez1;
switch (o)
{
case '+': {cout<<"num1+num2 = "<<num1+num2; break;}
case '-': {cout<<"num1-num2 = "<<num1-num2; break;}
case '*': {cout<<"num1*num2 = "<<num1*num2; break;}
case '/':
{
rez1=float(num1)/float(num2);
cout<<"num1/num2 = "<<rez1; break;
}
default: {cout<<"Error!"<<endl;}
}
return 0;
}
int RomanToInt(char rom[21])
{
int result=0, br=0, i, a=0, b, c, d;
while (rom[br]!='\0')
br++;
for (i=0; i<=br; i++)
{
result=result+convert(rom[i]);
}
for (i=0; i<=br; i++)
{
for (i=0; i<=br; i++)
{
if (i!=0)
a=convert(rom[i-1]);
b=convert(rom[i]);
c=convert(rom[i+1]);
if (rom[i]!='\0')
d=convert(rom[i+2]);
if (a==0 && b<c && b<d)
result=result;
else
if (a==0 && b<c)
result=result-2*b;
if (a!=0 && b>=a && b<c)
result=result-2*b;
if (b<c && b<d)
result=result;
if (b<=a && b<c)
result=result-2*b;
if (b>c && b<d)
result=result-2*b;
}
}
cout<<rom<<" = "<<result<<endl;
return result;
}
void main()
{
char rom1[21], rom2[21], o;
int num1, num2;
printf("Enter first Roman number: ");
gets(rom1);
printf("Enter second Roman number: ");
gets(rom2);
num1=RomanToInt(rom1);
num2=RomanToInt(rom2);
cout<<"Operation: +, -, * or /? ";
cin>>o;
operation(num1, num2, o);
cout<<endl;
}
BroTrevor
02-28-2006, 02:55 PM
Wow...
if a girl can post C++ code in the fort...is this ok?
I'm sufficiently impressed...
Ahem... I'm sufficiently blown away. I'm laughing out loud 'cause I have no idea what to say. I have no idea what that code means.
BroTrevor
02-28-2006, 05:01 PM
Kiki...Honorary guy in this thread ONLY.
Could you please explain the code a bit? We all might learn something here.
I tried it out (as is) and the compiler didn't even get past the first include file (said no such file or directory).
BroTrevor
02-28-2006, 05:14 PM
I'm thinking some of it got lost in translation or something.
I'd remove the ".h" from the includes there...maybe...I dunno...it's different...and I'd have to really apply myself to it to figure it out...
And I just haven't felt like it yet.
I'm using Microsoft Visual C++ 6.0.
File/ New/ Files/ C++ Source File and then copy code.
How does it work?:)
You must enter 2 Roman numbers, and operation (+, -, * or /). Function RomanToInt converts them to integer, and function operation calculates.
Lemme guess... you wrote it in firteen minutes, right?
Not really. It's combination of 3 programs that I wrote previously.
Does that help?
int RomanToInt(char rom[21])
{
int result=0, br=0, i, a=0, b, c, d;
while (rom[br]!='\0')
br++;
//Br=number of characters (for MXV br=3)
for (i=0; i<=br; i++)
{
result=result+convert(rom[i]);
// First result is total sum (for CMX result=100+1000+10=1110)
}
for (i=0; i<=br; i++)
{
for (i=0; i<=br; i++)
{
if (i!=0) // If not 0. position (first position in array is on position 0 in c++)
a=convert(rom[i-1]);
[ b=convert(rom[i]);
c=convert(rom[i+1]);
if (rom[i]!='\0')// If not end of array
d=convert(rom[i+2]);
if (a==0 && b<c && b<d)
result=result;
else
// I've added 100 for C, but C is left M, so I'm taking away 2*100
if (a==0 && b<c)
result=result-2*b;
if (a!=0 && b>=a && b<c)
result=result-2*b;
if (b<c && b<d)
result=result;
if (b<=a && b<c)
result=result-2*b;
if (b>c && b<d)
result=result-2*b;
}
}
cout<<rom<<" = "<<result<<endl;
return result;
}
Interesting... though I won't be using it for my project. There's acedemic honesty to consider. ;)
BroTrevor
02-28-2006, 07:43 PM
Honestly Joe,
Programming gets lots better once you get more familiar with it. The learning curve is harsh, but it's not too bad after that.
Yeah, well I haven't felt this stupid since.... hmmm.... I don't remember feeling this stupid!
And the first test I scored a 74% - that's my lowest score in three years! Granted, I made some stupid mistakes, didn't think through the questions (and in one case skipped a q all together). But still!
Flyboy
02-28-2006, 08:55 PM
maybe u should talk to my brother, Joe. He's just finnishing his sr yr in networking..and does all kinds of programming with c++ and every other imaginable thingy(just cant think of any others off the top of my head)..and hes into websphere and web designing. i think he has a library of about 15 books on programmin or sumthin. or maybe spamboy could help though im not exactly sure what kinda work he did.
SpamBoy is a newtorker, but he doesn't do C++. We do have a prog man at our church though, and he's giving me somepointers.
Philippe
02-28-2006, 10:44 PM
This is my current output:
num1: 1226 num2: 0
num1: 0 num2: 3
num1: 0 num2: 0
Press any key to continue . . .
I read your code and also try it with Visual C++ Express 2005 and I correctly get the first 2 numbers (1226 and 105). Maybe your input file is not formatted exactly as mine. I have done a cut from this site and paste it into notepad and then save that file.
Also I uncommented calc_Romans function and it does properly compute the result of that line (I get 1331).
And guess what, the error is simply that you uses a forward slash instead of a backward slash to be able to process all remainding lines.
Replace:
infile.ignore(100, '/n');
With:
infile.ignore(100, '\n');
The easiest way to spot such problems is to use a debugger. The one that come with Visual C++ Express Edition (http://msdn.microsoft.com/vstudio/express/visualC/default.aspx) is very nice and it is free (but it is a huge download around 400MB).
Philippe
02-28-2006, 11:07 PM
// First result is total sum (for CMX result=100+1000+10=1110)
// I've added 100 for C, but C is left M, so I'm taking away 2*100
}
Well, since in it assignement, it does not uses that rule (which is the correct way of writing romans numbers) but would instead write 910 as DCCCCX, it is much simpler to decode the value as a simple loop can be used.
Philippe
Phillippe man, I don;t care what everyone else is saying about you! You're a great guy! Thanks for the \n tip. I still don't know why you're output is different than mine. Here it is with just the \n fixed... calc_Romans is still commented out.
num1: 1226 num2: 0
num1: 0 num2: 1226
num1: 0 num2: 5
num1: 0 num2: 1666
num1: 0 num2: 550
num1: 1000 num2: 0
num1: 1551 num2: 0
num1: 0 num2: 25
num1: 0 num2: 11
num1: 0 num2: 0
Press any key to continue . . .
Again, my input file is:
MCCXXVI CV +
MCCXXVI MCCXXVI /
V I -
MDCLXVI III *
DL DXXXXVIII -
D L /
MDI CXI +
XXV IIII /
XI CII *
BroTrevor
03-01-2006, 10:02 AM
SpamBoy is a newtorker, but he doesn't do C++. We do have a prog man at our church though, and he's giving me somepointers.
Yeah, but at least he's not an old torker... Cuz you know how they get... The new torkers are so much nicer. Don't know what happened to that older class.
BroTrevor
03-01-2006, 10:03 AM
This almost makes me want to try this...
If only I didn't have 3 other projects I was supposed to do.
Philippe
03-01-2006, 03:02 PM
I still don't know why you're output is different than mine.
num1: 1226 num2: 0
num1: 0 num2: 1226
num1: 0 num2: 5
num1: 0 num2: 1666
num1: 0 num2: 550
num1: 1000 num2: 0
num1: 1551 num2: 0
num1: 0 num2: 25
num1: 0 num2: 11
num1: 0 num2: 0
Press any key to continue . . .
Well, are you sure that your file is exactly as shown. In particular, that you have only one space between numbers...
One problem that I have found is that if the file is not correctly formatted, notation might have an undesirable value in it when you start next iteration. Since the last character you get on a line is the operator (assuming properly formatted line) and the skip until after the '\n', you should get next character inside the loop (or alternatively set it to 'Z' which whould be added as 0).
// infile.get(notation); --- now inside loop
while (infile)
infile.get(notation);
// as before...
I think that your input file is not exactly what you pretend. I have found how to modify the input file to give the result you have. To help understanding, I will replace spaces by dots and '\n' by $.
MCCXXVI...CV.+$
MCCXXVI...MCCXXVI./$
V...I.-$
MDCLXVI...III.*$
DL.DXXXXVIII.-$
D..L./$
MDI...CXI.+$
XXV...IIII./$
XI...CII.*$
.$
One thing that might help you understand what goes wrong is to display the value of notation at stategic place (like at the beginning of get_Data).
Also be aware that since notation is passed by value, any change made inside the function will not be seen by the caller (this is correct but it might be confusing as you uses the same variable name).
What you should do is to add some code that would skip whitespace at proper locations if you must be able to read "not perfectly" formatted files.
Another trick that could help you debugging would be to replace 0 by some other values and check how it affect the result.
Philippe
03-01-2006, 03:09 PM
One problem that I have found is that if the file is not correctly formatted, notation might have an undesirable value in it when you start next iteration.
If fact, if you have 2 spaces, notation would have the value of the following character This explain the 1000 in your result. You have 'D' in notation and the next line start with 'D' and ' ' so you add 500 + 500 and get 1000.
But if you have 3 spaces, then notation would be a space and get_Data would immediatly returns 0. Then when you will want to read second number, you would be at the beginning of the first number.
Philippe
03-01-2006, 03:14 PM
Phillipe seems to be your buddy today Joe!
At least it seems to have a positive impact on my reputation points.
HotShot53
03-01-2006, 10:33 PM
Joe does have some powerful reputation giving ability ;)
I'll take a look at your suggestions when I've the time. Unfortunately, I'm not allowed to reformat the input.
BroTrevor
03-23-2006, 10:55 AM
Sooo....
How'd the project go? did you get an A?
I'm currently trying to make one of These (http://developer.ebusiness-apps.com/technologies/webdevelopment/codeandcomponents/ebawebcombov3/media/demos/classic/index.htm)
It'll be neat.
I did get an 'A'. I actually just finished a project with string manipulation and enum types - should get an 'A' on that too. Test aren't going so well, but what can I say?
Your project looks a lot more complex than mine
BroTrevor
03-23-2006, 06:26 PM
I've actually got it functional already.
It's very basic...now all I have to do is style it...it looks really rudimentary and not nice right now.
joris
04-23-2006, 05:52 PM
you know, I came across some language that allows one to write,
PLEASE DO .5 <- "?'"'?"'V"'#65535~"'?":1~'#0$#65535'"$":2~'#0$#65535'"'~'#0$#65535'"'~#1"$"'#65535~"'?":1~'#65535$#0'"$":2~'#65535$#0'"'~'#0$#65535'"'~#1"'~#1"$"'&"':2~:5'~'"'?"'?":5~:5"~"#65535$#65535"'~'#65535$#0'"$#32768'~'#0$#65535'"$"'?":5~:5"~"#65535$#65535"'~'#0$#65535'"'"$"':5~:5'~#1"'~#1"'~'#32768$#1'"~#1'$#1"~#3
without a single warning! now, isn't that neat?
ofcourse the meaning of this should be obvious to anyone ;)
HotShot53
04-24-2006, 12:55 AM
Lol, I agree with Joe....??????????????????????????
You sure you didn't encrypt it before giving it to us?
joris
04-24-2006, 02:26 PM
uhm... yes :) I'm sure i's not encrypted
joris
04-25-2006, 08:40 AM
read the manual at http://www.muppetlabs.com/~breadbox/intercal-man/
(just don't believe all the stuff at the internet about it, some people put negative names to it; in fact the only purpose of the language is to be weird and hard, and still be capable of calculating basically anything)
joris
05-05-2006, 10:23 AM
long time ago (some years really) I wrote some chess in javascript; you'll have to play against yourself, or you'll have to find someone who is just as weird as you, to sit next to you and play (instead of playing with a real chess board)
anyway, it's not perfect, but it's very playable; I can't guarentee there are no problems, but there shouldn't be big one's; it doesn't have an AI (computer player) either
if, after all, you still feel like playing it, follow this link:
http://ballz.ababa.net/jhuizer/chess/chess.html :-p
(I remember I tried to have some structure in the javascript, but I'm not too sure about how readable it is)
BroTrevor
05-05-2006, 10:28 AM
you know, I came across some language that allows one to write,
PLEASE DO .5 <- "?'"'?"'V"'#65535~"'?":1~'#0$#65535'"$":2~'#0$#65535'"'~'#0$#65535'"'~#1"$"'#65535~"'?":1~'#65535$#0'"$":2~'#65535$#0'"'~'#0$#65535'"'~#1"'~#1"$"'&"':2~:5'~'"'?"'?":5~:5"~"#65535$#65535"'~'#65535$#0'"$#32768'~'#0$#65535'"$"'?":5~:5"~"#65535$#65535"'~'#0$#65535'"'"$"':5~:5'~#1"'~#1"'~'#32768$#1'"~#1'$#1"~#3
without a single warning! now, isn't that neat?
ofcourse the meaning of this should be obvious to anyone ;)
Of course it is. That's your standard space shuttle launch sequence.
as for the chess... I'm not worthy man. I don't know that I'd even attempt something like that.
I'll stick to my small DOM manipulations for the time being.
joris
05-05-2006, 11:00 AM
:) the DOM part isn't too interesting, except that it's made in such a way that it should work on as many browsers as possible
The real job is all the checks and doing them in a smart way
BroTrevor
05-05-2006, 11:01 AM
The real job is all the checks and doing them in a smart way
I'm trying to learn "the smart way"
but, The DOM is primarily what I will be working with in the manipulation of web pages...
Today, I am not as tired as I was yesterday, and I had a cup of coffee.. (sumatra...<dreamy look>) So I should be able to finish off my javascript "object" appendix today in my "Ajax in Action" book.
joris
05-05-2006, 11:09 AM
I don't know "Ajax" in fact; is it just another buzzword or is it something very new? could you give some overview or so :)
BroTrevor
05-05-2006, 11:12 AM
I don't know "Ajax" in fact; is it just another buzzword or is it something very new? could you give some overview or so :)
Yeah, it's a buzzword.
Asynchronus Javascript and XML.
The big thing is the XMLhttprequest header thingiemajober...
Basically it's a way of using javascript more on the client and getting data asynchronously from the server without refreshing the html...
So you get data via javascript, and present that data on the web page on the fly through "innerhtml" and other ways of manipulating the DOM and stylesheet.
HotShot53
05-05-2006, 10:33 PM
Lol, it's a good thing some people understand what all that's about ;)
jtucker
05-13-2006, 09:02 PM
i'm kinda lost but that's okay:chef
vBulletin v3.5.3, Copyright ©2000-2012, Jelsoft Enterprises Ltd.