Professional Documents
Culture Documents
1 #include <iostream> #include <cstdlib> using namespace std; int main(){ char c; cout<<"Nhap mot ki tu tu ban phim: "; cin>>c; cout<<c<<"-> Ma ASCII = "<<int(c)<<endl; cout<<c<<"-> Ma co so 8 = "<<oct<<int(c)<<endl; cout<<c<<"-> Ma co so 16 = "<<hex<<int(c)<<endl; system("PAUSE"); } 1.2
cout<<"Nhap dien dung C: "; cin>>C; cout<<"Dien khang Xc= system("PAUSE"); return 0; } 1.3 #include <iostream> #include <cstdlib> "<<1/(2*PI*f*C)<<endl;
int main(){ float R1, R2, R3, x; cout<<"Xin moi cho 3 dien tro!"<<endl; cout<<"R1= "; cin>>R1; cout<<"R2= "; cin>>R2; cout<<"R3= ", cin>>R3; cout<<"noi tiep an phim 1,song song an phim 2"<<endl; cin>>x; while(x!=1&&x!=2) { cout<<"nhap lai: noi tiep an phim 1,song song an phim 2"<<endl; cin>>x; } if(x==1) cout<<"Rtd="<<R1+R2+R3<<endl; else cout<<"Rtd="<<1/(1/R1+1/R2+1/R3)<<endl; system("PAUSE"); return 0; }
2.1 #include <iostream> #include <cstdlib> #define const int N=20; using namespace std;
int main(){ int n,i,x; float R[20],Rss=0,Rnt=0; do{ cout<<"Xin moi cho so luong dien tro 0<N<20: "; cin>>n; if (n<=0 || n>=20) cout<<"Ban nhap khong dung!\n"; }while (n<=0 || n>=20);
for (i=0; i<n; i++) { do{ cout<<"R["<<i+1<<"]= "; cin>>R[i]; if (R[i]<=0) cout<<"Nhap lai\n"; }while (R[i]<=0); } for (i=0; i<n; i++) { Rnt+=R[i]; Rss+=1/R[i]; } cout<<"noi tiep an phim 1,song song an phim 2"<<endl; cin>>x; while(x!=1&&x!=2)
{ cout<<"nhap lai: noi tiep an phim 1,song song an phim 2"<<endl; cin>>x; } if(x==1) cout<<"Rnt="<<Rnt<<endl; else cout<<"Rss="<<Rss<<endl; system("PAUSE"); return 0; } 2.2 #include <iostream> #include <cstdlib> #include <stdio.h>
int main(){ char s[200]; int sotu=0, i=-1; cout<<"Nhap xau s: "; fflush (stdin); gets(s); if(strlen(s)>200) { cout<<"nhap qua 200 ki tu, nhap lai"<<endl; gets(s);
} while(s[++i]!=0) if(s[i]!=' '&&(s[i+1]==' '||s[i+1]==0)) sotu++; cout<<"so tu trong xau la: "<<sotu; system("PAUSE"); return 0; } 2.3 #include <iostream> #include <cstdlib> #include <string.h>
int main(){ float a[10][10]; float thang[10], tcot[10], tong=0; int m,n,i,j;
cout<<"Nhap kich thuoc mang: "; cout<<"so hang = "; cin>>m; cout<<"so cot = "; cin>>n; for (i=0; i<m; i++) for (j=0; j<n; j++) { cout<<"a["<<i<<"]["<<j<<"]= "; cin>>a[i][j];
} for (i=0; i<m; i++) { for (j=0; j<n; j++) { cout<<"a["<<i<<"]["<<j<<"]= "<<a[i][j]<<'\t'; } cout<<endl; } for (i=0; i<m; i++) for (j=0; j<n; j++) { tong+=a[i][j]; } for (i=0; i<m; i++) { thang[i]=0; for (j=0; j<n; j++)
thang[i]+=a[i][j]; } cout<<"tong= "<<tong<<endl; for (i=0; i<m; i++) cout<<"tong hang "<<i+1<<"= "<<thang[i]<<endl; for (j=0; j<n; j++) { tcot[j]=0; for (i=0; i<m; i++)
tcot[j]+=a[i][j]; } for(j=0; j<n; j++) cout<<"tong cot= "<<j+1<<"= "<<tcot[j]<<endl; system("PAUSE"); } 3.1 #include <iostream>
class Complex { private: double re,im; public: // Ham tao thiet lap tu 2 tham so Complex(double r=0, double i=0) : re(r), im(i) { } // Ham tao copy tu mot so phuc Complex (const Complex &c) : re(c.re), im(c.im) { }
public: Complex operator + (Complex c); // Ham dinh nghia toan tu cong 2 so phuc Complex operator - (Complex c); // Ham dinh nghia toan tu tru 2 so phuc Complex operator * (Complex c); // Ham dinh nghia toan tu nhan 2 so phuc Complex operator / (Complex c); // Ham dinh nghia toan tu chia 2 so phuc
public: // Ham dinh nghia toan tu luong ra friend ostream& operator << (ostream &out, Complex c) { return (out << '(' << c.re << ", " << c.im << "i) "); } };
int main() { // Khai bao 2 so phuc y va z, moi so duoc khoi tao tu 1 hoac 2 so thuc bat ky // Khai bao bien double a va khoi tao bang mot so thuc bat ky Complex y(2.0,2.9),z(3.0,3.0); double a = 0.3; cout << y <<" + "<< z <<" + "<< a <<" = "<< y + z + a <<endl; cout << y << " - " << z << " = " << y - z << endl; cout << y << " * " << z << " = " << y * z << endl; cout << y << " / " << z << " = " << y / z << endl;
class String{ private: int length; char * data; private: // Ham tao String tu mang ky tu voi do dai cho truoc String(int length,char *data) : length(length), data(data) { } public: // Ham tao mac dinh tao String rong String() : length(0), data(new char[1]) { data[0] = 0; }
// Ham tao String tu mot hang xau ky tu String(const char* s) { length = strlen(s); data = new char[length + 1]; strcpy(data, s); }
// Ham tao String copy tu mot String da co String(const String& s) : length(s.length), data(s.data) { }
// Ham so sanh hai String // Gia tri tra ve -1, 0, 1 tuong ung String <, ==, > String s int Compare(String s) { int r=strcmp(data,s.data); if (r>0) return 1; if (r<0) return -1; return 0; } public: // Toan tu chi so truy cap ky tu tai chi so index char & operator[] (int index) { return data[index]; } // Toan tu gan s vao String String& operator = (String& s); public: // Toan tu cong thuc hien them ky tu c vo cuoi String String operator + (char c); // Toan tu cong noi s vao cuoi String String operator + (String s); public: // Toan tu so sanh bang int operator == (String s); // Toan tu so sanh khac int operator != (String s); // Toan tu luong ra in String ra man hinh friend ostream& operator << (ostream& out, const String& s)
String & String::operator = (String& s) { delete[] data; length = s.length; data = new char[length + 1]; strcpy(data, s.data);
return (*this); }
String String::operator + (char c) { int length = this->length + 1; char * data = new char[length + 1];
return String(length,data); }
strcpy(data,this->data); strcat(data,s.data);
return String(length,data); }
int main(){ // Khai bao mot String s v khoi tao tu mot hang xau ky tu bat ky String s("Dai hoc bach kho"); char ch='a';
//Viet bieu thuc in ra man hinh khi them mot ky tu bat ky vo s cout<<s<<" + "<<ch<<" = "<<s+ch<<endl; //Viet bieu thuc in ra man hinh khi noi mot hang xau ky tu bat ky vao s String ss(" rat dep "); cout<<s<<" + "<<ch<<" + "<<ss<<" = "<<s+ch+ss+" =)))))))))))"<<endl;
cout << "hay nhap so m : "; cin >> m; cout << m << "! = " << factoria(m) << endl; system("pause"); }
Bai 2 #include <cstdlib> #include <iostream> #include <conio.h> #include <stdio.h> using namespace std;
float Pow(double x,int n) { float gt=1; if(n==0) {return 1;} else {gt=Pow(x,n-1)*x;} return gt; } int main(int argc, char *argv[]) { int n; float x,a;
do { cout<<"Nhap x = ";cin>>x; } while(x==0); do { cout<<"Nhap mu n = ";cin>>n; } while(0); a=Pow(x,n); printf(" %3.2f^%d = %6.2f \n ",x,n,a); system("PAUSE"); return EXIT_SUCCESS; } Bai 3 #include <cstdlib> #include <iostream> #include <conio.h> #include <stdio.h> using namespace std;
int USCLN(int &x,int &y) { if(x==y) {return x;} else if(x>y) {x=x-y;}
} int main(int argc, char *argv[]) { int a,b,c,e,d; do { cout<<" Nhap a = ";cin>>a; } while(a<=0); do { cout<<" Nhap b = ";cin>>b; } while(b<=0); d=a;e=b; c = USCLN(a,b); printf(" USCLN(%d,%d)= %d \n ",d,e,c); system("PAUSE"); return EXIT_SUCCESS; } Bai 4 #include <cstdlib>
float Pow(double x,int n) { float mu=1; if(n==0) {return 1;} else {mu=Pow(x,n-1)*x;} return mu; }
float Value(float A[],int n,float x0) { int i; float f=0; for (i=0;i<=n;i++) f=f+Pow(x0,i)*A[i]; return f; } int main(int argc, char *argv[]) { int n,i,j; do { cout<<"Nhap n = ";cin>>n;
} while(n<=0); float x,fx,A[n]; for(i=0;i<=n;i++) { printf("A[%d] = ",i);cin>>A[i]; } cout<<"Nhap gia tri x0 = ";cin>>x; fx=Value(A,n,x); printf(" f(x0) = %6.2f \n ",fx); system("PAUSE"); return EXIT_SUCCESS; } Bai 5 #include <cstdlib> #include <iostream> #include <conio.h> #include <math.h> #include <iostream> using namespace std; float Pow(double x,int n) { float mu=1; if(n==0) {return 1;} else {mu=Pow(x,n-1)*x;} return mu;
} float Value(float A[],int n,float x0) { int i; float f=0; for (i=0;i<=n;i++) f=f+Pow(x0,i)*A[i]; return f; } double Root(float A[], int n, float x1, float x2) { float e,a1,a2,a3,x3,x4=0,x5,x6;bool kt=true; cout<<"Do chinh xac Epsilon = ";cin>>e; do { a1=Value(A,n,x1);a2=Value(A,n,x2);a3=Value(A,n,(x2+x1)/2); //if (a1*a2>0) printf("Khong co nghiem trong khoang (%6.2f,%6.2f) ",x1,x2); if (a1==0) {break;} if (a2==0) {break;} if (a3==0) {x1=(x2+x1)/2;break;} if ((a1*a3<0)&&(a2*a3<0)) {x4=(x1+x2)/2;x5=x2;kt=false;} if (a1*a3<0) {x2=(x1+x2)/2;continue;} if (a2*a3<0) {x1=(x1+x2)/2;continue;} } while (((x1-x2)>e)||((x2-x1)>e)); cout<<"\nNghiem cua phuong trinh la x = "<<x1<<endl; if(!kt) {
do { a1=Value(A,n,x4);a2=Value(A,n,x5);a3=Value(A,n,(x4+x5)/2); if (a1==0) {break;} if (a2==0) {break;} if (a3==0) {x4=(x4+x5)/2;break;} if (a1*a3<0) {x5=(x4+x5)/2;continue;} if (a2*a3<0) {x4=(x4+x5)/2;continue;} } while (((x4-x5)>e)||((x5-x4)>e)); cout<<"\nNghiem cua phuong trinh la x = "<<x4<<endl; } cout<<"\nVoi do chinh xac Epsilon = "<<e<<endl;printf("\n\n"); return 0; } int main(int argc, char *argv[]) { int n,i,j; float x1,x2,t; do { cout<<"Nhap bac phuong trinh n = ";cin>>n; } while(n<=0); float x,A[n]; for(i=n;i>=0;i--)
{ printf("A[%d] = ",i);cin>>A[i]; } cout<<"Nghiem phuong trinh bac "<<n<<" trong khoang (x1,x2)"<<endl; cout<<"x1 = ";cin>>x1; cout<<"x2 = ";cin>>x2; t=Root(A,n,x1,x2); system("PAUSE"); return EXIT_SUCCESS; } Bai 6 #include <cstdlib> #include <conio.h> #include <math.h> #include <iostream> using namespace std; void InsertionSort(float A[],int n, int Indies[]) { float tg,t2; int i,k,tg2,t1; for(i=2;i<=n;i++) //Sap xep 1 lan (so lon nhat mang len tren cung) { if(A[Indies[i]]>A[Indies[1]]) { t1=Indies[1];t2=A[Indies[1]]; Indies[1]=Indies[i];A[Indies[1]]=A[Indies[i]];
Indies[i]=t1;A[Indies[i]]=t2; } }; /*for(i=1;i<=n;i++) { cout<<"A["<<Indies[i]<<"] = "<<A[Indies[i]]<<endl; } cout<<" i=n; do { if(i==(2*n-1)) {A[Indies[2*n]]=A[Indies[n]];Indies[2*n]=Indies[n];break;} Indies[i+1]=Indies[1]; A[Indies[i+1]]=A[Indies[1]]; for(k=2;k<=n;k++) { if(A[Indies[i+1]]>A[Indies[k]]) { tg2=Indies[i+1];tg=A[Indies[i+1]]; Indies[i+1]=Indies[k];A[Indies[i+1]]=A[Indies[k]]; Indies[k]= tg2;A[Indies[k]]=tg; } }; i++; } while (i<=(2*n-1)); "<<endl;*/
for(i=n+1;i<=2*n;i++) { cout<<"A["<<Indies[i]<<"] = "<<A[Indies[i]]<<endl; } } int main(int argc, char *argv[]) { int n; do { cout<<"Nhap so chi so cua mang Indies n = ";cin>>n; } while(n<=0); int i,j,Indies[n]; float A[2*n]; for(i=1;i<=n;i++) { Indies[i]=i; printf("A[%d] = ",i);cin>>A[Indies[i]]; } for(j=n+1;j<=n*2;j++) { Indies[j]=j; A[Indies[j]]=0; } cout<<"Mang sau khi sap xep kieu InsertionSort la:"<<endl;
Bai 8 #include <cstdlib> #include <conio.h> #include <math.h> #include <iostream> using namespace std; void SelectionSort(float A[],int n, int Indies[]) { float tg; int i,j,doi; for(i=1;i<n;i++) { for(j=i+1;j<=n;j++) { if (A[Indies[i]]>A[Indies[j]]) //sap xep tang dan (SelectionSort) { doi=Indies[i];tg=A[Indies[i]]; Indies[i]=Indies[j];A[Indies[i]]=A[Indies[j]]; Indies[j]=doi;A[Indies[j]]=tg; } }
} for(i=1;i<=n;i++) { cout<<"A["<<Indies[i]<<"] = "<<A[Indies[i]]<<endl; } } int main(int argc, char *argv[]) { int n; do { cout<<"Nhap so chi so cua mang Indies n = ";cin>>n; } while(n<=0); int i,j,Indies[n]; float A[n]; for(i=1;i<=n;i++) { Indies[i]=i; printf("A[%d] = ",i);cin>>A[Indies[i]]; } cout<<"Mang sau khi sap xep SelectionSort la:"<<endl; SelectionSort(A,n,Indies); system("PAUSE"); return EXIT_SUCCESS; }
Bai 8 #include <cstdlib> #include <conio.h> #include <math.h> #include <iostream> using namespace std; void BubbleSort(float A[],int n, int Indies[]) { float tg; bool kt; int i,j,tg2; do { i=1; kt=false; do { if(A[Indies[i]]>A[Indies[i+1]]) { tg2=Indies[i];tg=A[Indies[i]]; Indies[i]=Indies[i+1];A[Indies[i]]=A[Indies[i+1]]; Indies[i+1]= tg2;A[Indies[i+1]]=tg; kt=true; } i++; }
while (i<n); if (!kt) break; } while (1); for(i=1;i<=n;i++) { cout<<"A["<<Indies[i]<<"] = "<<A[Indies[i]]<<endl; }; }; int main(int argc, char *argv[]) { int n; do { cout<<"Nhap so chi so cua mang Indies n = ";cin>>n; } while(n<=0); int i,Indies[n]; float A[n]; for(i=1;i<=n;i++) { Indies[i]=i; printf("A[%d] = ",i);cin>>A[Indies[i]]; } cout<<"Mang sau khi sap xep kieu BubbleSort la:"<<endl; BubbleSort(A,n,Indies);
system("PAUSE"); return EXIT_SUCCESS; } Bai 9 #include <cstdlib> #include <conio.h> #include <math.h> #include <iostream> using namespace std; void Part(float A[],int n, int lb, int ub, int Indies[]) { float tg,khoa; int i,j,tg2; i=lb;j=ub; int t =(lb+ub)/2; khoa = A[Indies[t]]; if (lb>=ub) { return; } while (i<=j) { while (A[Indies[i]]<khoa) i++; while (A[Indies[j]]>khoa) j--; if(i<=j) {
tg2=Indies[i];tg=A[Indies[i]]; Indies[i]=Indies[j];A[Indies[i]]=A[Indies[j]]; Indies[j]=tg2;A[Indies[j]]=tg; i++; j--; } } Part(A,n,lb,j,Indies); Part(A,n,i,ub,Indies); for(i=1;i<=n;i++) { cout<<"A["<<Indies[i]<<"] = "<<A[Indies[i]]<<endl; } cout<<" }; int main(int argc, char *argv[]) { int n,lb,ub; do { cout<<"Nhap so chi so cua mang Indies n = ";cin>>n; } while(n<=0); /* do { cout<<"Nhap so chi so cua mang lb = ";cin>>lb; "<<endl;
} while(n<=0); do { cout<<"Nhap so chi so cua mang ub = ";cin>>ub; } while(n<=0);*/ int i,Indies[1000]; float khoa,A[2000]; for(i=1;i<=n;i++) { Indies[i]=i; printf("A[%d] = ",i);cin>>A[Indies[i]]; } cout<<"Mang sau khi sap xep kieu Part (QuckSort) la:"<<endl; Part(A,n,1,n,Indies); system("PAUSE"); return EXIT_SUCCESS; } Bai 10 #include <cstdlib> #include <conio.h> #include <math.h> #include <iostream> using namespace std;
void HeapRify(float *A, int i, int n,int Indies[]) //Vun d?ng dy con t? i d?n n { int max, left, right,tg2; float tg; left = 2*i + 1; //Nt con bn tri c?a i right = 2*i + 2; //Nt con bn ph?i c?a i if(left < n) //N?u i c nt con { max = left; //Tm ch? s? c?a nt con l?n nh?t if((right < n) && (A[Indies[right]] > A[Indies[left]])) max = right;
if(A[Indies[i]] < A[Indies[max]]) //N?u cha < con { tg2=Indies[i];tg=A[Indies[i]]; Indies[i]=Indies[max];A[Indies[i]]=A[Indies[max]]; Indies[max]=tg2;A[Indies[max]]=tg; HeapRify(A, max, n,Indies); } } } void BuildHeap(float *A, int n,int Indies[]) //Vun dy A[] thnh d?ng { for(int i = n/2; i >= 0; i--) HeapRify(A, i, n, Indies); } void HeapSort(float *A, int n,int Indies[]) //Hm s?p x?p {
float tg; int tg2; BuildHeap(A,n,Indies); for(int i = n-1; i > 0; i--) { tg2=Indies[0];tg=A[Indies[0]]; Indies[0]=Indies[i];A[Indies[0]]=A[Indies[i]]; Indies[i]=tg2;A[Indies[i]]=tg; HeapRify(A,0,i,Indies); } } int main(int argc, char *argv[]) { float *A; int n, i,*Indies; do { cout<<"Nhap so chi so cua mang Indies n = ";cin>>n; } while(n<=0); A = new float[n]; //Khai bo m?ng a g?m n ph?n t? nguyn Indies = new int[n]; for(i=0;i<n;i++) { Indies[i]=i; printf("A[%d] = ",i);cin>>A[Indies[i]];
} HeapSort(A,n,Indies); cout<<"\nMang sau khi sap xep kieu BuildHeap la:"<<endl; for(i = 0; i < n; i++) cout<<"A["<<Indies[i]<<"] = "<<A[Indies[i]]<<endl; cout<<"\n system("PAUSE"); return EXIT_SUCCESS; } Bai 11 // Bai 11.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <cstdlib> #include <conio.h> #include <math.h> #include <iostream> using namespace std; int BinarySearch(float A[],int n, int Indies[],int x) { float tg,t2; int i,k,tg2,t1; for(i=2;i<=n;i++) //Sap xep 1 lan (so lon nhat mang len tren cung) { if(A[Indies[i]]>A[Indies[1]]) { t1=Indies[1];t2=A[Indies[1]]; \n";
Indies[1]=Indies[i];A[Indies[1]]=A[Indies[i]]; Indies[i]=t1;A[Indies[i]]=t2; } }; /*for(i=1;i<=n;i++) { cout<<"A["<<Indies[i]<<"] = "<<A[Indies[i]]<<endl; } cout<<" i=n; do { if(i==(2*n-1)) {A[Indies[2*n]]=A[Indies[n]];Indies[2*n]=Indies[n];break;} Indies[i+1]=Indies[1]; A[Indies[i+1]]=A[Indies[1]]; for(k=2;k<=n;k++) { if(A[Indies[i+1]]>A[Indies[k]]) { tg2=Indies[i+1];tg=A[Indies[i+1]]; Indies[i+1]=Indies[k];A[Indies[i+1]]=A[Indies[k]]; Indies[k]= tg2;A[Indies[k]]=tg; } }; i++; } "<<endl;*/
while (i<=(2*n-1)); for(i=n+1;i<=2*n;i++) { cout<<"A["<<Indies[i]<<"] = "<<A[Indies[i]]<<endl; } int dem=0; for(i=n+1;i<=2*n;i++) { dem++; if (x==A[Indies[i]]) cout<<"Vi tri cua X = "<<x<<" trong mang A la ["<<Indies[i]<<"] "<<endl; } if (dem!=0)return 0; if (dem==0) {cout<<"Gia tri cua X = "<<x<<" khong thuoc mang A "<<endl;return (-1);}
} int main(int argc, char *argv[]) { int n; float x; do { cout<<"Nhap so chi so cua mang Indies n = ";cin>>n; } while(n<=0); int i,j,*Indies;
Indies=new int[n]; float *A; A = new float[2*n]; for(i=1;i<=n;i++) { Indies[i]=i; printf("A[%d] = ",i);cin>>A[Indies[i]]; } do { cout<<"Nhap so can tim trong mang Indies x = ";cin>>x; } while(1>2); for(j=n+1;j<=n*2;j++) { Indies[j]=j; A[Indies[j]]=0; } cout<<"Mang sau khi sap xep la:"<<endl; BinarySearch(A,n,Indies,x);
Bai 12
#include <cstdlib> #include <iostream> #include <conio.h> #include <math.h> #include <iostream> using namespace std;
int main(int argc, char *argv[]) { system("PAUSE"); return EXIT_SUCCESS; } // Vidu.cpp : Defines the entry point for the console application. // #include <stdafx.h> #include <cstdlib> #include <iostream> #include <conio.h> #include <math.h> #include <iostream> //using namespace std; //using namespace System; // //class Int32{ // // int value; public: static void Parse()
private: static void Convert(String^ value) { try { int number = Int32::Parse(value); Console::WriteLine("So chuyen doi la '{0}' sang la
// // // //}
public ref class ParseInt32{ public: static void Main() { Convert(" 179 "); Convert(" -204 "); Convert(" +809 "); Convert(" 178.3"); }
private: static void Convert(String^ value) { try { int number = Int32::Parse(value); Console::WriteLine("Converted '{0}' to {1}.", value, number); } catch (FormatException^) {
//intID1 = Int32.Parse(myValue.ToString()); //intID2 = Convert.ToInt32(myValue); // //public static int ToInt32(String value) { // // // // // //public static int ToInt32(string value) //{ // // // // // } return int.Parse(value, CultureInfo.CurrentCulture); if (value == null) { return 0; } if (value == null) return 0; return Int32.Parse(value, CultureInfo.CurrentCulture);
//} // //public static int Parse(string s) //{ // //} return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);