سورس مرتب سازی حبابی ( Bubble sort )
این سورس یک عدد گرفته و به تعداد همان عدد از ورودی دریافت می کند و آنها را با روش Bubble sort مرتب می کند .
کد:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//--------------------------------------------------------------------------
void read_list(int a[],int n){
int i;
for(i=0;i<n;i++){
printf("\n\n\t ENTER THE ELEMENT [%d] :: ",i);
scanf("%d",&a[i]);
}
}
//--------------------------------------------------------------------------
void print_list(int a[],int n){
int i;
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
//--------------------------------------------------------------------------
void bubble_sort(int a[],int n){
int i,j,temp;
for(i=0;i<n-1;i++){
for(j=0;j<n-1;j++)
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\n\n\t PASS %d :: ",i);
print_list(a,n);
}
}
//--------------------------------------------------------------------------
void main(){
int a[20],n;
clrscr();
printf("\n\n\t ENTER THE ARRAY LENGTH :: ");
scanf("%d",&n);
read_list(a,n);
printf("\n\n\t THE ARRAY ELEMENTS ARE AS FOLLOWS :: ");
print_list(a,n);
bubble_sort(a,n);
printf("\n\n\t THE SORTED LIST IS :: ");
print_list(a,n);
getch();
}
//--------------------------------------------------------------------------
برابر بودن رشته از دو طرف
این سورس که به زبان C++ نوشته شده است اینگونه عمل می کند که رشته را از دو طرف مقایسه می کنه اگر از هر دو طرف یک جور بود برای مثال کلمه RooR از هر طرفی که خوانده شود همان RooR می باشد .
کد:
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#define Max 20
main(){
char str[Max];
int i,max,j;
cout<<"\n Enter a statment :";
cin>>str;
max=strlen(str);
for(i=0,j=max-1;i<2;i++,j--)
if(str[i]!=str[j]){
cout<<"\n Not EQUAL";
exit(1);
}
cout<<"\n EQUAL";
}
جمع دو عدد بوسیله اشاره گرها
این سورس دو عدد را بوسیله اشاره گر از ورودی می گیرد و با هم جمع می کند .
کد:
#include <iostream.h>
#include <stdlib.h>
main(){
int *x,*y;
x=(int *) calloc(1,sizeof(int));
y=(int *) calloc(1,sizeof(int));
cout<<"\n Enter a number : ";
cin>>*x;
cout<<"\n Enter a number : ";
cin>>*y;
cout<<"\n Value = "<<*x+*y;
}
حاصل ضرب دکارتی دو مجموعه بوسیله لینک لیست
این پروژه نیز کاربرد مفید لینک لیست ها را نشان می دهد که ابتدا بی نهایت مجموعه با بی نهایت عضو می گیرد و سپس اسم دو مجموعه از مجموعه های ورودی را می گیرد و حاصل ضرب دکارتی آن مجموع ها را چاپ می کند .
کد:
A={1,2,3}
B={5,6}
A * B = { (1*5) (1*6)(2*5)(2*6)(3*5)(3*6) }
#include "iostream.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
struct number{
int count;
int num;
struct number *next;
}*nn,*ln,*fn;
struct collection{
char name[20];
struct number *p;
struct collection *next;
}*fc,*nc,*lc;
void add();
void print();
void move();
int *avali,*dovomi,c1,c2;
char a[20],b[20];
main(){
add();
move();
print();
}
//***********************************
void add(){
char x[20];
int tmp;
cout<<"\n Enter your set by n";
do{
cout<<"\n Enter a name for complex : ";
cin>>x;
if(strcmp(x,"final")==0)
break;
cout<<"\n\t { \n";
nc=(struct collection*)calloc(1,sizeof(struct collection));
if(nc==NULL)
exit(1);
if(fc==NULL)
fc=nc;
else
lc->next=nc;
lc=nc;
nc->next=NULL;
nc->p=NULL;
strcpy(nc->name,x);
do{
cout<<"\t\t";
cin>>x;
if(strcmp(x,"end")==0)
break;
else
tmp=atoi(x);
nn=(struct number*)calloc(1,sizeof(struct number));
if(nn==NULL)
exit(1);
nn->next=NULL;
nn->num=tmp;
nn->count=0;
if(nc->p==NULL){
nc->p=fn=nn;
fn->count=1;
}
else{
ln->next=nn;
fn->count+=1;
}
ln=nn;
}while(1);
cout<<"\n\t } \n";
}while(1);
}
//************************************************
void move(){
int i;
clrscr();
cout<<"\n Enter two collection (C B) : ";
cin>>a>>b;
nc=fc;
while(nc!=NULL){
if(strcmp(nc->name,a)==0){
ln=nc->p;
c1=ln->count;
avali=(int*)calloc(c1,sizeof(int));
for(i=0;i<c1;i++){
avali[i]=ln->num;
ln=ln->next;
}
}
else if(strcmp(nc->name,b)==0){
ln=nc->p;
c2=ln->count;
dovomi=(int*)calloc(c2,sizeof(int));
for(i=0;i<c2;i++){
dovomi[i]=ln->num;
ln=ln->next;
}
}
nc=nc->next;
}
}
//******************************************************************************
void print(){
int i,j;
clrscr();
cout<<"\n "<<a<<"*"<<b<<" = {";
for(i=0;i<c1;i++)
for(j=0;j<c2;j++)
cout<<"("<<avali[i]<<"*"<<dovomi[j]<<")";
cout<<"}";
getch();
}
پروژه فیبوناچی بازگشتی و غیر بازگشتی
این پروژه سورس سری فیبوناچی می باشد که هم به صورت بازگشتی و هم به صورت معمولی نوشته شده است .
کد:
#include "iostream.h"
#include "conio.h"
int fib (int);
int i;
main(){
int n;
clrscr();
cout<<"\n Enter a number : ";
cin>>n;
cout<<"\n\n value = ";
if(n==1 || n==2)
cout<<"0 ";
else if(n>2)
cout<<"0 1 ";
cout<<fib(n);
getch();
}
//*****************************************
int fib (int n){
int x=0,y;
if(n<=1)
return n;
x=fib(n-1);
y=fib(n-2);
if(i<x){
cout<<x<<" ";
i=x;
}
return x+y;
}
پروژه ای بسیار خوب برای کلاس ها ( ثبت نام دانشجو ) در ++c
این پروژه یک کلاسی است که امکان اضافه کردن دانشجو ، ویرایش ، جستجو و ... را دارد که امکان ذخیره سازی اطلاعات تو فایل و بارگذاری مجدد آن هنگام اجرای برنامه را دارا می باشد و همچنین حجم فایل ذخیره شده را نشان میدهد . در کل بگم که این سورس به صورت تمام با کلاس ها نوشته شده است و کد خوبی برای درک کلاس ها می باشد .
کد:
#include "iostream.h"
#include "conio.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "fcntl.h"
#define max 50
class student {
public :
char numst[11];
char name[15];
char famili[20];
double score[4];
double ave;
student(); //initializing
~student(); //destructing
set_null(){name[0]=numst[0]='\0';}
}st[max];
//************************************
student::student(){ //initializing
set_null();
}
//*************************************
student::~student(){ //destructing
set_null();
}
//*************************************
class Enter {
private :
void save();
public :
void add();
void edit();
void search();
void print();
void len_file();
void load();
}e;
//******************************************************************************
void Enter::add(){
int i;
double sum=0.00;
for(i=0;i<max && st[i].name[0]!='\0';i++);
clrscr();
cout<<"\n Enter a name : ";
cin>>st[i].name;
cout<<"\n Enter a famili : ";
cin>>st[i].famili;
cout<<"\n Enter a numst : ";
cin>>st[i].numst;
for(int j=0;j<4;j++){
cout<<"\n Enter a score "<<j+1<<" : ";
cin>>st[i].score[j];
sum+=st[i].score[j];
}
st[i].ave=sum/4;
e.save();
}
//******************************************************************************
void Enter::edit(){
char tmp[11];
int i,flag=0;;
double sum=0.00;
clrscr();
cout<<"\n Enter a numst for edit : ";
cin>>tmp;
for(i=0;i<max && st[i].name[0]!='\0';i++)
if(strcmp(st[i].numst,tmp)==0){
cout<<"\n Enter a new name : ";
cin>>st[i].name;
cout<<"\n Enter a new famili : ";
cin>>st[i].famili;
for(int j=0;j<4;j++){
cout<<"\n Enter a new score "<<j+1<<" : ";
cin>>st[i].score[j];
sum+=st[i].score[j];
}
st[i].ave=sum/4;
cout<<"\n for continue . press key...";
getch();
flag=1;
e.save();
}
if(flag==0){
cout<<"\n Not found . press key...";
getch();
return ;
}
}
//******************************************************************************
void Enter::search(){
char tmp[15];
int i,flag=0;
clrscr();
cout<<"\n Enter a name for search : ";
cin>>tmp;
for(i=0;i<max && st[i].name[0]!='\0';i++)
if(strcmp(st[i].name,tmp)==0){
cout<<"\n Name = "<<st[i].name;
cout<<"\n Famili = "<<st[i].famili;
for(int j=0;j<4;j++)
cout<<"\n Score "<<j+1<<" = "<<st[i].score[j];
cout<<"\n average = "<<st[i].ave;
cout<<"\n for continue . press key...";
getch();
flag=1;
}
if(flag==0){
cout<<"\n Not found . press key...";
getch();
return ;
}
}
//******************************************************************************
void Enter::print(){
int i;
clrscr();
for(i=0;i<max && st[i].name[0]!='\0';i++){
cout<<"\n Name = "<<st[i].name;
cout<<"\n Famili = "<<st[i].famili;
for(int j=0;j<4;j++)
cout<<"\n Score "<<j+1<<" = "<<st[i].score[j];
cout<<"\n average = "<<st[i].ave<<"\n\n";
}
cout<<"\n for continue . press key...";
getch();
}
//******************************************************************************
void Enter::save(void){
register int i;
FILE *fp;
fp=fopen("c:\\st.dat","w+b");
if(!fp){
cout<<"\n Cannot open file press key ...";
getch();
return ;
}
for(i=0;i<max && st[i].name[0]!='\0';i++)
fwrite(&st[i],sizeof(class student),1,fp);
fclose(fp);
}
//******************************************************************************
void Enter::load(void){
register int i;
FILE *fp;
clrscr();
fp=fopen("c:\\st.dat","rb");
if(!fp){
cout<<"\n Cannot open file press key ...";
getch();
return ;
}
for(i=0;i<max;i++){
fread(&st[i],sizeof(class student),1,fp);
if(feof(fp)){
clrscr();
cout<<" Data loaded press a key ...";
getch();
return ;
}
}
fclose(fp);
}
//******************************************************************************
void Enter::len_file(){
int handle;
clrscr();
handle=open("c:\\st.dat",O_RDONLY);
cout<<"\n File lenght in byte = "<<filelength(handle)<<" byte ";
getch();
close(handle);
}
//******************************************************************
void main(){
int num;
e.load();
do{
clrscr();
cout<<"\t\t Welcome\n";
cout<<"\n 1).Add";
cout<<"\n 2).Edit";
cout<<"\n 3).Search";
cout<<"\n 4).List";
cout<<"\n 5).Length file";
cout<<"\n 6).Exit";
cout<<"\n\n Enter a number (1-6) :";
num=getche()-48;
getch();
switch (num){
case 1 : e.add();
break;
case 2 : e.edit();
break;
case 3 : e.search();
break;
case 4 : e.print();
break;
case 5 : e.len_file();
break;
case 6 : exit(1);
}
}while(1);
}// end program
سورس مرتب سازی سریع ( Quick sort )
این سورس یک عدد گرفته و به تعداد همان عدد از ورودی دریافت می کند و آنها را با روش .Quick sort مرتب می کند .
کد:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//--------------------------------------------------------------------------
void read_list(int a[],int n){
int i;
for(i=0;i<n;i++){
printf("\n\n\t ENTER THE ELEMENT [%d] :: ",i);
scanf("%d",&a[i]);
}
}
//--------------------------------------------------------------------------
void print_list(int a[],int n){
int i;
for(i=0;i<n;i++)
printf("\n\n\t %d",a[i]);
}
//--------------------------------------------------------------------------
void quick_sort(int a[],int first,int last){
int low,high,temp,pivot;
low=first;
high=last;
pivot=a[(first+last)/2];
do{
while(a[low]<pivot)
low++;
while(a[high]>pivot)
high--;
if(low<=high){
temp=a[low];
a[low]=a[high];
a[high]=temp;
low=low+1;
high=high-1;
}
}while(low<=high);
if(first<high)
quick_sort(a,first,high);
if(low<last)
quick_sort(a,low,last);
}
//--------------------------------------------------------------------------
void main(){
int a[20],n;
clrscr();
printf("\n\n\t ENTER THE ARRAY LENGTH :: ");
scanf("%d",&n);
read_list(a,n);
printf("\n\n\t THE ARRAY ELEMENTS ARE AS FOLLOWS :: ");
print_list(a,n);
quick_sort(a,0,n-1);
printf("\n\n\t THE SOTED LIST IS :: ");
print_list(a,n);
getch();
}
//--------------------------------------------------------------------------