我参与8月更文挑战的第28天,活动详情查看:8月更文挑战
【程序91】
题目:时间函数举例1
-
程序分析: ctime分函数和类两种用途。ctime功能是 把日期和时间转换为字符串,而ctime类的对象表示的时间是基于格林威治标准时间(GMT)的。
-
程序源代码:
#include "stdio.h"
#include "conio.h"
#include "time.h"
void main()
{
time_t lt; /*define a longint time varible*/
lt=time(NULL);/*system time and date*/
printf(ctime(<)); /*english format output*/
printf(asctime(localtime(<)));/*tranfer to tm*/
printf(asctime(gmtime(<))); /*tranfer to Greenwich time*/
getch();
}
【程序92】
题目:时间函数举例2
-
程序分析: 函数原型: time_t time(time_t *timer)
参数说明: timer=NULL时得到当前日历时间(从1970-01-01 00:00:00到现在的秒数),timer=时间数值时,用于设置日历时间,time_t是一个unsigned long类型。如果 timer不为空,则返回值也存储在变量 timer中。
函数功能: 得到当前日历时间或者设置日历时间
函数返回: 当前日历时间\ -
程序源代码:
/*calculate time*/
#include "time.h"
#include "stdio.h"
#include "conio.h"
main()
{
time_t start,end;
int i;
start=time(NULL);
for(i=0;i<30000;i++)
printf("\1\1\1\1\1\1\1\1\1\1\n");
end=time(NULL);
printf("\1: The different is %6.3f\n",difftime(end,start));
getch();
}
【程序93】
题目:时间函数举例3
- 程序分析: clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。
在MSDN中,查得对clock函数定义如下:
clock_t clock(void) ;
简单而言,就是该程序从启动到函数调用占用CPU的时间。这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型。
- 程序源代码:
/*calculate time*/
#include "time.h"
#include "stdio.h"
#include "conio.h"
main()
{
clock_t start,end;
int i;
double var;
start=clock();
for(i=0;i<10000;i++)
printf("\1\1\1\1\1\1\1\1\1\1\n");
end=clock();
printf("\1: The different is %6.3f\n",(double)(end-start));
getch();
}
【程序94】
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。
- 程序分析: rand()函数用来产生随机数,但是,rand()的内部实现是用线性同余法实现的,是伪随机数,由于周期较长,因此在一定范围内可以看成是随机的。
rand()会返回一个范围在0到RAND_MAX(至少是32767)之间的伪随机数(整数)。
在调用rand()函数之前,可以使用srand()函数设置随机数种子,如果没有设置随机数种子,rand()函数在调用时,自动设计随机数种子为1。随机种子相同,每次产生的随机数也会相同。
rand()函数需要的头文件是:<stdlib.h>
rand()函数原型:int rand(void);
使用rand()函数产生1-100以内的随机整数:int number1 = rand() % 100+1;
- 程序源代码:
#include "time.h"
#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
main()
{
char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
srand(time(NULL));
printf("do you want to play it.('y' or 'n') \n");
loop:
while((c=getchar())=='y')
{
i=rand()%100;
printf("\nplease input number you guess:\n");
start=clock();
a=time(NULL);
scanf("%d",&guess);
while(guess!=i)
{
if(guess>i)
{
printf("please input a little smaller.\n");
scanf("%d",&guess);
}
else
{
printf("please input a little bigger.\n");
scanf("%d",&guess);
}
}
end=clock();
b=time(NULL);
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
if(var<15)
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25)
printf("\1\1 you are normal! \1\1\n\n");
else
printf("\1\1 you are stupid! \1\1\n\n");
printf("\1\1 Congradulations \1\1\n\n");
printf("The number you guess is %d",i);
}
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
if((c=getch())=='y')
goto loop;
}
【程序95】
题目:家庭财务管理小程序
-
程序分析: gotoxy(int x, int y)是 Borland C 扩充函数库 conio.h 中声明的一个函数,功能是将光标移动到指定位置。在当代的 Visual C++ 或 GCC 中可以自定义这个函数。
-
程序源代码:
/*money management system*/
#include "stdio.h"
#include "dos.h"
#include "conio.h"
main()
{
FILE *fp;
struct date d;
float sum,chm=0.0;
int len,i,j=0;
int c;
char ch[4]="",ch1[16]="",chtime[12]="",chshop[16],chmoney[8];
pp:
clrscr();
sum=0.0;
gotoxy(1,1);printf("|---------------------------------------------------------------------------|");
gotoxy(1,2);printf("| money management system(C1.0) 2000.03 |");
gotoxy(1,3);printf("|---------------------------------------------------------------------------|");
gotoxy(1,4);printf("| -- money records -- | -- today cost list -- |");
gotoxy(1,5);printf("| ------------------------ |-------------------------------------|");
gotoxy(1,6);printf("| date: -------------- | |");
gotoxy(1,7);printf("| | | | |");
gotoxy(1,8);printf("| -------------- | |");
gotoxy(1,9);printf("| thgs: ------------------ | |");
gotoxy(1,10);printf("| | | | |");
gotoxy(1,11);printf("| ------------------ | |");
gotoxy(1,12);printf("| cost: ---------- | |");
gotoxy(1,13);printf("| | | | |");
gotoxy(1,14);printf("| ---------- | |");
gotoxy(1,15);printf("| | |");
gotoxy(1,16);printf("| | |");
gotoxy(1,17);printf("| | |");
gotoxy(1,18);printf("| | |");
gotoxy(1,19);printf("| | |");
gotoxy(1,20);printf("| | |");
gotoxy(1,21);printf("| | |");
gotoxy(1,22);printf("| | |");
gotoxy(1,23);printf("|---------------------------------------------------------------------------|");
i=0;
getdate(&d);
sprintf(chtime,"%4d.%02d.%02d",d.da_year,d.da_mon,d.da_day);
for(;;)
{
gotoxy(3,24);printf(" Tab __browse cost list Esc __quit");
gotoxy(13,10);printf(" ");
gotoxy(13,13);printf(" ");
gotoxy(13,7);printf("%s",chtime);
j=18;
ch[0]=getch();
if(ch[0]==27)
break;
strcpy(chshop,"");
strcpy(chmoney,"");
if(ch[0]==9)
{
mm:
i=0;
fp=fopen("home.dat","r+");
gotoxy(3,24);printf(" ");
gotoxy(6,4);printf(" list records ");
gotoxy(1,5);printf("|-------------------------------------|");
gotoxy(41,4);printf(" ");
gotoxy(41,5);printf(" |");
while(fscanf(fp,"%10s%14s%f\n",chtime,chshop,&chm)!=EOF)
{
if(i==36)
{
getch();
i=0;
}
if((i%36)<17)
{
gotoxy(4,6+i);
printf(" ");
gotoxy(4,6+i);
}
else
if((i%36)>16)
{
gotoxy(41,4+i-17);
printf(" ");
gotoxy(42,4+i-17);
}
i++;
sum=sum+chm;
printf("%10s %-14s %6.1f\n",chtime,chshop,chm);
}
gotoxy(1,23);printf("|---------------------------------------------------------------------------|");
gotoxy(1,24);printf("| |");
gotoxy(1,25);printf("|---------------------------------------------------------------------------|");
gotoxy(10,24);printf("total is %8.1f$",sum);
fclose(fp);
gotoxy(49,24);printf("press any key to.....");getch();goto pp;
}
else
{
while(ch[0]!='\r')
{
if(j<10)
{
strncat(chtime,ch,1);
j++;
}
if(ch[0]==8)
{
len=strlen(chtime)-1;
if(j>15)
{len=len+1; j=11;}
strcpy(ch1,"");
j=j-2;
strncat(ch1,chtime,len);
strcpy(chtime,"");
strncat(chtime,ch1,len-1);
gotoxy(13,7);printf(" ");
}
gotoxy(13,7);printf("%s",chtime);ch[0]=getch();
if(ch[0]==9)
goto mm;
if(ch[0]==27)
exit(1);
}
gotoxy(3,24);printf(" ");
gotoxy(13,10);
j=0;
ch[0]=getch();
while(ch[0]!='\r')
{
if (j<14)
{
strncat(chshop,ch,1);
j++;
}
if(ch[0]==8)
{
len=strlen(chshop)-1;
strcpy(ch1,"");
j=j-2;
strncat(ch1,chshop,len);
strcpy(chshop,"");
strncat(chshop,ch1,len-1);
gotoxy(13,10);printf(" ");
}
gotoxy(13,10);printf("%s",chshop);ch[0]=getch();
}
gotoxy(13,13);
j=0;
ch[0]=getch();
while(ch[0]!='\r')
{
if (j<6)
{
strncat(chmoney,ch,1);
j++;
}
if(ch[0]==8)
{
len=strlen(chmoney)-1;
strcpy(ch1,"");
j=j-2;
strncat(ch1,chmoney,len);
strcpy(chmoney,"");
strncat(chmoney,ch1,len-1);
gotoxy(13,13);printf(" ");
}
gotoxy(13,13);printf("%s",chmoney);ch[0]=getch();
}
if((strlen(chshop)==0)||(strlen(chmoney)==0))
continue;
if((fp=fopen("home.dat","a+"))!=NULL);
fprintf(fp,"%10s%14s%6s",chtime,chshop,chmoney);
fputc('\n',fp);
fclose(fp);
i++;
gotoxy(41,5+i);
printf("%10s %-14s %-6s",chtime,chshop,chmoney);
}
}
getch();
}
【程序96】
题目:计算字符串中子串出现的次数
-
程序分析: 字符串字面量 用双引号括起来的内容称为字符串字面量,也叫作字符串常量。双引号中的字符和... 字符串数组和初始化 定义字符串数组时,必须让编译器知道需要多少空间,一种方法是足够空间的...
-
程序源代码:
#include "string.h"
#include "stdio.h"
#include "conio.h"
main()
{
char str1[20],str2[20],*p1,*p2;
int sum=0;
printf("please input two strings\n");
scanf("%s%s",str1,str2);
p1=str1;p2=str2;
while(*p1!='\0')
{
if(*p1==*p2)
{
while(*p1==*p2&&*p2!='\0')
{
p1++;
p2++;
}
}
else
p1++;
if(*p2=='\0')
sum++;
p2=str2;
}
printf("%d",sum);
getch();
}
【程序97】 题目:从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个#为止。
-
程序分析: getchar是读入函数的一种。它从标准输入里读取下一个字符,相当于getc(stdin)。返回类型为int型,为用户输入的ASCII码或EOF。
-
程序源代码:
#include "stdio.h"
main()
{
FILE *fp;
char ch,filename[10];
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=getchar();
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);
putchar(ch);
ch=getchar();
}
fclose(fp);
}
【程序98】
题目:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。输入的字符串以!结束。
-
程序分析: ASCII 编码中第 0~31 个字符(开头的 32 个字符)以及第 127 个字符(最后一个字符)都是不可见的(无法显示),但是它们都具有一些特殊功能,所以称为控制字符( Control Character)
-
程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
FILE *fp;
char str[100],filename[10];
int i=0;
if((fp=fopen("test","w"))==NULL)
{
printf("cannot open the file\n");
exit(0);
}
printf("please input a string:\n");
gets(str);
while(str[i]!='!')
{
if(str[i]>='a'&&str[i]<='z')
str[i]=str[i]-32;
fputc(str[i],fp);
i++;
}
fclose(fp);
fp=fopen("test","r");
fgets(str,strlen(str)+1,fp);
printf("%s\n",str);
fclose(fp);
getch();
}
【程序99】
题目:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件C中。
- 程序分析: fopen函数的功能是打开一个文件,其调用的一般形式为:
文件指针名=fopen(文件名,使用文件方式)。
“文件指针名”必须是被声明为FILE 类型的指针变量;“文件名”是被打开文件的文件名,类型是C风格字符串;“使用文件方式”是指文件的类型和操作要求。
fgetc是一种计算机C语言中的函数。意为从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节。格式:int fgetc(FILE *stream);。
- 程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
FILE *fp;
int i,j,n,ni;
char c[160],t,ch;
if((fp=fopen("A","r"))==NULL)
{
printf("file A cannot be opened\n");
exit(0);
}
printf("\n A contents are :\n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
ni=i;
if((fp=fopen("B","r"))==NULL)
{
printf("file B cannot be opened\n");
exit(0);
}
printf("\n B contents are :\n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]);
}
fclose(fp);
n=i;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(c[i]>c[j])
{t=c[i];c[i]=c[j];c[j]=t;}
printf("\n C file is:\n");
fp=fopen("C","w");
for(i=0;i<n;i++)
{
putc(c[i],fp);
putchar(c[i]);
}
fclose(fp);
getch();
}
【程序100】
题目:有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,况原有的数据和计算出的平均分数存放在磁盘文件"stud"中。
-
程序分析: fwrite() 是 C 语言标准库中的一个文件处理函数,功能是向指定的文件中写入若干数据块,如成功执行则返回实际写入的数据块数目。该函数以二进制形式对文件进行操作,不局限于文本文件。
-
程序源代码:
#include "stdio.h"
#include "conio.h"
struct student
{
char num[6];
char name[8];
int score[3];
float avr;
}stu[5];
main()
{
int i,j,sum;
FILE *fp;
/*input*/
for(i=0;i<5;i++)
{
printf("\n please input No. %d score:\n",i);
printf("stuNo:");
scanf("%s",stu[i].num);
printf("name:");
scanf("%s",stu[i].name);
sum=0;
for(j=0;j<3;j++)
{
printf("score %d.",j+1);
scanf("%d",&stu[i].score[j]);
sum+=stu[i].score[j];
}
stu[i].avr=sum/3.0;
}
fp=fopen("stud","w");
for(i=0;i<5;i++)
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
printf("file write error\n");
fclose(fp);
getch();
}
C语言基础作业
-
求100以内自然数中偶数之和。
-
输出所有200-400以内能被3整除且个位数字为6的整数。
-
打印所有水仙花数。所谓水仙花是指一个三位数,其个位数字的立方和等于该数
-
输入一个华氏温度,输出摄氏温度,计算公式为 (华氏度-32)×5÷9 要求结果保留两位小数。
-
输入一个不多于5位的正整数,判断他是几位数,并逆序输出。
-
判断某一年是否是闰年。 (条件: 能整除4且不能整除100 或者能整除400 )
-
运用位运算,将大写字母转换为小写字母。(注意: 使用位运算)
-
通过位运算交换两个变量的值
10.使用一个int类型的变量存放4个字符,再取出来。 比如:使用int data存放。‘A’'B' 'C' 'D'
-
输入任意三个数,按从小到大的顺序输出。
-
编程计算123+345+。。。。+99100101的值。
-
编程计算1!+2!+3!+。。+10!的值。
14.从键盘输入一个十进制整形数据,计算并输出其各位上数字之和(忽略正负号)。例如,输入 1234,输出10;输入-1234,输出10.
-
定义一个int类型的变量。在int类型变量里存放4个字符。再取出来(打印)。 再将其中的C字符变为K字符。
-
0-255 整数转二进制
-
二进制转整数输出
-
输出 9*9 乘法口诀表
-
回文判断
-
可逆素数
-
整数和浮点数转字符串
-
小明身高1.75,体重80.5kg。
请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数:
低于18.5:过轻 18.5-25:正常 25-28:过重 28-32:肥胖 高于32:严重肥胖
C语言字符串与数组处理作业
-
查找数据: 从键盘上输入字符串。判断字符串中,小写字母、大写字母、数字、其他字符的个数。 比如: “123ABCabc--- ” 。数字3 大写3 小写3 其他3 空格3 scanf(“%s”,buff); 输入数据什么时候结束? 遇到换行符、空格就会结束。
-
查找单词: 从键盘上输入字符串。判断单词出现的次数。 比如: “123sdjhfbdjf123skdcbds123” 123出现3次。
-
比较字符串。字符串A和C从键盘上随机输入。 功能:比较两个字符串是否相等。
-
判断回文字符串。比如:“12321”这是回文字符串。
-
字符串转整数。比如:“1234” 得到1234
-
定义一个int类型的数组。通过键盘赋值,判断数组中数据大于10的个数。
-
计算一个字符串数组的长度。字符串从键盘上输入。
比如: char buff[100]=”1234567”; //7 scanf(“%s”, buff);
-
字符串拷贝:定义两个字符串数组A ,C。拷贝:将A数组中的字符串复制到C数组中。
-
字符串拼接:定义两个字符串数组A ,C。拼接:将A数组中的字符串追加到C数组中。比如:char a[10]=”1234”; char c[10]=”5678”。最终: char c[10]=”12345678”。
填空题
1. char str[10] = "love you"; char *p = str; int n = 10; sizeof(str) = _________ ; strlen(str) = _______; sizeof(p)= ______ ; strlen(p) = ________; sizeof(n)= ________。
2. 设int arr[]={6,7,8,9,10}; int *ptr = arr; (*ptr++) += 123;printf("%d,%d",ptr,(++ptr));输出结果分别是 , 。
3. 设int nui[]={1,2,3,4};,则nui[1] = ; nui[4] = ;。
4. 若定义 unsigned char a, b, c; 则 a = 5; b = 15; c = 254; c = b & a << 2 + ~c; 执行后 c 的值为___________。
5. 若有定义:char c= '\101 '; 则变量c中包含的字符个数为 ,是哪些字符。
6. 如果unsigned char a = ~( 0x01 << 1),a的结果是 。
7. 若变量已正确定义,语句if(a>b) k=0; else k=1;请使用三目运算符号重新写这段代码;
8. 在32位windows 平台下,分别写出以下几种类型所占的字节大小。char: 字节;int: 字节;long: 字节;float: 字节;double: 字节;
9. 如果char a,b,c,d;且a=123,那么b=a/100= 、c=a%100= 、d=a%100/10= 。
10. 定义全局变量unsigned char a,b,计算表达式a+=b++之后,a=______,b=______。