PDA

View Full Version : سوال: توضیح کد



hafez1
جمعه 05 آبان 1391, 21:26 عصر
یکی برام توضیح بده این کدو:


#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void main()
{
string myText;
cin>>myText;
istringstream iss(myText);
string token;
string token2;
while(getline(iss, token, '+'))
{
istringstream iss2(token);
while(getline(iss2, token2, '-'))
{
if(token2!=token)
cout<<token2<<endl;
else
cout << token <<endl;

}

}}

به خصوص خط نهش.

omidshaman
یک شنبه 07 آبان 1391, 12:23 عصر
اولا تو while اخری یک if اضافست چون هر 2 تا یک نتیجه میدن
بعدش شما اگر می خوای بدونی این کد چحوری کار می کنه باید بدونی stream چیه و چجوری کار میکنه بهتره این page رو بخونی از 0 توضیح داده
http://www.cprogramming.com/tutorial/C++‎‎‎‎‎‎-iostreams.html
فقط ساده بگم این istringstream iss(myText); به کمپایلر میگه که iss ادرس یک "استرینگه" که موقعیتش توی حافظه اینه مثلا gdgg35df0



#include <iostream>

#include <string>

#include <sstream>

using namespace std;

int main()

{

string myText;


cin>>myText;

istringstream iss(myText);
string token;
string token2;

while(getline(iss, token, '+'))

{

istringstream iss2(token);

while(getline(iss2, token2, '-'))

{

cout<<token2<<endl;

}}}