免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1114 | 回复: 0
打印 上一主题 下一主题

C语言经典例题(100) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-06 15:05 |只看该作者 |倒序浏览

经典C语言程序设计100例
【程序1】
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去
       掉不满足条件的排列。
2.程序源代码:
main()
{
int i,j,k;
printf("\n");
for(i=1;i

【程序2】
题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高
    于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提
    成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于
    40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于
    100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。      
2.程序源代码:
main()
{
long int i;
int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;
scanf("%ld",&i);
bonus1=100000*0.1;bonus2=bonus1+100000*0.75;
bonus4=bonus2+200000*0.5;
bonus6=bonus4+200000*0.3;
bonus10=bonus6+400000*0.15;
  if(i
-----------------------------------------------------------------------------
【程序3】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后
       的结果满足如下条件,即是结果。请看具体分析:
2.程序源代码:
#include "math.h"
main()
{
long int i,x,y,z;
for (i=1;i2)/*如果是闰年且月份大于2,总天数应该加一天*/
sum++;
printf("It is the %dth day.",sum);}
-----------------------------------------------------------------------------
【程序5】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,
       然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
2.程序源代码:
main()
{
int x,y,z,t;
scanf("%d%d%d",&x,&y,&z);
if (x>y)
{t=x;x=y;y=t;} /*交换x,y的值*/
if(x>z)
{t=z;z=x;x=t;}/*交换x,z的值*/
if(y>z)
{t=y;y=z;z=t;}/*交换z,y的值*/
printf("small to big: %d %d %d\n",x,y,z);
}
-----------------------------------------------------------------------------
【程序6】
题目:用*号输出字母C的图案。
1.程序分析:可先用'*'号在纸上写出字母C,再分行输出。
2.程序源代码:
#include "stdio.h"
main()
{
printf("Hello C-world!\n");
printf(" ****\n");
printf(" *\n");
printf(" * \n");
printf(" ****\n");
}
-----------------------------------------------------------------------------
【程序7】
题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
1.程序分析:字符共有256个。不同字符,图形不一样。      
2.程序源代码:
#include "stdio.h"
main()
{
char a=176,b=219;
printf("%c%c%c%c%c\n",b,a,a,a,b);
printf("%c%c%c%c%c\n",a,b,a,b,a);
printf("%c%c%c%c%c\n",a,a,b,a,a);
printf("%c%c%c%c%c\n",a,b,a,b,a);
printf("%c%c%c%c%c\n",b,a,a,a,b);}
-----------------------------------------------------------------------------
【程序8】
题目:输出9*9口诀。
1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
2.程序源代码:
#include "stdio.h"
main()
{
  int i,j,result;
  printf("\n");
  for (i=1;i
【程序11】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月
    后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
2.程序源代码:
main()
{
long f1,f2;
int i;
f1=f2=1;
for(i=1;i
-----------------------------------------------------------------------------
【程序12】
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
       则表明此数不是素数,反之是素数。       
2.程序源代码:
#include "math.h"
main()
{
  int m,i,k,h=0,leap=1;
  printf("\n");
  for(m=101;m
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果nk,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,
  重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
2.程序源代码:
/* zheng int is divided yinshu*/
main()
{
int n,i;
printf("\nplease input a number:\n");
scanf("%d",&n);
printf("%d=",n);
for(i=2;i=90分的同学用A表示,60-89分之间的用B表示,
    60分以下的用C表示。
1.程序分析:(a>b)?a:b这是条件运算符的基本例子。
2.程序源代码:
main()
{
  int score;
  char grade;
  printf("please input a score\n");
  scanf("%d",&score);
  grade=score>=90?'A':(score>=60?'B':'C');
  printf("%d belongs to %c",score,grade);
}
-----------------------------------------------------------------------------
【程序16】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。
2.程序源代码:
main()
{
  int a,b,num1,num2,temp;
  printf("please input two numbers:\n");
  scanf("%d,%d",&num1,&num2);
  if(num1='a'&&c='A'&&c='0'&&c
  {
    if((j%i)==0)
    { n++;
     s=s-i;
     k[n]=i;
    }
   }
  if(s==0)
  {
  printf("%d is a wanshu",j);
  for(i=0;i
【程序21】
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
    第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下
    的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
1.程序分析:采取逆向思维的方法,从后往前推断。
2.程序源代码:
main()
{
int day,x1,x2;
day=9;
x2=1;
while(day>0)
  {x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/
  x2=x1;
  day--;
  }
printf("the total is %d\n",x1);
}
-----------------------------------------------------------------------------
【程序22】
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定
    比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出
    三队赛手的名单。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
       则表明此数不是素数,反之是素数。       
2.程序源代码:
main()
{
char i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/
for(i='x';i
*
***
******
********
******
***
*
1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重
       for循环,第一层控制行,第二层控制列。
2.程序源代码:
main()
{
int i,j,k;
for(i=0;i
-----------------------------------------------------------------------------
【程序27】
题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
1.程序分析:
2.程序源代码:
#include "stdio.h"
main()
{
int i=5;
void palin(int n);
printf("\40:");
palin(i);
printf("\n");
}
void palin(n)
int n;
{
char next;
if(n
-----------------------------------------------------------------------------
【程序28】
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第
    3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后
    问第一个人,他说是10岁。请问第五个人多大?
1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道
       第四人的岁数,依次类推,推到第一人(10岁),再往回推。
2.程序源代码:
age(n)
int n;
{
int c;
if(n==1) c=10;
else c=age(n-1)+2;
return(c);
}
main()
{ printf("%d",age(5));
}
-----------------------------------------------------------------------------
【程序29】
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
1. 程序分析:学会分解出每一位数,如下解释:(这里是一种简单的算法,师专数002班赵鑫提供)
2.程序源代码:
main( )
{
long a,b,c,d,e,x;
scanf("%ld",&x);
a=x/10000;/*分解出万位*/
b=x%10000/1000;/*分解出千位*/
c=x%1000/100;/*分解出百位*/
d=x%100/10;/*分解出十位*/
e=x%10;/*分解出个位*/
if (a!=0) printf("there are 5, %ld %ld %ld %ld %ld\n",e,d,c,b,a);
else if (b!=0) printf("there are 4, %ld %ld %ld %ld\n",e,d,c,b);
   else if (c!=0) printf(" there are 3,%ld %ld %ld\n",e,d,c);
     else if (d!=0) printf("there are 2, %ld %ld\n",e,d);
       else if (e!=0) printf(" there are 1,%ld\n",e);
}
-----------------------------------------------------------------------------
【程序30】
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。   
1.程序分析:同29例
2.程序源代码:
main( )
{
long ge,shi,qian,wan,x;
scanf("%ld",&x);
wan=x/10000;
qian=x%10000/1000;
shi=x%100/10;
ge=x%10;
if (ge==wan&&shi==qian)/*个位等于万位并且十位等于千位*/
  printf("this number is a huiwen\n");
else
  printf("this number is not a huiwen\n");
}
【程序31】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续
    判断第二个字母。
1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
2.程序源代码:
#include
void main()
{
char letter;
printf("please input the first letter of someday\n");
while ((letter=getch())!='Y')/*当所按字母为Y时才结束*/
{ switch (letter)
{case 'S':printf("please input second letter\n");
      if((letter=getch())=='a')
       printf("saturday\n");
      else if ((letter=getch())=='u')
          printf("sunday\n");
        else printf("data error\n");
      break;
case 'F':printf("friday\n");break;
case 'M':printf("monday\n");break;
case 'T':printf("please input second letter\n");
      if((letter=getch())=='u')
       printf("tuesday\n");
      else if ((letter=getch())=='h')
          printf("thursday\n");
        else printf("data error\n");
      break;
case 'W':printf("wednesday\n");break;
default: printf("data error\n");
   }
  }
}
-----------------------------------------------------------------------------
【程序32】
题目:Press any key to change color, do you want to try it. Please hurry up!
1.程序分析:            
2.程序源代码:
#include
void main(void)
{
int color;
for (color = 0; color
void main(void)
{
clrscr();/*清屏函数*/
textbackground(2);
gotoxy(1, 5);/*定位函数*/
cprintf("Output at row 5 column 1\n");
textbackground(3);
gotoxy(20, 10);
cprintf("Output at row 10 column 20\n");
}
-----------------------------------------------------------------------------
【程序34】
题目:练习函数调用
1. 程序分析:
2.程序源代码:
#include
void hello_world(void)
{
printf("Hello, world!\n");
}
void three_hellos(void)
{
int counter;
for (counter = 1; counter
void main(void)
{
int color;
for (color = 1; color
#include "math.h"
#define N 101
main()
{
int i,j,line,a[N];
for(i=2;ia[j]) min=j;
tem=a;
a=a[min];
a[min]=tem;
}
/*output data*/
printf("After sorted \n");
for(i=0;iend)
  a[10]=number;
else
  {for(i=0;inumber)
    {temp1=a;
     a=number;
    for(j=i+1;j");
scanf("%d",&num);
printf("\40:The square for this number is %d \n",SQ(num));
if(num>=50)
  again=TRUE;
else
  again=FALSE;
}
}
-----------------------------------------------------------------------------
【程序47】
题目:宏#define命令练习(2)
1.程序分析:            
2.程序源代码:
#include "stdio.h"
#define exchange(a,b) { \ /*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/
             int t;\
             t=a;\
             a=b;\
             b=t;\
            }
void main(void)
{
int x=10;
int y=20;
printf("x=%d; y=%d\n",x,y);
exchange(x,y);
printf("x=%d; y=%d\n",x,y);
}
-----------------------------------------------------------------------------
【程序48】
题目:宏#define命令练习(3)   
1.程序分析:
2.程序源代码:
#define LAG >
#define SMA y)?x:y
#define MINIMUM(x,y) (x>y)?y:x
void main()
{ int a=10,b=20;
#ifdef MAX
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
#undef MAX
#ifdef MAX
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#else
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#endif
#define MIN
#ifndef MIN
printf("\40: The lower one is %d\n",MINIMUM(a,b));
#else
printf("\40: The larger one is %d\n",MAXIMUM(a,b));
#endif
}
-----------------------------------------------------------------------------
【程序50】
题目:#include 的应用练习   
1.程序分析:
2.程序源代码:
test.h 文件如下:
#define LAG >
#define SMA

【程序51】
题目:学习使用按位与 & 。   
1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=1
2.程序源代码:
#include "stdio.h"
main()
{
int a,b;
a=077;
b=a&3;
printf("\40: The a & b(decimal) is %d \n",b);
b&=7;
printf("\40: The a & b(decimal) is %d \n",b);
}
-----------------------------------------------------------------------------
【程序52】
题目:学习使用按位或 | 。
1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=1            
2.程序源代码:
#include "stdio.h"
main()
{
int a,b;
a=077;
b=a|3;
printf("\40: The a & b(decimal) is %d \n",b);
b|=7;
printf("\40: The a & b(decimal) is %d \n",b);
}
-----------------------------------------------------------------------------
【程序53】
题目:学习使用按位异或 ^ 。   
1.程序分析:0^0=0; 0^1=1; 1^0=1; 1^1=0
2.程序源代码:
#include "stdio.h"
main()
{
int a,b;
a=077;
b=a^3;
printf("\40: The a & b(decimal) is %d \n",b);
b^=7;
printf("\40: The a & b(decimal) is %d \n",b);
}
-----------------------------------------------------------------------------
【程序54】
题目:取一个整数a从右端开始的4~7位。
程序分析:可以这样考虑:
(1)先使a右移4位。
(2)设置一个低4位全为1,其余全为0的数。可用~(~0>4;
c=~(~0=RIGHT)
  dx1=-dx1;
  if(y1=BOTTOM)
   dy1=-dy1;
  if(x2=RIGHT)
   dx2=-dx2;
  if(y2=BOTTOM)
   dy2=-dy2;
  if(++count>LINES)
  {
   setcolor(color);
   color=(color>=MAXCOLOR)?0:++color;
  }
}
closegraph();
}
【程序61】
题目:打印出杨辉三角形(要求打印出10行如下图)   
1.程序分析:
       1
       1  1
       1  2  1
       1  3  3  1
       1  4  6  4  1
       1  5  10 10 5  1 
2.程序源代码:
main()
{int i,j;
int a[10][10];
printf("\n");
for(i=0;in2) swap(pointer1,pointer2);
if(n1>n3) swap(pointer1,pointer3);
if(n2>n3) swap(pointer2,pointer3);
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3);
}
swap(p1,p2)
int *p1,*p2;
{int p;
p=*p1;*p1=*p2;*p2=p;
}
-----------------------------------------------------------------------------
【程序67】
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
1.程序分析:谭浩强的书中答案有问题。      
2.程序源代码:
main()
{
int number[10];
input(number);
max_min(number);
output(number);
}
input(number)
int number[10];
{int i;
for(i=0;i*max) max=p;
  else if(*parray;p--)
  *p=*(p-1);
  *array=array_end;
  m--;
  if(m>0) move(array,n,m);
}
-----------------------------------------------------------------------------
【程序69】
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出
    圈子,问最后留下的是原来第几号的那位。
1. 程序分析:
2.程序源代码:
#define nmax 50
main()
{
int i,k,m,n,num[nmax],*p;
printf("please input the total of numbers:");
scanf("%d",&n);
p=num;
for(i=0;i
题目:编写input()和output()函数输入,输出5个学生的数据记录。
1.程序分析:
2.程序源代码:
#define N 5
struct student
{ char num[6];
  char name[8];
  int score[4];
} stu[N];
input(stu)
struct student stu[];
{ int i,j;
  for(i=0;i\n");
for(i=0;idata=num;
  ptr->next=(link)malloc(sizeof(node));
  if(i==4) ptr->next=NULL;
  else ptr=ptr->next;
}
ptr=head;
while(ptr!=NULL)
{ printf("The value is ==>%d\n",ptr->data);
  ptr=ptr->next;
}
}
-----------------------------------------------------------------------------
【程序73】
题目:反向输出一个链表。   
1.程序分析:
2.程序源代码:
/*reverse output a list*/
#include "stdlib.h"
#include "stdio.h"
struct list
{ int data;
  struct list *next;
};
typedef struct list node;
typedef node *link;
void main()
{ link ptr,head,tail; 
  int num,i;
  tail=(link)malloc(sizeof(node));
  tail->next=NULL;
  ptr=tail;
  printf("\nplease input 5 data==>\n");
  for(i=0;idata=num;
   head=(link)malloc(sizeof(node));
   head->next=ptr;
   ptr=head;
  }
ptr=ptr->next;
while(ptr!=NULL)
{ printf("The value is ==>%d\n",ptr->data);
  ptr=ptr->next;
}}
-----------------------------------------------------------------------------
【程序74】
题目:连接两个链表。
1.程序分析:
2.程序源代码:
#include "stdlib.h"
#include "stdio.h"
struct list
{ int data;
struct list *next;
};
typedef struct list node;
typedef node *link;
link delete_node(link pointer,link tmp)
{if (tmp==NULL) /*delete first node*/
  return pointer->next;
else
{ if(tmp->next->next==NULL)/*delete last node*/
   tmp->next=NULL;
  else /*delete the other node*/
   tmp->next=tmp->next->next;
  return pointer;
}
}
void selection_sort(link pointer,int num)
{ link tmp,btmp;
  int i,min;
  for(i=0;idata;
  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);
}
}
link create_list(int array[],int num)
{ link tmp1,tmp2,pointer;
int i;
pointer=(link)malloc(sizeof(node));
pointer->data=array[0];
tmp1=pointer;
for(i=1;inext=NULL;
  tmp2->data=array;
  tmp1->next=tmp2;
  tmp1=tmp1->next;
}
return pointer;
}
link concatenate(link pointer1,link pointer2)
{ link tmp;
tmp=pointer1;
while(tmp->next)
  tmp=tmp->next;
tmp->next=pointer2;
return pointer1;
}
void main(void)
{ int arr1[]={3,12,8,9,11};
  link ptr;
  ptr=create_list(arr1,5);
  selection_sort(ptr,5);
}
-----------------------------------------------------------------------------
【程序75】
题目:放松一下,算一道简单的题目。
1.程序分析:
2.程序源代码:
main()
{
int i,n;
for(i=1;i1)
   break;
}
if(n%2==0)
{
  printf("Even=");
  sum=dcall(peven,n);
}
else
{
  printf("Odd=");
  sum=dcall(podd,n);
}
printf("%f",sum);
}
float peven(int n)
{
float s;
int i;
s=1;
for(i=2;iage)
  q=p++;
  m=q->age;}
printf("%s,%d",(*q).name,(*q).age);
}
-----------------------------------------------------------------------------
【程序79】
题目:字符串排序。
1.程序分析:
2.程序源代码:
main()
{
char *str1[20],*str2[20],*str3[20];
char swap();
printf("please input three strings\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("after being sorted\n");
printf("%s\n%s\n%s\n",str1,str2,str3);
}
char swap(p1,p2)
char *p1,*p2;
{
char *p[20];
strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p);
}
-----------------------------------------------------------------------------
【程序80】
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只
    猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了
    一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,
    问海滩上原来最少有多少个桃子?
1.程序分析:
2.程序源代码:
main()
{int i,m,j,k,count;
for(i=4;i=1000&&b=100)
output(b,i); }
}
-----------------------------------------------------------------------------
【程序82】
题目:八进制转换为十进制
1.程序分析:           
2.程序源代码:
main()
{ char *p,s[6];int n;
p=s;
gets(p);
n=0;
while(*(p)!='\0')
{n=n*8+*p-'0';
p++;}
printf("%d",n);
}
-----------------------------------------------------------------------------
【程序83】
题目:求0—7所能组成的奇数个数。
1.程序分析:
2.程序源代码:
main()
{
long sum=4,s=4;
int j;
for(j=2;jsqrt(b))
d=a-b;
else
break;
for(c=2;csqrt(d))
printf("%d=%d+%d\n",a,b,d);
}
}
-----------------------------------------------------------------------------
【程序85】
题目:判断一个素数能被几个9整除
1.程序分析:
2.程序源代码:
main()
{ long int m9=9,sum=9;
int zi,n1=1,c9=1;
scanf("%d",&zi);
while(n1!=0)
{ if(!(sum%zi))
n1=0;
else
{m9=m9*10;
sum=sum+m9;
c9++;
}
}
printf("%ld,can be divided by %d \"9\"",sum,c9);
}
-----------------------------------------------------------------------------
【程序86】
题目:两个字符串连接程序
1.程序分析:
2.程序源代码:
#include "stdio.h"
main()
{char a[]="acegikm";
char b[]="bdfhjlnpq";
char c[80],*p;
int i=0,j=0,k=0;
while(a!='\0'&&b[j]!='\0')
{if (a50);
for(i=1;i=0;i--)
printf("%d",aa);
}
-----------------------------------------------------------------------------
【程序90】
题目:专升本一题,读结果。
1.程序分析:
2.程序源代码:
#include "stdio.h"
#define M 5
main()
{int a[M]={1,2,3,4,5};
int i,j,t;
i=0;j=M-1;
while(i
【程序91】
题目:时间函数举例1
1.程序分析:
2.程序源代码:
#include "stdio.h"
#include "time.h"
void main()
{ time_t lt; /*define a longint time varible*/
lt=time(NULL);/*system time and date*/
printf(ctime(
-----------------------------------------------------------------------------
【程序94】
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
1.程序分析:
2.程序源代码:
#include "time.h"
#include "stdlib.h"
#include "stdio.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
-----------------------------------------------------------------------------
【程序95】
题目:家庭财务管理小程序
1.程序分析:
2.程序源代码:
/*money management system*/
#include "stdio.h"
#include "dos.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)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(j15)
{ 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
-----------------------------------------------------------------------------
【程序98】
题目:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。
    输入的字符串以!结束。
1.程序分析:
2.程序源代码:
#include "stdio.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!='!')
{ if(str>='a'&&strc[j])
{t=c;c=c[j];c[j]=t;}
printf("\n C file is:\n");
fp=fopen("C","w");
for(i=0;i



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/48859/showart_458277.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP