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

ما الخطأ في الكود ؟؟؟!

بدأه العسولة في 16 مارس 2012 · 2 رد · 429 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع



اخواني اتمنى الرد في اقرب وقت ،، عندي سؤال وحليته بس النواتج مب صحيحة ،، اذا تسمحوا مساعدتي في اعطائي نتيجة صحيحة ،،

هذا السؤال مع الكود


Write a program in C++ to do the following. create a stack of strings of size 10.
create a queue of strings of size 10.
(i) insert 10 students' names.
(ii) print the size of the queue.
(iii) delete the names from the queue. if the deleted name starts with the letter 'a' or 'A' then push the name into the stack of strings. repeat action (iii) until the queue becomes empty.
(iv) print the size of the stack.
(v) print all the names from the stack with the usual stack operations.




كود الحل

#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<stdlib.h>
#include<cstdlib>
using namespace std;
int main()
{
    string s[10];
    stack<string> ss;
    queue<string> stq;
    int count=0;
    cout<<"Enter 10 students Name:"<< endl;
    for(int i=0; i<10; i++)
    {
        cin>>s;
        stq.push(s);
        count++;
    }
    cout<<"The size of the queue="<<stq.size()<<endl;
    int count2=0;
    while(!stq.empty())
    {

        string x=stq.front();

        //if(x=="a" || x=="A")
          if(x.substr(1)=="a" || x.substr(1)=="A")
            ss.push(x);
        stq.pop();

    }
    cout<<"The size of the stack: "<< ss.size()<<endl;
    cout<<"The Names in the stack: "<<endl;
    while(!ss.empty())
    {
        cout<<endl<<ss.top();
        ss.pop();

    }
    while(!stq.empty())
    {
        stq.front();
        stq.pop();
    }
    cout<<endl<<"The size of the queue: "<<stq.size();
    return 0;
}
السلام عليكم
#2

أختى:

يمكنك مقارنة حرف بحرف أخر فى سلسلة نصية عن طريق:

string str1 = "Mohammed";
    if(str1.substr(0,1) == "M" )
    ;
    // or
    if(str1[0] == 'M')
    ;

الكود بعد التعديل:

/* 
 * File:   main.cpp
 * Author: Mohammed Auda
 * * Created on March 16, 2012, 8:02 AM
 */
/*
Write a program in C++ to do the following. 
create a stack of strings of size 10.
create a queue of strings of size 10.
(i) insert 10 students' names.
(ii) print the size of the queue.
(iii) delete the names from the queue. 
      if the deleted name starts with the letter 'a' or 'A' 
 *    then push the name into the stack of strings. 
 *    repeat action (iii) until the queue becomes empty.
(iv) print the size of the stack.
(v) print all the names from the stack with the usual stack operations.
 */

#include<iostream>
#include<string>
#include<stack>
#include<queue>
//#include<cstdlib>
using namespace std;

int main()
{
    string sName;
    int count = 0;

    stack<string> sstack;
    queue<string> qqueue;

    cout << "Enter 10 students Name: " << endl;
    for (int i = 0; i < 10; i++)
    {
        cout << "Name " << i + 1 << " :";
        cin >> sName;
        qqueue.push(sName);
    }

    cout << "Q 1 size: " << qqueue.size() << endl;
    while (!qqueue.empty())
    {
        string x = qqueue.front();
        if (x[0] == 'a' || x[0] == 'A')
            sstack.push(x);
        qqueue.pop();
    }

    cout << "The size of the stack: " << sstack.size() << endl;
    cout << "The Names in the stack: " << endl;

    while (!sstack.empty())
    {
        cout << "Name " << count + 1 << " :";
        cout << sstack.top() << endl;
        sstack.pop();
        count++;
    }

    return 0;
}

المخرجات:

Enter 10 students Name: 
Name 1 :Mohammed
Name 2 :Auda
Name 3 :AbuZayed
Name 4 :Try  
Name 5 :To
Name 6 :Solve
Name 7 :Code
Name 8 :bug
Name 9 :AudaNix
Name 10 :Caster
Q 1 size: 10
The size of the stack: 3
The Names in the stack: 
Name 1 :AudaNix
Name 2 :AbuZayed
Name 3 :Auda
Press [Enter] to close the terminal ...

تقبلى مرورى والله ولى التوفيق

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

#3

جزاك ربي كل خير أخي ،،،

وفقك البارئ لما يحب ويرضى

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