Académique Documents
Professionnel Documents
Culture Documents
Lp: 09T2_nhm7A
Bi 1: STACK
###############Node_t.h#################
#ifndef NODE_T_H
#define NODE_T_H
#include "iostream.h"
/*
* No description
*/
template <typename T>
class Node{
T Data;
Node<T> *link;
public:
Node(){
Data=0;
link=NULL;
}
Node(T item, Node<T> *link = NULL);
~Node(){ delete [] link;}
Node<T> &operator =(const Node<T> &N);
operator T();
friend istream &operator >>(istream &in, Node<T> &N);
friend ostream &operator <<(ostream &out,Node<T> &N);
};
//--------------------------------------------------------template<typename T>
Node<T>::Node(T item, Node<T> *link = NULL){
Data=item;
this->link=link;
}
//---------------------------------------------------------template<typename T>
Node<T> Node<T>::&operator =(const Node<T> &N){
Data=N.Data;
link=N.link;
}
//---------------------------------------------------------template<typename T>
Node<T>::operator T(){
return Data;
}
//---------------------------------------------------------template<typename T>
istream &operator >>(istream &in,Node<T> &N){
cout<<"Nhap vao phan tu: ";
in>>N.Data;
return ∈
}
//---------------------------------------------------------template<typename T>
###################Stack_t.h###################
// Class automatically generated by Dev-C++ New Class wizard
#ifndef STACK_T_H
#define STACK_T_H
#include "node_T.h"
/*
* No description
*/
template <typename T>
class Stack_T{
public:
Stack_T();
~Stack_T();
bool empty() const;
int &operator +=(const T &item);
int &operator --();
Node<T> &operator();
private:
Node<T> *top_node;
};
#endif // STACK_T_H
###############Stack.cpp##################
// Class automatically generated by Dev-C++ New Class wizard
// class constructor
template <typename T>
Stack_T<T>::Stack_T()
{
// insert your code here
Node *new_top = new Node<T>(item,NULL);
top_node = new_top;
}
// class destructor
template <typename T>
Stack_T<T>::~Stack_T()
{
delete [] this->top_node;
delete *this; // Giai phong con tro
// insert your code here
}
//====================================================================
template <typename T>
bool Stack_T<T>::empty(){
return top_node==NULL?true:false;
}
//====================================================================
template <typename T>
int Stack_T<T>::&operator +=(const T &item);
{
Node *new_top = new Node<T>(item, top_node);
if (new_top == NULL) return 0;//errorflow
top_node = new_top;
return 1;
}
//==================================================================
template <typename T>
int Stack_T<T>::&operator --()
{
Node *old_top = top_node;
if (top_node == NULL) return 0;//empty Stack
top_node = old_top.link;
delete old_top;
return 1;
}
//==================================================================
template<typename T>
Node<T> Stack_T<T>::&operator(){
if(top_node==NULL) return 0;
Node<T> tmp;
tmp=top_node;
tmp.link=NULL;
return tmp;
}
###################Main.cpp##########################
#include <cstdlib>
#include <iostream>
#include "node_T.h"
#include "stack_t.h"
using namespace std;
int main()
{
Stack_T<int> *S;
for(int i=0;i<10;i++) S+=i;
cin>>S();
system("PAUSE");
return EXIT_SUCCESS;
}
Bi 2: FILE VN BN
#include<stdio.h>
#include<fstrean.h>
#include<string.h>
#include<stdlib.h>
#include<strstrean.h>
int countKt(char str[], char kt) {
int dem=0;
for(int i=0; i<strlen(kt);i++)
if(str[i]==key)
dem++;
return dem;
}
int coutnTu(char[] *str,char *s,int n){
int dem=0;
for(int i=0;i<n;i++)
if(strcmp(s,str[i]==0) dem++;
return dem;
}
int main(){
ifstream in("vao.txt", ios::in);//lay file van ban tu nguon
ofstream out("ra.txt",ios::out);//Xuat file van ban sau khi da xu li
if(!in){
cout << Khong the tao duoc tep tin << in << endl;
exit(1);
}
if(!out){
cout << Khong the tao duoc tep tin << out << endl;
exit(1);
}
//Doc file vao theo tung dong
char line[255];
int coutline=0,demkt=0,demtu,tuDong;//dung de viet hoa dau van ban
char kt;
char *tudem;
char *tu[1000];
//Dem so ki tu;
cout<<"Nhap vao ki tu can dem: ";
cin>>kt;
cour<"Nhap vao tu can dem: ";
cin>>tudem;
//Doc file vao
while(in.getline(linein,255)){//ddoc toi da chuoi co chieu dai la 255 hoac gap ki tu
"Enter" luu vao mang "linein"
//xu li van ban theo tung dong
//Loai bo khoang trang thua
for(int k=0;k<strlen(line)-1;k++){
//loai bo ki tu trong dau!
while (line[0]==' ') strcpy(line,&line[1]);
//loai bo ki tu trong o cuoi
while (line[strlen(line)-1]==' ') line[strlen(line)-1]=NULL;
//loai bo cac ki tu o giua
for (int i=0; i<strlen(line)-1; i++)
if (line[i]==' ' && line[i+1]==' ')
{
}
demtu+=coutnTu(tu,tudem,tudong)
}
}
cout<<"Tan suat xuat hien cua ki tu %c la: %d",kt,demkt);
cout<<"Tan suat xuat hien cua tu %s la: %d",tudem,demtu);
Out.close();
in.close();
return 0;