hanhfuckhjbenanh Moderators Angle
Tổng số bài gửi : 11 Points : 22 Reputation : 100 Join date : 14/11/2009 Age : 35 Đến từ : Đồng Nai
| Tiêu đề: tài liệu tổng hợp kỹ thuật lập trình Tue Nov 24, 2009 4:50 pm | |
| http://www.mediafire.com/?my2nz20nkujhttp://www.mediafire.com/?tuj5ko5dijdhttp://www.mediafire.com/?iqimr3m2zyghttp://www.mediafire.com/?jyiyhyzwy3n - Code:
-
Tam giác passcal = đệ quy
#include <iostream.h> #include <fstream.h> #include <iomanip.h> void TamGiacCan(int); void TamGiacVuong(int); int Tamgiac(int ,int ); int Tamgiac(int a,int pt) { if(a == 1 || a==pt ) return 1; else return Tamgiac(a,pt-1) + Tamgiac(a-1,pt-1); } ifstream nhap; ofstream xuat; void main(void ) { nhap.open("input.txt");
xuat.open("output.txt"); xuat<<"Tam giac Pascal:" <<endl; int h; nhap>>h; //TamGiacVuong(h); //chi xuat ra 1 trong 2 TamGiacCan(h); } void TamGiacCan(int h) { for(int b=1;b<=h;b++) { xuat<<setw(h-b+1); for(int a=1; a<=b;a++) xuat<<Tamgiac(a,b)<<" "; xuat<<endl; } } void TamGiacVuong( int h) { for(int b=1;b<=h;b++) { for(int a=1;a<=b;a++) xuat<<setw(3)<<Tamgiac(a,b)<<" "; xuat<<endl; } nhap.close(); xuat.close(); } Ma Trận ziczac chéo
1 3 4 10 11 2 5 9 12 19 6 8 13 18 20 7 14 17 21 24 15 16 22 23 25
# include <iostream.h> # include <iomanip.h>
const int DONG = 10; const int COT = 10; int A[DONG][COT]; int B[DONG][COT];
int main () { int n; cout << " Nhap n: "; cin >> n; cout << " Ket qua: " << endl; int i, j, x = 2, flag = 1; A[0][0] = 1; A[n-1][n-1] = n * n; while (x <= n * n && flag < n) { if (flag % 2 != 0) { for (i = flag, j = 0; i >= 0; i--, j++) { A[i][j] = x++; } flag++; } else { for (j = flag, i = 0; j >= 0; j--, i++) { A[i][j] = x++; } flag++; } } int f = 1; if ( n % 2 != 0 ) { while ( x <= n * n ) { if (f % 2 == 0) { for (j = n-1, i = f; i < n; i++, j--) { A[i][j] = x++; } f++; } else { for (j = f, i = n-1; j < n; j++, i--) { A[i][j] = x++; } f++; } } for (i = 0; i < n; i++) { cout << endl; for (int j = 0; j < n; j++) { cout << setw(4) << A[i][j]; } cout << endl; } } else { while ( x <= n * n ) { if (f % 2 == 0) { for (j = f, i = n-1; j < n; j++, i--) { A[i][j] = x++; } f++; } else { for (j = n-1, i = f; i < n; i++, j--) { A[i][j] = x++; } f++; } } for (i = 0; i < n; i++) { cout << endl; for (int j = 0; j < n; j++) { cout << setw(4) << A[i][j]; } cout << endl; } } return 0; }
Tổng các phần tử ở vị trí fibonasi
#include <iostream.h> #include <iomanip.h> #define dong 20 #define cot 20
void Xuat (int a[][cot], int m , int n); int La_So_Fibo (int x);
void main () { int a[dong][cot]; int m , n , i , j , tong=0; cout << "Nhap so dong : "; cin >> m; cout << "Nhap so cot : "; cin >> n; cout << "Vui long nhap " << m*n << " phan tu : "; for (i=0; i<m; i++) for (j=0; j<n; j++) cin >> a[i][j]; Xuat (a, m , n); for (i=0; i<m; i++) { tong=0; for (j=0; j<n; j++) { if (La_So_Fibo(j)!=0) tong+=a[i][j]; } cout << "Tong cac phan tu o vi tri la so Fibo tren dong " << i << " la : " << tong << endl; } } void Xuat (int a[][cot], int m , int n) { for (int i=0; i<m; i++) { cout << endl; for (int j=0; j<n; j++) { cout << setw(4) << a[i][j]; } cout << endl; } } int La_So_Fibo (int x) { int x1=1 , x2=1 , x3=x1+x2; if (x==1) return 1; while (x3<x) { x1=x2; x2=x3; x3=x1+x2; } return x==x3; }
Ziczac ngang
#include <iostream.h>
const int size = 50 ; void zichzac(int [][size] , int , int ); void xuat ( int [][size] , int , int ); void main () { int n , m ; int a [size][size]; cout << " Nhap Kich Thuoc " << '\t' ; cin >> n >> m ; zichzac (a,n,m); xuat(a,n,m);
} void zichzac(int array [][size] , int n , int m ) { int num = 1 ; for (int i = 0 ; i < n ; i ++) { if( i%2 == 0) { for (int j = 0; j < m ; j ++) { array[i][j] = num ++ ; } } else if( i%2 != 0) for ( int j = m - 1 ; j >= 0 ; j --) { array[i][j] = num ++ ; } } } void xuat ( int array [][size] , int n , int m) { for ( int i = 0 ; i < n ; i++ ) { cout << "\n\t" ; for (int j = 0 ; j < m ; j ++) cout << array [i][j] << '\t' ; cout << endl; } }
Ma trận xoắn óc
5 4 3 6 9 2 7 8 1
#include<iostream.h> const int max=100; bool Bidung(int A[max][max],int m,int n,int x,int y,int dx,int dy,int dem); bool Bidung(int A[max][max],int m,int n,int x,int y,int dx,int dy,int dem) { if((x+dx<0)||(x+dx>=m)||(y+dy<0)||(y+dy>=n)) return true; else if(A[x+dx][y+dy]!=0) return true; else return false; } void main() { int A[max][max]={0}; int m,n; cout<<"nhap vao m n:"; cin >> m >> n; int x,y,dx,dy,dem,t; x=m-1; (*) y=n-1; dx=0; dy=1; dem=0; for(int i=0;i<m*n;i++) { dem++; A[x][y]=dem; while((Bidung(A,m,n,x,y,dx,dy,dem)==true) &&(dem<m*n)) { t=dx; dx=-dy; dy=t; } x+=dx; y+=dy; } for(i=0;i<m;i++) { for(int j=0;j<n;j++) { cout<<A[i][j]<<'\t'; } cout<<endl; }
}
Lưu ý : 1 2 3 8 9 4 7 6 5 thay x = 0, y = 0 vào *
3 2 1 4 9 8 5 6 7 thay x = 0, y = n-1 vào *
7 6 5 8 9 4 1 2 3 thay x = m-1, y =0 vào *
Xử lý khoảng trắng trong chuổi
#include<iostream.h> #include<string.h> //Xoa khoang trang hai ben char *trim(const char *ss) { char *s=new char[strlen(ss)]; strcpy(s,ss); int i=0; //Left Trim while(s[i]==' ') i++; for(int k=0;k<=strlen(s);k++) { s[k]=s[k+i]; } //Right Trim i=strlen(s)-1; while(s[i]==' '&&i>0) i--; s[i+1]=0; return s; } //Xoa khoang trang ben phai char *rtrim(const char *ss) { char *s=new char[strlen(ss)]; strcpy(s,ss); int i=strlen(s)-1; while(s[i]==' '&&i>0) i--; s[i+1]=0; return s; } //Xoa khoang trang ben trai char *ltrim(const char *ss) { char *s=new char[strlen(ss)]; strcpy(s,ss); int i=0; while(s[i]==' ') i++; int length=strlen(s); for(int k=0;k<=length;k++) s[k]=s[k+i]; return s; } //Xoa cac khoang trang thua trong xau char *ctrim(const char *ss) { char *s=new char[strlen(ss)]; strcpy(s,ss); int i=0; while(i<strlen(s)-1) { if(s[i]==' ' && s[i+1]==' '){ int k=i; do s[k]=s[k+1]; while(s[++k]!=0); }else i++; } return s; } void main() { char s[100]; cout<<"Nhap xau : "; cin.getline(s,100); cout<<"ALL_TRIM !"<<trim(s)<<"!"<<endl; cout<<"C_TRIM !"<<trim(ctrim(s))<<"!"<<endl; } =============================== #include <iostream.h> #include <string.h> void main () { char * s = new char [100] ; char * s1 = new char [100] ; cin.getline (s,50) ; int i , j , k , size , size1 ; size = strlen(s); // sau dau cham , thuong thanh hoa , hoa thanh thuong for (i = 0 ; i < strlen (s) ; i ++) { if ( s [i] == '.') if ( s [i + 1 ] >= 97 ) s [i + 1] = s [i + 1] - 32 ; else s [i + 1] = s [i + 1] + 32 ; } // gap dau phay thi co khoang trang for ( i = 0 ; i < strlen (s) ; i ++) { if ( s[i] == ',' || s[i] == '.') { size = strlen(s) + 50 ; for (j = i + 1 , k = 0 ; j < size ; j ++, k ++) s1[k] = s[j] ; s [i + 1] = ' ' ; for (j = i + 2 , k = 0 ; j < size ; j ++, k ++) s[j] = s1[k] ; } } // xoa khoang trang dau chuoi for (i = 0 ; i < strlen (s) ; i ++) { if (s [i] == ' ' && s [i + 1] == ' ') { s [i] = '\0' ; s1 = &s [i + 1] ; strcat(s,s1) ; i -- ; } if ( s[0] == ' ') { s1 = & s[1]; s [0] = '\0' ; strcat (s,s1) ; } if (s [strlen(s) - 1] == ' ') { s1 = & s [strlen(s) - 1] ; s [strlen(s) - 1] = '\0' ; strcat(s,s1) ; }
} if (s [0] >= 97) s[0] = s[0] - 32 ;
cout << s ; } Đếm số lần xuất hiện của chuổi con = đệ quy
#include<iostream.h> #define max 100 #include<string.h>
int dem(char a[max], char b[max]); int dem(char a[max], char b[max]) { char c[max]; if(strstr(a,b) != '\0') { strcpy(c,strstr(a,b)+strlen(b)); return 1+dem(c,b); }else return 0; } void main () { char s[max], s1[max]; cout<<"nhap vao mot chuoi cha : "; cin.getline(s,max); cout<<"nhap vao mot chuoi con : "; cin.getline(s1,max); cout<<"so lan xuat hien cua chuoi con : "; cout<<dem(s,s1)<<endl; }
Đảo số
#include <iostream.h> #include <math.h> int DAOSO (int); int DEM (int) ; void main () { int n ; cout << " hay nhap so " <<'\t' ; cin >> n ;DAOSO (n) ; cout << " So dc Dao thanh " << DAOSO (n) ; cout << endl; } int DEM(int n) { if (n <= 9) return 1 ; else return DEM(n/10) + 1 ; } int DAOSO(int n) { if (n <= 9) return n ; else return n%10*pow(10,DEM(n)-1) + DAOSO (n/10); }
Hoan vị
#include <iostream.h> const int SIZE = 50 ; void XUAT (int [] , int ); void DAO_SO(int [] , int ) ; int n ; void main () { cout << " nhap So di ban " << '\t' ; int i , array [SIZE]; cin >> n ; for (i = 0 ; i < n ; i ++) array [i] = i + 1 ; DAO_SO(array,n) ; } void DAO_SO (int array [SIZE] , int m) { if (m == 1) XUAT( array , n ) ; else { for (int i = 0 ; i < m ; i ++) { int t = array [i] ; array [i] = array [m - 1] ; array [m - 1] = t ; DAO_SO(array,m - 1) ; t = array [i] ; array [i] = array [m - 1] ; array [m- 1] = t ; } } } void XUAT(int array [SIZE] , int m ) { for (int i = 0; i < m ; i ++) { cout << array [i] << '\t' ; } cout << endl ; }
Nhân 2 Ma Trận
#include <iostream.h> #include<fstream.h> #define max 100 void main () { int i,j,m,n,p,q,k,s=0; int a[max][max],b[max][max],c[max][max]={0}; ifstream nhap; nhap.open("input.txt"); nhap>>m>>n; for(i=0;i<m;i++) for(j=0;j<n;j++) nhap>>a[i][j]; nhap>>p>>q; for(i=0;i<p;i++) for(j=0;j<q;j++) nhap>>b[i][j];
if(n!=p) cout<<"ko the nhan 2 ma tran"<<endl; else { for(i=0;i<m;i++) { for(j=0;j<q;j++) { for(k=0;k<n;k++) { s=s+a[i][k]*b[k][j]; c[i][j]=s; } s=0; } } } for(i=0;i<m;i++) { for(j=0;j<q;j++) cout<<c[i][j]<<'\t'; cout<<endl; } nhap.close();
}
Đảo chuỗi vừa nhập #include <iostream.h> #include <string.h> void daonguoc(char *); int main () { cout<<"chuong trinh nhap chuoi va dao nguoc chuoi:"; char mang[30]; cin.getline(mang,30); cout<<"mang duoc dao nguoc la:"; daonguoc(mang); return 0; } // ham dao nguoc so void daonguoc(char * p) { int j=strlen(p)-1; int i=0; char c; while(j>=i) { c=p[i]; p[i]=p[j]; p[j]=c; i++; j--; } cout<<p; } Xuất ra n số fibonaci đầu tiên #include<iostream.h> #include"iomanip.h" int fibonacci(int ); int main() { int n; cout<<"Nhap so nguyen duong n: "; cin>>n; if(n<=0) cout<<" Ban Nhap lai so lon hon 0 !!!"; for(int i=0;i<n;i++) cout<<fibonacci(i)<<" "; cout<<endl; return 0; } int fibonacci(int n) { int fibo; if(n==0||n==1) fibo=1; else fibo=fibonacci(n-1)+fibonacci(n-2); return fibo; } Ước Chung Lớn Nhất (bằng đệ quy) #include<iostream.h> int ucln( int a, int b); int ucln( int a, int b) { if(a==b) return a; else if (a<b) return ucln(a,b-a); else return ucln(a-b,b); } void main() { int a,b; cin>>a>>b; cout<<"UCLN la : "<<ucln(a,b)<<endl; } Tháp Hà Nội ( Bằng Đệ Quy) #include<iostream.h> void chuyen (int xp, int d); void chuyen (int xp, int d) { cout<<"chuyen tu cot "<<xp<<" sang cot "<<d<<endl; }
void thn(int m,int xp,int tg,int d); void thn(int m,int xp,int tg,int d) { if(m==1) chuyen(xp,d); else { thn(m-1,xp,d,tg); chuyen(xp,d); thn(m-1,tg,xp,d); } } void main () { int n; cout<<"nhap vao so phan tu can chuyen ";cin>>n; thn(n,1,2,3); } // biến chuổi in thường thành IN HOA char* mystrupr(char *s) { for (int i=0; i<strlen(s); i++) if (s[i] >= 'a' && s[i] <= 'z') s[i] = s[i] - 32;
return s; } // biến chuổi IN HOA thành in thường char* mystrlwr(char *s) { int n = strlen(s); for (int i=0; i<n; i++) if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] + 32;
return s; } // Xoá Kí Tự char* XoaKyTu(char *s, int vt) { if (vt>=0 && vt<strlen(s)) { // Cach 1 //for (int i=vt; i<strlen(s)-1; i++) // s[i] = s[i+1];
//s[strlen(s)-1] = '\0';
// Cach 2 for (int i=vt; i<strlen(s); i++) s[i] = s[i+1]; }
return s; } //Chuẩn Hoá Chuổi Kí Tự char* standard(char *s) { // Xoa khoang trang du dau chuoi while (s[0] == ' ') XoaKyTu(s, 0);
// Xoa khoang trang du cuoi chuoi while (s[strlen(s)-1] == ' ') XoaKyTu(s, strlen(s)-1);
// Xoa khoang trang du giua chuoi for (int i=1; i<strlen(s)-2; ) if (s[i] == ' ' && s[i+1] == ' ') XoaKyTu(s, i); else i++;
return s; }
Hieu 2 ma tran a va b Tong 2 ma tran a va b
#include <iostream.h>
const int ROWS=2,COLUMNS=3;
void sum(const int [][COLUMNS],const int[][COLUMNS]); void subtract(const int [][COLUMNS],const int[][COLUMNS]); void in(const int[][COLUMNS]);
int main() { const int a[ROWS][COLUMNS]= {{1,0,2}, {-1,3,5}}; const int b[ROWS][COLUMNS]= {{4,2,1}, {7,0,3}}; cout<<"Ma tran a:\n"; in(a); cout<<"Ma tran b:\n"; in(b); cout<<"Tong 2 ma tran a va b:\n"; sum(a,b); cout<<"Hieu 2 ma tran a va b:\n"; subtract(a,b); return 0; } void sum(const int a[ROWS][COLUMNS],const int b[ROWS][COLUMNS]) { int c[ROWS][COLUMNS]; for (int i=0;i<ROWS;i++) for (int j=0;j<COLUMNS;j++) c[i][j]=a[i][j]+b[i][j]; in(c); }
void subtract(const int a[ROWS][COLUMNS],const int b[ROWS][COLUMNS]) { int c[ROWS][COLUMNS]; for (int i=0;i<ROWS;i++) for (int j=0;j<COLUMNS;j++) c[i][j]=a[i][j]-b[i][j]; in(c); }
void in(const int a[ROWS][COLUMNS]) { for (int i=0;i<ROWS;i++) { for (int j=0;j<COLUMNS;j++) { cout<<a[i][j]<<'\t'; } cout<<'\n'; } } // in tat ca cac hoan vi so > 33...33 #include <iostream.h> #include<fstream.h> #define max 100 void xuatmatran(int a[max]); void hoanvi(int a[max],int m); bool sosanh(int a[max],int b[max]); int n,b[max]; ifstream nhap; ofstream xuat; void main() { int a[max]; xuat.open("output.txt");
xuat<<"Cac hoan vi can tim la : "<<endl; nhap.open("input.txt"); nhap>>n; ; for(int i=0;i<n;i++) { a[i]=i+1; b[i]=3; } hoanvi(a,n); } void hoanvi(int a[max],int m) { if(m==1 && sosanh(a,b)==true) { xuatmatran(a); } else { for(int i=0;i<m;i++) { int t=a[i]; a[i]=a[m-1]; a[m-1]=t; hoanvi(a,m-1); t=a[i]; a[i]=a[m-1]; a[m-1]=t; } }
} void xuatmatran(int a[max]) { for(int i=0;i<n;i++) { xuat<<a[i]<<" "; } xuat<<endl; } bool sosanh(int a[max],int b[max]) { int i=0; while(a[i] >= b[i]) { i++; if(a[i]>=b[i]) return true; } return false; nhap.close(); xuat.close(); } ZicZac Chéo /*16 14 13 7 // va cheo 15 12 8 6 nguoc 11 9 5 2 lai 10 4 3 1 */
#include <iostream.h> #include<fstream.h> #define max 100 void Ziczac(int [max][max],int, int); void Xuat(int [max][max],int, int); ifstream nhap;
ofstream xuat;
void main() { nhap.open("input.txt"); xuat.open("output.txt"); int a[max][max]={0},m,n; xuat<<"Ma tran ziczac la : "<<endl; nhap>>m>>n; Ziczac(a,m,n); Xuat(a,m,n); } void Ziczac(int a[max][max],int m, int n) { int i=n – 1 , j=m – 1 ; int num=1; bool up; while(num<= n*m) { a[i][j]=num++; if(up == false) {
if(j-1<0) { if(i+1 > n-1) i--, up= true; else i--,up= true; } else { if(i+1 > n-1) j--, up = true; else i++,j--; } /* if(i-1<0) { if(i+1 > n-1) j--,up = false; else j--,up = false; } else { if(j+1 > m-1) i--,up = false; else i--,j++; }*/ } else { if(i-1<0) { if(i+1 > n-1) j--,up = false; else j--,up = false; } else { if(j+1 > m-1) i--,up = false; else i--,j++; } /* if(j-1<0) { if(i+1 > n-1) i--, up= true; else i--,up= true; } else { if(i+1 > n-1) j--, up = true; else i++,j--; }*/ } } } void Xuat(int a[max][max],int m, int n) { for(int i=0;i<m;i++) { xuat<<"\n\n\t"; for(int j=0;j<n;j++) xuat<<a[i][j]<<'\t'; xuat << endl ; } } // hoan doi tri 2 bien nguyen dung con tro #include <iostream.h>
void Swap1(int x, int y); void Swap2(int& x, int& y); void Swap3(int* x, int* y);
void main() { int x = 5, y = 7; Swap1(x, y); // ham dung phuong phap truyen tri cout<<"\nSau khi goi Swap1(x, y) thi x = "<<x<< " va y = "<<y; Swap2(x, y); // goi ham co su dung truyen tham chieu cout<<"\nSau khi goi Swap2(x, y) thi x = "<<x<< " va y = "<<y; Swap3(&x, &y); // goi ham co su dung con tro cout<<"\nSau khi goi Swap3(&x,&y) thi x = "<<x<< " va y = "<<y; cout << endl; } void Swap1(int x, int y) { int z = x; x = y; y = z; } void Swap2(int& x,int& y) { int z = x; x = y; y = z; } void Swap3(int* x, int* y) { int z = *x; *x = *y; *y = z; } Kiem Tra Chuổi 2 có Là Hoán Vị Của Chuổi 1 Ko #include <iostream.h> #include <string.h> bool kiemtra(char [],char []); int dem(char [],char ); int main () { cout<<"chuong trinh kiem tra xem chuoi nay co phai la hoan vi cua chuoi kia ko:"; char mang1[20]; char mang2[20]; cout<<"ban hay nhap vao chuoi thu nhat:"; cin.getline( mang1,20); cout<<"ban hay nhap vao chuoi thu hai:"; cin.getline(mang2,20); if(kiemtra(mang1,mang2)) cout<<"chuoi 2 la hoan vi cua chuoi 1:"; else cout<<"khong phai."; cout<<endl; return 0; } // ham bool kiemtra(char mang1[],char mang2[]) { for (int i=0;i<strlen(mang1);i++) if (dem(mang1,mang1[i])!=dem(mang2,mang1[i]) ||strlen(mang1)!=strlen(mang2) ) return false ; return true ; } int dem (char mang[],char c) { int k=0; for (int i=0;i<strlen(mang);i++) if (c == mang[i]) k++; return k; } Điếm Số Kí Tự Trên Dòng và Xuất Ra Dòng Dài Nhất Ngắn Nhất
#include <iostream.h> #include <string.h> #include <fstream.h> /*void Cpoy(char * S , char * Scpy) ;*/ void DocVanBan (char * ) ; void Replace (char * ) ; void Replaces (char * ) ; ifstream nhap ; ofstream xuat ; ofstream xuat1 ; void main () { char * S = new char [100] ; DocVanBan (S) ; }
void DocVanBan (char * S) { nhap.open ("in.txt") ; xuat.open ("out.txt") ; xuat1.open ("out1.txt") ; char * Smax = new char [100] ; char * Smin = new char [100] ; char * Scpy = new char [100] ; char * oneScpy = new char [100] ; char * twoScpy = new char [100] ; int hang = 0 ; int len , hangmax = 0 , hangcheck = 0 ,hangmin=0; while (!nhap.eof ()) { nhap.getline(S,100) ; Replace(S) ; hang ++ ; len = strlen(S) ; if (len != 0) { strcpy(Scpy,S) ; Replaces(Scpy) ; } if (hangmax < len) { hangcheck = hang ; hangmax = len ; strcpy(Smax,S) ; strcpy(oneScpy,Scpy) ; oneScpy [len] = '\0' ; Smax [len] = '\0' ; } else(hangmin >len); { hangcheck = hang ; hangmin = len ; strcpy(Smin,S) ; strcpy(twoScpy,Scpy) ; twoScpy [len] = '\0' ; Smin [len] = '\0' ; } xuat << " Hang : " << hang << " , So Ky Tu : " << len << endl ; xuat << S << endl ; xuat1 << " Hang : " << hang << " , So Ky Tu : " << len << endl ; xuat1 << Scpy << endl ; } xuat << " hang dai nhat : " << hangcheck << " , So Ky Tu : " << hangmax << endl ; xuat << " Chuoi MAX : " << Smax << endl ; xuat << " hang ngan nhat : " << hangcheck << " , So Ky Tu : " << hangmin << endl ; xuat << " Chuoi MIN : " << Smin << endl ; xuat1 << " hang dai nhat : " << hangcheck << " , So Ky Tu : " << hangmax << endl ; xuat1 << " Chuoi MAX : " << oneScpy << endl ; xuat1 << " hang ngan nhat : " << hangcheck << " , So Ky Tu : " << hangmin << endl ; xuat1 << " Chuoi MIN : " << twoScpy << endl ; nhap.close () ; xuat.close () ; xuat1.close () ;
} void Replace (char * S )// xu li van ban Hoa thanh thuong { char * S1 = new char [100] ; int i , len; len = strlen(S) ; if (S [0] >= 97) S [0] -= 32 ; for (i = 0 ; i < len ; i ++) { if (S [i] == ' ' && S [i + 1] == ' ') { S1 =& S [i + 1] ; S [i] = '\0' ; strcat(S,S1) ; i -- ; } if (S [i] == '.') if (S [i + 1] >= 97 ) S [i + 1] -= 32 ; } if (S [0] == ' ') { S1 =& S [1] ; S [0] = '\0' ; strcat(S,S1) ; } if (S [len - 1] == ' ') { S1 =& S [len - 1] ; S [len - 1] = '\0' ; strcat(S,S1) ; } int j , k ; int len1 = len + 50 ; for (i = 0 ; i < len ; i ++) { if (S [i] == '.' || S [i] == ',') { for (j = i + 1 , k = 0 ; j < len1 ; j ++ , k ++) S1 [k] = S [j] ; S [i + 1] = ' ' ; for (j = i + 2 , k = 0 ; j < len1 ; j ++ , k ++) S [j] = S1 [k] ; } } // Hoa Sang Thuong for (i = 0 ; i < len ; i ++) if (S [i] >= 65 && S [i] < 97) S [i] += 32 ;
} void Replaces (char * S )// xu li van ban bien Hoa thanh thuong { char * S1 = new char [100] ; int i , len; len = strlen(S) ; if (S [0] >= 97) S [0] -= 32 ; for (i = 0 ; i < len ; i ++) { if (S [i] == ' ' && S [i + 1] == ' ') { S1 =& S [i + 1] ; S [i] = '\0' ; strcat(S,S1) ; i -- ; } if (S [i] == '.') if (S [i + 1] >= 97 ) S [i + 1] -= 32 ; } if (S [0] == ' ') { S1 =& S [1] ; S [0] = '\0' ; strcat(S,S1) ; } if (S [len - 1] == ' ') { S1 =& S [len - 1] ; S [len - 1] = '\0' ; strcat(S,S1) ; } int j , k ; int len1 = len + 50 ; for (i = 0 ; i < len ; i ++) { if (S [i] == '.' || S [i] == ',') { for (j = i + 1 , k = 0 ; j < len1 ; j ++ , k ++) S1 [k] = S [j] ; S [i + 1] = ' ' ; for (j = i + 2 , k = 0 ; j < len1 ; j ++ , k ++) S [j] = S1 [k] ; } } // thuong sang hoa for (i = 0 ; i < len ; i ++) if (S [i] >= 97) S [i] -= 32 ; } Phân Số ( Sử Dụng Strust) #include<fstream.h> #include<iostream.h> #include<math.h> struct fan_so { int tu, mau; };
int usc (fan_so T); int usc (fan_so T) { int x,y; x=abs(T.tu); y=abs(T.mau); while(x!=y) { if(x<y) y=y-x; else x=x-y; } return x; }
void toigian(fan_so T); void toigian(fan_so T) { ofstream xuat; xuat.open("output.txt"); if(T.mau<0 || (T.tu<0 && T.mau<0)) { if(usc(T)==1) xuat<<(T.tu*-1)<<"/"<<(T.mau*-1)<<endl; else xuat<<(T.tu/usc(T)*-1)<<"/"<<(T.mau/usc(T)*-1)<<endl; } else { if(usc(T)==1) xuat<<T.tu<<"/"<<T.mau<<endl; else xuat<<(T.tu/usc(T))<<"/"<<(T.mau/usc(T))<<endl; } }
void tong(fan_so A, fan_so B, fan_so T); void tong(fan_so A, fan_so B, fan_so T) { T.mau=A.mau*B.mau; T.tu=A.tu*B.mau+B.tu*A.mau; toigian(T); } void hieu(fan_so A, fan_so B, fan_so H); void tich(fan_so A, fan_so B, fan_so Ti); void thuong(fan_so A, fan_so B, fan_so Th); void hieu(fan_so A, fan_so B, fan_so H) { H.mau=A.mau*B.mau; H.tu=A.tu*B.mau-B.tu*A.mau; toigian(H); } void tich(fan_so A, fan_so B, fan_so Ti) { Ti.tu=A.tu*B.tu; Ti.mau=A.mau*B.mau; toigian(Ti); } void thuong(fan_so A, fan_so B, fan_so Th) { Th.tu=A.tu*B.mau; Th.mau=A.mau*B.tu; toigian(Th); } void main () { ofstream xuat; xuat.open("output.txt"); fan_so A,B,T,H,Ti,Th; cout<<"nhap phan so A : "; cin>>A.tu>>A.mau; cout<<"nhap phan so B : "; cin>>B.tu>>B.mau; cout<<"xuat phan so A : "<<A.tu<<"/"<<A.mau<<endl; cout<<"xuat phan so B : "<<B.tu<<"/"<<B.mau<<endl;
tong(A,B,T);
hieu(A,B,H);
tich(A,B,Ti); thuong(A,B,Th);cout<<endl; xuat.close(); } ///////////////////// Đề Thi Lần 1 ///////////////////////////////////////////// 1A…..Nhap Ma trận A vào input1A.txt . Ma tran B vào input1A2.txt . Nếu B*A được thì xuất ra kết quả vào output1.txt 1B…..Matran Zigzag vào output2.txt 2…..Nhap vào input2.txt một đoạn văn bản . xuất ra đoạn văn bản đó có bao nhiêu dòng và dòng nào ngan nhất vào output2.txt 3…..Strust hinh chu nhat 4……Xuất Tháp Hà Nội vào output4.txt /////////////////////////////////////////////////////////////////////////////////
[code] | |
|