PDA

View Full Version : مبتدی: کار با رشته ها در c++



alimooghashang
شنبه 25 اردیبهشت 1389, 19:24 عصر
سلام
گشتم نبود پس ببخشید
میشه بگید ، چطوری با c++ میشه یه رشته مثل این را گرفت


hi,my name is ali. i am 21 years old.


و به تک تک کاراکترهاش دسترسی داشته باشیم؟

من اینطوری نوشتم ارر میده


string str;
cout << "Enter Sentence: ";
cin>> str;

با تشکر

Salar Ashgi
شنبه 25 اردیبهشت 1389, 19:39 عصر
کد زیر رو اضافه کنید :



#include <string>
using namespace std;


موفق باشید .

alimooghashang
شنبه 25 اردیبهشت 1389, 19:42 عصر
کد زیر رو اضافه کنید :



#include <string>
using namespace std;


موفق باشید .
منborland c++ builder دارم

amir_civil
شنبه 25 اردیبهشت 1389, 19:45 عصر
// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
std::string str;
std::cin>>str;
size_t size;
size=sizeof(str);
for(int a=0;a<=((int)(size))-1;a++)
{
std::cout<<str[a]<<std::endl;
}
return 0;
}

alimooghashang
شنبه 25 اردیبهشت 1389, 19:50 عصر
چرا بجای sizeof از strlen استفاده نکردید؟

Salar Ashgi
شنبه 25 اردیبهشت 1389, 21:19 عصر
منborland C++‎ builder دارم

خوب هیچ فرقی نمیکنه ! تو هر کامپایلری که باشه نام هدر فایل که ثابته !

alimooghashang
شنبه 25 اردیبهشت 1389, 22:33 عصر
ببینید این برنامه!

اما تو cin اولی ارر میده




int _tmain()
{
int i=0,j=0,k=0;
std::string str,substr;
cout << "Enter Sentence: ";
cin >> str;
cout << "\nEnter Key: ";
cin >> substr;

bool found=false;
while(!found)
{
if(str[i]!=substr[j])
{
i++;
j=0;
k=0;
}
else if (str[i]==substr[j])
{
i++;
j++;
k++;
if (k==(int)(sizeof(substr)))
found=true;
}
}
int pos=i-k;
cout << pos<< endl;
return 0;
}



جمله تست شده این است

hi,my name is ali. i am 21 years old

و کلمه جستجو شده name است!

این برنامه ی جستجوی یک زیر رشته در یک رشته است!
لطفا کمک کنید!
نمیدونم چرا c++ یادم رفته :لبخند:

tdkhakpur
شنبه 25 اردیبهشت 1389, 23:51 عصر
حالا نمیشه اینطوری به یاد بیارید؟:لبخندساده:


int _tmain()
{
int pos, c;
char str[126], substr[32];
cout << "Enter Sentence: ";
cin.getline(str, 126);
cin.sync();
cout << "\nEnter Key: ";
cin.getline(substr, 32);
bool found=false;
pos = 0;
while( !found && pos<strlen(str))
{
for( c=0; c<strlen(substr) && (pos+c)<strlen(str); c++ )
if( str[pos+c]!=substr[c] ) break;
if( c == strlen(substr) )
found = true;
else
pos ++;
}
if( found )
cout << pos<< endl;
else
cout << "not found"<< endl;

return 0;
}

sh4mid
شنبه 25 اردیبهشت 1389, 23:53 عصر
میشه بگید ، چطوری با C++‎‎‎‎‎ میشه یه رشته مثل این را گرفت


hi,my name is ali. i am 21 years old.


و به تک تک کاراکترهاش دسترسی داشته باشیم؟برای گرفتن رشته تو C++‎‎‎‎ f باید از get استفاده کنی



int get();Extracts a character from the stream and returns its value (casted to an integer).istream& get ( char& c );Extracts a character from the stream and stores it in c.istream& get (char* s, streamsize n );Extracts characters from the stream and stores them as a c-string into the array beginning at s. Characters are extracted until either (n - 1) characters have been extracted or the delimiting character '\n' is found. The extraction also stops if the end of file is reached in the input sequence or if an error occurs during the input operation.
If the delimiting character is found, it is not extracted from the input sequence and remains as the next character to be extracted. Use getline (http://www.cplusplus.com/istream::getline) if you want this character to be extracted (and discarded).
The ending null character that signals the end of a c-string is automatically appended at the end of the content stored in s.
istream& get (char* s, streamsize n, char delim ); Same as above, except that the delimiting character is the one specified indelim instead of '\n'.istream& get (streambuf& sb);Extracts characters from the stream and inserts them in the stream buffer sb until either the delimiting character '\n' is found or end of file is reached. The extraction also stops if an error occurs either in the input sequence controled by the stream or in the output sequence controlled by sb.istream& get (streambuf& sb, char delim );Same as above, except that the delimiting character is the one specified indelim instead of '\n'.
Parameters

cA char variable to store the extracted character.sA pointer to an array of characters where the string is stored as a c-stringnMaximum number of characters to store (including the ternimating null character).
This is an integer value of type streamsize (http://www.cplusplus.com/streamsize).delimThe delimiting character. The operation of extracting succesive characters is stopped when this character is read. This parameter is optional, if not specified the function considers '\n' (a newline character) to be the delimiting character.sbAn output stream buffer (an object of class streambuf (http://www.cplusplus.com/streambuf) or any of its derived classes).
مثال



#include <iostream>
#include <fstream>
using namespace std;

int main () {
char c, str[256];
ifstream is;

cout << "Enter the name of an existing text file: ";
cin.get (str);

return 0;
}
برای جستجوی یک رشته تو یه رشته دیگه از می تونی از strstr استفاده کنی یا find

مثال



#include <stdio.h>
#include <string.h>

int main ()
{
char str[] ="This is a simple string";
char * pch=strstr (str,"simple");

if(pch)
{
//find it
}
return 0;
}
#include <string>
#include <iostream>

using namespace std;

int main( )
{
string str2 ( "Let me make this perfectly clear." );
cout << "The original string str2 is: " << str2 << endl;
basic_string <char>::size_type idxCh2a;

const char *cstr2 = "perfect";
idxCh2a = str2.find (cstr2);
if ( indexCh2a != string::npos )
{
//find it
}
else
{
//nothing
}

return 0;
}

alimooghashang
یک شنبه 26 اردیبهشت 1389, 00:07 صبح
دوست عزیز، من برنامه جستجو را با الگوریتم خودم نوشتم! تابع آماده نمیخوام!
فقط الان تو خوندن رشته مشکل دارم!
لطفا برنامه ای که گزاشتم رو تابع ورودیش را درست کنید اگه ممکنه
با تشکر

sh4mid
یک شنبه 26 اردیبهشت 1389, 00:44 صبح
ببین وقتی میخواهی رشته ای رو از ورودی بگیری که شامل Whitespace هست نمی تونی از
<< استفاده کنی باید از تابع getline استفاده کنی اینو با cin.getline اشتباه نگیر



int i=0,j=0,k=0;
string str,substr;
cout<<"Enter Sentence: ";

getline(cin,str);

cout<<"\nEnter Key: ";

getline(cin,substr);


istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str );
Get line from stream
Extracts characters from is and stores them into str until a delimitation character is found.

The delimiter character is delim for the first function version, and '\n' (newline character) for the second. The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.

If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.

Notice that unlike the c-string versions of istream::getline (http://www.cplusplus.com/istream::getline), these string (http://www.cplusplus.com/string) versions are implemented as global functions instead of members of the stream class.

Parameters

isistream (http://www.cplusplus.com/istream) object on which the extraction operation is performed.strstring (http://www.cplusplus.com/string) object where the extracted content is stored.delimThe delimiting character. The operation of extracting succesive characters is stopped when this character is read.

Return Value

The same as parameter is.

Errors are signaled by modifying the internal state flags:

flagerror eofbitThe end of the source of characters is reached during its operations. failbitNo characters were extracted because the end was prematurely found.Notice that some eofbit cases will also set failbit. badbitAn error other than the above happened.

Additionaly, in any of these cases, if the appropriate flag has been set with is's member function ios::exceptions (http://www.cplusplus.com/ios::exceptions), an exception of type ios_base::failure (http://www.cplusplus.com/ios_base::failure) is thrown.

alimooghashang
یک شنبه 26 اردیبهشت 1389, 00:46 صبح
بله من با borland c++ builder کار میکنم و ارر میده


Error: project.cpp(14,13):Call to undefined function 'getline'

sh4mid
یک شنبه 26 اردیبهشت 1389, 00:48 صبح
چه نسخه ای از borland C++‎ builder؟

alimooghashang
یک شنبه 26 اردیبهشت 1389, 00:50 صبح
ببینید وقتی اینطوری مینویسم جواب درسته!


char *str,*substr;
str="hi,my name is ali. i am 21 years old";
substr="name";


این را درست کنید که از ورودی جمله را بگیره!
مرسی

alimooghashang
یک شنبه 26 اردیبهشت 1389, 00:51 صبح
من c++ یادم رفته گیج گیچ شدم تو کار کردن با رشته ها :لبخند:

alimooghashang
یک شنبه 26 اردیبهشت 1389, 00:52 صبح
چه نسخه ای از borland C++‎ builder؟
نسخه 5.02 است

sh4mid
یک شنبه 26 اردیبهشت 1389, 01:08 صبح
بگذار نصب کنم بهت می گم

sh4mid
یک شنبه 26 اردیبهشت 1389, 01:35 صبح
ببین با این کارت راه می افته؟



cout << "Enter Sentence: ";
getline(cin,str,'\n');
cout << "\nEnter Key: ";
getline(cin,substr,'\n');

بدون خطا Compile شد فقط نتونست Link کنه

رفتم Header File شو دیدم




template<class charT, class traits, class Allocator>
istream& getline(istream& is,
basic_string<charT, traits,Allocator>& str, charT delim);


template<class charT, class IS_traits, class STR_traits, class STR_Alloc>
basic_istream<charT, IS_traits>&
getline(
basic_istream<charT, IS_traits>& is,
basic_string<charT, STR_traits, STR_Alloc>& str,
charT delim
#if 0
= IS_traits::newline()
#endif
);
انگار براش تعریف نشده که این تابع بتونه دو تا ورودی بگیره

به این تکه دقت کن



charT delim
#if 0
= IS_traits::newline()
#endif
طبق این delim نمی تونه پیش فرض 'n\' باشه

این Compiler عهد عتیق رو بی خیال شو اگه نمی خواهی که درگیر VC هم بشی لااقل از
http://www.bloodshed.net/devcpp.html
استفاده کن نسخه تحت Windows از Compiler قدرتمند GCC به همراه یک IDE

alimooghashang
یک شنبه 26 اردیبهشت 1389, 01:36 صبح
دوباره همین ارر


Error: project.cpp(14,13):Call to undefined function 'getline'

alimooghashang
یک شنبه 26 اردیبهشت 1389, 01:37 صبح
ببینید وقتی اینطوری مینویسم جواب درسته!


char *str,*substr;
str="hi,my name is ali. i am 21 years old";
substr="name";


این را درست کنید که از ورودی جمله را بگیره!
مرسی

ببینید این با VS2008 درست جواب میده
لطفا اگه میشه با cin درستش کنید که از ورودی جمله بگیره!

sh4mid
یک شنبه 26 اردیبهشت 1389, 01:54 صبح
ببینید این با VS2008 درست جواب میده
ببین اگه vs2008 داری پس ماجرای Borland چیه؟
اگه vs2008 داری همون
getline(cin,str) کارتو راه می اندازه
من الان borland v5.02 نصب کردم به کد بالا گیر داد ولی
getline(cin,str,'\n') رو تونست compile کنه

alimooghashang
یک شنبه 26 اردیبهشت 1389, 01:57 صبح
دوست عزیز برنامه را برای کسی مینویسم که زور بزنه TC داره رو سیستمش :لبخند:
خودم VS2008 دارم

alimooghashang
یک شنبه 26 اردیبهشت 1389, 01:58 صبح
در VS2008 هم ارر


Error 1 error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'char *' c:\documents and settings\hadi\desktop\saeid\saeid\saeid.cpp 62 saeid





int i=0,j=0,k=0;
char *str,*substr;
// str="hi,my name is ali. i am 21 years old";
// substr="name";

getline(cin,str);
getline(cin,substr);

sh4mid
یک شنبه 26 اردیبهشت 1389, 02:28 صبح
خطایی که گفتید بخاطر اینه که شما اومدید str و substr رو اشاره گر به char تعریف کردید و از اون توی تابع getline استفاده کردید که دومین عنصر ورودی اش از نوع string هست

اگه می خواهید با string کار کنید قید TC رو بزنید

alimooghashang
یک شنبه 26 اردیبهشت 1389, 02:34 صبح
خوب ممنون میشم بگید چطوری طول رشته رو بخونم

(int)(sizeof(substr))
این کار نمیده
مرسی

sh4mid
یک شنبه 26 اردیبهشت 1389, 02:44 صبح
اگه از string استفاده می کنید



string str ("Test string");
cout << "The length of str is " << str.length() << " characters.\n";
cout << "The size of str is " << str.size() << " characters.\n";

اگه از *char استفاده می کنید تابع strlen

alimooghashang
یک شنبه 26 اردیبهشت 1389, 02:49 صبح
اگه از *char میشد بهتر بود، ولی خوب تو خوندنش از ورودی مشکل داشتم!

sh4mid
یک شنبه 26 اردیبهشت 1389, 03:13 صبح
گه از *char میشد بهتر بودمی تونی از توابع data و c_str استفاده کنی



string str ("Hello");

cstr = new char [str.size()+1];
strcpy (cstr, str.c_str());

فرقشونم اینه که data اون رشته ای که تولید می کنه آخرش null نیست ولی اون یکی یه C-string تولید می کنه که آخرشم null هست

farzaad88
چهارشنبه 05 خرداد 1389, 17:55 عصر
سلام من میخوام حاصل عملیاتی مثل پایینی رو از طریق رشته ها تو c++بدست بیارم
2+3(3+9*1)2+3*11

fahim1369
شنبه 07 تیر 1393, 14:34 عصر
سلام چجوری یه رشته مثله رشته پایینیرو میشه گرفت؟
s-->aA