㈠ 請教,UNIX下c語言的問題,製作一個功能追加的getchar相關的函數mydetchar。信號與時間相關的!

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/time.h>
#include <string.h>

void handler(int sig)
{
if (sig != SIGALRM)
{
printf("Invalid signal %d\n", sig);
}
}

int mygetchar(int wait)
{
char ch;
int size, ret, clear;
struct sigaction act, old;
struct itimerval timer;

if (wait <= 0)
return -3;

act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGALRM, &act, &old);

/* set timer */
memset(&timer, 0, sizeof(timer));
timer.it_value.tv_sec = wait;
setitimer(ITIMER_REAL, &timer, NULL);

size = read(STDIN_FILENO, &ch, sizeof(ch));

clear = 1; /* assume we should disable timer after reading */
if (size == 0) /* eof */
{
ret = -1;
}
else if (size == -1 && errno == EINTR) /* timeout */
{
ret = -2;
clear = 0;
}
else if (size < 0) /* other errors */
{
ret = -3;
}
else
{
ret = ch;
}

if (clear)
{
/* disable the running timer */
timer.it_value.tv_sec = 0;
setitimer(ITIMER_REAL, &timer, NULL);
}
sigaction(SIGALRM, &old, NULL); /* restore sig action */

return ret;
}

int main()
{
int ch;
time_t t;

printf("input a char in 3 seconds\n");

ch = mygetchar(3);

t = time(NULL);

if (ch < 0)
printf("mygetchar returns %d, at %s\n", ch, ctime(&t));
else
printf("get char '%c', at %s\n", ch, ctime(&t));

return 0;
}

㈡ c語言什麼時候誕生的

1972年。
C 語言里的 時間/日期 計算 起點,有個 unix timestamp(unix 時間圖章)是 Jan 1, 1970 UTC。所以 c 語言發明時間,應當是 1970年1月1日以後。

1969-1973年在美國電話電報公司(AT&T)貝爾實驗室開始了C語言的最初研發。根據C語言的發明者丹尼斯·里奇 (Dennis Ritchie) 說,C 語言最重要的研發時期是在1972年。

細節: 最初的Unix是用匯編語言編寫的,一些應用是由叫做B語言的解釋型語言和匯編語言混合編寫的,在移植的過程中遇到不少麻煩。早在對Multics項目調整過程中,就迫切需要一門高級計算機語言做工具,為了解決程序的可移植性問題,肯·湯普遜和丹尼斯·里奇決定對它進行簡單改進,形成 「New B」 語言。但 New B 在Unix的移植方面依然不盡人意,此後里奇又對 New B 語言做了改進,C語言誕生。

㈢ 那個誰用C語言寫UNIX用了多長時間

不是吧 還有這樣的人材了?

㈣ C語言(unix)

//---------------------------------------------------------------------------

/*
* Program to parse a given sentence into words.
*/

#include <stdio.h>
#include <string.h>

// This function takes a character and a string
// as input and computes if the character is in
// the string or not. Returns 1 if yes, 0 otherwise.
int is_char_in_string(char c, char *s)
{
while (*s != '\0')
{
if (c == *s) return 1;
s++;
}
return 0;
}

main()
{
char line[255];
char delim[10] = " .,;!?"; // set of delimiting characters that can separate two words
int i;
int length;
int count=0;

// Get a line from user as input
printf("Enter a sentence: ");
gets(line);

// The line before we replace the delimiting characters with 0.
length = strlen(line);
for(i=0;i<length; ++i)
{
printf ("%3c ", line[i]);
}
printf("\n\n");

length = strlen(line);
//Replace delimiting characters with '\0'.
for(i=0;i<length; ++i)
{
if (is_char_in_string(line[i], delim))
line[i] = '\0';

}

// The line after we replace delimiting characters with 0.
for(i=0;i<length; ++i)
{
printf ("%3c ", line[i]);
}
printf("\n\n");
// Printing the line as integers so that we can see that space is
// replaced with 0.
for(i=0;i<length; ++i)
{
printf ("%3d ", line[i]);
}
printf("\n\n");

// Complete the program to count the number of words in the sentence and
// print each word separately, along with length of each word.

// If line = "Arizona is hot", your output should be
// Word 1: Arizona Length = 7
// Word 2: is Length = 2
// Word 3: hot Length = 3

for (i = 0; i<length;) {
printf("Word %d: %s Length = %d\n",++count,&line[i],strlen(&line[i]));
i+=strlen(&line[i])+1;

}

}

//---------------------------------------------------------------------------

㈤ C語言(unix)

/* triangle.c */
//---------------------------------------------------------------------------

#include <stdio.h>
int can(int,int,int);
void equilateral(int,int,int);
void isosceles(int,int,int);
void RightAngled(int,int,int);

int main(int argc, char* argv[])
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%s\n%s\n%s\n%s\n\n",
"Can a triangle can be formed with the given lengths?",
"Is the triangle equilateral?" ,
"If the triangle is not equilateral, it is isosceles?",
"Is the triangle right-angled?");

if (can(a,b,c))puts("Yes");
else puts("No");
equilateral(a,b,c);
isosceles(a,b,c);
RightAngled(a,b,c);

return 0;
}
int can(int a,int b,int c)
{
if (a+b>c&&a-b<c)
return 1;
return 0;
}

void equilateral(int a,int b,int c)
{
if (can(a,b,c))
if (a==b&&a==c) puts("Yes");
else puts("No");
else puts("Not applicable");
}

void isosceles(int a,int b,int c)
{
if (can(a,b,c))
if (a==b||a==c||b==c) puts("Yes");
else puts("No");
else puts("Not applicable");
}

void RightAngled(int a,int b,int c)
{
if (can(a,b,c))
if (a*a+b*b==c*c||a*a+c*c==b*b||a*a==b*b+c*c)
puts("Yes");
else puts("No");
else puts("Not applicable");
}
//---------------------------------------------------------------------------

/*collinear.c*/
//---------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
#define eps 1e-10
int main(int argc, char* argv[])
{
double x1,x2,x3,y1,y2,y3;
puts("Input x1&y1:");
scanf("%lf%lf",&x1,&y1);
puts("Input x2&y2:");
scanf("%lf%lf",&x2,&y2);
puts("Input x3&y3:");
scanf("%lf%lf",&x3,&y3);
if (fabs(x1-x2)<eps&&fabs(y1-y2)<eps) {
puts("\nERROR:the same points");
}
else if(fabs(x1-x2)<eps&&fabs(x1-x3)<eps||fabs(y1-y2)<eps&&fabs(y1-y3)<eps)
{
puts("\nCollinear!");
}
else if((x3-x1)/(x2-x1)-(y3-y1)/(y2-y1)<eps)
{
puts("\nCollinear!");
}
else puts("\nNot Collinear!");

return 0;
}
//---------------------------------------------------------------------------

/*number-words.c*/
//---------------------------------------------------------------------------

#include <stdio.h>

int main(int argc, char* argv[])
{
char *s[]={"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
char
*g[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
int i;
do
{
printf("Input i:\t");
scanf("%d",&i);
if (i>99||i<-99)
puts("ERROR: -100<i<100");
}while(i>99||i<-99);
putchar('\n');
if (i>=0&&i<20) {
puts(g[i]);
}
else if (i<0&&i>-20) {
printf("negative %s\n",g[i*-1]);
}
else if (i%10==0) {
i>0?printf("%s\n",s[i/10-2]):printf("negative %s\n",s[(-1*i)/10-2]);
}
else if (i>0) {
printf("%s-%s\n",s[i/10-2],g[i%10]);
}
else if (i<0) {
printf("negative %s-%s\n",s[-1*i/10-2],g[-1*i%10]);
}

return 0;
}
//---------------------------------------------------------------------------

以上程序均在SOLARIS5+GCC中測試通過,如果有其它問題,請留言。

㈥ 用c語言編寫時間在unix下怎麼顯示

轉碼

㈦ c語言(Unix)英文

sudoku-verifier.c:

//---------------------------------------------------------------------------

#include <stdio.h>

int issu(int sd[9][9])
{
int i,j,k,sx,sy;
for (i = 0; i<9; i++) {
for (k=1; k<10; k++) {
sx=0;
sy=0;
for (j=0; j<9; j++) {
sx+=sd[i][j]==k?1:0;
if (sx>1) return 0;
sy+=sd[j][i]==k?1:0;
if (sy>1) return 0;

}

}
}
return 1;
}
int main(int argc, char* argv[])
{
FILE *f;
int sd[9][9],i,j;
if (argc>1) f=fopen(argv[1],"r");
else f=fopen("sudoku-solution.txt","r");

for (i = 0; i<9; i++){
for (j=0; j<9; j++){
fscanf(f,"%d",&sd[i][j]);
printf("%-2d",sd[i][j]);
}
putchar('\n');
}
fclose(f);
puts(issu(sd)?"Result: Solution is Correct!":"Result: Solution is incorrect!");

return 0;
}
//---------------------------------------------------------------------------

string-palindrome.c:

//---------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[80];
int i,len;
gets(str);
len=strlen(str)/2;
for (i = 0; i<len; i++) {
if (str[i]!=str[strlen(str)-1-i]) {
printf("%s : not a palindrome\n",str);
break;
}
}
if (i==len) {
printf( "%s : palindrome\n",str);
}
return 0;
}
//---------------------------------------------------------------------------

㈧ unix/linux c語言編程

1、這個函數把第一個參數mesg列印到stdout文件中去,stdout一般對應於顯示器,准確的名字叫標准輸出
2、stdout是文件描述符,不用你自己來定義,已經由stdio.h定義了,一般為#define stdout 0
望點贊,謝謝。

㈨ Unix和C語言之間的聯系,簡答題,請賜教

Unix的作者是三個人:Ken Thompson、Dennis Ritchie 和 Douglas McIlroy
C語言的作者是一個人:Dennis Ritchie
這關系就不用我說了吧~~
Dennis當年參與開發Unix,使用匯編些的(後來貌似用B語言重寫過)
發現用這玩意寫不爽!於是就創造出了一個另自己爽的編程語言——C語言
後來又用C語言重寫了Unix系統~
這就是關系了~

㈩ 如何使用C語言程序獲取指定日期的UNIX時間戳

C/C++ code #include #include int main(void) { time_t tick; struct tm tm; char s[100]; tick = time(NULL); tm = *localtime(&tick); strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm); printf("%d: %s\n", (int)tick, s); return 0; }