Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,228 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,881 people online right now. Registration is fast and FREE... Join Now!




Grade a math exam...~

 
Reply to this topicStart new topic

Grade a math exam...~, i need to write a program to grade a math exam..can anyone help me get

ivy121
13 Mar, 2007 - 01:38 PM
Post #1

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 4


My Contributions
Write a program to grade a math exam. The answer sheet is in the following format:

10 + 15 = 25
30 – 1 = 28
6 * 2 = 12
10 / 3 = 3 ... 2
6 / 3 = 2 ... 0
3 x 2 = 6

Each calculation occupies a line. All calculations involve non-negative integers only. The result of a division consists of the quotient and the remainder, separated by three dots (as in lines 4 and 5 above). The remainder will be displayed even if it is zero (as in line 5 above). The output of your program should be in the following format:

1) 10 + 15 = 25
Correct.
2) 30 – 1 = 28
Incorrect. Answer is 29.
3) 6 * 2 = 12
Correct.
4) 10 / 3 = 3 ... 2
Incorrect. Answer is 3 ... 1.
5) 6 / 3 = 2 ... 0
Correct.
6) 3 x 2 = 6
Correct.
Your score: 4 out of 6

You don’t know how many questions there are in the test. Neither is known the order of these questions. In addition, either ‘x’/‘X’ or ‘*’ might be used for multiplication. The input file is c:\temp\test.txt and the result should be displayed on screen.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Grade A Math Exam...~
13 Mar, 2007 - 01:43 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,984



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
This site has a policy concerning homework, you must show us a good faith effort on your part to complete your assignment.

Post your code that you have completed so far in your attempt to fulfill your assignment and we will help you get it working.

But you must show some effort.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Grade A Math Exam...~
13 Mar, 2007 - 01:54 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
Well the good news is that you don't need to write a complicated expression parser or do any infix to postfix conversion etc.. each line has a given format

[0-9]+' '[+-/*xX]' '[0-9]+' = '([0-9]+|[0-9]+' ... '[0-9])

so all you need to do is use a basic tokenizer (there are some examples in the snippets section) to serperate the expresions into tokens (using a space as the delineator). you know that the first three should be number, operation, number... determine the operation, preform that on the two numbers, and then check the answer.
User is offlineProfile CardPM
+Quote Post

okyup
RE: Grade A Math Exam...~
13 Mar, 2007 - 02:35 PM
Post #4

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
Read each line, check the symbol after the first space or behind the first number, then perform that operation on both the numbers.

Use getline to get each line. You can sort through strings like stringname[position] or convert them to .c_str()
User is offlineProfile CardPM
+Quote Post

ivy121
RE: Grade A Math Exam...~
13 Mar, 2007 - 03:17 PM
Post #5

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 4


My Contributions
this is what i had so far. dont feel right at all...... somebody help me... crazy.gif



#include <iostream>
#include <string> //Pre-defined string data type
#include <iomanip> //output manipulators
#include <fstream> //File input/output
using namespace std;

int answer;
int num;
int sum;
int counter, numofcorrect, numofnotcorrect;
char ch1, ch2, ch3, ch4, ch5, correctornot;


int main()
{
ifstream data;
data.open("c:\temp\test.txt"); //open the input file.

while (!data.eof())
{
data >> num;
sum += num;
count++;
cout << "number = " << num << "count =" << count << "sum=" << sum <<endl;

}

switch (data)
{
case "+";
data >> num1 >> num2

cout << "10" << "+" << "15=" << num1+num2 << endl;
count++;

break;

case "-";
data >> num
cout << "30" << "-" << "1=" << num1+num2 << endl;
count++;

break;

case "*":
case "x":
case "X":
data >> num
cout << "6" << "*" << "2=" <<num1+num2 << endl;

cout << "you score:" << << "out of" << count << endl;


return 0;

}




User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Grade A Math Exam...~
15 Mar, 2007 - 03:52 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
well no, it doesn't seem right.
First off the file should have lines like "8 + 7 = 16" or "## op ## = ##" where the "##" is an number, "op" is an operation {+-*/xX}. If this is so, the code:
CODE
while (!data.eof())
{
data >> num;
sum += num;
count++;
cout << "number = " << num << "count =" << count << "sum=" << sum <<endl;

}
makes no sence? This would be grate if you had to take in a bunch of numbers in a file and print thier some, or find thier average or something.

CODE

int num1, num2, num3;
char op, eq;

cin >> num1 >> op >> num2 >> eq >> num3;


The code would have a line something like the above... of course you need to think a little more because sometimes the line will look like "## / ## = ## ... ##".


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:01PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month