سلام .
به پیشنهاد femme عزیز تصمیم گرفته شد بعضی از کدهای معمول در انجمن قرار داده شوند تا دسترسی دوستان به آن ها آسان تر باشد .
این کد ها از ساده ترین برنامه ها تا الگوریتم های پیشرفته معروف خواهند بود .
به مرور هم آپدیت خواهد شد .
Printable View
سلام .
به پیشنهاد femme عزیز تصمیم گرفته شد بعضی از کدهای معمول در انجمن قرار داده شوند تا دسترسی دوستان به آن ها آسان تر باشد .
این کد ها از ساده ترین برنامه ها تا الگوریتم های پیشرفته معروف خواهند بود .
به مرور هم آپدیت خواهد شد .
پست دوم هم فعلا رزرو باشه تا بعد ....
برنامه یافتن فاکتوریل یک عدد به روش غیربازگشتی :
کد:#include <iostream>
using namespace std;
int main()
{
int number;
cin >> number;
int fact = 1;
for( int i = 1; i <= number; i++ )
fact *= i;
cout << number << " != " << fact << endl;
return 0;
}
برنامه یافتن فاکتوریل یک عدد به روش بازگشتی :
کد:#include <iostream>
using namespace std;
int fact( int number )
{
if( number == 0 )
return 1;
return number * fact( number - 1 );
}
int main()
{
int number;
cin >> number;
cout << number << " != " << fact( number ) << endl;
return 0;
}
فاکتوریل اعداد بسیار بزرگ (این برنامه تا 1000 فاکتوریل محاسبه می کنه که میشه با تغییر دو عدد تو برنامه تا بی نهایت فاکتوریل هم مجاسبه کرد)
کد:#include <iostream>
#include <string>
using namespace std;
char f[ 3000 ];
char factorial[ 1001 ][ 3000 ];
void multiply( int k )
{
int len = strlen( f );
int c = 0, i = 0;
int sum;
while( i < len )
{
sum = c + ( f[ i ] - '0' ) * k;
f[ i ] = ( sum % 10 ) + '0';
i++;
c = sum / 10;
}
while( c > 0 )
{
f[ i++ ] = ( c % 10 ) + '0';
c /= 10;
}
f[ i ] = '\0';
for( int j = 0; j < i; j++ )
factorial[ k ][ j ] = f[ j ];
factorial[ k ][ i ] = '\0';
}
void fac()
{
strcpy_s( f, "1" );
for( int i = 2; i <= 1000; i++ )
multiply( i );
}
void print( int n )
{
int len = strlen( factorial[ n ] );
cout << n << "!= ";
for( int i = len - 1; i >= 0; i-- )
cout << factorial[ n ][ i ];
cout << endl;
}
int main()
{
int n;
factorial[ 0 ][ 0 ] = '1';
factorial[ 1 ][ 0 ] = '1';
fac();
while( cin >> n )
print( n );
return 0;
}
تابعی بسیار کارا برای بررسی اول بودن یک عدد :
کد:bool isPrime( int number )
{
if( number == 2 )
return true;
if( number % 2 == 0 || number == 1 )
return false;
for( int i = 3; i * i <= number; i += 2 )
{
if( number % i == 0 && number != i )
return false;
}
return true;
}
جمع بی نهایت عدد بی نهایت رقمی .
یعنی تا وقتی که عدد آخر 0 وارد نشده عدد بی نهایت رقمی (تا جایی که حافظه جا داشته باشد) می گیرد و همه را با هم جمع می کند .
با استفاده از خاصیت push_front تو لیست پیوندی این برنامه رو نوشتم :
کد:#include <iostream>
#include <vector>
#include <list>
#include <string>
using namespace std;
int main()
{
vector< string > integers;
string str;
while( true )
{
cin >> str;
if( str == "0" )
break;
integers.push_back( str );
}
vector< bool > erased( integers.size() );
int carry = 0;
int counter;
list< int > ans;
while( true )
{
for( int i = 0; i < integers.size(); i++ )
{
if( erased[ i ] == false )
{
carry += ( integers[ i ].at( integers[ i ].size() - 1 ) - 48 );
integers[ i ].erase( integers[ i ].end() - 1 );
}
}
counter = 0;
for( int i = 0; i < integers.size(); i++ )
{
if( integers[ i ].empty() )
{
erased[ i ] = true;
counter++;
}
}
if( counter == integers.size() )
break;
ans.push_front( carry % 10 );
carry /= 10;
}
while( carry != 0 )
{
ans.push_front( carry % 10 );
carry /= 10;
}
for( list< int >::iterator it = ans.begin(); it != ans.end(); ++it )
cout << *it;
cout << endl;
return 0;
}
برنامه اي كه جدول ضرب را چاپ ميكند:
[PHP]#include<iomanip.h>;
#include<iostream.h>;
int main()
{
for(int x=1;x<=10;++x)
{
for (int y=1;y<=10;++y)
cout<<setw(4)<<x*y;
cout<<endl;
}
return 0;
}[/PHP]
---------- Post added at 11:49 PM ---------- Previous post was at 11:45 PM ----------
برنامه اي كه رشته اي را خوانده و تمام حروف كوچك آن را بزرگ ميكند:
[PHP]#include<iostream.h>
#include<conio.h>
int main()
{
char a[21];
cin.get(a,20);
for (int i=0;a[i];i++)
if(a[i]>='a' && a[i]<='z')
a[i]-=32;
cout<<a;
getch();
return 0;
}[/PHP]
---------- Post added at 11:51 PM ---------- Previous post was at 11:49 PM ----------
برنامه اي كه از طريق تخصيص آرايه پويا .2 مقدار را از ورودي خوانده و مجموع مربعات انرا محاسبه و چاپ ميكند
[PHP]#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int *x,*y,s;
x=new int;
y=new int;
if (!x)
{
cout<<"\n allocation failure" ;
exit(1);
}
if(!y)
{
cout<<"\n allocation failure" ;
exit(1);
}
cin>>*x>>*y;
s=*x**x+*y**y;
cout<<s;
delete x;
delete y;
getch();
return 0;
}
[/PHP]
برنامه زير معادل آلماني چند لغت را داده و با وارد كردن لغت انگليسي آلماني آن نوشته ميشود
[PHP]#include<iostream.h>
#include<stdio.h>
#include<string.h>
int main()
{
char words[][2][40]={
"dog","hund",
"no", "nein",
"child","kind",
"year","jahr",
"i","ich",
"drive","fahren",
"house","haus",
"to","zu",
" "," "
};
char english[80];
gets(english);
int i=0;
while(strcmp(words[i][0]," "))
{
if(!strcmp(english,words[i][0]))
{
cout<<"german teranslation:"<<words[i][1];
break;
}
i++;
};
if(strcmp(english,words[i][0]))
cout<<"not found";
return 0;
}
[/PHP]
اين برنامه يك رشته و يك عدد مثل n از شما گرفته و كاراكتر موجود در محل n را از رشته حذف ميكند
[PHP]#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
clrscr();
char string[81];
int position;
cout<<"type a string"<<'\n';
cin.get(string,80);
cout<<"enter position for delete character";
cin>>position;
strcpy(&string[position],&string[position+1]);
cout<<"the result string is:"<<string;
getch();
return 0;
}
[/PHP]
برنامه زير با استفاده از اشاره گر تابع .تابعي را فراخواني كرده و دو رشته را از ورودي ميخواند و تشخيص ميدهد كه آيا 2 رشته با هم مساويند يا خير؟
[PHP]#include<iostream.h>
#include<conio.h>
#include<string.h>
void check(char *a,char *b,int (*cmp)(const char *,const char *)) ;
int main()
{
char s1[80],s2[80];
int (*p)(const char *,const char *);
clrscr;
p=strcmp;
cout<<"enter first string";
cin>>s1;
cout<<"enter second string";
cin>>s2;
check(s1,s2,p);
getch();
return 0;
}
//==================
void check(char *a,char *b,int (*cmp)(const char *,const char *))
{
if(!(cmp)(a,b))
cout<<"strings are equal";
else
cout<<"strings are not equal";
}[/PHP]
---------- Post added at 12:19 AM ---------- Previous post was at 12:17 AM ----------
برنامه ماشين حساب:
[PHP]#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b;
char ch;
cout<<"do you want to :\n";
cout<<"add,subtract,multiply,or divide?\n";
do {
cout<<"enter leter(a,s,m or d):";
cin>>ch;
}while(ch!='A'&& ch!='S'&& ch!='M'&& ch!='D');
cout<<"enter first number:";
cin>>a;
cout<<"enter second number:";
cin>>b;
switch(ch){
case 'A':
cout<<a+b;
break;
case'S':
cout<<a-b;
break;
case 'M':
cout<<a*b;
break;
case 'D':
if(b!=0) cout<<a/b;
}
return 0;
}
[/PHP]