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

比较两个文件内容

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

#include <stdio.h>
#include <stdlib.h>
void main()
{
  FILE *f1,*f2;
  int l,c;
  char ch1,ch2;
  if((f1=fopen("f1.txt","r"))==NULL){
    printf("File open error\n");
    exit(0);
  }
  if((f2=fopen("f2.txt","r"))==NULL){
    printf("File open error\n");
    exit(0);
  }
  l=c=1;
  while(!feof(f1)&&!feof(f2)){
    ch1=fgetc(f1);
    ch2=fgetc(f2);
    if(ch1!=ch2)
      break;
    else
      c++;
    if(ch1=='\n'){
      c=1;
      l++;
    }
  }
  if(fclose(f1)){
    printf("can't close file\n");
    exit(0);
  }
  if(fclose(f2)){
    printf("can't close file\n");
    exit(0);
  }
  printf("l=%d,c=%d\n",l,c);
}
Tags:

统计文件中字符

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

#include <stdio.h>
#include <stdlib.h>
void main()
{
  FILE *fp;
  char ch;
  int letter,digit,other;
  letter=digit=other=0;
  if((fp=fopen("a.txt","r"))==NULL){
    printf("File open error!\n");
    exit(0);
  }
  while(!feof(fp)){
    ch=fgetc(fp);
    if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
      letter++;
    else if(ch>='0'&&ch<='9')
      digit++;
    else
      other++;
  }
  printf("letter=%d,digit=%d,other=%d\n",letter,digit,other);
}

Tags:

连接两个链表

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct worker_struct{
  char name[20];
  int salary;
  struct worker_struct *next;

};
void main()
{
  void print_work(struct worker_struct *head);
  struct worker_struct *creat();
  struct worker_struct *link(struct worker_struct *list1,struct worker_struct *list2);
    struct worker_struct *list1,*list2;              
  list1=creat();
  list2=creat();
    link(list1,list2);
    print_work(list1);    
}
struct worker_struct *creat()
{
  struct worker_struct *head,*tail,*p;
  int salary;
  char name[20];
  int size=sizeof(struct worker_struct);
  head=tail=NULL;
  printf("Input name salary:\n");
  scanf("%s%d",name,&salary);
  while(salary>=0){
       p=(struct worker_struct *)malloc(size);
       strcpy(p->name,name);    
       p->salary=salary;
       p->next=NULL;
       if(head==NULL)
         head=p;
       else
         tail->next=p;
       tail=p;
       scanf("%20s%6d",name,&salary);
  }
  return head;
}  
void print_work(struct worker_struct *head)
{
      struct worker_struct *ptr;
    if(head==NULL){
      printf("\nNO Records\n");
      return;
    }
    printf("\nThe Workers' Records Are: \n");
    printf("   Name      salary\n");
    for(ptr=head;ptr;ptr=ptr->next)
    printf("    %s      %6d\n",ptr->name,ptr->salary);
}
struct worker_struct *link(struct worker_struct *list1,struct worker_struct *list2)
{

   struct worker_struct *ptr,*ptr1,*ptr2;
   ptr1=list1;
   ptr2=list1->next;
   ptr=list2;
   while(ptr2!=NULL)
   {
     ptr1=ptr2;
     ptr2=ptr1->next;
   }
   ptr1->next=ptr;
   return list1;
}
Tags:

结构指针:计算某日是该年中的第几天

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

#include <stdio.h>
struct year_month_day {
  int year;
  int month;
  int day;
};
int day_of_year(struct year_month_day *t);
void main()
{
  struct year_month_day ymd;
  printf("Input year,month,day: ");
  scanf("%d%d%d",&ymd.year,&ymd.month,&ymd.day);
  printf("%d\n",day_of_year(&ymd));
}
int day_of_year(struct year_month_day *t)
{
  int k,leap,res;
  int tab[2][13]={
    {0,31,28,31,30,31,30,31,31,30,31,30,31},
    {0,31,29,31,30,31,30,31,31,30,31,30,31}
  };
  leap=(t->year%4==0&&t->year%100!=0||t->year%400==0);
  res=t->day;
  for(k=1;k<t->month;k++)
    res+=tab[leap][k];
  return res;
}
Tags:

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

| 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:
Pages: 1/14 First page 1 2 3 4 5 6 7 8 9 10 Next page Final page [ View by Articles | List ]