Pages: 2/14 First page Previous page 1 2 3 4 5 6 7 8 9 10 Next page Final page [ View by Articles | List ]

结构指针:计算两个时刻之间的时间

| January 3, 2008 18:47 | timmy | Via Original

#include <stdio.h>
struct time_struct {
  int h;
  int m;
  int s;
};
void cal_time(struct time_struct *t1,struct time_struct *t2);
void main()
{
  struct time_struct time1,time2;
  printf("Input hour,minute,second of time1: ");
  scanf("%d:%d:%d",&time1.h,&time1.m,&time1.s);
  printf("Input hour,minute,second of time2: ");
  scanf("%d:%d:%d",&time2.h,&time2.m,&time2.s);
  cal_time(&time1,&time2);
}
void cal_time(struct time_struct *t1,struct time_struct *t2)
{
  int res_h,res_m,res_s,temp;
  temp=(t1->h-t2->h)*3600+(t1->m-t2->m)*60+t1->s-t2->s;
  if(temp<0)
    temp=-temp;
  res_h=temp/3600;
  temp%=3600;
  res_m=temp/60;
  res_s=temp%60;
  printf("%d:%d:%d\n",res_h,res_m,res_s);
}


Tags:

指定位置输出字符串

| January 3, 2008 18:46 | timmy | Via Original
输入一个字符串和一个字符,如果该字符在字符串中,就从该字符首次出现的位置开始输出字符串中的字符。例如,输入字符r和字符串program后,输出rogram。要求定义函数match(s, ch),在字符串s中查找字符ch,如果找到,返回第一次找到的该字符在字符串中的位置(地址);否则,返回空指针NULL。
例:(括号内为说明)
输入
3(repeat=3)
happy
a
happy
y
happy
b
输出
appy
y
Not found!



#include <stdio.h>
#include <string.h>
char *match(char *s,char ch);
void main()
{
  char s[80],ch;
  int ri,repeat;
  scanf("%d",&repeat);
  getchar();
  for(ri=1;ri<=repeat;ri++){
    gets(s);
    ch=getchar();
    getchar();
    if(!match(s,ch))
      printf("Not found!\n");
    else
      puts(match(s,ch));
  }
}
char *match(char *s,char ch)
{
  while(*s!='\0')
    if(*s==ch)
      return(s);
    else
      s++;
  return(NULL);
}
Tags:

藏头诗

| January 3, 2008 18:45 | timmy | Via Original
所谓藏头诗,就是将这首诗每一句的第一个字(1个汉字占2个字符)连起来,所组成的
内容就是该诗的真正含义。编写一个函数char  *change(char  s[4][20])实现藏头诗的解密。
输入一首藏头诗(假设只有4句),输出其真实含义。
例:
输入:
一叶轻舟向东流,
帆稍轻握杨柳手。
风纤碧波微起舞,
顺水任从雅客流。
输出:
一帆风顺




#include <stdio.h>
char p[16];
void main()
{
  char  *change(char s[4][20]);
  char  s[4][20];
  int  i;
  for(i=0;i<4;i++)
    scanf("%s",s[i]);
  puts(change(s));
}
char  *change(char  s[4][20])
{
  int  i,t,k;
  k=0;
  for(i=0;i<4;i++)
    for(t=0;t<2;t++)
      p[k++]=s[i][t];
  return (p);
}
Tags:

计算最长的字符串长度

| December 11, 2007 19:25 | timmy | Via Original
编写一个函数int max_len(char *s[ ],  int n),用于计算有n(n<10)个元素的指针数组s中最长的字符串的长度,并编写主程序验证。
例:(括号内为说明)
输入
5
1
333
55555
22
4444
输出
The max_length is:5



#include <stdio.h>
#include <string.h>
int max_len(char *s[],int n);
void main()
{
  int i,n;
  char s[10][80],*p[10];
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%s",s[i]);
    p[i]=s[i];
  }
  printf("The max_length is:%d\n",max_len(p,n));
}
int max_len(char *s[],int n)
{
  int i,res,temp_len;
  res=0;
  for(i=0;i<n;i++)
    res=(strlen(s[i])>res) ? strlen(s[i]):res;
  return res;
}
Tags:

计算平均成绩

| December 11, 2007 19:24 | timmy | Via Original
程序填空。建立一个学生的结构记录,包括学号、姓名和成绩。输入整数n(n<10),再输入n个学生的基本信息,要求计算并输出他们的平均成绩(保留2位小数)。
例:
输入
3
1  zhang  70
2  wang  80
3  qian  90
输出
80


#include <stdio.h>
void main( )
{
   int n,i;
   float sum ,average;
struct student{
int num;
char name[10];
int score;
}s[10];
   scanf("%d", &n);
   sum=0;
   for(i=1;i<=n;i++){
     scanf("%d%s%d",&s[i].num,s[i].name,&s[i].score);
     sum+=s[i].score;
   }
   average=1.0*sum/n;
   printf("%.2f\n", average);
}
Tags:
Pages: 2/14 First page Previous page 1 2 3 4 5 6 7 8 9 10 Next page Final page [ View by Articles | List ]