مجموعه سورس کدهای ++C

C++/C

توسط reza-irdev در 7 سال پیش
1 312.7k 61 1 سال پیش
reza-irdev xman
0
void quickSort(int x[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = x[(left + right) / 2];
while (i <= j)
{
while (x[i] < pivot)
i++;
while (x[j] > pivot)
j--;
if (i <= j)
{
tmp = x[i];
x[i] = x[j];
x[j] = tmp;
i++;
j--;
}
}
if (left < j)
quickSort(x, left, j);
if (i < right)
quickSort(x, i, right);
}
0
#include <iostream.h>

void main()
{
int x,y,c=1;
cout<<"Enter a number:"<<endl;
cin>>x;
y=x;
while(x>0)
{
for(int s=0;s<x;s++)
cout<<" ";
for(s=c;s>0;s--)
cout<<"*";
c+=2;
x--;
cout<<"\n";
}
c-=4;
x=(y-1);
while(x>0)
{
for(int s=y;s>=x;s--)
cout<<" ";
for(s=c;s>0;s--)
cout<<"*";
c-=2;
x--;
cout<<"\n";
}
}
0
#include <iostream.h>
#include <string.h>
void sort(char *[ ]);
int main( )
{
char *name[5] = {²sara², ²afsaneh², ²babak², ²saman², ²naser² };
sort(name);// display sorted strings
for(int i=0; i<5; ++i)
cout << name[ i ] << endl;
return 0; }
sort(char *name[ ])
{
char *t;
for(int i=0; i<4; ++i)
for(int j=i+1; j<5; ++j)
if(strcmpi(name[ i ], name[ j ]> 0)
{// interchange the two strings
t= name[ i ];
name[ j ] = name[ i ];
name[ i ] = t ;}
return ; }
0
#include <iostream.h>
long int factorial(int) ;
int main( )
{
int n ;
cout << ² n= ² ;
cin >> n ;
cout << endl << ² factorial = ² << factorial(n) << endl;
return 0 ;
}
long int factorial(int n)
{
if(n<=1)
return(1);
else
return(n *factorical(n-1) ) ;
}
0
#include <iostream.h>
long int fib(long int); // forward declaration
int main( )
{
long int r ;
int n ;
cout << ² Enter an integer value ² << endl ;
cin >> n ;
r = fib(n) ;
cout << r << endl ;
return 0 ;
}
long int fib(long int n)
{
if(n = = 1 || n= = 2)
return 1 ;
else
return(fib(n-1) + fib(n-2) ) ;
}
0
#include <stdlib.h>
#include < iostream.h>
int main( )
{
unsigned seed;
cout << "Enter seed value : " ;
cin >> seed ;
srand(seed);
for(int j=1; j<=10; ++j)
cout << rand( ) << ¢ \n ¢;
return 0 ;
}
0
#include <iostream.h>
int main( )
{
char next;
while((next = cin.get( ) ) !=EOF)
if(next != ¢ ¢ )
cout << next ;
return 0 ;
0
#include < iostream.h >
int main( )
{
float a, b, c;
cout << "Enter three real numbers" << endl ;
cin >> a >> b >> c; //
if(( a < b + c) &&(b < a+c) &&(c < a+b))
cout << "It is a triangle" ;
else
cout << "Not a triangle" ;
return 0 ;
}
0
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int a,c;
char b;
randomize();
for (int i=0;i<10;i++)
{
c=1+random(2);
a=1+random(9);
b=65+random(26);
switch (c)
{
case 1:
cout<<a;
break;
case 2:
cout<<b;
break;
}
}
getch();
}
0

برنامه ای بنویسید که ضرایب معادله درجه دوم را از ورودی بگیرد و معادله را حل کند معادله درجه دوم:AX^2+BX+C=0

#include<iostream.h>
#include<conio.h>
#include<math.h>
main(){
int a,b,c;
int x,x1,x2,delta;
cout<<"intere 3 adad";
cin>>a>>b>>c;
delta=b*b-4*a*c;
if (delta>0){
x1=(-b+sqrt(delta)/(2.*a));
x2=(-b-sqrt(delta)/(2.*a));
cout<<"x1="<<x1<<"x2="<<x2;
}
else
if (delta==0)
{
x=-b/(2.*a);
cout<<"x="<<x;
}
else
cout<<"not";
getch();
}
0
// string::find_first_of
#include <iostream>
#include <string>

int main ()
{
  std::string str ("PLease, replace the vowels in this sentence by asterisks.");
  unsigned found = str.find_first_of("aeiou");
  while (found!=std::string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  std::cout << str << '\n';

  return 0;
}

خروجی:

Pl**s*, *pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks.
0
// appending to string
#include <iostream>
#include <string>

int main ()
{
  std::string str;
  std::string str2="Writing ";
  std::string str3="print 10 and then 5 more";

  // used in the same order as described above:
  str.append(str2);                       // "Writing "
  str.append(str3,6,3);                   // "10 "
  str.append("dots are cool",5);          // "dots "
  str.append("here: ");                   // "here: "
  str.append(10u,'.');                    // ".........."
  str.append(str3.begin()+8,str3.end());  // " and then 5 more"
  str.append<int>(5,0x2E);                // "....."

  std::cout << str << '\n';
  return 0;
}

خروجی: Writing 10 dots here: .......... and then 5 more.....

0

برنامه ای بنویسید که عکس کلمات را نشان بدهد

// string::crbegin/crend
#include <iostream>
#include <string>

int main ()
{
  std::string str ("lorem ipsum");
  for (auto rit=str.crbegin(); rit!=str.crend(); ++rit)
    std::cout << *rit;
  std::cout << '\n';

  return 0;
}

Output: muspi merol

آخرین ویرایش: 25-12-2017 ساعت 18:06، توسط Reza
0

[CSHARP] // strings and c-strings #include <iostream> #include <cstring> #include <string>

int main () { std::string str ("Please split this sentence into tokens");

char * cstr = new char [str.length()+1]; std::strcpy (cstr, str.c_str());

// cstr now contains a c-string copy of str

char * p = std::strtok (cstr," "); while (p!=0) { std::cout << p << '\n'; p = strtok(NULL," "); }

delete[] cstr; return 0; } [/CSHARP] نتیجه:

Please
split
this
sentence
into
tokens
0

ادغام رشته های متنی در زبان ++c

// inserting into a string
#include <iostream>
#include <string>

int main ()
{
  std::string str="to be question";
  std::string str2="the ";
  std::string str3="or not to be";
  std::string::iterator it;

  // used in the same order as described above:
  str.insert(6,str2);                 // to be (the )question
  str.insert(6,str3,3,4);             // to be (not )the question
  str.insert(10,"that is cool",8);    // to be not (that is )the question
  str.insert(10,"to be ");            // to be not (to be )that is the question
  str.insert(15,1,':');               // to be not to be(:) that is the question
  it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
  str.insert (str.end(),3,'.');       // to be, not to be: that is the question(...)
  str.insert (it+2,str3.begin(),str3.begin()+3); // (or )

  std::cout << str << '\n';
  return 0;
}

نتیجه: to be, or not to be: that is the question...

آخرین ویرایش: 25-12-2017 ساعت 18:07، توسط Reza
0

بدست آوردن طول رشته متنی در ++C

// string::length
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.length() << " characters.\n";
  return 0;
}

خروجی: The size of str is 11 characters

آخرین ویرایش: 25-12-2017 ساعت 18:08، توسط Reza
0
// swap strings
#include <iostream>
#include <string>

main ()
{
  std::string buyer ("money");
  std::string seller ("goods");

  std::cout << "Before the swap, buyer has " << buyer;
  std::cout << " and seller has " << seller << '\n';

  seller.swap (buyer);

  std::cout << " After the swap, buyer has " << buyer;
  std::cout << " and seller has " << seller << '\n';

  return 0;
}

خروجی:

Before the swap, buyer has money and seller has goods
 After the swap, buyer has goods and seller has money
0
// string::shrink_to_fit
#include <iostream>
#include <string>

int main ()
{
  std::string str (100,'x');
  std::cout << "1. capacity of str: " << str.capacity() << '\n';

  str.resize(10);
  std::cout << "2. capacity of str: " << str.capacity() << '\n';

  str.shrink_to_fit();
  std::cout << "3. capacity of str: " << str.capacity() << '\n';

  return 0;
}

خروجی:

1. capacity of str: 100
2. capacity of str: 100
3. capacity of str: 10
0

کاربرد string::rfind در++C با مثال:

// string::rfind
#include <iostream>
#include <string>

int main ()
{
  std::string str ("The sixth sick sheik's sixth sheep's sick.");
  std::string key ("sixth");

  unsigned found = str.rfind(key);
  if (found!=std::string::npos)
    str.replace (found,key.length(),"seventh");

  std::cout << str << '\n';

  return 0;
}

خروجی: The sixth sick sheik's seventh sheep's sick.

0
// string::substr
#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (12,12);   // "generalities"

  unsigned pos = str.find("live");         // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}

خروجی:

generalities live in details.

0
// swap strings
#include <iostream>
#include <string>

main ()
{
  std::string buyer ("money");
  std::string seller ("goods");

  std::cout << "Before the swap, buyer has " << buyer;
  std::cout << " and seller has " << seller << '\n';

  seller.swap (buyer);

  std::cout << " After the swap, buyer has " << buyer;
  std::cout << " and seller has " << seller << '\n';

  return 0;
}

خروجی:

Before the swap, buyer has money and seller has goods
 After the swap, buyer has goods and seller has money
0

مثال:

// invalid_argument example
#include <iostream>       // std::cerr
#include <stdexcept>      // std::invalid_argument
#include <bitset>         // std::bitset
#include <string>         // std::string

int main (void) {
  try {
    // bitset constructor throws an invalid_argument if initialized
    // with a string containing characters other than 0 and 1
    std::bitset<5> mybitset (std::string("01234"));
  }
  catch (const std::invalid_argument& ia) {
	  std::cerr << "Invalid argument: " << ia.what() << '\n';
  }
  return 0;
}

خروجی: Invalid argument: bitset::_M_copy_from_string

0
// length_error example
#include <iostream>       // std::cerr
#include <stdexcept>      // std::length_error
#include <vector>         // std::vector

int main (void) {
  try {
    // vector throws a length_error if resized above max_size
    std::vector<int> myvector;
    myvector.resize(myvector.max_size()+1);
  }
  catch (const std::length_error& le) {
	  std::cerr << "Length error: " << le.what() << '\n';
  }
  return 0;
}

خروجی: Length error: vector::_M_fill_insert

0
// out_of_range example
#include <iostream>       // std::cerr
#include <stdexcept>      // std::out_of_range
#include <vector>         // std::vector

int main (void) {
  std::vector<int> myvector(10);
  try {
    myvector.at(20)=100;      // vector::at throws an out-of-range
  }
  catch (const std::out_of_range& oor) {
    std::cerr << "Out of Range error: " << oor.what() << '\n';
  }
  return 0;
}

خروجی: Out of Range error: vector::_M_range_check

0

باز کردن فایلها در در ++C

// filebuf::close()
#include <iostream>
#include <fstream>


int main () {
  std::ifstream is;
  std::filebuf * fb = is.rdbuf();

  fb->open ("test.txt",std::ios::in);

  // appending operations

  fb->close();

  return 0;
}
آخرین ویرایش: 25-12-2017 ساعت 18:10، توسط Reza
0

std::filebuf::is_open

// filebuf::is_open() example
#include <iostream>
#include <fstream>

int main () {
  std::ifstream is;
  std::filebuf * fb = is.rdbuf();
  fb->open ("test.txt",std::ios::in);

  if ( fb->is_open() )
    std::cout << "the file is open.\n";
  else
    std::cout << "the file is not open.\n";

  fb->close();

  return 0;
}
0

std::filebuf::open

// filebuf::open()
#include <iostream>
#include <fstream>

int main () {
  std::ifstream is;
  std::filebuf * fb = is.rdbuf();

  fb->open ("test.txt",std::ios::out|std::ios::app);

  // >> appending operations here <<

  fb->close();

  return 0;
}
0

مثال کپی کردن فایل:

// copy a file using file stream buffers
#include <fstream>      // std::filebuf, std::fstream
#include <cstdio>       // EOF

int main () {
  std::fstream src,dest;
  src.open ("test.txt");
  dest.open ("copy.txt");

  std::filebuf* inbuf  = src.rdbuf();
  std::filebuf* outbuf = dest.rdbuf();

  char c = inbuf->sbumpc();
  while (c != EOF)
  {
    outbuf->sputc (c);
    c = inbuf->sbumpc();
  }

  dest.close();
  src.close();

  return 0;
}
0
// read file data using associated buffer's members
#include <iostream>     // std::cout
#include <fstream>      // std::filebuf, std::ifstream

int main () {
  std::ifstream ifs ("test.txt", std::ifstream::binary);

  // get pointer to associated buffer object
  std::filebuf* pbuf = ifs.rdbuf();

  // get file size using buffer's members
  std::size_t size = pbuf->pubseekoff (0,ifs.end,ifs.in);
  pbuf->pubseekpos (0,ifs.in);

  // allocate memory to contain file data
  char* buffer=new char[size];

  // get file data
  pbuf->sgetn (buffer,size);

  ifs.close();

  // write content to stdout
  std::cout.write (buffer,size);

  delete[] buffer;

  return 0;
}
1
// print the content of a text file.
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () {
  std::ifstream ifs;

  ifs.open ("test.txt", std::ifstream::in);

  char c = ifs.get();

  while (ifs.good()) {
    std::cout << c;
    c = ifs.get();
  }

  ifs.close();

  return 0;
}

خوش آمدید

برای طرح سوال، ایجاد بحث و فعالیت در سایت نیاز است ابتدا وارد حساب کاربری خود شوید. در صورتی که هنوز عضو سایت نیستید میتوانید در عرض تنها چند ثانیه ثبت نام کنید.

لطفا ابتدا لاگین کنید