summaryrefslogtreecommitdiffstats
path: root/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src
downloadStudium-master.tar.gz
Studium-master.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src')
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.cpp47
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.h29
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.cpp127
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.h49
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/Util.h78
-rw-r--r--Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/main.cpp23
6 files changed, 353 insertions, 0 deletions
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.cpp
new file mode 100644
index 0000000..fcbd64a
--- /dev/null
+++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.cpp
@@ -0,0 +1,47 @@
+/*
+ * DiceSim.cpp
+ *
+ * Created on: 13.05.2011
+ * Author: sven
+ */
+
+#include <cstdlib>
+#include <ctime>
+#include <iostream>
+
+#include "DiceSim.h"
+
+DiceSim::DiceSim(unsigned numDice, unsigned numRolls)
+:mNumDice(numDice),mNumRolls(numRolls),mEyesAbsFreq((numDice * 6))
+{
+ srand(time(NULL));
+ for(mResultIter = mEyesAbsFreq.begin() ; mResultIter != mEyesAbsFreq.end() ; mResultIter++)
+ {
+ *mResultIter = 0;
+ }
+ for(unsigned n=0; n< numRolls; n++)
+ {
+ unsigned eyesSum = 0;
+ for(unsigned d=0; d<numDice; d++) {
+ eyesSum += rollDie();
+ }
+ mEyesAbsFreq.at(eyesSum - numDice)++;
+ }
+ unsigned freqSum = 0;
+ for(mResultIter = mEyesAbsFreq.begin() ; mResultIter != mEyesAbsFreq.end() ; mResultIter++)
+ {
+ freqSum += *mResultIter;
+ }
+ std::cout << "Num rolls: " << freqSum << std::endl;
+}
+
+DiceSim::~DiceSim() {
+ // TODO Auto-generated destructor stub
+}
+
+unsigned DiceSim::rollDie()
+{
+ unsigned eyes = 0;
+ eyes = 1 + (int) (6.0 * (rand() / (RAND_MAX + 1.0)));
+ return eyes;
+}
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.h
new file mode 100644
index 0000000..f7b5c59
--- /dev/null
+++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.h
@@ -0,0 +1,29 @@
+/*
+ * DiceSim.h
+ *
+ * Created on: 13.05.2011
+ * Author: sven
+ */
+
+#ifndef DICESIM_H_
+#define DICESIM_H_
+
+#include <vector>
+
+class DiceSim {
+public:
+ DiceSim(unsigned numDice, unsigned numRolls);
+ virtual ~DiceSim();
+ const std::vector<unsigned>& getResult() const
+ {
+ return mEyesAbsFreq;
+ }
+private:
+ unsigned mNumDice;
+ unsigned mNumRolls;
+ std::vector<unsigned> mEyesAbsFreq;
+ std::vector<unsigned>::iterator mResultIter;
+ unsigned rollDie();
+};
+
+#endif /* DICESIM_H_ */
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.cpp
new file mode 100644
index 0000000..cd5e750
--- /dev/null
+++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.cpp
@@ -0,0 +1,127 @@
+/*
+ * DiceSimController.cpp
+ *
+ * Created on: 11.05.2011
+ * Author: sven
+ */
+#ifndef DICESIMCONTROLLER_H_
+ #include "DiceSimController.h"
+#endif
+
+#ifndef _LIBGLADEMM_XML_H
+ #include <libglademm/xml.h>
+#endif
+
+#ifndef DICESIM_H_
+ #include "DiceSim.h"
+#endif
+
+#include <iostream>
+#include <fstream>
+#include "Util.h"
+
+// because of static
+const char* DiceSimController::UI_FILENAME = "ui/dicesim.glade";
+
+DiceSimController::DiceSimController() {
+ Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(UI_FILENAME);
+ builder->get_widget("mainwindow",mPtrMainWin);
+ builder->get_widget("startbtn",mPtrStartBtn);
+ builder->get_widget("dicecombo",mPtrDiceCombo);
+ builder->get_widget("rollscombo",mPtrRollsCombo);
+ builder->get_widget("textview1",mPtrTextView);
+
+ mRefPtrTextBuffer = Gtk::TextBuffer::create();
+ mPtrTextView->set_buffer(mRefPtrTextBuffer);
+
+ mRollsTreeStore = Gtk::TreeStore::create(mRollsCols);
+ mPtrRollsCombo->set_model(mRollsTreeStore);
+ mDiceTreeStore = Gtk::TreeStore::create(mDiceCols);
+ mPtrDiceCombo->set_model(mDiceTreeStore);
+
+ Gtk::TreeModel::Row rowDice1 = *(mDiceTreeStore->append());
+ rowDice1[mDiceCols.mData] = 1;
+ Gtk::TreeModel::Row rowDice2 = *(mDiceTreeStore->append());
+ rowDice2[mDiceCols.mData] = 2;
+ Gtk::TreeModel::Row rowDice10 = *(mDiceTreeStore->append());
+ rowDice10[mDiceCols.mData] = 10;
+
+ Gtk::TreeModel::Row rowRolls500 = *(mRollsTreeStore->append());
+ rowRolls500[mRollsCols.mData] = 500;
+ Gtk::TreeModel::Row rowRolls1000 = *(mRollsTreeStore->append());
+ rowRolls1000[mRollsCols.mData] = 1000;
+ Gtk::TreeModel::Row rowRolls10000 = *(mRollsTreeStore->append());
+ rowRolls10000[mRollsCols.mData] = 10000;
+
+ mPtrDiceCombo->pack_start(mDiceCols.mData);
+ mPtrRollsCombo->pack_start(mRollsCols.mData);
+ mPtrDiceCombo->set_active(0);
+ mPtrRollsCombo->set_active(0);
+
+ mPtrMainWin->signal_hide().connect(sigc::ptr_fun(&Gtk::Main::quit));
+ mPtrStartBtn->signal_clicked().connect(sigc::mem_fun(this,&DiceSimController::on_startbutton_clicked));
+}
+
+DiceSimController::~DiceSimController() {
+ // TODO Auto-generated destructor stub
+}
+
+void DiceSimController::startApp(Gtk::Main& kit)
+{
+ kit.run(*mPtrMainWin);
+}
+
+void DiceSimController::on_startbutton_clicked()
+{
+ Gtk::TreeModel::iterator diceIter = mPtrDiceCombo->get_active();
+ Gtk::TreeModel::Row diceRow = *diceIter;
+ Gtk::TreeModel::iterator rollsIter = mPtrRollsCombo->get_active();
+ Gtk::TreeModel::Row rollsRow = *rollsIter;
+ unsigned numDice = diceRow[mDiceCols.mData];
+ unsigned numRolls = rollsRow[mRollsCols.mData];
+ std::cout << "dice: " << numDice << " rolls: " << numRolls << std::endl;
+ DiceSim diceSim(numDice,numRolls);
+ std::string text = "Eyes\tabs. freq.\n";
+ const std::vector<unsigned>& results = diceSim.getResult();
+ std::vector<unsigned>::const_iterator it = results.begin();
+ unsigned n=numDice;
+ for( ; it != results.end() ; it++)
+ {
+ if (n <= 6 * numDice) {
+ text += Util::ToUString(n);
+ text += "\t";
+ text += Util::ToUString(*it);
+ text += "\n";
+ n++;
+ }
+ }
+ mRefPtrTextBuffer->set_text(text);
+ writeToCSV(results,numDice);
+ startGnuPlot();
+}
+
+void DiceSimController::writeToCSV(const std::vector<unsigned>& results, unsigned numDice)
+{
+ std::ofstream csvFile;
+ csvFile.open("dicesim.csv", std::ios::out);
+ std::vector<unsigned>::const_iterator it;
+ unsigned n=numDice;
+ for(it = results.begin() ; it != results.end() ; it++) {
+ if (n <= 6 * numDice) {
+ if (it != results.begin()) {
+ csvFile << std::endl;
+ }
+ csvFile << n << ";";
+ csvFile << *it;
+ n++;
+ }
+ }
+ csvFile.close();
+}
+void DiceSimController::startGnuPlot()
+{
+ int i = system("gnuplot dicesim.plt -p");
+ if (i != 0) {
+ std::cerr << "Error starting gnuplot: " << i << std::endl;
+ }
+}
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.h
new file mode 100644
index 0000000..3c39e30
--- /dev/null
+++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.h
@@ -0,0 +1,49 @@
+/*
+ * DiceSimController.h
+ *
+ * Created on: 11.05.2011
+ * Author: sven
+ */
+
+#ifndef DICESIMCONTROLLER_H_
+#define DICESIMCONTROLLER_H_
+
+#ifndef _GTKMM_H
+ #include <gtkmm.h>
+#endif
+
+class DiceSimController {
+public:
+ DiceSimController();
+ virtual ~DiceSimController();
+ void startApp(Gtk::Main& kit);
+private:
+
+ class Columns : public Gtk::TreeModel::ColumnRecord {
+ public:
+ Columns() {
+ add(mData);
+ }
+
+ ~Columns() {}
+
+ Gtk::TreeModelColumn<unsigned> mData;
+ };
+ static const char* UI_FILENAME;
+ Gtk::Window* mPtrMainWin;
+ Gtk::Button* mPtrStartBtn;
+ Gtk::ComboBox* mPtrDiceCombo;
+ Gtk::ComboBox* mPtrRollsCombo;
+ Glib::RefPtr<Gtk::TreeStore> mDiceTreeStore;
+ Glib::RefPtr<Gtk::TreeStore> mRollsTreeStore;
+ Gtk::TextView* mPtrTextView;
+ Glib::RefPtr<Gtk::TextBuffer> mRefPtrTextBuffer;
+ Columns mDiceCols;
+ Columns mRollsCols;
+
+ void on_startbutton_clicked();
+ void writeToCSV(const std::vector<unsigned>& results, unsigned numDice);
+ void startGnuPlot();
+};
+
+#endif /* DICESIMCONTROLLER_H_ */
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/Util.h b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/Util.h
new file mode 100644
index 0000000..3ff7d76
--- /dev/null
+++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/Util.h
@@ -0,0 +1,78 @@
+/*
+ * Util.h
+ *
+ * Created on: 31.03.2011
+ * Author: sven
+ */
+
+#ifndef UTIL_H_
+#define UTIL_H_
+
+#ifndef _GLIBCXX_SSTREAM
+ #include <sstream>
+#endif
+#ifndef _GLIBMM_USTRING_H
+ #include <glibmm/ustring.h>
+#endif
+
+class Util
+{
+public:
+ static Glib::ustring ToUString(unsigned short val)
+ {
+ std::ostringstream ssIn;
+ ssIn << val;
+ Glib::ustring res = ssIn.str();
+ return res;
+ }
+
+ static Glib::ustring ToUString(short val)
+ {
+ std::ostringstream ssIn;
+ ssIn << val;
+ Glib::ustring res = ssIn.str();
+ return res;
+ }
+
+ static Glib::ustring ToUString(unsigned val)
+ {
+ std::ostringstream ssIn;
+ ssIn << val;
+ Glib::ustring res = ssIn.str();
+ return res;
+ }
+
+ static Glib::ustring ToUString(double val)
+ {
+ std::ostringstream ssIn;
+ ssIn << val;
+ Glib::ustring res = ssIn.str();
+ return res;
+ }
+
+ static unsigned short ToUShort(Glib::ustring& val)
+ {
+ std::istringstream buffer(val.raw());
+ unsigned short res;
+ buffer >> res;
+ return res;
+ }
+
+ static unsigned ToUint(Glib::ustring& val)
+ {
+ std::istringstream buffer(val.raw());
+ unsigned res;
+ buffer >> res;
+ return res;
+ }
+
+ static short ToShort(Glib::ustring& val)
+ {
+ std::istringstream buffer(val.raw());
+ short res;
+ buffer >> res;
+ return res;
+ }
+};
+
+#endif /* UTIL_H_ */
diff --git a/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/main.cpp b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/main.cpp
new file mode 100644
index 0000000..c27bcd4
--- /dev/null
+++ b/Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/main.cpp
@@ -0,0 +1,23 @@
+/*
+ * main.cpp
+ *
+ * Created on: 11.05.2011
+ * Author: sven
+ */
+
+#ifndef DICESIMCONTROLLER_H_
+ #include "DiceSimController.h"
+#endif
+
+#ifndef _GTKMM_H
+ #include <gtkmm.h>
+#endif
+
+int main(int argc, char* argv[])
+{
+ Gtk::Main kit(argc,argv);
+ DiceSimController diceSimController;
+ diceSimController.startApp(kit);
+
+ return 0;
+}