#include <iostream>
using namespace std;
struct ElementMR{
unsigned int l,c;
double val;
};
struct NodMR{
ElementMR tuplu;
NodMR *next;
};
NodMR* InserareSf(NodMR* l,ElementMR e){
NodMR* nou=new NodMR;
nou->tuplu=e;
nou->next=NULL;
if(!l)
return nou;
else{
NodMR* temp=l;
while(temp->next)
temp=temp->next;
temp->next=nou;
return l;
}
}
NodMR* ConversieToLSMatriceRara(double A[][100], unsigned int m, unsigned int n,
char *err, unsigned int *nrEl){
*nrEl=0;
unsigned int i,j;
NodMR *lstMR=NULL;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(A[i][j])
(*nrEl)++;
double pondere=*nrEl;
pondere=pondere/(m*n);
if(pondere>=0.0015 && pondere<=0.03){
*err=1;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(A[i][j]){
ElementMR t;
t.l=i+1;
t.c=j+1;
t.val=A[i][j];
lstMR=InserareSf(lstMR,t);
}
return lstMR;
}
else{
*err=0;
return 0;
}
}
NodMR* StergereInc(NodMR* l){
if(l){
NodMR* temp=l;
l=l->next;
delete temp;
}
return l;
}
NodMR* ExtragereNodPoz(NodMR* l,unsigned int poz, unsigned int n){
if(poz<1 || poz >n)
return 0;
else{
NodMR *temp=l;
unsigned int i;
for(i=1;i<poz;i++)
temp=temp->next;
return temp;
}
}
bool esteMatriceRara(double nrElementeNenule, double nrLinii, double nrColoane)
{
double gradUmplere = 0;
gradUmplere = nrElementeNenule/(nrLinii * nrColoane);
if (gradUmplere < 0.03) return true;
else
return false;
}
// Exista nod pe linia si coloana precizata, intoarce true(adevarat)
bool existaNodPePozitia(NodMR* lista, int linia, int coloana, double * val)
{
lista = lista->next;
while (lista!= NULL)
{
if (lista->tuplu.l == linia && lista->tuplu.c == coloana)
{
*val = lista->tuplu.val;//valoarea se va putea lua prin referinta dupa executia metodei
return true;
}
lista = lista->next;
}
return false;
}
NodMR* citireListaMatriceRara(int nrElementeNenule)
{
NodMR* nod = new NodMR();
NodMR* capLista =nod;
nod->next = NULL;
int linia, coloana;
double val;
for (int i = 0; i < nrElementeNenule; i++)
{
cout<<"Dati pozitia pe linie:";
cin>>linia;//citesc pozitia pe linie
cout<<"Dati pozitia pe coloana:";
cin>>coloana;//citesc pozitia pe coloana
cout<<"Dati valoarea de pe linia "<<linia<<" si coloana "<<coloana<<endl;
cin>>val;
ElementMR elem;
elem.l = linia;
elem.c = coloana;
elem.val = val;
//inserare in lista
InserareSf(nod, elem);
}
return capLista;
}
void afisareLista(NodMR* lista)
{
lista = lista->next;
while (lista!= NULL)
{
cout<<"Linia: "<<lista->tuplu.l;
cout<<"Coloana: "<<lista->tuplu.c;
cout<<"Valoare: "<<lista->tuplu.val<<endl;
lista = lista->next;
}
}
//Adunarea a 2 matrici rare sub forma de liste
NodMR* adunareMatriciRare(NodMR* matr1, NodMR* matr2)
{
NodMR* capLista = new NodMR();
NodMR* nod = capLista;
NodMR* capListaMatrice1 =matr1;
NodMR* capListaMatrice2 =matr2;
matr1 = matr1->next;
while (matr1!= NULL) //primul nod contine doar referinta si nu informatie utila, de aceea se foloseste al doilea elem
{//parcurg prima matrice, daca gasesc elemente care nu se regasesc in adoua matrice le pun ca atare in matricea rezultat al adunarii
double val =0;
if (!existaNodPePozitia(capListaMatrice2, matr1->tuplu.l, matr1->tuplu.c, &val ))
{
InserareSf(capLista,matr1->tuplu);
}
else
{
//daca exista un element pe aceeasi pozitie in maticea 2 ca cea din matricea 1, adaugam cele 2 valori si le punem in matricea rezultat al adunarii
ElementMR elem;
elem.l = matr1->tuplu.l;
elem.c = matr1->tuplu.c;
elem.val = matr1->tuplu.val + val;
InserareSf(capLista,elem);
}
matr1 = matr1->next;
}
matr2 = matr2->next;
while (matr2!= NULL)
{//parcurg a doua matrice, daca gasesc elemente care nu se regasesc in prima matrice le pun ca atare in matricea rezultat al adunarii
double val =0;
if (!existaNodPePozitia(capListaMatrice1, matr2->tuplu.l, matr2->tuplu.c, &matr2->tuplu.val ))
{
ElementMR elem;
elem.l = matr2->tuplu.l;
elem.c = matr2->tuplu.c;
elem.val = matr2->tuplu.val;
InserareSf(capLista,elem);
}
//daca exista. deja avem adunarea la pasul precedent
matr2 = matr2->next;
}
return capLista;
}
void main()
{
bool esteMatrRara = false;
int nrElementeNenule, nrLinii, nrColoane;
do
{
cout<<"Dati numarul de elemente nenule:";
cin>>nrElementeNenule;
cout<<"Dati numarul de linii ale matricei rare:";
cin>>nrLinii;
cout<<"Dati numarul de coloane ale matricei rare:";
cin>>nrColoane;
esteMatrRara = esteMatriceRara(nrElementeNenule, nrLinii, nrColoane);
if (esteMatrRara == false)
{
cout<<"Gradul de umplere depaseste 30%. Matricea nu este o matrice rara.";
}
}
while (esteMatrRara == false);
NodMR* listaMatriceRara1 = citireListaMatriceRara(nrElementeNenule);//se citeste prima matrice
cout<<"Afisare matrice rara 1";
afisareLista(listaMatriceRara1);
cout<<"Dati numarul de elemente nenule matricea a doua:";
cin>>nrElementeNenule;
NodMR* listaMatriceRara2 = citireListaMatriceRara(nrElementeNenule);//se citeste a doua matrice
cout<<"Afisare matrice rara 2";
afisareLista(listaMatriceRara2);
cout<<"Afisare matrice adunata rezultata";
NodMR* listaAdunata = adunareMatriciRare(listaMatriceRara1, listaMatriceRara2);
afisareLista(listaAdunata);
char c;
cin>>c;
}
//NodMR* lista1;
// double Mat[25][100],val;
// unsigned int m, n, i, j;
// unsigned int nrEl;
// char err;
// m=25;
// n=100;
// NodMR *lstMatRara=NULL;
// for(i=0;i<m;i++)
// for(j=0;j<n;j++)
// Mat[i][j]=0;
//
// char opt='d';
// while(opt=='d'){
// do{
// cout<<"Introduceti indexul de linie a elementului nenul: ";
// cin>>i;
// }
// while(i>25 || i==0);
// do{
// cout<<"Introduceti indexul de coloana a elementului nenul:";
// cin>>j;
// }
// while(j>100 || j==0);
// cout<<"Introduceti valoarea elementului nenul: ";
// cin>>val;
// if(!Mat[i-1][j-1])
// Mat[i-1][j-1]=val;
// else{
// cout<<"Pozitia indicata contine un element de valoare \
//nenula! Rescrieti elementul?(d/n): ";
// cin>>opt;
// if(opt=='d')
// Mat[i-1][j-1]=val;
// }
// cout<<"Continuati?(d/n): ";
// cin>>opt;
// }
//
// lstMatRara=ConversieToLSMatriceRara(Mat,m,n,&err,&nrEl);
//
// if(err){
// cout<<"Structura de tip Matrice Rara este:"<<endl;
// //afisare lista simpla
// for(i=1;i<=nrEl;i++){
// NodMR *t;
// t=ExtragereNodPoz(lstMatRara,i,nrEl);
// if(t){
// cout<<t->tuplu.l<<"\t"<<t->tuplu.c<<"\t" \
//<<t->tuplu.val<<endl;
// }
// }
//
// //stergere lista simpla
// while(lstMatRara)
// lstMatRara=StergereInc(lstMatRara);
// }
// else
// cout<<"Masivul bidimensional nu indeplineste criteriile \
//de matrice rara!"<<endl;
//
//
// //
// char c;
// scanf_s("%c", &c);
//}
meditatii-informatica
sâmbătă, 12 decembrie 2015
marți, 2 decembrie 2014
Program 3 will be worth 100 points and will
be assigned points according to the following scheme:
Program
compiles 25
Points
Documentation
Header documentation (personal) 10
Points
Internal documentation (what your program
is doing) 10 Points
Style
Indentation 5
Points
White space 5
Points
Output
Correctly calculated 20
Points
Correct use of Functions 15
Points
Correctly printed output (on monitor) 10
Points
Total
100 Points
Assignment:
Part 1:
Write
a program to do the following:
Write a program to implement pattern
matching and sorting techniques. Give user a menu choice to either implement
pattern matching or sorting technique. Keep running this code until this user
decides to exit.
Pattern
Matching Part:
Accept some text from the user. Accept the
string that needs to be searched. Your program is supposed to print the number
of occurrences of the string found within the text, and the position at which
the pattern was found. Look at the following sample output:
Sample Output:
Enter
text: “Call me Ishmael. Some years ago - never mind
how long precisely - having little or no money in my purse, and nothing
particular to interest me on shore, I thought I would sail about a little and
see the watery part of the world. It is a way I have of driving off the spleen,
and regulating the circulation”
String to search – “Some years ago”
Number of occurrences – 1
Position Found – 18
Sorting Techniques/Methods:
Generate 10 random numbers from 1 - 100 and
sort those using the following sorting method. Each sorting method should be an
individual function:
Bubble Sort – Look at the
pseudo code explanation and pseudo code is in text book
Input Validation: Make sure only unique
random numbers are generated
Sample Output:
Initial
Set of numbers: 9 34 12 1 45 67 55 100 11 91
Bubble
Sort – 1 9 11 12 34 45 55 67 91 100
Part
2 (optional):
Create a new program. Copy the contents of
your PART A code. Modify the above
code to add another sorting technique to the program.
Selection Sort – Look at
the pseudo code explanation and pseudo code is in text book
Modify the menu choice, so when user
chooses sorting, you give them 2 more menu choices to choose from: Bubble Sort,
Selection Sort. Keep doing this user decided to exit. (If user chooses exit,
then exit back to the Main Menu choice)
Sample Output:
Initial
Set of numbers: 9 34 12 1 45 67 55 100 11 91
Bubble
Sort – 1 9 11 12 34 45 55 67 91 100
Initial
Set of numbers: 19 44 22 11 55 77 25 100 3 81
Selection
Sort – 3 11 19 22 25 44 55 77 81 100
Before
you start writing code, take some time to think about design of this program.
Decompose into a set of functions, and create a main() function that reads like
an outline for your solution to the problem. Make sure each function does one
task.
* * Be sure to print the header documentation as you
did in the fist programming assignment.
* *
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#include "sortalg.h"
//All the main menu options.
enum MAIN_MENU_OPTIONS {PATTERN_MATCHING, SORTING_TECHNIQUES, MAIN_MENU_EXIT, UNKNOWN};
//Search for occurences of searchText into text. Output in foundPositions the positions in the text where the searchtext is found.
//Returns number of occurences.
int seachForText(std::string text, std::string searchText, std::vector<int> &foundPositions)
{
int numberOfOccurences = 0;
std::size_t pos = text.find(searchText, 0);
while(pos != std::string::npos)
{
foundPositions.push_back(pos+1);
pos = text.find(searchText, pos+1);
numberOfOccurences++; //if text is found, increase by 1 the counter of texts found
}
return numberOfOccurences;
}
//Display a vector of numbers(integer) separated by space.
void displayVector(std::vector<int> vect)
{
std::vector<int>::const_iterator i;
for(i=vect.begin(); i!=vect.end(); ++i)
{
std::cout<<(*i)<<" ";
}
}
//Display search menu.
void GetSearchTextAndFullText(std::string &fullText, std::string &searchString)
{
//display to the screen to inform the user to enter some text
std::cout<<"Please enter some text:";
//read from the user the text into 'fullText' variable(some text).
std::getline (std::cin,fullText);
//display to the screen to inform the user to enter search text
std::cout<<"The string that needs to be searched:";
//read from the user the text to be searched into 'searchText' variable
std::getline (std::cin, searchString);
}
//display found positions
void displayFoundPositions(std::vector<int> foundPos)
{
std::cout<<"Position found - ";
displayVector(foundPos);
}
//display number of occurences
void displayNumberOfOccurences(int number)
{
std::cout<<"Number of occurences - "<<number<<std::endl;
}
//Menu for search - read input and search search, and output to the screen number of occurences and found positions
void patternMatchingMenu()
{
//search the input string into text
std::string fullText, searchText;
GetSearchTextAndFullText(fullText, searchText);//input by user full text and text to be seached
std::vector<int> foundPos;
int numberOfOccurences = seachForText(fullText, searchText, foundPos); //search the positions of searched text and full text and return number of occurences
//display number of occurences
displayNumberOfOccurences(numberOfOccurences);
displayFoundPositions(foundPos);
}
//Generate numbers = numberOfItems, values between minValue and maxValue.
//Returns the numbers generated in a vector.
std::vector<int> generateUniqueNumbers(int numberOfItems, int minValue, int maxValue)
{
std::vector<int> generatedValues;
for (int p = 0; p < numberOfItems; p++)
{
int num = minValue;
bool found = false;
do
{
found = false;
num = rand() % maxValue + minValue; //generate the number
std::vector<int>::const_iterator i;
for(i=generatedValues.begin(); i!=generatedValues.end(); ++i) //if the number is already in the list, generate another number (numbers are unique)
//the list items are compared with generated number
{
if ((*i) == num)
{
found = true;
}
}
}
while (found == true);
generatedValues.push_back(num); //if not already in list, add it
}
return generatedValues;
}
void displaySortingTechniquesMenu();
void displaySortingTechniquesMenuLoop();
//read the option selected from main menu
MAIN_MENU_OPTIONS readMainMenuOption()
{
//read line from keyboard
std::string read;
std::getline (std::cin,read);
if (read[0] == '1')
return PATTERN_MATCHING;
if (read[0] == '2')
return SORTING_TECHNIQUES;
if (read[0] == '3')
return MAIN_MENU_EXIT;
return UNKNOWN;//if none of these, uknown menu
}
//Display menu according to option selected in main menu
void mainMenuSelection(MAIN_MENU_OPTIONS option)
{
switch(option)
{
case PATTERN_MATCHING:
patternMatchingMenu();
break;
case SORTING_TECHNIQUES:
displaySortingTechniquesMenuLoop();
break;
case MAIN_MENU_EXIT:
break;
}
}
//displays the main menu texts
void displayMainMenu()
{
std::cout<<std::endl<<"Main Menu"<<std::endl;
std::cout<<"1.Pattern Matching"<<std::endl;
std::cout<<"2.Sorting techniques"<<std::endl;
std::cout<<"3.Exit"<<std::endl;
}
//Displays the main menu while '3'= Exit is selected
void displayMainMenuLoop()
{
MAIN_MENU_OPTIONS option = UNKNOWN;
do
{
//displays on screen the menu
displayMainMenu();
option = readMainMenuOption();
mainMenuSelection(option);
}
while(option != MAIN_MENU_EXIT); //exit the loop on selecting 3 option
}
//Display sorting techniques menu
void displaySortingTechniquesMenu()
{
std::cout<<std::endl<<"Please select an option."<<std::endl;
std::cout<<std::endl<<"1.Bubble sort"<<std::endl;
std::cout<<"2.Selection sort"<<std::endl;
std::cout<<"3.Exit"<<std::endl;
}
//Displays menu for selecting sort algorithm, select algorithm sort 1 = Bubble Sort, 2 = Selection Sort, 3 = Exit
void displaySortingTechniquesMenuLoop()
{
std::string read;
do
{
displaySortingTechniquesMenu();
//read from keyboard user input - menu selection
std::getline (std::cin,read);
if (read[0] == '1') //select bubble sort algorithm
{
std::vector<int> vectorUniqueNumbers = generateUniqueNumbers(10, 1, 100); //generate 10 numbers, from 1 to 100
cout<<"Initial set of numbers: "; //display text
displayVector(vectorUniqueNumbers); //display generated vector
bubble_sort(vectorUniqueNumbers); //sort the vector
std::cout<<std::endl<<"Bubble sort - "; //display text
displayVector(vectorUniqueNumbers); //display sorted vector
}
if (read[0] == '2')//select selection sort algorithm
{
std::vector<int> vectorUniqueNumbers = generateUniqueNumbers(10, 1, 100);//generate 10 numbers, from 1 to 100
cout<<"Initial set of numbers: "; //display text
displayVector(vectorUniqueNumbers); //display generated vector
selectionSort(vectorUniqueNumbers); // sort the vector
std::cout<<std::endl<<"Selection sort - ";//display text
displayVector(vectorUniqueNumbers); //display sorted vector
}
if (read[0] == '3') //select display main menu
{
}
}while(read[0] != '3'); //exit the loop on selecting 3 option
}
//main function - entry point
int main()
{
displayMainMenuLoop();
}
#include <iostream>
#include <string>
#include <vector>
//Bubble sort algorithm on a vector
void bubble_sort(std::vector<int>& a)
{
for (int i = a.size(); i > 0;i--)
{
for (int j = 0, k = 1; k < i;j++, k++)
{
if (a[j] > a[k])
{
int swap = a[j];
a[j] = a[k];
a[k] = swap;
}
}
}
}
//swap items on position i with item in position j in vector
void swap(std::vector<int> & data, int i, int j)
{
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
//selection sort algorithm on a vector
void selectionSort(std::vector<int> & data)
{
int length = data.size();
for (int i = 0; i < length; ++i)
{
int min = i;
for (int j = i+1; j < length; ++j)
{
if (data[j] < data[min])
{
min = j;
}
}
if (min != i)
{
swap(data, i, min);
}
}
}
duminică, 18 mai 2014
sâmbătă, 25 ianuarie 2014
C++ 11 FEATURES
Non-static data member initializers
http://msdn.microsoft.com/en-us/library/hh567368.aspx
1.
#include <iostream>
class A {
public:
int a = 7;
};
void main(){
A a;
std::cout<< a.a;
}
--Visual Studio 2012 - nu suporta initializarea variabilei membre
Error 1 error C2864: 'A::a' : only static const integral data members can be initialized within a class
Vs 2013 OK
2.Initializer lists
class A {
public:
A(int a)
{
this->a = a;
}
int a = 7;
};
Non-static data member initializers
http://msdn.microsoft.com/en-us/library/hh567368.aspx
1.
#include <iostream>
class A {
public:
int a = 7;
};
void main(){
A a;
std::cout<< a.a;
}
--Visual Studio 2012 - nu suporta initializarea variabilei membre
Error 1 error C2864: 'A::a' : only static const integral data members can be initialized within a class
Vs 2013 OK
2.Initializer lists
class A {
public:
A(int a)
{
this->a = a;
}
int a = 7;
};
void main()
{
A a{ 8 };// {} instead of ()
std::cout<< a.a;
}
Books for Microsoft .NET Certification
MCTS Self-Paced Training Kit (Exam 70-511): Windows Applications Development with Microsoft® .NET Framework 4
http://www.microsoft.com/learning/en-us/book.aspx?id=14321
MCTS Self-Paced Training Kit (Exam 70-515): Web Applications Development with Microsoft® .NET Framework 4
http://www.amazon.com/MCTS-Self-Paced-Training-Exam-70-515/dp/0735627401
Call function from DLL
Native dll function call C++
typedef __DLL_FUNCTION bool PMyFUNCT (const wchar_t* szCon, const wchar_t* szName); HINSTANCE lib; void myMethod(const wchar_t* szCon, const wchar_t* szName)
{
int lastError = 0; bool bSaved = false; lib = LoadLibrary("mylib.dll"); if(lib) { // Get the address of the function PMyFUNCT *cFunction; cFunction = ( PMyFUNCT *)GetProcAddress(lib, "_myFunct"); if(cFunction) { cFunction(szCon, szName); } } }
typedef __DLL_FUNCTION bool PMyFUNCT (const wchar_t* szCon, const wchar_t* szName); HINSTANCE lib; void myMethod(const wchar_t* szCon, const wchar_t* szName)
{
int lastError = 0; bool bSaved = false; lib = LoadLibrary("mylib.dll"); if(lib) { // Get the address of the function PMyFUNCT *cFunction; cFunction = ( PMyFUNCT *)GetProcAddress(lib, "_myFunct"); if(cFunction) { cFunction(szCon, szName); } } }
News in .NET 4.5
What's New in the .NET Framework 4.5 - http://msdn.microsoft.com/en-us/library/ms171868(v=vs.110).aspx
C++
C++ 11 - standard http://en.wikipedia.org/wiki/C%2B%2B11
Support for C++ Features in Visual Studio 2012/2013
http://msdn.microsoft.com/en-us/library/hh567368.aspx
Ex. Variadic_templates
template <typename... BaseClasses> class ClassName : public BaseClasses... {
public:
ClassName(BaseClasses&&... base_classes) : BaseClasses(base_classes)... {}
};
Abonați-vă la:
Postări (Atom)