<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Social Networking Service</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
padding: 20px;
}
.card {
margin-bottom: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-center mb-4">Social Networking Service</h1>
<!-- ログインフォーム -->
<div id="loginForm" class="card w-50 mx-auto">
<div class="card-body">
<h2 class="card-title text-center mb-4">Login</h2>
<div class="form-group">
<input type="text" id="loginUsername" class="form-control" placeholder="Username">
</div>
<div class="form-group">
<input type="password" id="loginPassword" class="form-control" placeholder="Password">
</div>
<button onclick="login()" class="btn btn-primary btn-block">Login</button>
<button onclick="registerForm()" class="btn btn-secondary btn-block">Register</button>
</div>
</div>
<!-- 登録フォーム -->
<div id="registerForm" class="card w-50 mx-auto" style="display: none;">
<div class="card-body">
<h2 class="card-title text-center mb-4">Register</h2>
<div class="form-group">
<input type="text" id="registerName" class="form-control" placeholder="Full Name">
</div>
<div class="form-group">
<input type="text" id="registerUsername" class="form-control" placeholder="Username">
</div>
<div class="form-group">
<input type="password" id="registerPassword" class="form-control" placeholder="Password">
</div>
<button onclick="register()" class="btn btn-primary btn-block">Register</button>
<button onclick="loginForm()" class="btn btn-secondary btn-block">Back to Login</button>
</div>
</div>
<!-- プロフィール -->
<div id="profile" class="card w-50 mx-auto" style="display: none;">
<div class="card-body">
<h2 class="card-title text-center mb-4">Profile</h2>
<p><strong>Name:</strong> <span id="profileName"></span></p>
<p><strong>Username:</strong> <span id="profileUsername"></span></p>
<button onclick="logout()" class="btn btn-danger btn-block">Logout</button>
</div>
</div>
<!-- 投稿フォーム -->
<div id="postForm" class="card w-75 mx-auto" style="display: none;">
<div class="card-body">
<h2 class="card-title text-center mb-4">Create Post</h2>
<div class="form-group">
<textarea id="postContent" class="form-control" rows="3" placeholder="What's on your mind?"></textarea>
</div>
<button onclick="createPost()" class="btn btn-primary btn-block">Post</button>
</div>
</div>
<!-- 投稿一覧 -->
<div id="postList" class="w-75 mx-auto mt-4"></div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
let currentUser = null; // 現在のログインユーザー
let users = []; // ユーザーの配列
let posts = []; // 投稿の配列
function login() {
const username = document.getElementById('loginUsername').value;
const password = document.getElementById('loginPassword').value;
const user = users.find(u => u.username === username && u.password === password);
if (user) {
currentUser = user;
showProfile();
} else {
alert('Invalid username or password.');
}
}
function logout() {
currentUser = null;
hideAll();
document.getElementById('loginForm').style.display = 'block';
}
function registerForm() {
hideAll();
document.getElementById('registerForm').style.display = 'block';
}
function register() {
const name = document.getElementById('registerName').value;
const username = document.getElementById('registerUsername').value;
const password = document.getElementById('registerPassword').value;
users.push({ name, username, password });
alert('Registration successful! Please login.');
loginForm();
}
function loginForm() {
hideAll();
document.getElementById('loginForm').style.display = 'block';
}
function showProfile() {
hideAll();
document.getElementById('profile').style.display = 'block';
document.getElementById('profileName').textContent = currentUser.name;
document.getElementById('profileUsername').textContent = currentUser.username;
document.getElementById('postForm').style.display = 'block';
displayPosts();
}
function createPost() {
const postContent = document.getElementById('postContent').value;
if (postContent.trim() !== '') {
const post = {
id: Date.now(),
content: postContent,
author: currentUser.name,
authorUsername: currentUser.username,
likes: 0,
comments: []
};
posts.unshift(post); // 最新の投稿を先頭に追加
displayPosts();
document.getElementById('postContent').value = ''; // 投稿後、入力欄を空にする
}
}
function displayPosts() {
const postList = document.getElementById('postList');
postList.innerHTML = '';
posts.forEach(post => {
const postElement = document.createElement('div');
postElement.innerHTML = `
<div class="card mb-3">
<div class="card-body">
<p class="card-text">${post.content}</p>
<small class="text-muted">Posted by ${post.author} (@${post.authorUsername}) at ${new Date(post.id).toLocaleString()}</small><br>
<button onclick="likePost(${post.id})" class="btn btn-primary btn-sm mt-2">Like (${post.likes})</button>
<button onclick="showComments(${post.id})" class="btn btn-secondary btn-sm mt-2">Comments</button>
</div>
</div>
`;
postList.appendChild(postElement);
});
}
function likePost(postId) {
const post = posts.find(p => p.id === postId);
post.likes++;
displayPosts();
}
function showComments(postId) {
const post = posts.find(p => p.id === postId);
const comments = prompt('Enter your comment:');
if (comments !== null && comments.trim() !== '') {
post.comments.push({ author: currentUser.name, content: comments });
displayPosts();
}
}
function hideAll() {
document.getElementById('loginForm').style.display = 'none';
document.getElementById('registerForm').style.display = 'none';
document.getElementById('profile').style.display = 'none';
document.getElementById('postForm').style.display = 'none';
}
users.push({ name: 'User One', username: 'user1', password: 'password1' });
users.push({ name: 'User Two', username: 'user2', password: 'password2' });
hideAll();
document.getElementById('loginForm').style.display = 'block';
</script>
</body>
</html>
投稿者: chosuke
Javascript 文字列の整形
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My JavaScript</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>
main.js
'use strict';
{
const string = prompt('Name?');
//if(string.toLowerCase() === 'taro'){
if(string.toUpperCase().trim() === 'TARO'){
console.log('Corrent!');
}else{
console.log('Wrong!');
}
}
ruby メソッドをオーバーライド
class Score
def initialize(subject, score)
@subject = subject
@score = score
end
def get_info
"#{@subject}/#{@score} -> #{get_result}"
end
private
def get_result
@score >= 80 ? "Pass" : "Fail"
end
end
class MathScore < Score
def initialize(score)
super("Math", score)
end
private
# ovierride
def get_result
@score >= 50 ? "Pass" : "Fail"
end
end
class EnglishScore < Score
def initialize(score)
super("English", score)
end
end
class User
def initialize(name, score)
@name = name
@score = score
end
def get_info
"Name: #{@name}, Score: #{@score.get_info}"
end
end
user1 = User.new("Taro", MathScore.new(70))
user2 = User.new("Jiro", EnglishScore.new(90))
puts user1.get_info
puts user2.get_info
ruby 子クラス
class Score
def initialize(subject, score)
@subject = subject
@score = score
end
def get_info
"#{@subject}/#{@score} -> #{get_result}"
end
private
def get_result
@score >=80 ? "Pass" : "Fail"
end
end
class MathScore < Score
def initialize(score)
super("Math", score)
end
end
class EnglishScore < Score
def initialize(score)
super("Math", score)
end
end
class User
def initialize(name, score)
@name = name
@score = score
end
def get_info
"Name: #{@name}, Score: #{@score.get_info}"
end
end
user1 = User.new("Taro", MathScore.new(70))
user2 = User.new("Jiro", EnglishScore.new(90))
puts user1.get_info
puts user2.get_info
C# string型とchar型
using System;
class string01
{
public static void Main()
{
char[] chararray = new char[3];
chararray[0] = 'a';
chararray[1] = 'b';
chararray[2] = 'c';
string str;
str = new string(chararray);
Console.WriteLine(str);
char[] title = { '魔', '王', 'と', '勇', '者'};
string strTitle = new string(title);
Console.WriteLine(strTitle);
string strx = "魔王と勇者の伝説";
int n = strx.Length;
Console.WriteLine("「{0}」の文字数は{1}です", strx, n);
char c = strx[1];
Console.WriteLine("「{0}」の2番目の文字は「{1}」です", strx, c);
}
}
python 簡単な会話モデルの構築
from nltk.chat.util import Chat, reflections
pairs = [
[‘こんにちは’, [‘こんにちは!’, ‘どうもこんにちは!’]],
[‘元気ですか?’, [‘はい、元気です!’, ‘お尋ねいただきありがとうございます。’]],
[‘名前は何ですか?’, [‘私はチャットボットです。’, ‘私の名前はチャットボットです。’]],
[‘さようなら’, [‘さようなら!またね。’, ‘良い一日を!’]],
]
chatbot = Chat(pairs, reflections)
def chatbot_response(user_input):
return chatbot.respond(user_input)
ユーザーとの対話
while True:
user_input = input(“あなた: “)
response = chatbot_response(user_input)
print(“チャットボット:”, response)
C# 配列
using System;
class array01
{
public static void Main()
{
int[] Weapon = new int[3];
Weapon[0] = 10;
Weapon[1] = 20;
Weapon[2] = 30;
// 宣言と同時に初期化
int[] myarray2 = new int[3] { 10, 20, 30 };
// 要素数を省略することも可能
int[] myarray3 = new int[] { 10, 20, 30 };
//別な方法
int[] myarray4;
myarray4 = new int[] { 10, 20, 30 };
}
}
ruby メソッドを実装
class Score
def initialize(subject, score)
@subject = subject
@score = score
end
def get_info
"#{@subject}/#{@score}"
end
end
class User
def initialize(name, score)
@name = name
@score = score
end
def get_info
"Name: #{@name}, Score: #{@score.get_info}"
end
end
user1 = User.new("Taro", Score.new("Math", 70))
user2 = User.new("Jiro", Score.new("English", 90))
puts user1.get_info
puts user2.get_info
ruby rssserver
require ‘cgi’
require ‘rss’
require ‘net/http’
def parse(page_source)
dates = page_source.scan(%r!(\d+)年 ?(\d+)月 ?(\d+)日
!).map { |year, month, day| [year.to_i, month.to_i, day.to_i] }
url_titles = page_source.scan(%r!^(.+?)
!).map { |url, title| [CGI.unescapeHTML(url), CGI.unescapeHTML(title)] }
url_titles.zip(dates).map { |(url, title), date| [url, title, Time.local(*date)] }
end
def format_text(title, url, url_title_time_ary)
text = “Title: #{title}\nURL: #{url}\n\n”
url_title_time_ary.each do |aurl, atitle, atime|
text << “* (#{atime})#{atitle}\n”
text << ” #{aurl}\n”
end
text
end
def format_rss(title, url, url_title_time_ary)
RSS::Maker.make(“2.0”) do |maker|
maker.channel.updated = Time.now.to_s
maker.channel.link = url
maker.channel.title = title
maker.channel.description = title
url_title_time_ary.each do |aurl, atitle, atime|
maker.items.new_item do |item|
item.link = aurl
item.title = atitle
item.updated = atime
item.description = atitle
end
end
end
end
uri = URI(“http://crawler.sbcr.jp/samplepage.html”)
response = Net::HTTP.get_response(uri)
parsed = parse(response.body.force_encoding(“UTF-8”))
formatter = case ARGV.first
when “rss-output”
:format_rss
else
:format_text
end
puts send(formatter, “WWW.SBCR.JP トピックス”, “http://crawler.sbcr.jp/samplepage.html”, parsed)
Ruby RSS
require 'rss'
require 'open-uri'
# RSSフィードのURL
rss_url = 'http://2ch-ranking.net/rss/livemarket1.rdf'
# RSSフィードを取得してパース
rss_content = URI.open(rss_url).read
rss = RSS::Parser.parse(rss_content)
# フィードのタイトルと記事を出力
puts "フィードのタイトル: #{rss.channel.title}"
puts "-----"
rss.items.each do |item|
puts "タイトル: #{item.title}"
puts "リンク: #{item.link}"
puts "概要: #{item.description}"
puts "-----"
end
