C+ Mới - Duy anh
. viết chương trình tính tổng các số lẻ.
#include<iostream.h>
void nhapdl(int a[],int n)
{
for (int i=0;i<n;i++)
{ cout<<"a["<<i<<"]= ";
cin>>a[i];
}
}
void indl (int a[],int n)
{
for (int i=0;i<n;i++)
cout<<"a["<<i<<"]= "<<a[i];
}
void main ()
{
int a[10],b[10],j=0,n,tong=0;
cout<<" nhap so phan tu cua mang la : ";
cin>>n;
nhapdl(a,n);
indl(a,n);
for (int i=0 ;i<n;i++)
if (a[i]%2!=0) tong+=a[i];
cout<<"
tong cac so le = "<<tong ;
}
bài viết chương trình tính tổng
s=1+3+5+7+...+(2n-1)
#include<iostream.h>
void main ()
{
int n,tong=0;
char tl;
do
{
cout<<" nhap n= ";
cin>>n;
cout<<"s= ";
for (int i=1;i<=n;i+=2)
{
tong += i;
cout<<i;
if( i<(n-1) ) cout<<"+";
}
cout<<"= "<<tong;
cout<<"
ban co muon tiep tuc khong neu co bam y or Y";
cin>>tl;
}
while (tl=='y'|| tl=='Y');
}
2. Xây dựng 1 lớp công dân gồm các thuộc tính hodem,ten,namsinh,gioitinh, xd hàm tạo, pt thức hiển thị, hạm bạn so sánh tuổi của 2 công dân .
# include<iostream.h>
# include<string.h>
class congdan
{
private :
char hodem[20];
char ten[20];
int namsinh;
char gioitinh[5];
public :
congdan (char hodem1[20],char ten1[20],int namsinh1,char gioitinh1[])
{
strcpy(hodem,hodem1);
strcpy(ten,ten1);
namsinh=namsinh1;
strcpy(gioitinh,gioitinh1);
}
void hienthi()
{
cout<<"
hovaten : "<<hodem<<" "<<ten;
cout<<"
nam sinh "<<namsinh;
cout<<"
gioi tinh :
"<<gioitinh;
cout<<"
tuoi: "<<tuoi();
}
int tuoi()
{ int tuoi;
return (tuoi=2012-namsinh) ;
}
friend void sosanh(congdan,congdan);
};
void sosanh(congdan a,congdan b)
{ int tuoi;
if (a.tuoi() >b.tuoi() )
cout<<"
"<<a.ten<<"
nhieu tuoi hon "<< b.ten ;
else
cout<<b.ten<<"
nhieu tuoi hon"<< a.ten ;
}
void main ()
{
congdan a("hoang van", " hung",1992,"nam");
congdan b("nguyen thi"," binh",1995,"nu");
a.hienthi();
b.hienthi();
sosanh(a,b);
}
bài viết chương trình lập 1 mảng sắp xếp theo giá trị tăng dần của mảng đó.
#include<iostream.h>
void nhapdl(int a[],int n)
{
for (int i=0;i<n;i++)
{
cout<<"a["<<i<<"]= ";
cin>>a[i];
}
}
void indl(int a[],int n)
{
for (int i=0;i<n;i++)
cout<<"a["<<i<<"]= "<<a[i];
cout<<"
";
}
void sapxep(int a[],int n)
{ int tg=0;
for (int i=n-1;i>0;i--)
for (int j=0;j<i+1;j++)
if (a[j]>a[j+1])
{
tg=a[j];
a[j]=a[j+1];
a[j+1]=tg;
}
}
void main ()
{
int a[20],n;
cout<<"
nhap so phan tu la ";cin>>n;
nhapdl(a,n);
cout<<"
mang chua sap xep la : ";
indl(a,n);
cout<<"
mang sau khi sap xep la : ";
sapxep(a,n);
indl(a,n);
}
viết chương trình xây dựng 1 lớp công dân bao gồm các thuộc tính hodem, ten, tuổi,hesoluong.sửa dụng hàm tạo, phương thức hiển thị ,phương thức tính lương . lương=hesoluong*512 . phương thức hiển thị hiển thị tất cả các thuộc tính .
#include<iostream.h>
#include<string.h>
class canbo
{
private :
char hodem[20];
char ten[20];
int tuoi;
int hesoluong;
public :
canbo( char hodem1[],char ten1[],int tuoi1,int hesoluong1)
{
strcpy(hodem,hodem1);
strcpy(ten,ten1) ;
tuoi=tuoi1 ;
hesoluong=hesoluong1;
}
void hienthi ()
{
cout<<"
hovaten : "<<hodem<<" "<<ten;
cout<<"
tuoi"<<tuoi;
cout<<"
he so luong : "<<hesoluong;
cout<<"
luong cua can bo la : "<<luong ();
}
float luong ()
{
return (hesoluong*512);
}
};
void main ()
{
canbo a("hoang van", "hung",20,20);
a.hienthi();
}
bài lập chương trình về lớp tính khoảng cách giữa 2 điểmtính độ dài :
#include<iostream.h>
#include<math.h>
class diem
{
private :
int x;
int y;
public :
diem( int x1,int y1)
{
x=x1;
y=y1;
}
void hienthi()
{
cout<<"("<<x<<","<<y <<")";
cout<<"
";
}
friend float tinhdodai(diem,diem);
};
float tinhdodai (diem a, diem b)
{
return sqrt( pow(a.x-b.x,2) +pow(a.y+b.y,2));
}
void main()
{
diem a(3,6);
diem b(9,4);
a.hienthi();
b.hienthi();
cout<<"do dai doan thang la " <<tinhdodai(a,b);
}
bài lập chương trình lớp kiểm tra điều kiện kết hôn.
#include<iostream.h>
#include<string.h>
#include<conio.h>
class congdan
{ private :
char hovaten[30];
char gioitinh[10];
int tuoi;
public :
void khoitao(char[],char[],int);
friend void ketluan(congdan,congdan);
};
void congdan :: khoitao(char hovaten1[],char gioitinh1[],int tuoi1)
{ strcpy(hovaten,hovaten1);
strcpy(gioitinh,gioitinh1);
tuoi=tuoi1;
return;
}
void ketluan(congdan a,congdan b)
{ if ((strcmpi(a.gioitinh,b.gioitinh)==0) && (a.tuoi>=18) &&(b.tuoi>=18))
cout<<" co the ket hon "<<endl;
else
cout<<"khong the ket hon"<<endl;
}
void main()
{ congdan a,b;
a.khoitao("hoang van hung","nam",19);
b.khoitao("nguyen thi huong","nu" ,18);
ketluan(a,b);
}
lập chương trình chuẩn hóa 1 xâu cắt hết những khoảng trắng thừa
#include<iostream.h>
#include<string.h>
void xoa(char a[],int i)
{ while (a[i]!='\0')
{ a[i]=a[i+1];
i++;}
}
void main()
{
char s[30];
int i=0,j=0;
cout<<" nhap vao chuoi s: ";
cin.get();cin.get(s,30);
while(s[i]==' ')
{i++;}
while(s[i]!='\0')
{ while(s[i]==' '&&s[i+1]==' ')
xoa(s,i);i++;
}
cout<<s<<"co"<<strlen(s)<<" ky tu";
}
viết ctrình xây dưng lớp sv gồm mã sv,họ tên,điểm toán,điểm văn...hàm tạp,phương thức hiển thị,tính điểm tb
#include <iostream.h>
#include <string.h>
class sinhvien
{
private:
char maso[10], ten[50];
float dt,dv;
public:
void khoitao(char maso1[], char ten1[], float dt1,float dv1)
{
strcpy(maso,maso1);
strcpy(ten,ten1);
dt=dt1;
dv=dv1;
}
void hienthi()
{
cout<<"ma so sinh vien la: "<<maso;
cout<<"
ten sinh vien: "<<ten;
cout<<"
diem toan: "<<dt;
cout<<"
diem van: "<<dv;
cout<<"
diem trung binh la: "<<dtb()<<"
";
}
friend float dtb();
float sinhvien::dtb()
{
return((dt*2)+(dv*3))/5;
} };
void main()
{
sinhvien A,B;
A.khoitao("12345","nguyen thi thu hang",5,4);
A.hienthi();
B.khoitao("56789","tri dep zai",9,8);
B.hienthi();
}
Bạn đang đọc truyện trên: AzTruyen.Top