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

实验1__熟悉Project组织应用程序

| February 28, 2008 12:06 | timmy | Via Original
test1.cpp

#include<iostream.h>
#include<math.h>
#include "test1.h"
void main()
{
  int i,a[10],N;
  int res_max,res_min,res_aver;
  int max,min,aver;
  N=10;
  for(i=0;i<N;i++)
    cin>>a[i];
  cout<<"Testing Function aMAX:";
  res_max=aMAX(a,N);
  cout<<"max="<<res_max<<endl;
  cout<<"Testing Function aMIN:";
  res_min=aMIN(a,N);
  cout<<"min="<<res_min<<endl;
    cout<<"Testing Function aAVE:";
  res_aver=aAVE(a,N);
  cout<<"aver="<<res_aver<<endl;
  cout<<"Testing Function aMAX_MIN_AVE:";
  aMAX_MIN_AVE(a,N,max,min,aver);
  cout<<"Prime_sum="<<prime_SUM(a,N)<<endl;
  cout<<"After sort:"<<endl;
  aSORT(a,N);
}

test1.h

int aMAX(int *a,int n)
{
  int i,res;
  res=a[0];
  for(i=1;i<n;i++)
    if(res<a[i]) res=a[i];
  return res;
}
int aMIN(int *a,int n)
{
  int i,res;
  res=a[0];
  for(i=1;i<n;i++)
    if(res>a[i]) res=a[i];
  return res;
}
int aAVE(int *a,int n)
{
  int res,i;
  res=0;
  for(i=0;i<=n;i++)
    res+=a[i];
  res/=n;
  return res;
}
void aMAX_MIN_AVE(int *a, int n, int &max, int &min, int &aver)
{
  int i;
  max=min=aver=a[0];
  for(i=1;i<n;i++){
    if(max<a[i]) max=a[i];
    if(min>a[i]) min=a[i];
    aver+=a[i];
  }
  aver/=n;
  cout<<"max="<<max<<" "<<"min="<<min<<" "<<"aver="<<aver<<endl;
}
int prime(int m)
{
  int i,y;
  if(m!=1){
    for(i=2;i<=sqrt(m);i++)
      if(m%i==0) break;
      if(i>sqrt(m))
        y=1;  
      else
        y=0;
  }
  else
    y=0;
  return y;
}
int prime_SUM(int *a, int n)
{
  int i,res;
  res=0;
  for(i=0;i<n;i++)
    if(prime(a[i]))
      res+=a[i];
  return res;
}
void aSORT(int *a,int n)
{
  int k,i,index,temp;
  for(k=0; k<n; k++){
    index=k;
    for(i=k+1;i<n;i++)
      if(a[i]<a[index])
        index=i;
    temp=a[index];
        a[index]=a[k];
        a[k]=temp;
    }
  for(i=0;i<n;i++)
    cout<<a[i]<<" ";
  cout<<endl;
}

Download ( 4 downloads)
Only registered users can download this file. Please Register or Login

Tags: ,

比较两个文件内容

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