-Cheetah-
13-01-2007, 10:36 AM
السلام عليكم ورحمة الله وبركاته
عمل البرنامج:
يولد الرنامج عددين (1-9) ويطلب من المستخدم إدخال ناتج ضربهما.
يعرض رسالة مختارة عشوائيا من ثلاث رسائل. (تختلف هذه الرسائل تبعا لصحة الإجابة).
بعد عشرة أسئلة يعرض نسبة مئوية لأداء المستخدم.
يتوقف البرنامج في حالة إدخال رمز EOF :
(Ctrl+Z).
المشاكل:
1-البرنامج لا يتوقف بإدخال EOF، بل يخرج باستمرار وبلا نهاية إحدى الرسائل الثلاث الخاصة بالإجابة الخطأ.
2-عند استعمال in=cin.get البرنامج لا يقرأ القيمة المدخلة بواسطة المستخدم.
# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;
void main()
{
int n1,n2;
int res;
int in;
int countGlob = 1;
int countWrong = 0;
int test;
int rcase;
bool clear = false;
srand(time(0));
n1 = (rand()%9)+1;
n2 = (rand()%9)+1;
res = n1*n2;
cout << "How much is " << n1 << " times " << n2 << endl;
cin >> in;
while ((in)!=EOF)
{
rcase = (rand()%3)+1;
if (in==res)
{
test=1;
}
else
{
test=2;
}
switch(test)
{
case 1:
if (rcase==1)
{
cout << "Great job, keep it up." << endl;
}
else
if (rcase==2)
{
cout << "Correct!!" << endl;
}
else
{
cout << "Right, keep up the good work." << endl;
}
if (countGlob==10)
{
cout << "Your percentage is "
<< (static_cast<double>(10-countWrong)/countGlob)*100 << "%."<< endl;
countWrong = 0;
countGlob = 0;
}
n1 = (rand()%9)+1;
n2 = (rand()%9)+1;
res = n1*n2;
cout << "How much is " << n1 << " times " << n2 << endl;
cin >> in;
clear = false;
countGlob++;
break;
case 2:
if (rcase==1)
{
cout << "Wrong, try again." << endl;
}
else
if (rcase==2)
{
cout << "Too bad, give it another shot." << endl;
}
else
{
cout << "No, don't give up!" << endl;
}
if (!clear)
{
countWrong++;
clear = true;
}
cin >> in;
break;
}
}
}
# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;
void main()
{
int n1,n2;
int res;
int in;
int countGlob = 1;
int countWrong = 0;
int test;
int rcase;
bool clear = false;
srand(time(0));
n1 = (rand()%9)+1;
n2 = (rand()%9)+1;
res = n1*n2;
cout << "How much is " << n1 << " times " << n2 << endl;
while ((in=cin.get())!=EOF)
{
rcase = (rand()%3)+1;
if (in==res)
{
test=1;
}
else
{
test=2;
}
switch(test)
{
case 1:
if (rcase==1)
{
cout << "Great job, keep it up." << endl;
}
else
if (rcase==2)
{
cout << "Correct!!" << endl;
}
else
{
cout << "Right, keep up the good work." << endl;
}
if (countGlob==10)
{
cout << "Your percentage is "
<< (static_cast<double>(10-countWrong)/countGlob)*100 << "%."<< endl;
countWrong = 0;
countGlob = 0;
}
n1 = (rand()%9)+1;
n2 = (rand()%9)+1;
res = n1*n2;
cout << "How much is " << n1 << " times " << n2 << endl;
clear = false;
countGlob++;
break;
case 2:
if (rcase==1)
{
cout << "Wrong, try again." << endl;
}
else
if (rcase==2)
{
cout << "Too bad, give it another shot." << endl;
}
else
{
cout << "No, don't give up!" << endl;
}
if (!clear)
{
countWrong++;
clear = true;
}
break;
}
}
}
الكود الأول يتعرض للمشكلة الأولى، والثاني للثانية، والاختلاف بينهما في استعمال cin.get
عمل البرنامج:
يولد الرنامج عددين (1-9) ويطلب من المستخدم إدخال ناتج ضربهما.
يعرض رسالة مختارة عشوائيا من ثلاث رسائل. (تختلف هذه الرسائل تبعا لصحة الإجابة).
بعد عشرة أسئلة يعرض نسبة مئوية لأداء المستخدم.
يتوقف البرنامج في حالة إدخال رمز EOF :
(Ctrl+Z).
المشاكل:
1-البرنامج لا يتوقف بإدخال EOF، بل يخرج باستمرار وبلا نهاية إحدى الرسائل الثلاث الخاصة بالإجابة الخطأ.
2-عند استعمال in=cin.get البرنامج لا يقرأ القيمة المدخلة بواسطة المستخدم.
# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;
void main()
{
int n1,n2;
int res;
int in;
int countGlob = 1;
int countWrong = 0;
int test;
int rcase;
bool clear = false;
srand(time(0));
n1 = (rand()%9)+1;
n2 = (rand()%9)+1;
res = n1*n2;
cout << "How much is " << n1 << " times " << n2 << endl;
cin >> in;
while ((in)!=EOF)
{
rcase = (rand()%3)+1;
if (in==res)
{
test=1;
}
else
{
test=2;
}
switch(test)
{
case 1:
if (rcase==1)
{
cout << "Great job, keep it up." << endl;
}
else
if (rcase==2)
{
cout << "Correct!!" << endl;
}
else
{
cout << "Right, keep up the good work." << endl;
}
if (countGlob==10)
{
cout << "Your percentage is "
<< (static_cast<double>(10-countWrong)/countGlob)*100 << "%."<< endl;
countWrong = 0;
countGlob = 0;
}
n1 = (rand()%9)+1;
n2 = (rand()%9)+1;
res = n1*n2;
cout << "How much is " << n1 << " times " << n2 << endl;
cin >> in;
clear = false;
countGlob++;
break;
case 2:
if (rcase==1)
{
cout << "Wrong, try again." << endl;
}
else
if (rcase==2)
{
cout << "Too bad, give it another shot." << endl;
}
else
{
cout << "No, don't give up!" << endl;
}
if (!clear)
{
countWrong++;
clear = true;
}
cin >> in;
break;
}
}
}
# include <iostream>
# include <cstdlib>
# include <ctime>
using namespace std;
void main()
{
int n1,n2;
int res;
int in;
int countGlob = 1;
int countWrong = 0;
int test;
int rcase;
bool clear = false;
srand(time(0));
n1 = (rand()%9)+1;
n2 = (rand()%9)+1;
res = n1*n2;
cout << "How much is " << n1 << " times " << n2 << endl;
while ((in=cin.get())!=EOF)
{
rcase = (rand()%3)+1;
if (in==res)
{
test=1;
}
else
{
test=2;
}
switch(test)
{
case 1:
if (rcase==1)
{
cout << "Great job, keep it up." << endl;
}
else
if (rcase==2)
{
cout << "Correct!!" << endl;
}
else
{
cout << "Right, keep up the good work." << endl;
}
if (countGlob==10)
{
cout << "Your percentage is "
<< (static_cast<double>(10-countWrong)/countGlob)*100 << "%."<< endl;
countWrong = 0;
countGlob = 0;
}
n1 = (rand()%9)+1;
n2 = (rand()%9)+1;
res = n1*n2;
cout << "How much is " << n1 << " times " << n2 << endl;
clear = false;
countGlob++;
break;
case 2:
if (rcase==1)
{
cout << "Wrong, try again." << endl;
}
else
if (rcase==2)
{
cout << "Too bad, give it another shot." << endl;
}
else
{
cout << "No, don't give up!" << endl;
}
if (!clear)
{
countWrong++;
clear = true;
}
break;
}
}
}
الكود الأول يتعرض للمشكلة الأولى، والثاني للثانية، والاختلاف بينهما في استعمال cin.get