سلام دوستان
هدف از ايجاد اين تاپيك قرار دادن پروژهاي برنامه نويسي پايان ترم مون هست .
شايد كه نه حتما ميتونيم بهم كمك كنيم تا مشكلاتمون تو برنامه نويسي راحت تر حل بشه .
ممنون ميشم پروژه هاتون رو تو اين بخش قرار بدهيد @};-@};-@};-
نمایش نسخه قابل چاپ
سلام دوستان
هدف از ايجاد اين تاپيك قرار دادن پروژهاي برنامه نويسي پايان ترم مون هست .
شايد كه نه حتما ميتونيم بهم كمك كنيم تا مشكلاتمون تو برنامه نويسي راحت تر حل بشه .
ممنون ميشم پروژه هاتون رو تو اين بخش قرار بدهيد @};-@};-@};-
اين پروژه پايان ترم يكي از دوستان خودمون تو همين سايت بود كه كمك شون كردم :"> و پروژه شون رو تحويل دادن ولي هنوز فرصت نكردم برم ازشون بپرسم كه چند شدند .
ولي مطمئنم كه نمره شون خوب شده {big green} .
برنامه ای بنویسید که بتواند یک کلاسی ازدانشجویان رابه صورت آرایه ای ازاشاره گرها تعریف کند که هراشاره گر به یک ساختمان ازساختارstudent اشاره میکند.(new student وiشماره دانشجو) سپس از فایل ورودی نمرات دانشجورا برحسب شماره دانشجو ونمره دانشجو دریافت کرده وپس از محاسبه معدل کلاس درصفحه خروجی معدل را نشان دهد ولیست را به صورت شده برحسب نمره وفقط آنهایی که نمره بالای میانگین را دارند در یک فایل خروجی دیگر ذخیره کند
اينم جواب پروژه شون :
کد PHP:
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
struct Student
{
int ID;
char FirstName[20];
char LastName[30];
double Mark;
} st;
struct Student *classA[30];
void new_student(int st_id);
void Print_StudentList();
float CalcAverage();
void Sort_Students();
int StudentCount;
float ClassAverage;
void main()
{
clrscr();
cout << "\Enter Student Number :";
int i;
cin >> StudentCount;
for (i=1;i<=StudentCount;i++)
{
new_student(i);
}
clrscr();
ClassAverage = CalcAverage();
printf("\n\n\n ClassA Average : %.2f ",ClassAverage);
Sort_Students();
Print_StudentList();
getch();
}
void new_student(int st_id)
{
classA[st_id] = classA[0] + (sizeof(st) * st_id) ;
classA[st_id]->ID = st_id;
cout << "\nEnter Student[" << st_id << "].LastName :";
cin >> classA[st_id]->LastName;
cout << "\nEnter Student[" << st_id << "].FirstName:";
cin >> classA[st_id]->FirstName;
cout << "\nEnter Student[" << st_id << "].Mark:";
cin >> classA[st_id]->Mark;
}
void Print_StudentList()
{
int i;
//.................................
//... Save In File ................
//.................................
FILE *fp;
char filename[20] = "c:\\st.txt";
fp = fopen(filename,"w");
if (fp!=NULL)
{
fprintf(fp,"\nStudent List : ");
for (i=1;i<=StudentCount;i++)
{
if (classA[i]-> Mark > ClassAverage)
fprintf(fp,"\n%d : %s %s : %.2f ",classA[i]->ID,classA[i]->FirstName,classA[i]->LastName,classA[i]->Mark);
else
break;
}
fclose(fp);
cout << "\nList Of Students Save in File : \"" << filename << "\"\n";
}
else
{
cout << "\nError On Create File " << filename << "\n";
}
//.................................
//... Preview On Screen ...........
//.................................
cout << "\n\nStudentList\n\n";
for (i=1;i<=StudentCount;i++)
{
if (classA[i]-> Mark > ClassAverage)
cout << "\n" << classA[i]->ID << ": " << classA[i]->FirstName << " | " << classA[i]->LastName << " | " << classA[i]->Mark;
else
break;
}
//.................................
//.................................
//.................................
}
float CalcAverage()
{
int i;
float sumMarks=0;
for (i=1;i<=StudentCount;i++)
{
sumMarks += classA[i]->Mark;
}
return (sumMarks / StudentCount);
}
void Sort_Students()
{
int i,j;
for (i=1;i<=StudentCount;i++)
{
for (j=i+1;j<=StudentCount;j++)
{
if (classA[j]->Mark > classA[i]->Mark )
{
struct Student *Temp;
Temp = classA[j];
classA[j] = classA[i];
classA[i] = Temp;
}
}
}
}
سلام
خب وقتی شما بحثی رو مهم قرار دادین لااقل یه کم گسترشش بدین و بگین که فلان پروژه به چه زبانی نوشته شده.
هر چقدر هم گشتم خبری از کلکسیون ارزشمند مایکروسافت یعنی ویژوال استودیو نبود.
یعنی از دوستان علاقه مند به این موضوع کسی اینجا نیست؟
این هم یکی از برنامه ها که از یه فایل 1.txt می خونه و ان سطرهایی که دقیقا مثل هم هستند را پاک میکنه تا متن های اضافی پاک بشه و فایلی که سطر تکراری نداره را به عنوان 2.txt ذخیره می کنه
کد PHP:
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<fstream>
using std::fstream;
#include<string.h>
int khat;
char id[33];
int natije;
char checkid[33];
int cmp;
char getid[33];
int check(char checkid[33]) {
fstream m3("c:\\2.txt");
for (int j = 1; j <=khat;j++) {
m3.getline(getid,32);
cmp = strcmp(checkid,getid);
if (cmp == 0) {
cout<<checkid<<endl;
return 0;
}
}
return cmp;
}
int main () {
fstream m2("c:\\2.txt");
cout<<"tedad khotot?\n";
cin >> khat;
fstream m1("c:\\1.txt");
for (int i = 1; i<=khat;i++) {
m1.getline(id,32);
natije = check(id);
if (natije == 0) {
continue;
}
else {
m2<<id<<endl;
}
}
return 0;
}
گرفتن ping سایت ها تنها با کلیک بر روی یک فایل
این یک برنامه کوچک هست که در ویندوز پس از کامپایل باید از فایل اجرایی یک shortcut بسازید، سپس با راست کلیک کردن بر روی آن و انتخاب properties به قسمت Targets بروید. در آن مسیر فایل نوشته شده است، حال فقط کافیست آدرس وب سایت مورد نظر یا IP مورد نظر را در ادامه آن مثلا به شکل زیر داخل " " بنویسید. از این به بعد هرگاه بر روی این shortcut کلیک کنید پنجره کنسول باز می شود و سایت را ping می کند:(به تعداد مورد نظر نیز می توانید shortcut بسازید)
کد PHP:
"E:\MyPingApp.exe" "ping www.yahoo.com"
برنامه در کامپایلرهای قدیمی و یا جدید اما در حالت اسکی:
کد PHP:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(int agrc, char *argv[])
{
system( argv[1] );
getch();
return 0;
}
در حالت یونیکد (در کامپایلرهای جدید):
کد PHP:
int _tmain(int argc, _TCHAR* argv[])
{
_tsystem( argv[1] );
_getch();
return 0;
}
اطلاعات عمومی دانشجو
خصوصیات برنامه:
1)وارد کردن نمره به تعداد دلخواه
2)بازه نمرات بین 0 تا 100
3)مشخص کردن وضعیت نمرات
4)گرفتن میانگین
5)مشخص کردن تعداد افراد قبول و مردود شده
کد PHP:
#include <iostream.h>
#include <conio.h>
int main()
{
float x, counter=1, counterp=0, counterf=0,ave=0,y,m;
cout<<"How many mark? ";
cin>>y;
cout<<"Pass and Fail: ";
cin>>m;
cout<<"#########################################";
cout<<"\n";
while (counter<=y){
cout<<"Mark "<<counter<<": ";
cin>>x ;
counter=counter+1;
ave+=x;
if (x>100){
cout<<"ERROR:FALSE MARK"<<"\n";
cout<<"\n";
cout<<"egain enter the ";
counter=counter-1;
}
else if (x>=m){
cout<<"Pass"<<"\n"<<"*********************"<<"\n"<<"\n";
counterp+=1;
}
else{
cout<<"Fail"<<"\n"<<"*********************"<<"\n"<<"\n";
counterf+=1;
}
}
if (counter=y){
cout<<"Pass number: "<<counterp<<"\n";
cout<<"Fail number: "<<counterf<<"\n";
cout<<"Average: "<<ave/y<<endl;
}
getch();
return 0;
}
اوور لودینگ اپراتورها در کلاس در C++
کد PHP:
#include <iostream.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
class string
{
private:
char text[40];
public:
void show()
{
cout << text << "\n";
}
string operator + (string s)
{
string temp;
strcpy(temp.text, text);
strcat (temp.text, s.text);
return temp;
}
string operator + (char s[])
{
string temp;
strcpy(temp.text, text);
strcat (temp.text, s);
return temp;
}
string operator++()
{
char temp[40];
strcpy(temp , text);
strcat(text,temp);
}
friend string operator +(char s[] , string s2)
{
string temp;
strcpy(temp.text , s);
strcat(temp.text , s2.text);
return temp;
}
string operator =(char s[])
{
strcpy(text , s);
}
لیست پیوندی در زیان c به زبان ساده
مختصات یک دایره را می گیرد و در لیست قرار می دهد
}کد PHP:
#include <stdio.h>
#include <stdlib.h>
struct circle
{
int x , y , r;
circle *link;
}*circlestart;
//----------------------------------------------------------------------------
int circleinsert()
{
circle *ptr;
ptr = (circle*) malloc(sizeof(circle)); //creat a memmory for new item
ptr->link = NULL;
printf("Please Enter x y r seprated by space\n");
int x , y , r;
scanf("%d %d %d" , &x , &y , &r);
ptr->x = x;
ptr->y = y;
ptr->r = r;
if (circlestart == NULL) //if the list is empty put the new one on the first position
{
circlestart = ptr;
return 0;
}
circle *p1;
p1 = circlestart;
while ( p1->link != NULL) //searches for the last item in the list
{
p1 = p1->link;
}
p1->link = ptr;
return 0;
}
//----------------------------------------------------------------------------
int circleshow()
{
if (circlestart == NULL) //if the list is empty
{
printf("No Circle Excist To Show\n");
return 0;
}
circle *ptr;
ptr = circlestart;
while (ptr != NULL) //nevigate all of the list and print it
{
printf("X: %d Y: %d R: %d\n" , ptr->x , ptr->y , ptr->r);
ptr = ptr->link;
}
return 0;
}
//----------------------------------------------------------------------------
int circledelete()
{
if (circlestart == NULL)//if the list is empty
{
printf("No circles to delete\n");
return 0;
}
int x , y ,r;
printf("Please Enter x y r seprate by space to delete\n");
scanf("%d %d %d" , &x , &y , &r);
if (circlestart->x == x && circlestart->y == y && circlestart->r == r) //if the item is in the first position
{
circle *p;
p = circlestart;
circlestart = circlestart->link;
free(p);
printf("deleted\n");
return 0;
}
circle *ptr , *preptr;
ptr = circlestart;
while (ptr->link != NULL) // nevigate all the list to find the item and delete it
{
preptr = ptr;
ptr = ptr->link;
if (ptr->x == x && ptr->y == y && ptr->r == r)
{
(*preptr).link = (*ptr).link;
printf("deleted\n");
free(ptr);
return 0;
}
}
printf("Not Found To Delete\n");
return 0;
}
//----------------------------------------------------------------------------
void menu()
{
printf("Please select an item with entering the number of it\n");
printf("1- Creat a new circle\n");
printf("2- Show circles\n");
printf("3- Delete an exicting circle\n");
printf("4- exit\n");
}
//----------------------------------------------------------------------------
int main()
{
int key;
do
{
menu();
scanf("%d" , &key);
switch(key)
{
case 1:
circleinsert();
break;
case 2:
circleshow();
break;
case 3:
circledelete();
break;
case 4:
return 0;
}
}while(1);
return 0;
الگوریتم کلی و سورس کامل مثلث n عددی:
}کد PHP:
#include <iostream.h>
int cNum (int k)
{
int val =1;
while( k/10 > 0 )
{
k /=10;
val ++;
}
return val;
}
int cNumCount (int k)
{
int val = 0;
for( int i=k; i>0 && cNum(i)== cNum(k); i--)
val ++;
return val;
}
int main()
{
int n, c, b, j;
cout<<"\n add ro vared konid >";
cin>>n;
c = n*2;
b = 1;
for(int i=0;i<n;i++){
cout<<"\n";
for(j=0;j<c-i-cNumCount(b)*(cNum(i)-1);j++)
cout<<" ";
for( j=1;j<=b;j++)
cout<<j;
for( j=1;j<i+cNum(i);j++)
cout<<j;
b+=1;
}
return 0;
برنامه ای که داخل فایل(Data.Txt) کلمه ای رو که بیشترین تکرار رو داشته باشه رو برمیگردونه.
کد PHP:
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
void main()
{
clrscr();
FILE *f1;
struct txt
{
char name[20];
int rep;
}list[10];
f1=fopen("Data.txt","rb");
for(int i=0;i<10;i++)
list[i].rep=0;
int j=0;
char A[20];
while(f1 != NULL && !feof(f1))
{
fscanf(f1,"%s",&A);
for(int i=0;i<j;i++)
if( strcmp(list[i].name, A)==0 ){
list[i].rep++;
break;
}else{
strcpy(list[j].name, A);
list[j].rep = 1;
}
j++;
}
fclose(f1);
int max=0;
for(int k=1;k<10;k++)
if(list[k].rep>list[max].rep)
max=k;
cout<<list[max].name;
getch();
}
برنامه کاربردی برای آموزش استفاده از فایل و Structها
}کد PHP:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
struct address {
char name[30] ;
char street[30] ;
char city[20] ;
char state[3] ;
char zip[10] ;
struct address *next ;
struct address *prior ;
} list_entry ;
struct address *start ;
struct address *last ;
void enter() , display() , search() ;
void save() , load() , list() , del();
void display(struct address *info, int *row);
struct address *find(char *);
int menu_select();
struct address *store(struct address *, struct address *);
int main ()
{
start = last = NULL ;
for(;;) {
switch(menu_select()) {
case 1: enter(); break ;
case 2 : del(); break ;
case 3: list() ; break ;
case 4: search(); break ;
case 5: save(); break ;
case 6: load(); break ;
case 7: exit(0) ;
}//end of switch
}//end of for
}//end of main
//****************
int menu_select()
{
char s[5];
clrscr() ;
gotoxy(25, 4) ;
printf("1. enter a name ") ;
gotoxy(25, 6) ;
printf("2. delete a name ") ;
gotoxy(25, 8) ;
printf("3. list all files ") ;
gotoxy(25, 10) ;
printf("4. search ") ;
gotoxy(25, 12) ;
printf("5. save the file ") ;
gotoxy(25, 14) ;
printf("6. load the file ") ;
gotoxy(25, 16) ;
printf("7. quit ") ;
do {
gotoxy(20, 18) ;
printf("enter your select--www.gach18.blogfa.ir(1-7):");
gets(s);
} while (atoi(s) < 0 || atoi(s) > 7) ;
return atoi(s) ;
}
//*********************
void enter ()
{
struct address *info ;
int i ;
char ch ;
clrscr() ;
gotoxy(3, 2) ;
printf(" name street city state zip");
gotoxy(3, 3) ;
printf(" ------------ -------- ");
printf("-------- ----- ------- ");
i = 4 ;
for (;;) {
info = (struct address *)malloc(sizeof(list_entry)) ;
if(!info) {
printf("\n out of memory. press a key ") ;
getch();
return ;
}
gotoxy(3, i) ;
gets(info -> name) ;
if (!info -> name[0]) {
gotoxy(15, i + 1) ;
printf("press a key to continue");
getch() ;
break ;
}//end of if
gotoxy(18, i);
gets(info -> street) ;
gotoxy(28, i) ;
gets(info -> city) ;
gotoxy(38, i) ;
gets(info -> state) ;
gotoxy(45, i) ;
gets(info -> zip) ;
i++ ;
start = store(info, start) ;
} /* entry loop */
}
//**************
struct address *store(struct address *i, struct address *top)
{
struct address *old, *p ;
if(last == NULL) {
i -> next = NULL ;
i -> prior = NULL ;
start = i;
last = i ;
return i ;
}
p = top ;
old = NULL ;
while (p != NULL) {
if(strcmp(p -> name, i -> name) < 0) {
old = p ;
p = p -> next ;
}//end of if
else {
if (p -> prior) {
p -> prior -> next=i ;
i -> next=p ;
i -> prior=p -> prior;
p -> prior=i ;
return top ;
}//end of if
i -> next = p ;
i -> prior = NULL ;
p -> prior = i ;
return i ;
}//end of if
} // end of while
old -> next = i ;
i -> next = NULL ;
i -> prior = old ;
last = i ;
return start ;
}
//******************
void del()
{
struct address *info;
char name[80];
gotoxy(20, 20) ;
printf(" enter name for delete : ") ;
gets(name) ;
info = find(name) ;
if(info == NULL) {
gotoxy(10, 20) ;
printf(" name not found! press a key to continue.");
getch() ;
}
if (info)
if (start == info)
{
start = info -> next ;
if(start)
start -> prior = NULL ;
else
last = NULL ;
} //end of if
else {
info -> prior -> next = info -> next;
if(info != last)
info -> next -> prior = info -> prior;
else
last = info -> prior ;
} //end of else
free(info) ;
gotoxy(10,20) ;
printf("name deleted, press a key to continue.");
getch() ;
}
//*******************************
struct address *find(char *name)
{
struct address *info ;
info = start ;
while(info != NULL) {
if (strcmp(name, info -> name) == 0)
return info;
info = info -> next ;
}
return NULL ;
}
//*****************
void list ()
{
struct address *info ;
int i ;
info = start ;
clrscr() ;
gotoxy(3, 2) ;
printf(" name street city state zip");
gotoxy(3, 3) ;
printf(" ------------ -------- -");
printf("------- ----- ------- ");
i = 4 ;
while(info != NULL) {
display(info, &i) ;
info = info -> next ;
}
gotoxy(15, i + 2) ;
printf("press a key to continue.");
getch() ;
}
//*******************
void display(struct address *info, int *row)
{
gotoxy(3, *row) ;
printf("%s", info -> name) ;
gotoxy(18, *row) ;
printf("%s", info -> street) ;
gotoxy(28, *row) ;
printf("%s", info -> city) ;
gotoxy(38, *row) ;
printf(info -> state) ;
gotoxy(47, *row) ;
printf(info -> zip) ;
*row = *row + 1 ;
}
//**************************
void search()
{
char name[40] ;
int i ;
struct address *info;
gotoxy(20, 20) ;
printf(" enter name to find : ");
gets(name) ;
info = find(name) ;
if(info == NULL) {
gotoxy(10, 20) ;
printf(" name not found! press a key to continue.");
getch() ;
}//end of if
else {
clrscr() ;
gotoxy(3, 2) ;
printf(" name street city state zip");
gotoxy(3, 3) ;
printf(" ------------ -------");
printf("- -------- ----- ------- ") ;
i = 4 ;
display(info ,&i) ;
gotoxy(15, i + 2) ;
printf("press a key to continue.");
getch() ;
}//end of else
}
//*********************
void save()
{
struct address *info ;
FILE *fp ;
if((fp = fopen("l.dat","wb")) == NULL) {
printf("\n cannot open file. ") ;
getch();
exit(1) ;
}//end of if
gotoxy(20, 20) ;
printf("<< saving file >>") ;
info = start ;
while(info) {
fwrite(info, sizeof(struct address), 1, fp);
info = info -> next ;
}//end of while
fclose(fp) ;
gotoxy(15, 22) ;
printf("file successfuly saved press a key...") ;
getch() ;
}
//********************
void load ()
{
struct address *info , *temp = NULL;
FILE *fp ;
fp = fopen("l.dat","rb") ;
if(fp == NULL) {
printf("\n cannot open file.");
getch();
exit(1) ;
}
while(start) {
info = start -> next ;
free(info) ;
start = info ;
}
gotoxy(20,20) ;
printf(" << loading file >> ") ;
start = NULL ;
while (!feof(fp)) {
info = (struct address *)
malloc(sizeof(struct address)) ;
if(1 != fread(info, sizeof(struct address), 1, fp))
break ;
if(start == NULL) {
temp = start = info ;
info -> prior = NULL ;
info -> next = NULL ;
}//end of if
else {
info -> next = NULL ;
temp -> next = info ;
info -> prior = temp ;
temp = info;
}
}//end of while
last = temp ;
fclose(fp) ;
gotoxy(15,22) ;
printf("file successfuly loaded press a key ...") ;
getch();
رسم خط
کد PHP:
#include <graphics.h>
#include<dos.h>
#include<iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
int main(void)
{
clrscr();
int m,n,x,y,xa,xb,ya,yb,color,dx,dy;
cout<<"Enter (xa,ya) And (xb,yb):\n";
cin>>xa>>ya>>xb>>yb>>color;
xa=abs(xa);
ya=abs(ya);
xb=abs(xb);
yb=abs(yb);
if(xa<xb)
{
x=xa;
y=ya;
}
else
{
x=xb;
y=yb;
}
m=(yb-ya)/(xb-xa);
if(m<1)
{
dx=1;
dy=m;
n=abs(xb-xa);
}
else
{
dy=1;
dx=1/m;
n=abs(yb-ya);
}
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
for(int i=0;i<n;i++)
{
putpixel(int(x),int(y),color);
x+=dx;
y+=dy;
}
getch();
closegraph();
return 0;
دفتر تلفن ساده با کلاس ها
کد PHP:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
//=====================================
//=====================================
struct address {
char name[30] ;
char street[30] ;
char city[20] ;
char state[3] ;
char number[14] ;
struct address *next ;
struct address *prior ;
} list_entry ;
//=====================================
//=====================================
struct address *start ;
struct address *last ;
void enter() , display() , search() ;
void list() , del();
void display(struct address *info, int *row);
struct address *find(char *);
int menu_select();
struct address *store(struct address *, struct address *);
//=====================================
//=====================================
//****************
int menu_select()
{
char s[5];
system("cls");
gotoxy(25,4) ;
printf("1. enter a name ") ;
gotoxy(25,6) ;
printf("2. delete a name ") ;
gotoxy(25, 8) ;
printf("3. list all files ") ;
gotoxy(25, 10) ;
printf("4. search ") ;
gotoxy(25, 12) ;
printf("5. quit ") ;
do {
gotoxy(20, 18) ;
printf("enter your select--power By majid (1-5):");
gets(s);
} while (atoi(s) < 0 || atoi(s) > 5) ;
return atoi(s) ;
}
//*********************
void enter ()
{
struct address *info ;
int i ;
char ch ;
system("cls") ;
gotoxy(3, 2) ;
printf(" name street city state number");
gotoxy(3, 3) ;
printf(" ------------ -------- ");
printf("-------- ----- ------- ");
i = 4 ;
for (;;) {
info = (struct address *)malloc(sizeof(list_entry)) ;
if(!info) {
printf("\n out of memory. press a key ") ;
getch();
return ;
}
gotoxy(3, i) ;
gets(info -> name) ;
if (!info -> name[0]) {
gotoxy(15, i + 1) ;
printf("press a key to continue");
getch() ;
break ;
}//end of if
gotoxy(18, i);
gets(info -> street) ;
gotoxy(28, i) ;
gets(info -> city) ;
gotoxy(38, i) ;
gets(info -> state) ;
gotoxy(45, i) ;
gets(info -> number) ;
i++ ;
start = store(info, start) ;
} /* entry loop */
}
//**************
struct address *store(struct address *i, struct address *top)
{
struct address *old, *p ;
if(last == NULL) {
i -> next = NULL ;
i -> prior = NULL ;
start = i;
last = i ;
return i ;
}
p = top ;
old = NULL ;
while (p != NULL) {
if(strcmp(p -> name, i -> name) < 0) {
old = p ;
p = p -> next ;
}//end of if
else {
if (p -> prior) {
p -> prior -> next=i ;
i -> next=p ;
i -> prior=p -> prior;
p -> prior=i ;
return top ;
}//end of if
i -> next = p ;
i -> prior = NULL ;
p -> prior = i ;
return i ;
}//end of if
} // end of while
old -> next = i ;
i -> next = NULL ;
i -> prior = old ;
last = i ;
return start ;
}
//******************
void del()
{
struct address *info;
char name[80];
gotoxy(20, 20) ;
printf(" enter name for delete : ") ;
gets(name) ;
info = find(name) ;
if(info == NULL) {
gotoxy(10, 20) ;
printf(" name not found! press a key to continue.");
getch() ;
}
if (info)
if (start == info)
{
start = info -> next ;
if(start)
start -> prior = NULL ;
else
last = NULL ;
} //end of if
else {
info -> prior -> next = info -> next;
if(info != last)
info -> next -> prior = info -> prior;
else
last = info -> prior ;
} //end of else
free(info) ;
gotoxy(10,20) ;
printf("name deleted, press a key to continue.");
getch() ;
}
//*******************************
struct address *find(char *name)
{
struct address *info ;
info = start ;
while(info != NULL) {
if (strcmp(name, info -> name) == 0)
return info;
info = info -> next ;
}
return NULL ;
}
//*****************
void list ()
{
struct address *info ;
int i ;
info = start ;
system("cls") ;
gotoxy(3, 2) ;
printf(" name street city state number");
gotoxy(3, 3) ;
printf(" ------------ -------- -");
printf("------- ----- ------- ");
i = 4 ;
while(info != NULL) {
display(info, &i) ;
info = info -> next ;
}
gotoxy(15, i + 2) ;
printf("press a key to continue.");
getch() ;
}
//*******************
void display(struct address *info, int *row)
{
gotoxy(3, *row) ;
printf("%s", info -> name) ;
gotoxy(18, *row) ;
printf("%s", info -> street) ;
gotoxy(28, *row) ;
printf("%s", info -> city) ;
gotoxy(38, *row) ;
printf(info -> state) ;
gotoxy(47, *row) ;
printf(info -> number) ;
*row = *row + 1 ;
}
//**************************
void search()
{
char name[40] ;
int i ;
struct address *info;
gotoxy(20, 20) ;
printf(" enter name to find : ");
gets(name) ;
info = find(name) ;
if(info == NULL) {
gotoxy(10, 20) ;
printf(" name not found! press a key to continue.");
getch() ;
}//end of if
else {
system("cls") ;
gotoxy(3, 2) ;
printf(" name street city state number");
gotoxy(3, 3) ;
printf(" ------------ -------");
printf("- -------- ----- ------- ") ;
i = 4 ;
display(info ,&i) ;
gotoxy(15, i + 2) ;
printf("press a key to continue.");
getch() ;
}//end of else
}
//*********************
int main()
{
start = last = NULL ;
for(;;) {
switch(menu_select()) {
case 1: enter(); break ;
case 2 : del(); break ;
case 3: list() ; break ;
case 4: search(); break ;
case 5: exit(0) ;
}//end of switch
}//end of for
}//end of main
آبجی به نظرت بچه ها با این کارشون تنبل نمیشن؟؟؟
کتابخانه با امکان ورود - ویرایش- حذف -به امانت گرفتن و پس دادن کتاب
کد PHP:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
//===========================
//===========================
class book
{
friend class library;
private:
char book_name[11];
char explain[51];
char aouther[11];
char part;
char user[11];
int reserved;
book *next;
public:
book();
void edit();
void reserv();
void getback();
}
//---------------------------
book::book()
{
clrscr();
cout<<"============ add a new book =================="
<<"\n\nto insert new book ,enter flow informations:"
<<"\n\nbook name?";
gets(book_name);
book_name[10]=NULL;
cout<<"\naouther name?";
gets(aouther);
aouther[10]=NULL;
cout<<"\nany explain?";
gets(explain);
explain[50]=NULL;
part=book_name[0];
reserved=0;
next=NULL;
cout<<"\n\ninformathions set!";
getch();
}
//---------------------------
void book::edit()
{
cout<<"error ";
}
//---------------------------
void book::reserv()
{
clrscr();
cout<<"\n=========== reserving book ===================";
if(reserved==0)
{
cout<<"\n\nuser name?";
gets(user);
user[11]=NULL;
reserved=1;
}
if(reserved==0)
{
cout<<"\n\nsorry! book has been reserved befor"
<<"by user:";
puts(user);
}
getch();
}
//---------------------------
void book::getback()
{
reserved=0;
cout<<"\n\nbook got bak.";
getch();
}
//===========================
//===========================
class library
{
public:
library();
void run_menu();
private:
book *parts[24];
void insert(book*);
void find();
void search();
}
//---------------------------
library::library()
{
for(int i=0;i<=23;i++)
parts[i]=NULL;
}
//---------------------------
void library::run_menu()
{
char ch='n';
while(ch!='4')
{
clrscr();
cout<<"================= LIBRARY =================="
<<"\n\n1:add a new book."
<<"\n2:find a book."
<<"\n3:search a book."
<<"\n4:exit."
<<"press numbers:";
ch=getch();
if(ch=='1')
{
book *n=new book;
insert(n);
}
if(ch=='2')
{
find();
}
if(ch=='3')
{
search();
}
}//while
}
//---------------------------
void library::insert(book *s)
{
int d=s->part-97;
if(parts[d]==NULL)
{
parts[d]=s;
}
else
{
book *p=parts[d],*q;
while(p!=NULL && strcmp(p->book_name,s->book_name)<0)
{
q=p;
p=p->next;
}
q->next=s;
s->next=p;
}
}
//---------------------------
void library::find()
{
clrscr();
cout<<"=========== edit / delete books ============"
<<"\nenter exact book name:";
char name[11];
gets(name);
name[11]=NULL;
int d=name[0]-97;
book *p=parts[d],*q;
while(p!=NULL && strcmp(p->book_name,name)!=0)
{
q=p;
p=p->next;
}
if(p==NULL)
{
cout<<"not found!";
}
if(p!=NULL)
{
cout<<"\n\ndelete it?('d') or edit?('e')"
<<" or reserv?('r') or getback?('g') :";
char ch=getch();
if(ch=='d')
{
q->next=p->next;
delete p;
}
if(ch=='e')
{
p->edit();
}
if(ch=='r')
{
p->reserv();
}
if(ch=='g')
{
p->getback();
}
}
getch();
}
//---------------------------
void library::search()
{
char ch='6';
while(ch!='4')
{
clrscr();
cout<<"================== SEARCH ===================";
cout<<"\n\n1:search for name."
<<"\n2:search for aouther."
<<"\n3:search for explanations."
<<"\n4:back to main menu.(press numbers)";
ch=getch();
if(ch=='1')
{
cout<<"\n\nenter exact name:";
char name[10];
gets(name);
name[11]=NULL;
int d=name[0]-97;
book *p=parts[d];
while(p!=NULL && strcmp(p->book_name,name)!=0)
{
p=p->next;
}
if(p==NULL)
{
cout<<"\nnot founded!";
}
else
{
cout<<"\n\n";
cout<<"name:";puts(p->book_name);
cout<<"aouther:";puts(p->aouther);
cout<<"explain:";puts(p->explain);
if(p->reserved==1)
{
cout<<"RESERVED by:";
puts(p->user);
}
else
{
cout<<"NOT RESERVES";
}
}//else
getch();
}//1
if(ch=='2')
{
cout<<"\n\nenter exact aouther name:";
char name[11];
gets(name);
name[11]=NULL;
int f=0;
for(int i=0;i<=23;i++)
{
book *p=parts[i];
while(p!=NULL)
{
if(strcmp(p->aouther,name)==0)
{
f++;
cout<<"\n"<<f<<":";
cout<<"name:";puts(p->book_name);
cout<<"aouthor:";puts(p->aouther);
cout<<"explain:";puts(p->explain);
if(p->reserved==1)
{
cout<<"RESERVED BY";
puts(p->user);
}
else
{
cout<<"NOT RESERVED";
}
}
p=p->next;
}//while
}
getch();
}//2
if(ch=='3')
{
cout<<"\n\nenter key(15char):";
char name[16];
gets(name);
name[15]=NULL;
int f=0;
for(int i=0;i<=23;i++)
{
book *p=parts[i];
while(p!=NULL)
{
char *tokenptr;
tokenptr=strtok(p->explain," ");
while(tokenptr!=NULL)
{
if(strcmp(tokenptr,name)==0)
{
f++;
cout<<"\n"<<f<<":";
cout<<"name:";puts(p->book_name);
cout<<"aouthor:";puts(p->aouther);
cout<<"explain:";puts(p->explain);
if(p->reserved==1)
{
cout<<"RESERVED BY";
puts(p->user);
}
else
{
cout<<"NOT RESERVED";
}
}
tokenptr=strtok(NULL," ");
}
p=p->next;
}//while
}//for i
getch();
}//3
}
}
//===========================
//===========================
int main()
{
clrscr();
library l1;
l1.run_menu();
return(0);
}
تغییر رنگ نوشته متن :
کامپایلر : Borland C++ 5.02
روش اول :
روش دوم :کد PHP:
#include <iostream>
#include <conio>
int main(){
for(int i=1;i<=4;i++){
textcolor(i+8);
cprintf("Welcome to C++ World");
cout<<endl;}
getch();
}
کد PHP:
#include <iostream>
#include <conio>
#include <stdlib>
int main(){
for(int i=1;i<=4;i++){
system("color 0A");
cout<<"Welcome to C++ World\n";
}
getch();
}
خب این هم برنامه ای که با روش بازگشتی عددی از مبنای 10 رو به هر مبنای تبدیل میکنه ;):
}کد PHP:
#include <iostream>
#include <conio>
void Base(int n,int x){
if(n<x)
cout<<n;
else{
Base(n/x,x);
cout<<n%x;
}
}
//=====================
int main(){
int x,n;
cout<<"Enter a number in decimal :\n";
cin>>n;
cout<<"Enter base :\n";
cin>>x;
cout<<"------------------------\n";
Base(n,x);
getch();
این همبرنامه ای برای پیدا کردن فاکتوریل یک عدد بروش برنامه نویسی پویا
کد PHP:
#include <iostream>
#include <conio>
int main(){
int n;
cout<<"Enter a number :\n";
cin>>n;
int *a = new int[n+1];
a[0] = 1;
for(int i=1;i<=n;i++)
a[i] = i*a[i-1];
for(int i=0;i<=n;i++)
cout<<i<<"! = "<<a[i]<<endl;
getch();
}
وارد کردن اطلاعات دانشجویان؛جستجو بر اساس دو فیلد؛خذف دانشجو بر اساس دو فیلد؛ذحیره اطلاعات در فایل؛مرتب کردن اسامی بر اساس الفبای لاتین و.....
کد PHP:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include<string.h>
#define MAX 100
struct student {
char name[10] ;
float avrage ;
int unit ;
int amount_course ;
int student_number;
} st[MAX];
void first_evalution(void);
void enter(void);
void search(void);
void search2(void);
void del_name(int);
void display(void);
void top_student();
void fail_student();
void show(int *, int);
void title(int *);
int menu_select(void);
int spare_space(void) ;
void save (void);
void load(void);
void in_style(int l);
void delete_main(void);
void delete_name2(void);
void search_main(void);
static int l;
int main(void)
{
char choice ,ans;
first_evalution();
for(;;){
choice = menu_select() ;
switch(choice) {
case 1: enter();in_style(l); break;
case 2: search_main(); break ;
case 3: top_student(); break;
case 4: fail_student(); break ;
case 5: delete_main(); break ;
case 6: save();break;
case 7: load();break;
case 8: display();break;
case 9:{
printf("are you sure you want to quit ?(y/n)");
ans=getche();
clrscr();
if(ans=='y')
exit(0);break; }
}
}
}
void first_evalution(void)
{
register int i ;
for(i = 0; i < MAX; ++i)
st[i].name[0] = '\0' ;
}
int menu_select(void)
{
char s[10];
int g ;
printf("\n\t(1) << enter information >>");
printf("\n\t(2) << search menu >>");
printf("\n\t(3) << show top students >>");
printf("\n\t(4) << show fail students >>");
printf("\n\t(5) << delete menu >>");
printf("\n\t(6)<< save info >>");
printf("\n\t(7)<< load info >>");
printf("\n\t(8)<< display info >>");
printf("\n\t(9)<< quit from program >>");
do {
printf("\n\t Please enter your choice(1-9): ") ;
gets(s);
clrscr();
g = atoi(s);
} while(g < 0 || g > 9) ;
return (g) ;
}
void enter(void)
{
char name3[30];
float grade, sum_grade = 0;
int t, unit, sum_unit = 0 ,j,i;
t = spare_space() ;
if(t == -1){
printf("\n sorry the list is full.....press any key to continue");
getch();
return ;
}
printf("\n\t enter name:") ;
gets(st[t].name) ;
strcpy(name3,st[t].name);
for(i=0;i<t && st[i].name[0] ;i++)
{
if(strcmp(st[i].name,name3)==0){
printf("the similar name is already in the list. contine any way?(y,n)");
if(getche()=='n')
{
clrscr();
st[t].name[0]='\0';
return ;}}
}
l++;
clrscr();
printf("\n\tenter student number:") ;
scanf("%d", &st[t].student_number) ;
clrscr();
printf("\n\tenter number of course:") ;
scanf("%d", &st[t].amount_course) ;
clrscr();
for(j = 1; j <= st[t].amount_course; j++) {
printf("\n\tenter grade for course %d:",j);
scanf("%f", &grade) ;
clrscr();
printf("\n\tenter unit of course %d:", j);
scanf("%d", &unit) ;
clrscr();
sum_grade += grade * unit ;
sum_unit += unit ;
}
st[t].unit=sum_unit;
st[t].avrage = sum_grade / sum_unit ;
}
int spare_space(void)
{
register int i ;
for(i = 0; st[i].name[0] && i< MAX ; ++i);
if(i == MAX) return -1 ;
return i ;
}
void delete_main(void)
{
char c[10];
int h;
do {
printf("how do you want to delete info ?");
printf("\n\t\t\t(1)=base on name");
printf("\n\t\t\t(2)=base on student number");
gets(c);
clrscr();
h=atoi(c);} while(h<0 || h>2);
if(h==1){
delete_name2();}
if(h==2){
del_name(l);}
if(h!=1 && h!=2)
return;
}
void del_name(int l)
{
int i ;
char ans;
printf("\n\tenter record #(0 - %d):",l) ;
scanf("%d", &i) ;
printf("are u sure you want to delete record %d?(y/n)");
ans=getche();
clrscr();
if(ans=='y'){
if( st[i].name[0]){
st[i].name[0]='\0' ;
printf("deleted Successfully !!");}
else if(!st[i].name[0])
printf("this record is not in the list....press any key to continue...."); }
getch();
clrscr();
}
void delete_name2(void)
{
char name2[30],ans;
int i,h,w=0;
printf("\tenter a name to delete");
gets(name2);
printf("are you sure you want to delete %s information?(y/n)",name2);
ans=getche();
clrscr();
if(ans=='y'){
for(i=0;i<MAX;i++)
{
if(strcmp(st[i].name,name2)==0) {
h=i;
w=1; }}
if(w==1){
st[h].name[0]='\0' ;
printf("deleted Successfully !!");
}
if(w==0) {
printf("the one you are looking for is not in the list.press any key to continue.........."); }}
getch();
clrscr();
}
void display(void)
{
int i, r = 0;
;
title(&r) ;
for(i = 0; i < MAX; ++i)
if(st[i].name[0])
show(&r, i);
gotoxy(114,r);
printf("\n\t please press any key to continue ") ;
getch();
clrscr();
gotoxy(29,r+1);
getch();
}
void fail_student(void)
{
int i, r=0 ,a=0;
for(i = 0; i < MAX; ++i){
if(st[i].name[0] && st[i].avrage < 12){
a=1;
title(&r);
show(&r, i) ; }
}
if(a==0){
printf("there is no fail student!");}
gotoxy(25,r+1);
printf(" please press any key to continue ") ;
getch();
clrscr();
}
void top_student(void)
{
int i, r=0 ,a=0;
for(i = 0; i < MAX; ++i){
if(st[i].name[0] && st[i].avrage >= 17){
a=1;
title(&r);
show(&r, i) ; }
}
if(a==0){
printf("there is no top student!");}
gotoxy(25,r+1);
printf(" please press any key to continue ") ;
getch();
clrscr();
}
void title(int *r)
{
*r = 1;
gotoxy(25,*r);
printf(" << all the info in list are: >>");
gotoxy(6,*r+2);
printf(" name avrage ");
printf(" unit amount_course st_no ");
gotoxy(6,*r+3);
printf(" ________ ________ ");
printf(" ______ ______ ____ ");
*r = 4;
}
void show(int *r, int i)
{
gotoxy(14,*r);
printf("%s ", st[i].name) ;
gotoxy(26,*r);
printf("%.2f ", st[i].avrage) ;
gotoxy(40,*r);
printf("%d ", st[i].unit) ;
gotoxy(54,*r);
printf("%d ", st[i].amount_course) ;
gotoxy(70,*r);
printf("%u ", st[i].student_number) ;
(*r) ++ ;
}
void search_main(void)
{
char j[10];
int y;
do {
printf("how do you want to search?");
printf("\n\t\t\t(1)=base on name");
printf("\n\t\t\t(2)=base on student number");
gets(j);
clrscr();
y=atoi(j);} while (y<0 || y>2);
if(y==1){
search();}
if(y==2){
search2();}
if(y!=1 && y!=2)
return ;
}
void search(void)
{
int i,r=0,m=0,t=0;
char name2[30];
printf("enter a name to search");
gets(name2);
clrscr();
for(i=0;st[i].name[0];i++)
{
if(strcmp(name2,st[i].name)==0){
t=i;
m=1; }}
if(m==1){
title(&r) ;
show(&r, t);}
if (m==0)
printf("the one you are looking for is not in the list......press any key to continue");
getch();
clrscr();
}
void save(void)
{
FILE *fp ;
register int i ;
fp = fopen("project", "wb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
clrscr();
return ;
}
for(i = 0 ; i < MAX ; i++)
if(*st[i].name)
fwrite(&st[i], sizeof(struct student), 1, fp) ;
clrscr() ;
gotoxy(20, 10) ;
printf("data saved.press a key.");
getch() ;
clrscr();
}
void load(void){
FILE *fp ;
register int i ;
fp = fopen("project", "rb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
clrscr();
return ;
}
for(i = 0 ; i < MAX; i++) {
fread(&st[i], sizeof(struct student), 1, fp);
if(feof(fp)) {
clrscr() ;
gotoxy(20,10) ;
printf("data are loaded.press a key...........");
getch() ;
clrscr();
return ;
}
}
}
void search2(void)
{
int i,num2,r=0,a;
printf("enter the student number for the one you are looking for");
scanf("%d",&num2);
a=0;
for(i=0;i<MAX;i++){
if(st[i].student_number==num2){
a=1;
title(&r);
show(&r,i);}}
if(a==0)
printf("the one you are lookig for is not in the list...press any key to continue...");
getch();
clrscr();
}
void in_style(int l)
{
int i,j;
char name2[30];
struct student st1={0};
for(i=l-1;i>0;i--){
for(j=0;j<i;j++)
if(strcmp(st[j].name,st[j+1].name)>0){
st1=st[j];
st[j]=st[j+1];
st[j+1]=st1;
}
}
return;
}
کد زیر به روش بازگشتی عناصر یک آرایه رو چاپ میکنه :
کد PHP:
#include <iostream>
#include <conio>
void Print_array(int a[],int n){
if(n==1)
cout<<a[n-1]<<" ";
else{
Print_array(a,n-1);
cout<<a[n-1]<<" ";
}
}
//===============================
int main(){
int a[]={2,6,8,5,3};
Print_array(a,5);
getch();
}
جمع و ضرب دو عدد بروش بازگشتی
کد PHP:
#include <iostream>
#include <conio>
int Rec_Add(int a,int b){
if(b==0)
return a;
else
return Rec_Add(a+1,b-1);
}
//===========================
int Rec_Multiply(int a,int b){
if(b==1)
return a;
else
return a+Rec_Multiply(a,b-1);
}
بزرگترین مقسوم علیه مشترک دو عدد به صورت بازگشتی
کد PHP:
#include <conio.h>
#include <iostream.h>
int gcd(int,int);
int main()
{
int x,y;
cout<<"enter amount for x and y:";
cin>>x>>y;
int k=gcd(x,y);
cout<<k;
getch();
return 0;
}
int gcd(int x,int y)
{
if(y==0)
return x;
else
return gcd(y,x&y);
}
محاسبه توان یک عدد به صورت بازگشتی
کد PHP:
#include <iostream.h>
#include <conio.h>
int power(int,int);
int main()
{
int exp,base;
cout<<"enter base,exp:";
cin>>base>>exp;
int h=power(base,exp);
cout<<"it equals by:"<<h;
getch();
return 0;
}
int power(int base,int exp)
{
if(exp==1)
return base;
else
return base*power(base,exp-1);
}
کلاس کار با اعداد حقیقی
کد PHP:
#include"iostream.h"
#include<CONIO.H>
class Complex{
double re,im;
public:
Complex(){re=0,im=0;}
Complex(double r){re=r,im=0;}
Complex(double r,double i){re=r,im=i;}
void setr(double x){re=x;}
void seti(double x){im=x;}
double getr(){return re;}
double geti(){return im;}
void set(double r,double i)
{
re=r;
im=i;
}
void print(ostream &o){
o<<re;
if(im>=0)o<<'+';
o<<im<<'i';
}
Complex operator+(Complex rhs){
Complex h;
h.re=re+rhs.re;
h.im=im+rhs.im;
return h;
}
Complex operator*(Complex rhs){
Complex h;
h.re=re*rhs.re - im*rhs.im;
h.im=im*rhs.re + re*rhs.im;
return h;
}
Complex operator*(double x){
Complex h;
h.re=re*x;
h.im=im*x;
return h;
}
/*
Complex operator *(double x,Complex rhs){
Complex h;
h.setr(x*rhs.getr());
h.seti(x*rhs.geti());
// return c*x;
return h;
}
*/
Complex operator/(Complex rhs){
double d=rhs.re * rhs.re + rhs.im*rhs.im;
Complex h;
h.re=(re*rhs.re + im*rhs.im)/d;
h.im=(im*rhs.re - re*rhs.im)/d;
return h;
}
};
ostream &operator <<(ostream &o,Complex cc){
cc.print(o);
return (o);
}
void main(){
clrscr();
Complex a(2,3),b(-1,4),c;
//a.set(2,3);
//b.set(-1,4);
c=a+b;
cout<<"\t\t\n a + b = "<<c;
c=a*b;
cout<<"\t\t\n a * b = "<<c;
c=a/b;
cout<<"\t\t\n a / b = "<<c;
c=a*2;
cout<<"\t\t\n a* 2 = "<<c;
}
کد PHP:
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 200
#define INVALID -1
#ifndef array_list_h
#define array_list_h
class array_list {
public:
array_list(int sz = 0);
~array_list() { clear(); }
bool isEmpty(void) { return (num == 0); }
bool isInList(int sample);
int getLength(void) { return num; }
int getItem(int pos) { return a[pos]; }
void setItem(int pos, int newItem) { a[pos] = newItem; }
void insert(int pos, int newItem);
void remove(int pos);
void bubbleSort(void);
void insertionSort(void);
void selectionSort(void);
void heapSort(void);
void mergeSort(void) { mergeSortStub(0, num-1); }
void quickSort(void) { quickSortStub(0, num-1); }
int sequentialSearch(int key);
int binarySearch(int key) { return binarySearchStub(0, num-1, key); }
void clear() { for (int i = 0; i < SIZE; i++) a[i] = INVALID; num = 0; }
void display() { for (int i = 0; i < num; i++) cout << a[i] << " "; cout << endl; }
private:
void swap(int& x, int& y) { int temp = x; x = y; y = temp; }
int findMin(int curr, int last);
void fixHeap(int heapsize, int root, int k);
void constructHeap(int root);
int deleteMin(void);
void merge(int start, int last);
void mergeSortStub(int start, int last);
int random(int start, int last) { srand(time(NULL)); return start + rand() % (last-start+1); }
int partition(int start, int last);
void quickSortStub(int start, int last);
int binarySearchStub(int start, int last, int key);
int num;
int a[SIZE];
};
array_list::array_list(int sz)
{
int i;
for (i = 0; i < sz; i++)
a[i] = 0;
for (i = sz; i < 200; i++)
a[i] = INVALID;
num = sz;
}
bool array_list::isInList(int sample)
{
bool check = false;
for (int i = 0; i < num; i++)
if (a[i] == sample) {
check = true;
break;
}
return check;
}
void array_list::insert(int pos, int newItem)
{
if (pos > getLength())
return;
for (int i = num; i > pos; i--)
a[i] = a[i-1];
a[pos] = newItem;
num++;
}
void array_list::remove(int pos)
{
if (pos >= getLength())
return;
for (int i = pos; i < num-1; i++)
a[i] = a[i+1];
a[num-1] = INVALID;
num--;
}
//====================== SORTING ==========================//
void array_list::bubbleSort(void)
{
for (int i = 0; i < num-1; i++)
for (int j = i+1; j < num; j++)
if (a[i] > a[j])
swap(a[i], a[j]);
}
void array_list::insertionSort(void)
{
for (int i = 0; i < num; i++) {
int j = i;
while ((j > 0) && (a[j] < a[j-1])) {
swap(a[j], a[j-1]);
j--;
}
}
}
int array_list::findMin(int curr, int last)
{
if (last - curr == 1) {
if (a[curr] < a[last])
return curr;
else
return last;
}
else {
int rest = findMin(curr+1, last);
if (a[curr] < a[rest])
return curr;
else
return rest;
}
}
void array_list::selectionSort(void)
{
for (int i = 0; i < num-1; i++) {
int j = findMin(i, num-1);
swap(a[j], a[i]);
}
}
void array_list::fixHeap(int heapsize, int root, int k)
{
if (2*root+1 > heapsize) // the root has no child
a[root] = k;
else {
int largerSubHeap;
if (2*root+1 == heapsize) // the root has 1 child
largerSubHeap = 2*root+1;
else // the root has 2 children
largerSubHeap = (a[2*root+1] > a[2*root+2]) ? (2*root+1) : (2*root+2);
if (k >= a[largerSubHeap])
a[root] = k;
else {
a[root] = a[largerSubHeap];
fixHeap(heapsize, largerSubHeap, k);
}
}
}
void array_list::constructHeap(int root)
{
int k = a[root];
if (2*root+1 >= num) // the root has no child
return;
else if (2*root+2 == num) // the root has 1 child
constructHeap(2*root+1);
else { // the root has 2 children
constructHeap(2*root+1);
constructHeap(2*root+2);
}
fixHeap(num, root, k);
}
void array_list::heapSort(void)
{
int heapsize;
constructHeap(0);
for (heapsize = num; heapsize >= 2; heapsize--) {
int currentMax = a[0];
int k = a[heapsize-1];
fixHeap(heapsize-1,0, k);
a[heapsize-1] = currentMax;
}
}
void array_list::merge(int start, int last)
{
int i, j, k;
int aux[SIZE];
int mid = (start + last) / 2;
for (i = start; i <= mid; i++)
aux[i] = a[i];
for (i = mid+1; i <= last; i++)
aux[last+mid+1-i] = a[i];
j = start; k = last;
for (i = start; i <= last; i++)
a[i] = (aux[j] < aux[k]) ? aux[j++] : aux[k--];
}
void array_list::mergeSortStub(int start, int last)
{
if (last > start) {
int mid = (last + start) / 2;
mergeSortStub(start, mid);
mergeSortStub(mid+1, last);
merge(start, last);
}
}
int array_list::partition(int start, int last)
{
swap(a[start], a[random(start, last)]);
int pivot = a[start];
int leftwall = start;
for (int i = start+1; i <= last; i++) {
if (a[i] < pivot) {
leftwall++;
swap(a[i], a[leftwall]);
}
}
swap(a[start], a[leftwall]);
return leftwall;
}
void array_list::quickSortStub(int start, int last)
{
if (last > start) {
int pivot = partition(start, last);
quickSortStub(start, pivot-1);
quickSortStub(pivot+1, last);
}
}
//====================== SEARCHING ==============//
int array_list::sequentialSearch(int key)
{
int i = 0;
while ((i < num) && (a[i] != key))
i++;
if (i == num)
return -1;
else
return i;
}
int array_list::binarySearchStub(int start, int last, int key)
{
if (last < start)
return -1;
else {
int mid = (start + last) / 2;
if (key == a[mid])
return mid;
else if (key < a[mid])
return binarySearchStub(start, mid-1, key);
else
return binarySearchStub(mid+1, last, key);
}
}
یه برنامه که طول رشته رو تا رسیدن به Enter حساب میکنه
کد PHP:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;int i=0;
while(cin.get()!='\n')
i++;
cout<<i;
getch();
}
n جمله اول فیبوناچی رو حساب میکنه !
کد PHP:
#include <iostream.h>
#include <conio.h >
main(){
int x=1;
int y=1,z=0;
int n;
cout <<"\n enter number:";
cin >>n;
cout<<x;
cout <<y;
for (int i=2;i<n;i++){
z=x+y;
x=y;
y=z;
cout<<z;
}
getch();
}
تعداد فاصله هایی که تو رشته هست رو میده!
کد PHP:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;char ch;
while((ch=cin.get())!='\n')
{
if(ch==' ')
i++;
}
cout<<i;
getch();
}
کد PHP:
#include<iostream.h>
#include<conio.h>
void star(int n)
{
if(n!=0)
{star (n-1);
for (int i=1 ; i<=n ; i++)
cout<<"*";
cout <<"\n";
}
}
void main ()
{
clrscr();
int n;
cout <<"enter a number :" ;
cin >> n;
star(n);
getch();
}
برج هانوی
کد PHP:
#include <stacks.h>
#include <iostream.h>
TStack <int> s[3];
void transfer(int n,int from ,int to, int temp){
if(n>0){
// move n-1 disks from origin to temporary
transfer(n-1, from,temp,to);
// move n th disk from origin to destination
s[to].Push(s[from].Pop());
//move n-1 disks from temporary to destination
transfer(n-1, temp,to,from);
}
}
void PrintStacks(){
for (int i=0;i<3;i++){
cout << "\n<";
while (!s[i].IsEmpty()){
cout << s[i].Pop()<<',';
}
cout <<']';
}
}
main(){
int n;
cout<<"n:";cin>>n;
for (int i=n;i>0;i--)s[0].Push(i);
transfer(n,0,1,2);
PrintStacks();
}
برای محاسبه تابع sin هست
کد PHP:
#include<iostream.h>
#include<conio.h>
int pow(int a, int b);
int fact(int a);
double sin(int(*p)(int a,int b),int(*q)(int b),int x);
void main(){
int x;
clrscr();
cout<<"Please Enter Number : ";
cin>>x;
cout<<fact(x)<<endl<<pow(x,x);
//cout<<sin(pow,fact,x);
getch();
}
int pow(int a,int b){
int pow=1;
for (int i=1;i<=b;i++)
pow*=a;
return pow;
}
int fact(int a){
int fact=1;
for(int i=1;i<=a;i++)
fact*=i;
return fact;
}
double sin(int(*p)(int a,int b),int(*q)(int b),int x){
double sum=0;
int b=1;
for(int i=1;i<=15;i+=2){
sum+=(pow)(x,i)/(fact)(i)*b;
b=-b;
}
return sum;}
یه تابع برای محاسبه انتگرال معین یه تابع :
کد PHP:
double integral(double (*f)(double),double a,double b,int n)
{
double s=0,dx=fabs((b-a)/n);
for(double i=0;i<n;i++)
s+=(f(a+i*dx)+f(a+(i+1)*dx));
return dx*s/2;
}
برای محاسبه ریشه n ام یک عدد
کد PHP:
double root(double n,double a)
{
double x1=1,x0;
do
{
x0=x1;
x1=(n-1)/n*x0+a/(n*pow(x0,n-1));
}while(fabs(x1-x0)>1e-6);
return x1;
}
کد PHP:
#include <iostream>
using namespace std;
int main()
{
int n, cnt;
cin >> n;
for (cnt = 0; n; n &= n - 1, cnt++);
cout << cnt << endl;
return 0;
}
خب این ها رو با کلی گشتن پیدا کردم پر پروژهای برنامه نویسی به زبان سی هست
انواع نمونه برنامه های C و C++
http://www.functionx.com/cpp/examples/index.htm
http://www.cplusplus.com/src/
http://www.josuttis.com/libbook/examples.html
http://www.cs.indiana.edu/~sabry/teaching/intro/fa96/code/index.html
http://www.cplusplus.happycodings.com/Algorithms/index.html
http://www.brpreiss.com/books/opus4/programs/index.html
http://www.kralidis.ca/gis/cPlusPlus/sampleCode/
http://people.sc.fsu.edu/~burkardt/cpp_src/cpp_src.html
http://www.thefreecountشry.com/sourcecode/cpp.shtml
حاصلضرب بازگشتی با عملگر جمع :
تفکیک ارقام بازگشتی:کد PHP:
#include <stdio.h>
int plural(int n,int m){
if(n==0) return 0; else
return plural(n-1,m)+m;
}
void main(){
int m=6,n=9;
printf("%d\n",plural(n,m));
}
اینم بازگشتی ب.م.م و ک.م.مکد PHP:
#include <stdio.h>
int crack(int n){
printf("%d\n",n%10);
if ((n)>1) return crack(n/10);else
return 0;
}
void main(){
crack(43543);
}
کد PHP:
int bmm(int a,int b){
if (b) return bmm(b,a%b);else
return a;
}
void main(){
int m=5,n=20;
printf("bmm= %d\nkmm=%d\n",bmm(m,n),((m*n)/bmm(m,n)));
}
مثبت باینری و اگه منفی مکمل 2 که خودش بشه:
مجموع عناصر دور یک ماترس رو بدست میاره :کد PHP:
#include <iostream.h>
#include <stdlib.h>
void main(){
int a;
char str[32];
cout<<"your number :\n";
cin>>a;
if(a>0){cout<<itoa(a, str, 2)<<endl;}else{cout<<itoa(((~a)+1), str, 2)<<endl;};
}
کد PHP:
#include <iostream>
#include <conio.h>
using namespace std;
void main(){
int a[5][5];
int sum=0;
for (int i=0;i<=4;i++){
for (int j=0;j<=4;j++){
a[i][j]=getch()-48;
cout<<" "<<a[i][j];
if(((i==0)||(i==4))&&((j!=0)&&(j!=4))) sum+=a[i][j];
تبدیل میانوندی به پسوندی
کد PHP:
#include<stdio.h>
#include<string.h>
#define size 10
char stack[size];
int tos=0,ele;
void push();
char pop();
void show();
int isempty();
int isfull();
char infix[30],output[30];
int prec(char);
//Functions for operations on stack
void push(int ele)
{
stack[tos]=ele;
tos++;
}
char pop()
{
tos--;
return(stack[tos]);
}
void show()
{
int x=tos;
printf("--The Stack elements are.....");
while(x!=0)
printf("%c, ",stack[--x]);
}
//Function to get the precedence of an operator
int prec(char symbol)
{
if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;
}
int main()
{
int i=0,j=0,k=0,length;
char temp;
printf("\nEnter an infix expression:");
scanf("%s",infix);
printf("\nThe infix expresson is %s",infix);
length=strlen(infix);
for(i=0;i<length;i++)
{
//Numbers are added to the out put QUE
if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!=')' && infix[i]!='(' )
{
output[j++]=infix[i];
printf("\nThe element added to Q is:%c",infix[i]);
}
//If an operator or a bracket is encountered...
else
{
if(tos==0) //If there are no elements in the stack, the operator is added to it
{
push(infix[i]);
printf("\nThe pushed element is:%c",infix[i]);
}
else
{ //Operators or pushed or poped based on the order of precedence
if(infix[i]!=')' && infix[i]!='(')
{
if( prec(infix[i]) <= prec(stack[tos-1]) )
{
temp=pop();
printf("\n the poped element is :%c",temp);
output[j++]=temp;
push(infix[i]);
printf("\n The pushed element is :%c",infix[i]);
show();
}
else
{
push(infix[i]);
printf("\nThe pushed element is:%c",infix[i]);
show();
}
}
else
{
if(infix[i]=='(')
{
push(infix[i]);
printf("\nThe pushed-- element is:%c",infix[i]);
}
if(infix[i]==')')
{
temp=pop();
while(temp!='(')
{output[j++]=temp;
printf("\nThe element added to Q is:%c",temp);
//temp=pop();
printf("\n the poped element is :%c",temp);
temp=pop();}
}
}
}
}
printf("\nthe infix expression is: %s",output);
}
while(tos!=0)
{
output[j++]=pop();
}
printf("the infix expression is: %s\n",output);
}