[C++] Glavobolja

C, C++, Java, PHP, Ruby...

Moderator/ica: Moderatori/ce

Pravila foruma
U naslovu teme unutar uglatih zagrada navesti o kojem jeziku je riječ. Primjer: [Java]
Odgovori
Nele
Postovi: 248
Pridružen/a: 07 lip 2009, 12:55
Spol: M

[C++] Glavobolja

Post Postao/la Nele »

Evo koda:

Kod: Označi sve

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//prototypes of functions
void openAnswerKeyFile(ifstream&);

// put into array
void readAnswerKeyFile(string answerArray[], ifstream&);

void openStudentFile(ifstream&);
//void openOutputFile(ofstream&);

void readStudentAnswers(string studentAnswersArray[], ifstream&);
int compare(string answers[], string studentAnswersArray[], bool missedQuestions[]);

int main() {

    // creates object answerFile of type ifstream
    string answers[25];
    string studentAnswersArray[27];
    bool missedQuestions[25] = {false};
    ifstream answerFile;
    openAnswerKeyFile(answerFile);

    ifstream studentAnswers;
    openStudentFile(studentAnswers);

    //ofstream outputFile;
    //openOutputFile(outputFile);

    readAnswerKeyFile(answers, answerFile);
    readStudentAnswers(studentAnswersArray, studentAnswers);
    //string studentName=studentAnswersArray[1];
    //cout << "student name " << studentName; 
   int numberOfWrong= compare(answers, studentAnswersArray, missedQuestions);
   cout << "Number of wrong is " << numberOfWrong;
    
    //outputFile.close();
    return 0;
}

// opens answer file

void openAnswerKeyFile(ifstream& answerFile) {

    // filenames are declared as an array because 
    // ifstream class method open requires char as parameter

    char answerFilename[] = "Answer Key 1.txt";
    char answerFilename2[] = "Answer Key 2.txt";
    char answerFilename3[] = "Answer Key 3.txt";
    string userInput = "";

    while (userInput != answerFilename && userInput != answerFilename2 && userInput != answerFilename3) {
        cout << "Enter answer key filename: " << endl;
        getline(cin, userInput);
    }

    // take from user number of file. Number is located at index 11 


    answerFile.open(answerFilename);
}

void openStudentFile(ifstream& studentAnswers) {

    // filenames are declared as an array because 
    // ifstream class method open requires char as parameter

    char studentAnswerFilename[] = "Student Answers 1.txt";
    char studentAnswerFilename2[] = "Student Answers 2.txt";
    char studentAnswerFilename3[] = "Student Answers 3.txt";
    string userInput = "";
    while (userInput != studentAnswerFilename && userInput != studentAnswerFilename2 && userInput != studentAnswerFilename3) {
        cout << "Enter student answers filename: " << endl;
        getline(cin, userInput);
    }

    // takes from user number of file. Number is located at index 16
    studentAnswers.open(studentAnswerFilename);

}

void openOutputFile(ofstream& outputFile) {

    char outputFilename[] = "";

    cout << "Enter output filename (max 25 characters): " << endl;

    //takes input from user
    cin.getline(outputFilename, 25);
    // open file

    outputFile.open(outputFilename);
    if (!outputFile.is_open()) {
        cout << "Cant open file";
    }
}

void readAnswerKeyFile(string answerArray[], ifstream& answerFile) {
    string line;
    short i = 0;
    while (answerFile.good()) {
        getline(answerFile, line);
        answerArray[i] = line;
        i++;
    }

    answerFile.close();
}

void readStudentAnswers(string studentAnswersArray[], ifstream& studentAnswers) {
    string line;
    short i = 0;
    while (studentAnswers.good()) {
        getline(studentAnswers, line);
        studentAnswersArray[i] = line;
        i++;
    }

    studentAnswers.close();
}

int compare(string answers[], string studentAnswersArray[], bool missedQuestions[]) {
    short numberOfWrong=0;
    for (short i = 0, j = 2; i < 25, j < 27; i++, j++) {
        
        if (answers[i] != studentAnswersArray[j]) {
            missedQuestions[i] = true;
            cout << answers[i];
            numberOfWrong++;
      
        }
    }
    return numberOfWrong;
}
Program treba da uporedi odgovore sa tacnim odgovorima i da ispise tacan odgovor i netacan odgovor.
Imaju 2 ulazne datoteke: jedna je s tacnim odgovorima imena Answer Key 1.txt i u njoj su odgovori poredani ovako:
A
C
D
A
Druga datoteka je Student Answers Key 1.txt i ovog oblika:

Identifikacijski broj studenta
Ime i prezime
A
C
B
A

Lista problema s kodom:
1. Zbog cega mi u liniji

if (answers != studentAnswersArray[j]) {
missedQuestions = true;
cout << answers;
izbaci samo 1 output? Tacno mi prebroji 4 pogresna odgovora ali linija cout << answers; ispise samo jedan.
2. U zadatku kaze da se netacni odgovori stave u bool niz i oznace sa true. Kakvu korist imam od toga?
3. Odgovori se trebaju provjeriti samo jednom.
Privitak/ci
zahtjevi.png
(74.84 KiB) Preuzeto 1041 put.
Avatar
stefan
Moderator
Postovi: 4366
Pridružen/a: 28 sij 2009, 18:46
Spol: M
OS: openSUSE Leap KDE

Re: [C++] Glavobolja

Post Postao/la stefan »

  1. Kopirao sam tvoj kod i radi OK. Ispisuje pogrešan odgovor i točno izračuna broj pogrešnih odgovora.
  2. Zgodno je imat niz u kojem je spremljena informacija da li je i-ti odgovor točan ili pogrešan. Da ne moraš ponovno provjeravati odgovore.
  3. Pogledaj 2
Mali savijet, ako već koristiš C++ (iostream, string) onda bi ti praktičnije bilo koristiti vector umjesto arraya/polja/niza.

Kod: Označi sve

vector<string> answers;
for(int i=0;i<(int)answers.size();i++)
{
    if(answer[i]==necemu) bla bla
}
Jer upotreba arraya ti je više ceovski nego ceplusplusovski :D
Like some other animals, the gecko can perform a neat trick when threatened by a predator: it can amputate its own tail. The dropped tail serves to distract the predator, and by losing it, the lizard can run faster.
Nele
Postovi: 248
Pridružen/a: 07 lip 2009, 12:55
Spol: M

Re: [C++] Glavobolja

Post Postao/la Nele »

1. Izgleda da mi kompajler ne radi. Probao na windowsu i kod normalno radi.
2. Ne razumijem
Meni bi bilo nekako logicnije ovako:
if (answer != correct) {
zapisi u datoteku da je netacan
}
Ovako opet moram prolaziti kroz niz da vidim koji je true a koji false.

Kurs je pocetnicki. Nije nam dozvoljeno koristiti napredne stvari.
Avatar
stefan
Moderator
Postovi: 4366
Pridružen/a: 28 sij 2009, 18:46
Spol: M
OS: openSUSE Leap KDE

Re: [C++] Glavobolja

Post Postao/la stefan »

Recimo da ovaj kod koristiš za provjeru 50 ispita i želiš nekakvu statistiku.. Npr. želio bi vidjeti koliko je postotak točnih odgovora po pojedinom pitanju. Tada je zgodno je sve imati u memoriji... da ne moraš ponovno otvarati fajlove ako ti zatrebaju neke informacije.

Pisanje (da li je zadatak točan ili ne) u datoteku je potencijalno loše. Jer te iste podatke (na drugi način) imaš u answer fajlu.
Imaš 2 fajla answer.txt (A, B, A, A, D, B, C...) i ocjenjeno.txt (1, 0, 0, 1, 1, 1, 1...). Tko garantira da su podaci usklađeni? Koji fajl je relevantan?

No naravno, često ti treba neki eksport, i u tom slučaju ćeš pisati u fajlove...

Zaključak:
Ako neku analizu/statistiku rezultata misliš raditi u ovom kodu - onda drži sve u memoriji. Ako želiš rezultate ukrcati u Calc ili Excel tada ih spremi u fajl.
Like some other animals, the gecko can perform a neat trick when threatened by a predator: it can amputate its own tail. The dropped tail serves to distract the predator, and by losing it, the lizard can run faster.
Odgovori