الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

C++

بدأه **SaRa** في 6 مارس 2008 · 8 رد · 984 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاتة

تحية طيبة لكل مشرف وعضو في هذا المنتدى المتميز

تكفون أطلب مساعدتكم أعرف أنه الوقت متأخر ولكن الحمد لله الله هداني لهذا المنتدى المتميز

وطبعا هذي أول مشاركة لي وتكفون أي شخص عندة خبرة ويقدر يساعدني ف حل هذا السؤال ما يبخل علي والف شكر لكل محب لفعل الخير

أترككم مع السؤال

Your task for this assignment is to write a simple keyword analyzer. Your program should ask read two files: "reservedwords.txt" file that contains all the data type reserved words which are data types: int, double, char, bool, enum, struct, class, … and an input file that your program should ask the user to enter its name. Then your program will read the input file, analyze its content (each statement, where a statement ends by ;) and save the reserved key word (data type) only as a result and print them as an output. Finally, the program should compare the reserved words along with the set of stored keywords and return true if the statement is a data declaration.

Your program should use:

· Arrays, pointers and strings.

· Files.

· Functions.

For example if the input file contains the following paragraph:

int x, y;

double z;

enum week {SAT, SUN, MON, TUE, WED, THU, FRI};

cout<<x;

Then the program should save the reserved words which are int, double, enum. After that, compare each word with the list of reserved word and return true a statement contains a reserved word otherwise return false.

ولا تبخلو علي أروكم ترائ التسليم بكرا المساء وألف شكر مرة ثانية

وهذا صورة من برنامجي الي حاولت احلة لكن بدون فايدة النتلئج خطا ف الجزئية الأخير

#include <iostream>

#include <fstream>

#include <cstring>

#include<iomanip>

using namespace std;

//Declaration of global data which are common to all the functions

void ReadInputFile (char []);

void ReadReservedFile ();

void SaveWord (char* word);

void SaveWord2 (char* word2);

bool CompareWords ();

// Two Arrayes of 100 words having a maximum of 50

//charcaters each

char KeyWord [100][50];

char KeyWord2 [30][50];

int i=0;

int s=0;

int ss=0;

int ii=0;

int main()

{

char fileName [20];

cout<<"Please type the name of the input file:"<<endl;

cin>> fileName;

cout<<endl;

ReadInputFile (fileName);

//PrintResult();

cout<<endl;

int u=0;

for (int k=0; k<i; k++)

{

cout<<KeyWord[k];

u++;

}

cout<<endl;

cout<<"********************************";

cout<<endl;

ReadReservedFile ();

for (int z=0; z<ii; z++)

cout<<KeyWord2[z];

cout<<endl;

cout<<"********************************";

cout<<endl;

bool SeeReasult = CompareWords();

for (int zz=0; zz<i; zz++)

{

cout<<KeyWord[zz]<<"\t\t"<<SeeReasult;

}

cout<<endl;

return 0;

}

void ReadInputFile (char fileName [])

{

ifstream InputFile;

char line[80];

char *wordptr;

InputFile.open(fileName);

while(!InputFile.eof()) {

InputFile.getline(line, 100, ';');

wordptr=strtok(line ," * << >>");

int counter =1;

while (wordptr!=NULL && counter !=2 )

{

SaveWord(wordptr);

counter++;

wordptr=strtok(NULL," ");

}

}

}

void ReadReservedFile ()

{

ifstream ReservedFile;

char line2[100];

char *PWord;

ReservedFile.open("reservedwords.txt");

while(!ReservedFile.eof()) {

ReservedFile.getline(line2, 100, '\0');

PWord=strtok(line2," ");

while (PWord!=NULL) {

SaveWord2(PWord);

PWord=strtok(NULL," ");

}

}

}

void SaveWord (char* wordptr)

{

strcpy(KeyWord,wordptr);

strcat(KeyWord," ");

i++;

s++;

}

void SaveWord2 (char* PWord)

{

strcpy(KeyWord2[ss],PWord);

strcat(KeyWord2[ss]," ");

ii++;

ss++;

}

bool CompareWords ()

{

bool True = true;

bool Fulse = false;

for ( int a=0; a<i; a++)

for(int aa=0; aa<ii; aa++)

{

if(strcmp( KeyWord2[aa] , KeyWord[a])==0)

return True;

if(strcmp(KeyWord2[aa] , KeyWord[a])!=0)

return Fulse;

}

return True , Fulse;

}

والف شكرا مرة ثانية ((عاشقة الجنة)) ..

تم تعديل هذه المشاركة بواسطة **SaRa** في 7 مارس 2008 في 15:29

#2

ممكن توضحي إنتى بتعملى إيه فى الجزء دا يا أختى؟

wordptr=strtok(line ," * << >>");

int counter =1;
while (wordptr!=NULL && counter !=2 )
{
SaveWord(wordptr);
counter++;
wordptr=strtok(NULL," ");
}

و كملاحظة مبدئية, الدالة strtok لا ترى >> على أنها شئ واحد و لكن تراها على أنهم 2 delimiters الأول > و الثانى > فالحصيلة أنها ترى أن ال delimiter هو الحرف > فقط.

أيضا فى الدالة CompareWords, أنا مش فاهمها كويس بس شكلها فيها حاجة غلط, زى مثلا return اللى فى الأخر دى true, false ؟ يعنى إيه؟ إنتى تقصدى إيه من الدالة دى بالظبط؟

تم تعديل هذه المشاركة بواسطة Blueteeth في 7 مارس 2008 في 02:37

#3

هلا أخوي

شكرا لأهتمامك

wordptr=strtok(line ," * << >>");

الدالة strtok لا ترى >> على أنها شئ واحد و لكن تراها على أنهم 2 delimiters الأول > و الثانى > فالحصيلة أنها ترى أن ال delimiter هو الحرف > فقط((معلومة جديدة تشكر عليها أخوي))

ف هذا اللين int counter =1; أنا سويتة ع أساس يتم تخزين فقط الكلمة الأولى من كل سطر من المصفوفة line **في المصفوفة wordptr ولي بتنسخ الكلمة المحفوضة من line ف أراي جديدة من نوع تو دايمنش ولي هية keyWord[][] ويتم هذا بواسطة هذي الفنكشن

{

strcpy(KeyWord,wordptr);

strcat(KeyWord," ");

i++;

s++;

}

**لأنه من ضمن الشروط عشان لوب يتم وما يخرج ( counter !=2 )

وبالنسة لي الداله

CompareWords هذي بتقارن كل كلمة من المصفوفة المحفوضة بواسطة الدالة SaveWord والموجودة في المصفوفة KeyWord مع كل الكلمات المحفوضة بواسطة الدالة

SaveWord 2 والموجودة ف المصفوفة keyWord2 وإذا حصلت الكلمة مطابقة بتسوي return true(1)i واذا لم تجدreturn false(0)i

وملاحظة التسليم اليوم وألف شكر ع الجهد المبذول جزاك الله الف خير أخي العزيز

تم تعديل هذه المشاركة بواسطة **SaRa** في 7 مارس 2008 في 08:51

#4

82 عدد القراء لهذا الموضوع ولا رد

لا حول ولا قوة إلا بالله

شباب الهمة ترا عجلة الزمن للتسليم تقترب باقي بضع سويعات

تحياتي لكل من حاول ويحاول مساعدتي

#5

هل هذا ما تريدين؟>

CONSOLE
Please type the name of the input file:

stu.txt

int double enum cout

********************************

int double enum

********************************

int 1

double 1

enum 1

cout 0

Press any key to continue . . .

إذا كان هو, فهذا هو الكود, عملت عليه تعديلات بسيطة>

#include <iostream>
#include <fstream>
#include <cstring>
#include<iomanip>

using namespace std;

//Declaration of global data which are common to all the functions
void ReadInputFile (char []);
void ReadReservedFile ();
void SaveWord (char* word);
void SaveWord2 (char* word2);
bool CompareWords (int);

// Two Arrayes of 100 words having a maximum of 50
//charcaters each
char KeyWord [100][50];
char KeyWord2 [30][50];

int i=0;
int s=0;
int ss=0;
int ii=0;

int main()
{
	char fileName [20];
	cout<<"Please type the name of the input file:"<<endl;
	cin>> fileName;


	cout<<endl;
	ReadInputFile (fileName);
	//PrintResult();
	cout<<endl;
	int u=0;
	for (int k=0; k<i; k++)
	{
		cout<<KeyWord[k];
		u++;
	}

	cout<<endl;
	cout<<"********************************";
	cout<<endl;

	ReadReservedFile ();
	for (int z=0; z<ii; z++)
		cout<<KeyWord2[z];

	cout<<endl;
	cout<<"********************************";
	cout<<endl;

//	bool SeeReasult = CompareWords();
	for (int zz=0; zz<i; zz++)
	{
		cout<<KeyWord[zz]<<"\t\t"<</*SeeReasult*/CompareWords(zz) << endl;
	}

	cout<<endl;
	return 0;
}

void ReadInputFile (char fileName [])
{
	ifstream InputFile;
	char line[80];
	char *wordptr;

	InputFile.open(fileName);


	while(!InputFile.eof()) {
		InputFile.getline(line, 100, ';');
		wordptr=strtok(line ," * << >>");

		int counter =1;
		while (wordptr!=NULL && counter !=2 )
		{
			SaveWord(wordptr);
			counter++;
			wordptr=strtok(NULL," ");
		}
	}
}

void ReadReservedFile ()
{
	ifstream ReservedFile;
	char line2[100];
	char *PWord;

	ReservedFile.open("reservedwords.txt");

	while(!ReservedFile.eof()) {
		ReservedFile.getline(line2, 100, '\0');
		PWord=strtok(line2," ");

		while (PWord!=NULL) {
			SaveWord2(PWord);
			PWord=strtok(NULL," ");
		}
	}
}

void SaveWord (char* wordptr)
{
	strcpy(KeyWord,wordptr);
	strcat(KeyWord," ");
	i++;
	s++;
}

void SaveWord2 (char* PWord)
{
	strcpy(KeyWord2[ss],PWord);
	strcat(KeyWord2[ss]," ");
	ii++;
	ss++;
}

bool CompareWords (int stat_index)
{

	// Actually it doesn't make much sense to declare these two variables
	/*bool True = true;
	bool Fulse = false;*/

	for ( int a=0; a<ii; a++)
//		for(int aa=0; aa<ii; aa++)
//		{
		if(strcmp( KeyWord2[a] , KeyWord[stat_index])==0)
			return true;

//			if(strcmp(KeyWord2[aa] , KeyWord[a])!=0)
//			return false;
//		}

	return false /*, Fulse*/;
}

و هذا هو الفايل المدخل>

int x, y;double z;enum week {SAT, SUN, MON, TUE, WED, THU, FRI};cout<<x;

و هذا هو فايل ال reservedwords

int double enum

ملاحظة: الفايلات لازم تكون بالصورة دى بالظبط علشان الكود يشتغل, يعنى متحاوليش تدخلى سطر جديد فى أى فايل, لو عايزة تضيفى حاجة كملى على نفس السطر, علشان فى الكود مش بتاخدى فى حسبانك الحرف "\n" و بالتالى هو بيعتبره حرف من ضمن ال token .

#6

الف الف الف شكر أخوي جزاك الله ألف خير إن شاء الله

ومشان الملاحظة عن الفايلات أنا ضبطها وسويت

/nل

وشغال الحمد لله وتشكر أخي وباقي التعديلات أنا بسويها ولم يخب ظني بالمنتدى وباأعضاء لي فية وأنا أعد بأن أكرس كل مجهودي لخدمة المنتدى

ورد الجميل لرقي هذا المنتدى المتميز

وتشكر أخي

تحياتي لك ولكل مجتهد يسعى لرقي وخدمة هذا المنتدى

#7

الحمد لله أختى لا داعى للشكر أهلا بك فى أى وقت.

لكن لى بعض الملاحظات البسيطة على طريقة كتابتك للبرنامج:

- لا تستخدمى ال global variables "إلا عند الضرورة القصوى" و أعتقد أن هذه الحالة ليست "ضرورة قصوى" بالمرة.

- من الأسهل إستخدام STL::string بدلا من إستخدام ال c-strings.

- إجعلى أسماء المتغيرات معبرة أكثر من هذا, فلا معنى ل ss أو ii أو Keyword2.

بالتوفيق إن شاء الله.

#8

مرحبا أخوي بك مرة ثانية الشكر موصول لك

بس عندي بعض الأسئلة في الحل

1.

 bool CompareWords (int stat_index)

ما وظيفة stat_index في هذا اللين

2.ما ذا لو أردت أن أطبع الريزيرفد ورد من الملف الاول بدون الcin & cout كيف بيكون التعديل ف الكود

3.مالفرق بين STL::string وc-strings

ملاحظة( نصائحك وصلت وأنا من الناصطات ل نصائحك) ~عاشقة

#9

1- ال state_index هو إختصار statement_index و هو ال index بتاع الجملة (statement) فى المصفوفة keyword2 والتى, داخل الفنكشن CompareWords تقارنيها بكل الكلمات المفتاحية فى keyword.

2- مش فاهم تقصدى إيه بالظبط؟ قصدك تطبعى ب printf مثلا؟

3- ال std::string دا object (أسف هو std::string مش stl::string غلطة مطبعية :) و لكنه ما زال يتبع ال STL) يتيح لك إمكانيات متقدمة بالنسبة ل c-string, على سبيل المثال تقدرى تقارنى ما بين 2 strings,

str1>str2, str<=str2 ......

تقدرى تضيفى عليه

str1+="Hello"

تقدرى تدخلى حرف فى النص insert تمسحى حرف من النص erase تدورى على substring ..... و دا لينك للكلاس> http://www.cplusplus.com/reference/string/string/