• پایان فعالیت بخشهای انجمن: امکان ایجاد موضوع یا نوشته جدید برای عموم کاربران غیرفعال شده است

تمرین هایی برای سی++

bloody

کاربر فعال علم و دانش
کاربر فعال
تاریخ عضویت
19 آپریل 2007
نوشته‌ها
1,256
لایک‌ها
17
محل سکونت
IRAN
منظورت اینجوریه مستر؟!
کد:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip.h>
void main()
{
int j,i;
 for (i=1;i<=10;i++){
	for (j=1;j<=10;j++){
		  cout<<setw(2)<<i<<"*"<<j<<"="<<i*j;
		  if(j%10==0) cout<<endl<<endl;
}
}
}
 

nsh

Registered User
تاریخ عضویت
19 ژانویه 2006
نوشته‌ها
685
لایک‌ها
4
محل سکونت
i & j : 2 & 3
جان ببخشید اون تابع که من گفتم ربطی نداشت یه جا دیگه استفاده میشه شما باید از تابع <graphics.h>استفاده کنی
ممنون که کمک کردی :D
 

هستی

همکاربازنشسته
تاریخ عضویت
12 آپریل 2007
نوشته‌ها
3,894
لایک‌ها
709
سن
37
محل سکونت
My Dreams
سلام.می شه کد این برنامه رو برام بنویسید آخه من سر جلسه تابع نبودم
با استفاده از تابع فاکتوریل عدد را به دو روش (بدون ارسال و با ارسال. دو برنامه جدا از هم) مقدار بدست آورید.
منتظرم.
مرسی:)
 

anahid19

کاربر تازه وارد
تاریخ عضویت
8 آپریل 2007
نوشته‌ها
17
لایک‌ها
0
حالا اگر بخواهیم که 100 جمله اول دنباله فیبوناچی را چاپ کنیم و از تابع کمکی استفاده نکنیم چی کار باید بکنیم؟
 

Arash_j13

Registered User
تاریخ عضویت
18 فوریه 2005
نوشته‌ها
778
لایک‌ها
2
محل سکونت
مشهد
يه بار قبلا در مورد فيبونا چي بحث شده تو همين تاپيک ولي دوباره مي گم
ساده ترين راه اينکه از تابع بازگشتي معروف استفاده کنيد
کد:
int fibo(int n)
{
	if(n < 2 )
		return 1;
	return fibo(n-1)+fibo(n-2);
}

استفاده از اين فقط يه مشکل خيلي خيلي کوچيک داره ممکنه عمرتون کافي نباشه تا اين تابع جمله صدم رو حساب کنه

بهتره بدون استفاده از تابع بازگشني بنويسيد البته اگه اصرار به ازگشتي داشته باشيد روش هاي بازگشتي سريعي توي همين تاپيک گذاشته شده
کد:
int fibo(int n)
{
	int a1,a2;
	a1=a2=1;
	
	for(int tmp,i = 2 ;  i< n ; ++i)
	{
		tmp,=a1+b2;
		a2=a1;
		a1=tmp;
	}
	
	return a1;
}
 

هستی

همکاربازنشسته
تاریخ عضویت
12 آپریل 2007
نوشته‌ها
3,894
لایک‌ها
709
سن
37
محل سکونت
My Dreams
سلام.می شه کد این برنامه رو برام بنویسید آخه من سر جلسه تابع نبودم
با استفاده از تابع فاکتوریل عدد را به دو روش (بدون ارسال و با ارسال. دو برنامه جدا از هم) مقدار بدست آورید.
منتظرم.
مرسی:)

____________________
بابا یک هم جواب منو بده. منظور من فاکتوریل بود نه سری فیبوناچی bloody جان. منتظرم:(
 

Arash_j13

Registered User
تاریخ عضویت
18 فوریه 2005
نوشته‌ها
778
لایک‌ها
2
محل سکونت
مشهد
منظور شما رو فهمیدم ولی یکم بهتر نگاه کنید می بینید که سوال فیبو ناچی هم بود
واقعا فاکتوریل کار سختیه؟
کد:
long long int fact(int n)
{
	if(n<=1)
		return 1;
	return n*fact(n-1); 
}
 

هستی

همکاربازنشسته
تاریخ عضویت
12 آپریل 2007
نوشته‌ها
3,894
لایک‌ها
709
سن
37
محل سکونت
My Dreams
اولا من با شما نبودم لطفا پست رو درست مطالعه کنید دوما چرا شما آدم رو ناراحت می کنید با برخوردتون؟؟؟؟؟؟؟؟؟؟؟؟. در ضمن من خودم نوشتمش. دیگه لازم نیست زحمت بکشید
 

Arash_j13

Registered User
تاریخ عضویت
18 فوریه 2005
نوشته‌ها
778
لایک‌ها
2
محل سکونت
مشهد
خوب پش اگه نوشتید دگه دوبار درخواست نکنید
 

corona

کاربر تازه وارد
تاریخ عضویت
12 می 2007
نوشته‌ها
9
لایک‌ها
0
محل سکونت
mashhad
اقا ارش سلام
این برنامه به ذهنم رسیده ولی نمیدونم مشکلش چیه که به گرفتن حروف خاتمه نمی ده و تو خروجی چاپ نمی کنه؟:(
کد:
#include <iostream>
#include <conio.h>
#include <vector>
using namespace std;

void get();
int main()
{
    get();
    getch();
    return 0;
}
void get()
{
    char ch;
    ch=getch();
    if(ch!='q'||ch!='Q')               
       get();
    else
    {
       cout<<ch;   
       return;
       }
       
}

join us
4pk329y.jpg
 

Arash_j13

Registered User
تاریخ عضویت
18 فوریه 2005
نوشته‌ها
778
لایک‌ها
2
محل سکونت
مشهد
خب مشکل خیلی کوچیک اما اساسی داره مشکل اینکه یه کارکتر همزمان نمی تونه دو مقدار داشته باشه پس کارکتر ch هم همزمان نمی تونه هم q باشه هم Q
 

corona

کاربر تازه وارد
تاریخ عضویت
12 می 2007
نوشته‌ها
9
لایک‌ها
0
محل سکونت
mashhad
کد:
/*
  Name:Complex class               compiler:dev-c++ 4.9.9.2
  Copyright: www.codecorona.com
  Author: Mohammad yazdani
  Description: This class make complex number like a simple data type in c++.
*/
//**********************header files********************************************
#include <iostream>
#include <conio.h>
using namespace std;
//***************designing class************************************************
class complex{
    public:
           complex(int=0,int=0);
           complex operator+(complex);
           complex operator+(int);
           complex operator-(complex);  
           complex operator-(int);
           complex operator*(complex);
           complex operator*(int);
           complex operator/(complex);
           complex operator/(int);
           friend ostream&operator<<(ostream&,complex);
           friend istream&operator>>(istream&,complex&);
      
    private:  
      float real;
      float unreal;
};
//*********implementing member functions****************************************
complex::complex(int a,int b)
{
  real=a;
  unreal=b;
}
//*********************************
complex complex::operator+(complex b)
{
    complex c;
    c.real=real+b.real;
    c.unreal=unreal+b.unreal;
    return c;
}
//**********************************
complex complex::operator+(int a)
{
     this->real+=a;
     this->unreal+=a;
     return *this;
} 
//***********************************       
complex complex::operator-(complex b)
{
        complex c;
        c.real=real-b.real;
        c.unreal=unreal-b.unreal;
        return c;
}
//************************************
complex complex::operator-(int a)
{
    this->real-=a;
    this->unreal-=a;
    return *this;
} 
//************************************      
complex complex::operator*(complex b)
{
        complex c(0,0);
        c.real=this->real*b.real;
        c.unreal=this->unreal*b.unreal*(-1);
        return c;
}
//*************************************
complex complex::operator*(int a)
{
     this->real*=a;
     this->unreal*=a;
     return *this;
} 
//**************************************       
complex complex::operator/(complex b)
{
        complex c;
        c.real=real/b.real;
        c.unreal=unreal/b.unreal;
        return c;
}
//***************************************
complex complex::operator/(int a)
{
        this->real/=a;
        this->unreal/=a;
        return *this;
} 
//***************************************       
ostream&operator<<(ostream&out,complex b)
{
    out<<b.real<<(b.unreal>0?'+':' ')<<b.unreal<<'i';
    return out;
}
//****************************************
istream&operator>>(istream&in,complex& b)
{
   cout<<"please enter the the real section:";
   in>>b.real;
   cout<<"\nplease enter the unreal section:";
   in>>b.unreal;
   return in;
}
//****************************************main function*************************
int main()   
{
    complex a(4,3),b(2,6);
    cout<<endl<<a*b;
    cout<<endl<<a;
    cout<<endl<<a/b;
    cout<<endl<<a-2;
    getch();
    return 0;
}                                                                                                                                   
//******************************************************************************
 

Arash_j13

Registered User
تاریخ عضویت
18 فوریه 2005
نوشته‌ها
778
لایک‌ها
2
محل سکونت
مشهد
خیلی خوبه البته کامل نیست مثلا
کد:
2+z
با خطا مواجه می شه
و همین طور مثلا
کد:
z-2
روی عدد z تاثیر داشته باشه ولی تو کد شما تاثیر می زاره در ضمن بهتر برای راحتی کار اپریتور تبدیل نوع رو هم اور لود کنید
 

corona

کاربر تازه وارد
تاریخ عضویت
12 می 2007
نوشته‌ها
9
لایک‌ها
0
محل سکونت
mashhad
آرش جان سلام فکر کنم این یکی کامل باشه البته یه مشکلی تو کامپایل داره که نمی فهمم چی میگه اگه به من بگی ممنون می شم. در ضمن در مورد اورلود اپراتور تبدیل نوع هم اگه توضیح بدی ممنون می شم.
کد:
#include <iostream>
#include <conio.h>
using namespace std;
//***designing class*************//
class complex{
    public:
           complex(int=0,int=0);
           complex operator+(complex);
           complex operator+(int);
           complex operator+(int,complex);
           complex operator-(complex);  
           complex operator-(int);
           complex operator-(int,complex);
           complex operator*(complex);
           complex operator*(int,complex);
           complex operator*(int);
           complex operator/(complex);
           complex operator/(int,complex);
           complex operator/(int);
           friend ostream&operator<<(ostream&,complex);
           friend istream&operator>>(istream&,complex&);
      
    private:  
      int real;
      int unreal;
};
//*********implementing funcion members************
complex::complex(int a,int b)
{
  real=a;
  unreal=b;
}
//****************
complex complex::operator+(complex b)
{
    complex c;
    c.real=this->real+b.real;
    c.unreal=this->unreal+b.unreal;
    return c;
}
//*****************
complex complex::operator+(int b)
{
 complex c;
 c.real=this->real+b;
 c.unreal=this->unreal+b;       
 return c;
}
//*****************        
complex complex::operator+(int b,complex a)
{
  return (a+b);
}
//*****************        
complex complex::operator-(complex b)
{
        complex c;
        c.real=real-b.real;
        c.unreal=unreal-b.unreal;
        return c;
}
//*****************
complex complex::operator-(int b)
{
  complex c;
  c.real=this->real-b;     
  c.unreal=this->unreal-b;     
  return c;      
}
//*****************  
complex complex::operator-(int b,complex a)
{
  complex c;
  c.real=b-a.real;
  c.unreal=b-a.unreal;
  return c;
}
//******************               
complex complex::operator*(complex b)
{
        complex c;
        c.real=real*b.real;
        c.unreal=unreal*b.unreal*(-1);
        return c;
}
//*****************
complex complex::operator*(int b)
{
    complex c;
    c.real=this->real*b;
    c.unreal=this->unreal*b;
    return c;
}
//******************
complex complex::operator*(int b,complex a)
{
     return (a*b);
}
//*******************               
complex complex::operator/(complex b)
{
        complex c;
        c.real=real/b.real;
        c.unreal=unreal/b.unreal;
        return c;
}
//******************
complex complex::operator/(int b)
{
        complex c;
        c.real=this->real/b;
        c.unreal=this->unreal/b;
        return c;
}
//*******************
complex complex::operator/(int b,complex a)
{
     complex c;
     c.real=b/a.real;
     c.unreal=b/a.unreal;
     return c;
}
//********************                 
ostream&operator<<(ostream&out,complex b)
{
    out<<b.real<<(b.unreal>0?'+':' ')<<b.unreal<<'i';
    return out;
}
//*******************
istream&operator>>(istream&in,complex& b)
{
   cout<<"please enter the the real section:";
   in>>b.real;
   cout<<"\nplease enter the unreal section:";
   in>>b.unreal;
   return in;
}
//****************************************main function*************************
int main()   
{
    complex a(4,3),b(2,6);
    cout<<a+b<<endl<<a+2<<endl<<2+a;
    cout<<endl<<a-b<<endl<<2-b<<endl<<b-2;
    cout<<endl<<a*b<<endl<<a*2<<2*a;
    cout<<endl<<a/b<<endl<<a/2<<endl<<2/a;
    getch();
    return 0;
}                                                                                                                                   
//******************************************************************************




join us"www.codecorona.com
4pk329y.jpg
 

Arash_j13

Registered User
تاریخ عضویت
18 فوریه 2005
نوشته‌ها
778
لایک‌ها
2
محل سکونت
مشهد
الان عجله دارم می خوام برم برگشتم نگاه می کنم دوست داشتی هم بیار حضوری بهت مشکلش رو بگم
 

sha19425

Registered User
تاریخ عضویت
10 ژانویه 2006
نوشته‌ها
281
لایک‌ها
21
بابا یه تمرین بذارید دیگه !

مساله هشت وزیر را برای n وزیر با رسم گرافیک بنویسید .
 

Arash_j13

Registered User
تاریخ عضویت
18 فوریه 2005
نوشته‌ها
778
لایک‌ها
2
محل سکونت
مشهد
هشت وزیر قبلا حل شده راه حل گرافیکیش هم هست
بیاید یه تابع بنویسید که یه لیست از اعداد مثبت و منفی رو بگیره بزرگترین زیر لیست توی این لیست که مقدار ماکزیمم می شه رو فقط با یه بار پیمایس لیست حساب کنه
 

corona

کاربر تازه وارد
تاریخ عضویت
12 می 2007
نوشته‌ها
9
لایک‌ها
0
محل سکونت
mashhad
کد:
/*
  Name:The MOVMENT OF A HOURSE IN A CHESS 
  Copyright:WWW.CODECORONA.COM 
  Author:MOHAMMAD YAZDANI 
*/
//*******************************HEADER FILES***********************************
#include <iostream>
#include <conio.h>
#include <string>
#include <iomanip>
#include <cstdlib>
#define row 8
#define col 8
using namespace std;
//***************************FUNCTION PROTOTYPE*********************************
void print(int[row][col]);
void input(int&,int&);
void move (int[row][col],int,int);
void motion(int&,int&,int,bool&);
//*****************************MAIN FUNCTION************************************
int main()
{
    int table[row][col]={0};
    int x,y;
    input(x,y);
    move(table,x,y);
    cout<<"the final table to made with your movment is:\n\n";
    print(table);
    getch();
    return 0;
}
//******************************************************************************
void input(int&x,int&y)
{
     int z=-1;
     
    do{
       system("cls");  
        z++;
       if(z)
        cout<<"\a";   
       cout<<"please enter the row for start between(0 -7):";
       cin>>x;
       cout<<"\nplease enter the coloum for start (0-7):";
       cin>>y;
       }while((x<0||x>7)||(y<0||y>7));
}
//******************************************************************************
void move(int t[row][col],int x,int y)
{
  t[x][y]=1;
  int k=0;   
   cout<<"the start point for this board is row "<<x<<" && coloum "<<y<<endl<<endl;
   print(t);
  while(k++<8)
  {   
     int number;
     static int pos=1;
     int mov=0;
     bool error=false;
    
  do{
     system("cls");
     print(t);
     cout<<"\nthe poosible movment in the board.";
     cout<<"\n\nnumber 1: 2 cell top && 1 cell left"
           "\n\nnumber 2: 2 cell top && 1 cell right"
           "\n\nnumber 3: 2 cell left && 1 cell down"
           "\n\nnumber 4: 2 cell left && 1 cell top."
           "\n\nnumber5: 2 cell right && 1 cell down"
           "\n\nnumber 6: 2 cell right && 1 cell top"
           "\n\nnumber 7: 2 cell down && 1 cell right"
           "\n\number 8: 2 cell down && 1 cell left."
           "\n\nnumber 9:Exit from program.";
     cout<<"             \n \n\nplease choos one of this movment:";                 
           cin>>number;
           }while(number<1||number>9);
      switch (number)
      {
           case 1:
                motion(x,y,1,error);
                if(error)
                {
                 cerr<<"\athis movment can not be done in chess.\n";
                 k--;
                 getch();
                 print (t);
                   }//end of if
                 else
                 {
                   t[x][y]=++pos;  
                   print(t);
                   }//end of else
                   break;
            case 2:
                 motion(x,y,2,error);
                 if(error)
                {
                 cerr<<"\athis movment can not be done in chess.\n";
                 k--;
                 getch();
                 print(t);
                 }//end of if
                 else
                  {
                   t[x][y]=++pos; 
                   print(t);
                   }//end of else
                 break;
            case 3:
                 motion(x,y,3,error);
                  if(error)
                {
                 cerr<<"\athis movment can not be done in chess.\n";
                 k--;
                 getch();
                 print(t);
                 }//end of if
                  else
                  {
                   t[x][y]=++pos; 
                   print(t);
                   }//end of else
                 break;
             case 4:
                 motion(x,y,4,error);
                  if(error)
                {
                 cerr<<"\athis movment can not be done in chess.\n";
                 k--;
                 getch();
                 print(t);
                 }
                  else
                  {
                   t[x][y]=++pos; 
                   print(t);
                   }
                 break;
             case 5:
                 motion(x,y,5,error);
                  if(error)
                {
                 cerr<<"\athis movment can not be done in chess.\n";
                 k--;
                 getch();
                 print(t);
                 
                 }
                  else
                  {
                   t[x][y]=++pos; 
                   print(t);
                   }
                 break;
              case 6:
                  motion(x,y,6,error);
                   if(error)
                {
                 cerr<<"\athis movment can not be done in chess.\n";
                 k--;
                 getch();
                 print(t);
                 }
                  else
                  {
                   t[x][y]=++pos; 
                   print(t);
                   }
                break;
               case 7:
                  motion(x,y,7,error);
                   if(error)
                {
                 cerr<<"\athis movment can not be done in chess.\n";
                 k--;
                 getch();
                 print(t);
                 }
                  else
                  {
                   t[x][y]=++pos; 
                   print(t);
                   }
                   
                break;
                case 8:
                  motion(x,y,8,error);
                   if(error)
                {
                 cerr<<"\athis movment can not be done in chess.\n";
                 k--;
                 getch();
                 print(t);
                 }
                  else
                  {
                   t[x][y]=++pos; 
                   print(t);
                   }
                  break;
                 case 9:
                      exit(0); 
       }//end of switch    
       }//end of while(1)
  }//end of move function
//******************************************************************************          
void print(int t[row][col])
{
    int i,j;
    cout<<"       "; 
    for(i=0;i<row;i++,cout<<endl<<endl<<"       ")
      for(j=0;j<col;j++)
       cout<<t[i][j]<<"   ";
    getch();   
}
//******************************************************************************
void motion(int &x,int&y,int t,bool& index)
{
 
     switch (t){
            case 1:
                 x-=2;
                 y-=1;
                 if((x<0 ||y<0)||(x>7||y>7))
                 {
                   index=true;
                   x+=2;
                   y+=1;
                   }//end of if
                 else
                  index=false;  
                  
                 break;
             case 2:
                  x-=2;
                  y+=1;    
                  if((x<0 ||y<0)||(x>7||y>7))
                 {
                   index=true;
                   x+=2;
                   y-=1;
                   }//end of if
                 else
                   index=false;  
                 
                  break;
             case 3:
                  y-=2;
                  x+=1;
                  if((x<0 ||y<0)||(x>7||y>7))
                 {
                   index=true;
                   x-=1;
                   y+=2;
                   }//end of if
                 else
                   index=false;  
                 
                  break;
             case 4:
                  y-=2;
                  x-=1;
                  if((x<0 ||y<0)||(x>7||y>7))
                 {
                   index=true;
                   x+=1;
                   y+=2;
                   }//enf of if
                 else
                   index=false;  
                 
                  break;
             case 5:
                  y+=2;
                  x+=1;
                  if((x<0 ||y<0)||(x>7||y>7))
                 {
                   index=true;
                   x-=1;
                   y-=2;
                   }//enf of if
                 else
                   index=false;  
                                
                  break;
             case 6:
                  y+=2;
                  x-=1;
                  if((x<0 ||y<0)||(x>7||y>7))
                 {
                   index=true;
                   x+=1;
                   y-=2;
                   }//end of if
                 else
                   index=false;  
                 
                  break;
             case 7:
                  x+=2;
                  y+=1;
                  if((x<0 ||y<0)||(x>7||y>7))
                 {
                   index=true;
                   x-=2;
                   y-=1;
                   }//enf of if
                 else
                   index=false;  
                 
                  break;
             case 8:
                  x+=2;
                  y-=1;
                  if((x<0 ||y<0)||(x>7||y>7))
                 {
                   index=true;
                   x-=2;
                   y+=1;
                   }//end of if
                 else
                   index=false;  
                 
                  break;
                  }//end of switch
}                                 
//******************************************************************************


4pk329y.jpg
 
بالا