Membuat Duplikat Facebook (Fokus Bagian Akun & Posting)
C Facebook Syntax
Create syntax in C about facebook
Here's a simple example of a Facebook-like system in C, focusing on user profiles and posting updates:
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define a structure for a post
typedef struct Post {
char content[256];
struct Post* next;
} Post;
// Define a structure for a user
typedef struct User {
char name[50];
Post* posts;
struct User* next;
} User;
// Function to create a new user
User* createUser(char* name) {
User* user = (User*)malloc(sizeof(User));
strcpy(user->name, name);
user->posts = NULL;
user->next = NULL;
return user;
}
// Function to create a new post
Post* createPost(char* content) {
Post* post = (Post*)malloc(sizeof(Post));
strcpy(post->content, content);
post->next = NULL;
return post;
}
// Function to add a post to a user's profile
void addPost(User* user, char* content) {
Post* post = createPost(content);
if (user->posts == NULL) {
user->posts = post;
} else {
Post* current = user->posts;
while (current->next != NULL) {
current = current->next;
}
current->next = post;
}
}
// Function to display a user's profile
void displayProfile(User* user) {
printf("Profile of %s:\n", user->name);
Post* current = user->posts;
int i = 1;
while (current != NULL) {
printf("Post %d: %s\n", i, current->content);
current = current->next;
i++;
}
}
int main() {
// Create users
User* user1 = createUser("John Doe");
User* user2 = createUser("Jane Doe");
// Add posts
addPost(user1, "Hello, world!");
addPost(user1, "This is my second post.");
addPost(user2, "Hi, everyone!");
// Display profiles
displayProfile(user1);
printf("\n");
displayProfile(user2);
return 0;
}
This code defines two structures: Post and User. Each User has a name and a linked list of Posts. The createUser and createPost functions allocate memory for new users and posts, respectively. The addPost function adds a new post to a user's profile, and the displayProfile function prints out a user's profile, including all their posts.
Note: This is a very basic example and doesn't include any error checking or memory deallocation, which you would want to add in a real application.
Thank You 😊
Comments
Post a Comment