รวมโค๊ดภาษาซี

Control statements

คำนวณผลรวมของ 1-10
รับตัวเลขจากผู้ใช้ แล้วตรวจสอบว่าเป็นเลขคู่ (แสดงคำว่า Even ) หรือเลขคี่ (แสดงคำว่า Odd)
ฟังก์ชันหาค่าเฉลี่ยของเลขจำนวนเต็ม 2 จำนวน (คืนค่าเป็นทศนิยม)
คำนวณหาพื้นที่ของสามเหลี่ยม โดยรับค่าความสูงของสามเหลี่ยม (Height) และค่าของฐานสามเหลี่ยม (Base)
คำนวณหาพื้นที่ของวงกลม โดยรับค่ารัศมี (Radius) และกำหนดให้ค่าพายเท่ากับ 3.14
โปรแกรมรับตัวเลขทางแป้นพิมพ์เป็นเลขฐานสิบ (0-7) จากนั้นแสดงผลเป็นเลขฐานสองแบบสามหลัก
พิมพ์แม่สูตรคูณ แม่ 2 ถึง 25
ตัดเกรด A – F ด้วยคำสั่ง if
ตัดเกรด A – F ด้วยคำสั่ง switch … case
รับวันที่ (1-30) ของเดือนกันยายน พ.ศ. 2558 แล้วแสดงวัน (อาทิตย์ จันทร์ อังคาร พุธ พฤหัส ศุกร์ เสาร์)
Display Character from A to Z Using Loop
checking is Prime or not
Prime Numbers Between two Interval number
Fibonacci series
Floyd’s Triangle
Display all factors of a number

Recursive

ผลรวม
Factorial
Fibonacci at n (terms)

Other

Display its own source code

Continue reading

Posted in C

Linked List Class

#include <iostream>

using namespace std;

class linklist
{
private:

struct node
{
int data;
node *link;
}*p;

public:

linklist();
void append( int num );
void add_as_first( int num );
void addafter( int c, int num );
void del( int num );
void display();
int count();
~linklist();
};

linklist::linklist()
{
p=NULL;
}

void linklist::append(int num)
{
node *q,*t;

if( p == NULL )
{
p = new node;
p->data = num;
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;

t = new node;
t->data = num;
t->link = NULL;
q->link = t;
}
}

void linklist::add_as_first(int num)
{
node *q;

q = new node;
q->data = num;
q->link = p;
p = q;
}

void linklist::addafter( int c, int num)
{
node *q,*t;
int i;
for(i=0,q=p;i<c;i++)
{
q = q->link;
if( q == NULL )
{
cout<<“\nThere are less than “<<c<<” elements.”;
return;
}
}

t = new node;
t->data = num;
t->link = q->link;
q->link = t;
}

void linklist::del( int num )
{
node *q,*r;
q = p;
if( q->data == num )
{
p = q->link;
delete q;
return;
}

r = q;
while( q!=NULL )
{
if( q->data == num )
{
r->link = q->link;
delete q;
return;
}

r = q;
q = q->link;
}
cout<<“\nElement “<<num<<” not Found.”;
}

void linklist::display()
{
node *q;
cout<<endl;

for( q = p ; q != NULL ; q = q->link )
cout<<endl<<q->data;

}

int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;

return c;
}

linklist::~linklist()
{
node *q;
if( p == NULL )
return;

while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}

int main()
{
linklist ll;
cout<<“No. of elements = “<<ll.count();
ll.append(12);
ll.append(13);
ll.append(23);
ll.append(43);
ll.append(44);
ll.append(50);

ll.add_as_first(2);
ll.add_as_first(1);

ll.addafter(3,333);
ll.addafter(6,666);

ll.display();
cout<<“\nNo. of elements = “<<ll.count();

ll.del(333);
ll.del(12);
ll.del(98);
cout<<“\nNo. of elements = “<<ll.count();
return 0;
}
ที่มา: dreamincode.net/
เพิ่มเติม: How to create Linked list using C/C++

SWATH (Smart Word Analysis for Thai)

————-
SWATH (Smart Word Analysis for THai)
————-
ผู้พัฒนาเวอร์ชั่นแรก

ผู้นำมาพัฒนาต่อ (ตอนนี้เวอร์ชั่น 0.4.1)

Windows Version

————-
libdatrie
————-

อื่นๆ

S_ISDIR

S_ISDIR
S_ISDIR checks the file mode m to see whether the file is a directory. If so it returns True
freepascal.org

#ifndef S_ISDIR
#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
#endif

#ifndef S_ISREG
#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
#endif

mail.python.org

#ifndef S_ISDIR
#define S_ISDIR(mode)  (((mode) & S_IFMT) == S_IFDIR)
#endif

#ifndef S_ISREG
#define S_ISREG(mode)  (((mode) & S_IFMT) == S_IFREG)
#endif

linuxquestions.org

How to mix C and C++ ใน VC2005

เมื่อนำ code ภาษา c (ไฟล์ .c) มารัน อยู่ใน Project เดียวกันกับ .cpp แล้วเกิด Error ดังนี้
/Ycstdafx.h command-line option was not found in the source file
ให้แก้ไขด้วยการเพิ่ม #include “stdafx.h” ในไฟล์ .c
จากนั้นไปที่ Project > Properties > Configuration Properties > C/C++ > Precomplied Headers > Create/Use Precomplied Header
ให้เลือก Use Precomplied Header (/Yu)
เป็นอันเสร็จ

QR Code: in Python

libpng12-0
libpng12-dev
pkg-config

Install libqrencode
http://fukuchi.org/works/qrencode/index.en.html
Libqrencode is a C library for encoding data in a QR Code symbol, a kind of 2D symbology that can be scanned by handy terminals such as a mobile phone with CCD. The capacity of QR Code is up to 7000 digits or 4000 characters, and is highly robust.
Download: http://fukuchi.org/works/qrencode/qrencode-3.1.1.tar.gz

Compile & install
./configure
make
make install

Usage
qrencode -h

Install pyqrencode
http://github.com/bitly/pyqrencode
Python bindings for libqrencode

Download
http://github.com/bitly/pyqrencode/archives/master

Install
python setup.py install

Usage
python test_qr.py

Problem
$ python test_qr.py
Traceback (most recent call last):
File “test_qr.py”, line 1, in <module>
from qrencode import Encoder
ImportError: libqrencode.so.3: cannot open shared object file: No such file or directory
Solution
$ export LD_LIBRARY_PATH=/usr/local/lib

QueryPerformanceCounter()

ฟังก์ชั่น QueryPerformanceCounter() ใช้จับเวลาการทำงาน เป็นฟังก์ชั่นที่มีใน Windows ส่วนใน Linux ต้องเขียนฟังก์ชั่นนี้เอง โดย Code แสดงดังข้างล่างนี้
This function basically recreates the win32 QueryPerformanceCounter() on Linux, play with this code:

[code lang=”c”]
#include <sys/time.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <assert.h>

/* Helpful conversion constants. */
static const unsigned usec_per_sec = 1000000;
static const unsigned usec_per_msec = 1000;

/* These functions are written to match the win32
signatures and behavior as closely as possible.
*/
bool QueryPerformanceFrequency(int64_t *frequency)
{
/* Sanity check. */
assert(frequency != NULL);

/* gettimeofday reports to microsecond accuracy. */
*frequency = usec_per_sec;

return true;
}

bool QueryPerformanceCounter(int64_t *performance_count)
{
struct timeval time;

/* Sanity check. */
assert(performance_count != NULL);

/* Grab the current time. */
gettimeofday(&time, NULL);
*performance_count = time.tv_usec + /* Microseconds. */
time.tv_sec * usec_per_sec; /* Seconds. */

return true;
}
[/code]
ที่มา: TIGForums

C language – compare file

บอกตำแหน่งเริ่มต้นที่ไฟล์ทั้ง 2 ไฟล์ แตกต่างกัน
[code lang=”c”]
//Compare File
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
FILE *fp1, *fp2;
char ch1, ch2, same;
unsigned long l;

if (argc != 3) {
printf("Usage: compare <file 1> <file 2>\n");
exit(1);
}

/* open first file */
if ((fp1 = fopen(argv[1], "rb")) == NULL) {
printf("Cannot open first file.\n");
exit(1);
}

/* open second file */
if ((fp2 = fopen(argv[2], "rb")) == NULL) {
printf("Cannot open second file.\n");
exit(1);
}

l = 0;
same = 1;
/* compare the files */
while (!feof(fp1)) {
ch1 = fgetc(fp1);
if (ferror(fp1)) {
printf("Error reading first file.\n");
exit(1);
}
ch2 = fgetc(fp2);
if (ferror(fp2)) {
printf("Error reading second file.\n");
exit(1);
}
if (ch1 != ch2) {
printf("Files differ at byte number %lu", l);
same = 0;
break;
}
l++;
}
if (same)
printf("Files are the same.\n");

if (fclose(fp1) == EOF) {
printf("Error closing first file.\n");
exit(1);
}

if (fclose(fp2) == EOF) {
printf("Error closing second file.\n");
exit(1);
}

return 0;
}
[/code]

Posted in C