Thread Closed 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ primjeri
Author Message
Gogy Offline
____
*

Posts: 2,379
Joined: Feb 2006
Post: #11
RE: C++ primjeri
Hm... teško je prosudit sad. Bit objektivan. Pa ako se slažete promijenit ćemo pravila igre.

Početni - 1 bod
Srednji - 2 boda
Napredni - 3 boda
Ekspert - 4 boda

Dakle Schmrz i Puž dobivaju po 2 boda za riješene zadatke.

Ako ni ovo ne smatrate dobrim rješenjem, predložite vi pravila.

Pravila foruma | Twitter - Facebook - Google+ |
04-08-2008 10:23 AM
Visit this user's website Find all posts by this user
Len0 Offline
Forumaš
***

Posts: 303
Joined: Mar 2007
Reputation: 3
Post: #12
RE: C++ primjeri
Code:
#include<iostream>
#include<string>
using namespace std;
int main(int argv, int argc[]){
    string drive;
    while(1){
    cout<<"Unesite putanju to foldera: ";
    string path;
    getline(cin, path, '\n');
    string naredba;
    naredba="dir " + path + "/a-d";
    system(naredba.c_str());
    cout<<"Unesite extenziju i ime fajla kojeg zelite editovati: ";
    string fajl;
    getline(cin, fajl, '\n');
    cout<<"Sta zelite uraditi sa fajlom?\nDelete - 1\nRename - 2\n"<<endl;
    int odl;
    do{cin>>odl;
    string temp;
    if(odl==1){
        temp="del " + path + "\\" + fajl;
        system(temp.c_str());
    }
    else if(odl==2){
        cout<<"Unesite novo ime fajla: ";
        cin.ignore();
        string nime;
        getline(cin,nime,'\n');
        temp="ren " + path + "\\" + fajl + " " + nime;
        system(temp.c_str());
    }
    else cout<<"Izaberite 1 ili 2"<<endl;
    }while(odl != 1 || odl != 2);
    cout<<"************************"<<endl<<endl;
    }
    
    system("pause");
return 0;
}

["hip","hip"]
(hip hip array!)
06-08-2008 12:01 PM
Find all posts by this user
Gogy Offline
____
*

Posts: 2,379
Joined: Feb 2006
Post: #13
RE: C++ primjeri
Hoće li sad netko drugi zadati zadatak?

Pravila foruma | Twitter - Facebook - Google+ |
08-08-2008 12:18 PM
Visit this user's website Find all posts by this user
schmrz Offline
____
*

Posts: 567
Joined: Feb 2007
Post: #14
RE: C++ primjeri
DRUGI ZADATAK

Nivo: Srednji
Zadatak: Napisati dva programa (sender, receiver); Jedan program ce sluziti za slanje fajlove, a drugi za primanje fajlova od prvog programa.

I have no drinking problems. I drink. Get drunk. Fall down. NO PROBLEM
Registered As Linux User #484215
Moj skromni blog
Savjet za buduće programere: ovdje
08-08-2008 01:37 PM
Find all posts by this user
Mr.JA Offline
Novi korisnik
*

Posts: 58
Joined: Jul 2008
Reputation: 0
Post: #15
RE: C++ primjeri
dva programa u jednom ili?
općenito napravit jedan pa drugi?
09-08-2008 12:40 AM
Find all posts by this user
schmrz Offline
____
*

Posts: 567
Joined: Feb 2007
Post: #16
RE: C++ primjeri
Kako god hoces, samo da imas mogucnost da posaljes ali i da primis fajl.

I have no drinking problems. I drink. Get drunk. Fall down. NO PROBLEM
Registered As Linux User #484215
Moj skromni blog
Savjet za buduće programere: ovdje
09-08-2008 11:07 AM
Find all posts by this user
schmrz Offline
____
*

Posts: 567
Joined: Feb 2007
Post: #17
RE: C++ primjeri
Rijesenje za drugi zadatak - prvi dio:

Evo prvi dio zadatka, ako niko ne napise drugi dio (server) za sedmicu dana napisat cu i njega. Ako neko ipak napise moze uzeti sve bodove :)

Komentari (iako ih nema mnogo), nazivi varijabli su pisani na engleskom jeziku jer kada pokusam da to sve pisem na nasem onda na kraju dobijem mijesani koji estetski nije prihvatljiv.

!! Dodajte slijedece reference u opcijama za linker: Ws2_32.lib Mswsock.lib Advapi32.lib
Code:
// Winsock application
// Date: August 25, 2008
// Author:    Amir Hadzic (schmrz)
// Usage: Sending files using winsock.

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>

#define DEFAULT_PORT "1234"
#define DEFAULT_BUF 512



using namespace std;

void reportError(char *e_function, int errorCode);
int sendData(SOCKET *aSocket, char *path);
int recvData(SOCKET *aSocket);

int  main(int argc, char *argv[]){
    WSADATA wsaData;
    int iResult;
    struct addrinfo *result = NULL,
                    *ptr = NULL,
                     hints;

    // argument check
    if (argc < 3) {
        cout << "Usage: " << argv[0] << " server-name path-to-file\n";
        return 1;
    }
    
    // Init winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0){
        reportError("WSAStartup()", iResult);
        WSACleanup();
        return 1;        
    }
    
    
    
    
    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    // Resolve the server address and port
    iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {
        reportError("getaddrinfo()", iResult);
        WSACleanup();
        return 1;
    }

    // create socket
    SOCKET connSocket = INVALID_SOCKET;
    ptr = result;

    connSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
    if (connSocket == INVALID_SOCKET){
        reportError("socket()", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // connect to server
    iResult = connect(connSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    if (iResult == SOCKET_ERROR){
        closesocket(connSocket);
        connSocket = INVALID_SOCKET;
    }

    if (connSocket == INVALID_SOCKET){
        reportError("connect()", WSAGetLastError());
        WSACleanup();
        return 1;
    }
    
    // send data to server
    cout << "Sending data..." << endl;
    iResult = sendData(&connSocket, argv[2]);
    if (iResult != 0){
        closesocket(connSocket);
        WSACleanup();
        return 1;
    }

    // shutdown the sending part of the socket
    iResult = shutdown(connSocket, SD_SEND);
    if (iResult == SOCKET_ERROR){
        reportError("shutdown(send)", WSAGetLastError());
        closesocket(connSocket);
        WSACleanup();
        return 1;
    }

    // receive data
    iResult = recvData(&connSocket);
    if (iResult != 0){
        closesocket(connSocket);
        WSACleanup();
        return 1;
    }

    // close socket
    closesocket(connSocket);


    // cleanup
    WSACleanup();
    return 0;
}

void reportError(char *e_function, int errorCode){
    cout << "Error when calling " << e_function << ". Error code: " << errorCode << endl;
}

int sendData(SOCKET *aSocket, char *path){
    char *datablock;
    ifstream::pos_type size;
    ifstream file(path, ios::in|ios::binary|ios::ate);
    int iResult;
    
    if (file.is_open()){
        size = file.tellg();
        datablock = new char[size];
        
        
        // read file
        file.seekg(ios::beg);
        file.read(datablock, size);
        file.close();
        
    
        iResult = send(*aSocket, datablock, size, 0);    
        if (iResult == SOCKET_ERROR){
            reportError("send()", WSAGetLastError());
            return WSAGetLastError();
        }

        delete datablock;
        cout << "Bytes sent: " << iResult << endl;
    } else
    {
        reportError("is_open()", 1);
        return 1;
    }

    

    
    

    return 0;
}

int recvData(SOCKET *aSocket){
    char recvbuf[DEFAULT_BUF];
    int recvbuf_len = DEFAULT_BUF;
    int iResult;
    int bytes = 0;

    cout << "Receiving: \n";
    do
    {
        iResult = recv(*aSocket, recvbuf, recvbuf_len, 0);
    
        if (iResult > 0){
            bytes = bytes + iResult;
            for (int i = 0; i < strlen(recvbuff), i++){
                cout << recvbuff[i] << endl;
            }
        }
        else if(iResult == 0){
            cout << "\nConnection closed!" << endl;
            cout << "Bytes received: " << bytes << endl;
        }
        else {
            cout << endl;
            reportError("recv()", WSAGetLastError());
            return WSAGetLastError();
        }

    } while (iResult > 0);


    return 0;
}

Ovo je vrlo jednostavan program i moze primiti samo jednu vezu. Ako neko unaprijedi program neka ga ponovo postavi ovdje, mozda dobije i kakvih bodova od gogy-a. :-) A za one koji budu posebno marljivi saljemo cokoladu ili pivu, po izboru ucesnika He he

I have no drinking problems. I drink. Get drunk. Fall down. NO PROBLEM
Registered As Linux User #484215
Moj skromni blog
Savjet za buduće programere: ovdje
25-08-2008 05:44 PM
Find all posts by this user
duwanda Offline
Novi korisnik
*

Posts: 10
Joined: Feb 2009
Reputation: 0
Post: #18
RE: C++ primjeri
wow stvarno si pro...ja sam tek usao u vode preogramiranja i pitam se kako si to sve naucio??ja tek mogu napraviti program za zbrajanje..mnozenje itd...daj mi reci kako si ti ucio
17-02-2009 06:44 PM
Find all posts by this user
schmrz Offline
____
*

Posts: 567
Joined: Feb 2007
Post: #19
RE: C++ primjeri
Pa ne bih bas rekao da sam pro :) Ali hvala na komplimentu :)

Ako si zainteresovan za programiranje i time se zelis baviti vremenom ces nauciti jer ne moze se preko noci. Ja sam ucio iz raznih primjera, clanaka po netu, besplatnih e-knjiga i sl.

Ako ti programiranje nije --glavno-- zanimanje onda predlazem da zanimas sa nekim drugim jezicima za koje treba manje vremena da se savladaju, u potpunosti ili makar neki srednji nivo. Naprimjer Python ili Ruby.

Nemoj da te zavara sto su pocetni primjeri u konzoli i nemoj misliti da su to --samo-- skriptni jezici. I jedan i drugi mogu koristiti vecinu poznatih gui toolkita za izradu grafickih aplikacija.

I have no drinking problems. I drink. Get drunk. Fall down. NO PROBLEM
Registered As Linux User #484215
Moj skromni blog
Savjet za buduće programere: ovdje
17-02-2009 08:19 PM
Find all posts by this user
duwanda Offline
Novi korisnik
*

Posts: 10
Joined: Feb 2009
Reputation: 0
Post: #20
RE: C++ primjeri
da sam ja admin dao bih ti 1000 rep
18-02-2009 09:39 PM
Find all posts by this user
Thread Closed 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)