From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../Aufgabenblatt2/DiceSim/src/DiceSim.cpp | 47 ++++++++ .../Aufgabenblatt2/DiceSim/src/DiceSim.h | 29 +++++ .../DiceSim/src/DiceSimController.cpp | 127 +++++++++++++++++++++ .../Aufgabenblatt2/DiceSim/src/DiceSimController.h | 49 ++++++++ .../Aufgabenblatt2/DiceSim/src/Util.h | 78 +++++++++++++ .../Aufgabenblatt2/DiceSim/src/main.cpp | 23 ++++ 6 files changed, 353 insertions(+) create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.cpp create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSim.h create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.cpp create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/DiceSimController.h create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/Util.h create mode 100644 Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src/main.cpp (limited to 'Master/Modellbildung_und_Simulation/Aufgabenblatt2/DiceSim/src') 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 +#include +#include + +#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 + +class DiceSim { +public: + DiceSim(unsigned numDice, unsigned numRolls); + virtual ~DiceSim(); + const std::vector& getResult() const + { + return mEyesAbsFreq; + } +private: + unsigned mNumDice; + unsigned mNumRolls; + std::vector mEyesAbsFreq; + std::vector::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 +#endif + +#ifndef DICESIM_H_ + #include "DiceSim.h" +#endif + +#include +#include +#include "Util.h" + +// because of static +const char* DiceSimController::UI_FILENAME = "ui/dicesim.glade"; + +DiceSimController::DiceSimController() { + Glib::RefPtr 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& results = diceSim.getResult(); + std::vector::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& results, unsigned numDice) +{ + std::ofstream csvFile; + csvFile.open("dicesim.csv", std::ios::out); + std::vector::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 +#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 mData; + }; + static const char* UI_FILENAME; + Gtk::Window* mPtrMainWin; + Gtk::Button* mPtrStartBtn; + Gtk::ComboBox* mPtrDiceCombo; + Gtk::ComboBox* mPtrRollsCombo; + Glib::RefPtr mDiceTreeStore; + Glib::RefPtr mRollsTreeStore; + Gtk::TextView* mPtrTextView; + Glib::RefPtr mRefPtrTextBuffer; + Columns mDiceCols; + Columns mRollsCols; + + void on_startbutton_clicked(); + void writeToCSV(const std::vector& 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 +#endif +#ifndef _GLIBMM_USTRING_H + #include +#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 +#endif + +int main(int argc, char* argv[]) +{ + Gtk::Main kit(argc,argv); + DiceSimController diceSimController; + diceSimController.startApp(kit); + + return 0; +} -- cgit v1.2.3