C语言实例.docx
- 文档编号:9580536
- 上传时间:2023-02-05
- 格式:DOCX
- 页数:34
- 大小:23.81KB
C语言实例.docx
《C语言实例.docx》由会员分享,可在线阅读,更多相关《C语言实例.docx(34页珍藏版)》请在冰豆网上搜索。
C语言实例
●c语言随机函数小汇
●编程语言
(1)rand()函数:
原型:
intrand(void)
功能:
产生从0到RAND_MAX(0x7fff)之间的随机数。
头文件:
stdlib.h
例:
#include
#include
intmain()
{
intk;
k=rand();
printf("%d\n",k);
return0;
}
编译运行,发现他的确产生随机数了,但是,每次运行程序产生的随机数都是一样的,为什么呢?
因为随机数取在C语言中采用的是固定序列,所以每次执行所取的是同一个数。
那么如何写一个程序,让它每次运行时产生的随机数都不一样呢?
往下看:
(2)srand()函数:
原型:
voidsrand(unsignedseed)
功能:
产生随机数的起始发生数据,和rand函数配合使用
头文件:
stdlib.h time.h
例:
#include
#include
#include
intmain(void)
{
inti;
time_tt;
srand((unsigned)time(&t));
printf("Tenrandomnumbersfrom0to99\n\n");
for(i=0;i<10;i++)
printf("%d\n",rand()%100);
return0;
}
这时运行程序,会发现每次产生的随机数都不一样。
这是因为这里采用了时间作为种子,而时间在每时每刻都不相同,所以就产生了"随机"的随机数了。
所以,要想产生不同的随机数,在使用rand之前需要先调用srand。
这样做相当麻烦,有没一个更简单的函数呢?
往下看:
(3)randomize()函数:
原型:
intrandomize(void)
功能:
通过time函数来得到一个随机数
头文件:
stdlib.h time.h
该函数就相当于srand((unsign)(time(NULL))不过应注意的是randomize()的功能要通过time来实现所以在调用它时头文件要包含time.h。
(4)产生一个X到Y的随机数:
由于rand产生的随机数从0到rand_max,而rand_max是一个很大的数,那么如何产生从X~Y的数呢?
只要这样写:
k=rand()%(Y-X+1)+X;
这样,就可以产生你想要的任何范围内的随机数了。
题目:
家庭财务管理小程序
1.程序分析:
2.程序源代码:
/*moneymanagementsystem*/
#include"stdio.h"
#include"dos.h"
main()
{
FILE*fp;
structdated;
floatsum,chm=0.0;
intlen,i,j=0;
intc;
charch[4]="",ch1[16]="",chtime[12]="",chshop[16],chmoney[8];
pp:
clrscr();
sum=0.0;
gotoxy(1,1);printf("|---------------------------------------------------------------------------|");
gotoxy(1,2);printf("|moneymanagementsystem(C1.0)2000.03|");
gotoxy(1,3);printf("|---------------------------------------------------------------------------|");
gotoxy(1,4);printf("|--moneyrecords--|--todaycostlist--|");
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__browsecostlistEsc__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("listrecords");
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("totalis%8.1f$",sum);
fclose(fp);
gotoxy(49,24);printf("pressanykeyto.....");getch();gotopp;
}
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)
gotomm;
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);
}}}
题目:
一个偶数总能表示为两个素数之和。
1.程序分析:
2.程序源代码:
#include"stdio.h"
#include"math.h"
main()
{inta,b,c,d;
scanf("%d",&a);
for(b=3;b<=a/2;b+=2)
{for(c=2;c<=sqrt(b);c++)
if(b%c==0)break;
if(c>sqrt(b))
d=a-b;
else
break;
for(c=2;c<=sqrt(d);c++)
if(d%c==0)break;
if(c>sqrt(d))
printf("%d=%d+%d\n",a,b,d);
}
}
题目:
创建一个链表。
1.程序分析:
2.程序源代码:
/*creatalist*/
#include"stdlib.h"
#include"stdio.h"
structlist
{intdata;
structlist*next;
};
typedefstructlistnode;
typedefnode*link;
voidmain()
{linkptr,head;
intnum,i;
ptr=(link)malloc(sizeof(node));
ptr=head;
printf("pleaseinput5numbers==>\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr->data=num;
ptr->next=(link)malloc(sizeof(node));
if(i==4)ptr->next=NULL;
elseptr=ptr->next;
}
ptr=head;
while(ptr!
=NULL)
{printf("Thevalueis==>%d\n",ptr->data);
ptr=ptr->next;
}
}
题目:
反向输出一个链表。
1.程序分析:
2.程序源代码:
/*reverseoutputalist*/
#include"stdlib.h"
#include"stdio.h"
structlist
{intdata;
structlist*next;
};
typedefstructlistnode;
typedefnode*link;
voidmain()
{linkptr,head,tail;
intnum,i;
tail=(link)malloc(sizeof(node));
tail->next=NULL;
ptr=tail;
printf("\npleaseinput5data==>\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr->data=num;
head=(link)malloc(sizeof(node));
head->next=ptr;
ptr=head;
}
ptr=ptr->next;
while(ptr!
=NULL)
{printf("Thevalueis==>%d\n",ptr->data);
ptr=ptr->next;
}}
题目:
连接两个链表。
1.程序分析:
2.程序源代码:
#include"stdlib.h"
#include"stdio.h"
structlist
{intdata;
structlist*next;
};
typedefstructlistnode;
typedefnode*link;
linkdelete_node(linkpointer,linktmp)
{if(tmp==NULL)/*deletefirstnode*/
returnpointer->next;
else
{if(tmp->next->next==NULL)/*deletelastnode*/
tmp->next=NULL;
else/*deletetheothernode*/
tmp->next=tmp->next->next;
returnpointer;
}
}
voidselection_sort(linkpointer,intnum)
{linktmp,btmp;
inti,min;
for(i=0;i { tmp=pointer; min=tmp->data; btmp=NULL; while(tmp->next) {if(min>tmp->next->data) {min=tmp->next->data; btmp=tmp; } tmp=tmp->next; } printf("\40: %d\n",min); pointer=delete_node(pointer,btmp); } } linkcreate_list(intarray[],intnum) {linktmp1,tmp2,pointer; inti; pointer=(link)malloc(sizeof(node)); pointer->data=array[0]; tmp1=pointer; for(i=1;i {tmp2=(link)malloc(sizeof(node)); tmp2->next=NULL; tmp2->data=array[i]; tmp1->next=tmp2; tmp1=tmp1->next; } returnpointer; } linkconcatenate(linkpointer1,linkpointer2) {linktmp; tmp=pointer1; while(tmp->next) tmp=tmp->next; tmp->next=pointer2; returnpointer1; } voidmain(void) {intarr1[]={3,12,8,9,11}; linkptr; ptr=create_list(arr1,5); selection_sort(ptr,5); } 题目: 编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数 1/1+1/3+...+1/n(利用指针函数) 1.程序分析: 2.程序源代码: main() #include"stdio.h" main() { floatpeven(),podd(),dcall(); floatsum; intn; while (1) { scanf("%d",&n); if(n>1) break; } if(n%2==0) { printf("Even="); sum=dcall(peven,n); } else { printf("Odd="); sum=dcall(podd,n); } printf("%f",sum); } floatpeven(intn) { floats; inti; s=1; for(i=2;i<=n;i+=2) s+=1/(float)i; return(s); } floatpodd(n) intn; { floats; inti; s=0; for(i=1;i<=n;i+=2) s+=1/(float)i; return(s); } floatdcall(fp,n) float(*fp)(); intn; { floats; s=(*fp)(n); return(s); } 题目: 填空练习(指向指针的指针) 1.程序分析: 2.程序源代码: main() {char*s[]={"man","woman","girl","boy","sister"}; char**q; intk; for(k=0;k<5;k++) { ;/*这里填写什么语句*/ printf("%s\n",*q); } } 题目: 字符串排序。 1.程序分析: 2.程序源代码: main() { char*str1[20],*str2[20],*str3[20]; charswap(); printf("pleaseinputthreestrings\n"); scanf("%s",str1); scanf("%s",str2); scanf("%s",str3); if(strcmp(str1,str2)>0)swap(str1,str2); if(strcmp(str1,str3)>0)swap(str1,str3); if(strcmp(str2,str3)>0)swap(str2,str3); printf("afterbeingsorted\n"); printf("%s\n%s\n%s\n",str1,str2,str3); } charswap(p1,p2) char*p1,*p2; { char*p[20]; strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p); } 题目: 海滩上有一堆桃子,五只猴子来分。 第一只猴子把这堆桃子凭据分为五份,多了一个,这只 猴子把多的一个扔入海中,拿走了一份。 第二只猴子把剩下的桃子又平均分成五份,又多了 一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的, 问海滩上原来最少有多少个桃子? 1.程序分析: 2.程序源代码: main() {inti,m,j,k,count; for(i=4;i<10000;i+=4) {count=0; m=i; for(k=0;k<5;k++) { j=i/4*5+1; i=j; if(j%4==0) count++; else break; } i=m; if(count==4) {printf("%d\n",count); break;} } } 题目: 有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数 1.程序分析: 2.程序源代码: main() { intnumber[20],n,m,i; printf("thetotalnumbersis: "); scanf("
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 实例