Hay every one
Hay everyone I need help
I have project in subject programming languages about make one idea on two languages c and java .
I have made it in java and c++ but I couldn't in c

This is the code in C++

// Link.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdafx.h"
#include "iostream"
#include "string"

using namespace std;
class node
{
    public:
        int value;            
        string bookName;
        string status;
        node *next;          //pointer to next node 
        node *prev;          //pointer to previous node 
};

class dlist
{
    public:
        node *front;       //pointer to front of list   
        node *back;        //pointer to back of list  

    dlist()
    {
        front=NULL;
        back=NULL;
    }

    void insertFront(node *value);             
    void insertBack(node *);
    void removeFront();
    void removeBack();
    void insertBefore(node *value,node *nodeB);
    void insertAfter(node *n,node *nodeA);
    void removeBefore(node *nodeB);
    void removeAfter(node *nodeA);
    void removeNode(node *newNode);
    void printDListFront();
    void printDListBack();

    node* findNode(int value){
        node *newNode;
        newNode=new node();
        newNode=front;
        while((newNode->value!=value)&&(newNode->next!=NULL)){
            newNode=newNode->next;
        }
        return newNode;
    }
};

//insert a node before nodeB
void dlist::insertBefore(node *value,node *nodeB)    
{
    node *newNode;
    newNode=new node();
    newNode->prev=nodeB->prev;
    newNode->next =nodeB;
    newNode->value =value->value;
    newNode->bookName=value->bookName;
    newNode->status=value->status;

    if(nodeB->prev==NULL)
    {
        this->front=newNode; 
    }
    nodeB->prev=newNode;
}

//insert a node before the front node 
void dlist::insertFront (node *value)
{
    node *newNode;
    if(this->front==NULL)
    {
        newNode=new node();
        this->front=newNode;
        this->back =newNode;
        newNode->prev=NULL;
        newNode->next=NULL;
        newNode->value=value->value;
        newNode->bookName=value->bookName;
        newNode->status=value->status;
    }
    else
    {
        insertBefore(value,this->front );
    }
}

//insert a node after  nodeB
void dlist::insertAfter(node *n,node *nodeB)
{
    node *newNode;
    newNode=new node();
    newNode->next= nodeB->next ;
    newNode->prev  =nodeB;
    newNode->value =n->value;
    newNode->bookName =n->bookName;
    newNode->status =n->status;
    if(nodeB->next==NULL)
    {
        this->back =newNode; 
    }
    nodeB->next=newNode;
}
//insert a node after the last node 
void dlist::insertBack (node *value)
{          
    if(this->back==NULL)
    {
        cout<<"insert at back";
        insertFront(value);
    }
    else
    {
        cout<<"insert at back";
        insertAfter(value,this->back  );
    }
}


//remove the front node 
void dlist::removeFront ()
{
    removeNode(this->front);
}

//remove a back node 
void dlist::removeBack  ()
{
    removeNode(this->back);
}

//remove before a node 
void dlist::removeBefore(node *nodeB)
{
    if(nodeB->prev==this->front)
    {
        this->front=nodeB;
        this->front->prev=NULL;
    }
    else
    {
        removeNode(nodeB->prev);
    }
}

//remove after a node 
void dlist::removeAfter(node *nodeA)
{
    if(nodeA->next==this->back)
    {
        this->back=nodeA;
        this->back->next=NULL;
    }
    else
    {
        removeNode(nodeA->next);
    }
}

//remove a perticular node 
void dlist::removeNode(node *nodeToRemove)
{
    if(nodeToRemove==this->front)
    {
        this->front=this->front->next;
        this->front->prev=NULL;
    }
    else if (nodeToRemove==this->back)
    {
        this->back=this->back->prev;
        this->back->next=NULL ;
    }
    else
    {
        nodeToRemove->prev->next=nodeToRemove->next;
        nodeToRemove->next->prev=nodeToRemove->prev;
    }
}

//Print the list from front 
void dlist::printDListFront()
{
    node* curr2;
    curr2= this->front;

    while(curr2!=NULL)
    {
        cout<<curr2->value<<"\t ";
        cout.width(45);
        cout<<curr2->bookName;
        cout<<"\t "<<curr2->status<<"\n";
        curr2=curr2->next;
    }
    cout<<endl;
}


void dlist::printDListBack()
{
    node* curr2;
    curr2= this->back;
    
    while(curr2!=NULL)
    {
        cout<<curr2->value<<" "<<curr2->bookName<<" "<<curr2->status;
        curr2=curr2->prev;
    }
    cout<<endl;
}

void main()
{
    int user,choice,id;
    node *newBook;
    newBook=new node();
    dlist *st ;
    st= new dlist();
    newBook->bookName="Algorithms and Complexity ";
    newBook->value=1;
    newBook->status="Available";
    st->insertBack(newBook);
    newBook->bookName="The Assembly Language Database ";
    newBook->value=2;
    newBook->status="Available";
    st->insertBack(newBook);
    newBook->bookName="C++ Coding Standard ";
    newBook->value=3;
    newBook->status="Available";
    st->insertBack(newBook);
    newBook->bookName="Introduction To OOP Using C++ ";
    newBook->value=4;
    newBook->status="Available";
    st->insertBack(newBook);
    newBook->bookName="CGI Programming on the World Wide Web";
    newBook->value=5;
    newBook->status="Available";
    st->insertBack(newBook);
    newBook->bookName="MySQL Reference Manual";
    newBook->value=6;
    newBook->status="Available";
    st->insertBack(newBook);
    newBook->bookName="Practical PHP Programming";
    newBook->value=7;
    newBook->status="Available";
    st->insertBack(newBook);
    newBook->bookName="Comparison of Different SQL Implementations";
    newBook->value=8;
    newBook->status="Available";
    st->insertBack(newBook);
    newBook->bookName="Introducing Visual Basic 2005 for Developers";
    newBook->value=9;
    newBook->status="Available";
    st->insertBack(newBook);
    newBook->bookName="Introduction To Structured Query Language ";
    newBook->value=10;
    newBook->status="Available";
    st->insertBack(newBook);
    
    cout<<"Welcome to the Library System...!\n\n"<<endl;
    cout<<"If you are a librarian press 1 or student&teacher press 2:\nif you want to exit press 0..\n"<<endl;
    cin>>user;
    while(user!=0){
        if(user==1){
            cout<<"Enter the number of your choice from the following:\n";
            cout<<"  1- show list of books.\n";
            cout<<"  2- Add new book.\n";
            cout<<"  3- Remove book from list.\n";
            cout<<"  4- Borrow book.\n";
            cout<<"  5- Return book.\n\n";
            cout<<"  0- Exit...\n\n";
            cin>>choice;
            while(choice!=0){
                switch(choice){
                    case 1:

                        cout<<"BookID  \t Book Name  \t  Availability\n"<<endl;
                        cout<<"------------------------------------------------------\n"<<endl;
                        st->printDListFront();
                        break;
                    case 2:
                        cout<<"Enter Book Id:\n";
                        cin>>newBook->value;
                        cout<<"Enter Book Name:\n";
                        cin>>newBook->bookName;
                        newBook->status="Available";
                        st->insertAfter(newBook,st->findNode((newBook->value-1)));
                        break;
                    case 3:
                        cout<<"Enter Book's ID you want to remove\n\n";
                        cin>>id;
                        st->removeNode(st->findNode(id));
                        break;
                    case 4:
                        cout<<"Enter Id of the book:\n\n";
                        cin>>id;
                        if(st->findNode(id)->status=="Available"){
                            st->findNode(id)->status="Not Available";
                            cout<<"Done....\n\n";
                        }else
                            cout<<"Soory..the book you choosed is Not Available...\n\n";
                        break;
                    case 5:
                        cout<<"Enter Id of the book:\n\n";
                        cin>>id;
                        st->findNode(id)->status="Available";
                        cout<<"Done....\n\n";
                        break;
                    case 0:
                        choice=0;
                        break;
                }
                cout<<"Enter the number of your choice from the following:\n";
                cout<<"  1- show list of books.\n";
                cout<<"  2- Add new book.\n";
                cout<<"  3- Remove book from list.\n";
                cout<<"  4- Borrow book.\n";
                cout<<"  5- Return book.\n\n";
                cout<<"  0- Exit...\n\n";
                cin>>choice;
            }
        }
        else if(user==2){
            cout<<"Enter the number of your choice from the following:\n";
            cout<<"  1- show list of books.\n";
            cout<<"  2- show book availability.\n";
            cout<<"  0- Exit...\n";
            cin>>choice;
            while(choice!=0){

                switch(choice){
                    case 1:
                        cout<<"BookID  \t Book Name  \t  Availability\n"<<endl;
                        cout<<"------------------------------------------------------\n"<<endl;
                        st->printDListFront();
                        break;
                    case 2:
                        cout<<"Enter Id of the book:\n\n";
                        cin>>id;
                        cout<<"The book is "<<st->findNode(id)->status<<"...\n\n";
                        break;
                    case 0:
                        choice=0;
                        break;
                }
                cout<<"Enter the number of your choice from the following:\n";
                cout<<"  1- show list of books.\n";
                cout<<"  2- show book availability.\n";
                cout<<"  0- Exit...\n";
                cin>>choice;
            }
        }
    
    cout<<"Welcome to the Library System...!\n\n"<<endl;
    cout<<"If you are a librarian press 1 or student&teacher press 2:\nif you want to exit press 0..\n"<<endl;
    cin>>user;
    choice=0;
    }
}

I did it on c++
but the requirement in c not in c++
could someone help me and make it in c or explain how I can make the change
thanks for you

why did you include stdafx.h? That's only needed for MFC programs.

Why did you write it in c++ if the requirement was to write it in C? To convert to C you have to

  • change the header files, include stdio.h and string.h. Others may also be needed depending on the program.
  • replace class with struct,
  • remove all class methods and access specifiers,
  • remove scope indicators all functions such as void dlist::printDListBack() renamed to void printDListBack() ,
  • pass class objects as parmeters to those functions
  • replace cout with printf()
  • replace cin with scanf() or getline(), depending on what it is getting from the keyboard
  • replace void main() with int main()

The above may not be an exhaustive list. Its going to take a lot of work on your part to convert that program. Good luck :)

Edited 4 Years Ago by Ancient Dragon: n/a

Why did you write it in c++ if the requirement was to write it in C?

My guess is he found the code on the web and didn't write a line of it...

my guess keep your guess for your self , and if u find it on any web come and make your guess

Comments
There's no "namespace std" in Java. Tool!

This thread, or actually the other one, gave me a chuckle. It doesn't set the new record for chutzpah, but it ranks up there. Apparently it's not just a cross-post, it's a quadruple post (here, cprogramming, Stack Overflow, bytes.com). I almost want to up-vote it for the brazenness. Nah, down-vote it is, although what are the odds he cares? We need to bring back the good old days where one down-vote made someone red.

This article has been dead for over six months. Start a new discussion instead.