unix时间c语言
㈠ 请教,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; }