کد 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<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);
}