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

حطا منطقي عند تنفيذ البرنامج

بدأه المـــــاسة في 7 أبريل 2012 · 2 رد · 457 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

اتوقع ان الخطا بـ while او ان محرري لايعمل جيدا

هذا الكود

#include<iostream>
using namespace std;
const int MAX=5;
void push(int stack[],int *top,int value)
{
	if (*top < MAX-1)
	{
		*top=*top+1;
		stack[*top]=value;
		return;
	}
	else
		cout<<"stack is full\n";
}
int pop(int stack[],int *top)
{
	int value;
	if (*top > -1)
	{
		value=stack[*top];
		*top=*top-1;
		return value;
	}

	else
		return -1;
}
void main(){
	int stack[MAX],top=-1,value,answer,choice;

	cout<<"1. to push element\n"  <<"2. to pop element\n"<<"3. to counting list\n"
		<<"4. to print elements\n"<<"5. to return top\n"<<"6. to ensure if stack is full\n"
		<<"7. to ensure if stack is empty\n";

	cout<<"want to try (y/n)\n";    cin>>answer;

	while(answer=='Y'||answer=='y')	{
		cout<<"Enter your choise:\n"; cin>>choice;

	switch(choice)	{ 
	case 1:	cout<<"Enter the value to push:\n";
			cin>>value;
			push(stack,&top,value);
		    break;
	case 2: value=pop(stack,&top);
			if(value!=-1)
				cout<<"the value poped is "<<value<<endl;
			else
				cout<<"stack is empty\n";
		   break;
	default:cout<<"invalide choise\n";
		}
	cout<<"more ?(y/n)\n";	cin>>answer;	
	}/*end while */   
}
#2

يبدو من هذا الكود انه ليس الكود الكامل لاجراء كافة العمليات على المتراصة. فأنه يدعم علميتى الادخال والحذف(الاخراج) من المتراصة

الكود بالشكل التالى يعمل . لكن تذكر عليك بمواصلة استكمال التمرين . والتأكد من مخرجاته باستكمال باقى العمليات على المتراصة

#include<iostream>
using namespace std;
const int MAX = 5;

void push(int stack[], int *top, int value)
{
    if (*top < MAX - 1)
    {
        *top = *top + 1;
        stack[*top] = value;        
    }
    else
        cout << "stack is full\n";
}

int pop(int stack[], int *top)
{
    int value;
    if (*top > -1)
    {
        value = stack[*top];
        *top = *top - 1;
        return value;
    }

    else
        return -1;
}

int main()
{
    int stack[MAX], top = -1, value, choice;
    string answer;

    cout << "1. to push element\n"
            << "2. to pop element\n"
            << "3. to counting list\n"
            << "4. to print elements\n"
            << "5. to return top\n"
            << "6. to ensure if stack is full\n"
            << "7. to ensure if stack is empty\n";


    cout << "want to try (y/n): ";
    cin >> answer;

    while (answer == "Y" || answer == "y")
    {
        cout << "Enter your choise: ";
        cin >> choice;

        switch (choice)
        {
        case 1: cout << "Enter the value to push: ";
            cin >> value;
            push(stack, &top, value);
            break;
        case 2: value = pop(stack, &top);
            if (value != -1)
                cout << "the value poped is " << value << endl;
            else
                cout << "stack is empty.\n";
            break;
        default:cout << "invalide choise\n";
        }
        cout << "more ?(y/n): ";
        cin >> answer;
    }/*end while */

    return 0;
}

المخرجات:

1. to push element
2. to pop element
3. to counting list
4. to print elements
5. to return top
6. to ensure if stack is full
7. to ensure if stack is empty
want to try (y/n): y
Enter your choise: 1
Enter the value to push: 7
more ?(y/n): y
Enter your choise: 4
invalide choise
more ?(y/n): y
Enter your choise: 1
Enter the value to push: 777
more ?(y/n): y
Enter your choise: 2
the value poped is 777
more ?(y/n): n
Press [Enter] to close the terminal ...

تحياتى العطرة والله ولى التوفيق

تم تعديل هذه المشاركة بواسطة AudaNix في 8 أبريل 2012 في 07:49

4

post-15367-027053900%201340345097.gifpost-15367-087183200%201340344597.gif
post-247365-0-10080400-1379403554_thumb.

#3

نعم اريد اكماله جزاك الله كل خير

الخطا الذي ارتكبته هو تعريف answer كـ int بدلا من char

مواضيع مشابهة