早上爬起来看了阿根廷和荷兰的比赛,没有迪马利亚的阿根廷踢得还真累,最后120分钟的0:0换来了点球大战,很怀疑阿根廷是不是总在培养点球大将,4:2的优势取胜,简直nice。罗梅罗,这个守门员还真是值得表扬啊。时隔24年,阿根廷竟在巴西的土地上踢进了决赛,期待决赛的阿根廷和德国,尽管大多数人认为德国实力比较强,但个人还是喜欢这支阿根廷队,梅西,伊瓜因,迪马利亚,阿圭罗,加油。
接着打代码,早上看完球赛补了会儿觉,一下子就到中午了,看来下午晚上得加把劲了,尽量今天把这本书结束,还有好多事等着我做呢。不过得先看书,指针这一部分得好好看看,还有后面的链表部分,数据结构里也要讲到,得看看。
看了三个小时的指针,虽然中间也有休息一下,不过感觉效率明显没有上个学期刚学的时候好,但是却有一份别样的收获,又说不出来懂了什么,指针毕竟是C语言的精华所在,还是得多看看。大概等到学了别的计算机的知识后,在回头看几遍的时候,可能又会有新发现,而并非目前的浅显的只会读而不擅长写,不过也是缺乏练习的原因,因为很少在编程中用指针,当然跟没练多少有关,没怎么碰到非用指针不可的地方,也算是恶性循环吧,导致指针的不熟悉。
25.通过指针变量访问整型变量。
#include
#include
int main()
{
int a,b;
int *pointer_1,*pointer_2;
a=100;b=10;
pointer_1=&a;
pointer_2=&b;
printf("%d,%d\n",a,b);
printf("%d,%d\n",*pointer_1,*pointer_2);
return 0;
}
26.输入a和b两个整数,按先大后小的顺序输出a和b。
#include
#include
int main()
{
int *p1,*p2,*p,a,b;
scanf("%d,%d",&a,&b);
p1=&a;p2=&b;
if(a
用函数处理,并用指针类型数据作函数参数。
#include
#include
int main()
{
void swap(int *p1,int *p2);
int a,b;
int *pointer_1,*pointer_2;
scanf("%d,%d",&a,&b);
pointer_1=&a;pointer_2=&b;
if(a
27.用选择法对10个整数按由大到小顺序排序。
#include
#include
int main()
{
void sort(int x[],int n);
int *p,i,a[10];
p=a;
for(i=0;i<10;i++)
scanf("%d",p++);
p=a;
sort(p,10);
for(p=a,i=0;i<10;i++)
{
printf("%d",*p);
p++;
}
return 0;
}
void sort(int x[],int n)
{
int i,j,k,t;
for(i=0;ix[k]) k=j;
if(k!=i)
{
t=x[i];x[i]=x[k];x[k]=t;
}
}
}
28.输出二维数组有关的值。
#include
#include
#define FORMAT "%d,%d\n"
int main()
{
int a[3][4]={1,3,5,7,9,11,13,15,17,19,21,23};
printf(FORMAT,a,*a);
printf(FORMAT,a[0],*(a+0));
printf(FORMAT,&a[0],&a[0][0]);
printf(FORMAT,a[1],a+1);
printf(FORMAT,&a[1][0],*(a+1)+0);
printf(FORMAT,a[2],*(a+2));
printf(FORMAT,&a[2],a+2);
printf(FORMAT,a[1][0],*(*(a+1)+0));
return 0;
}
29.将字符串a复制为字符串b。
#include
#include
int main()
{
char a[]="I am a boy.",b[20];
int i;
for(i=0;*(a+i)!='\0';i++)
*(b+i)=*(a+i);
*(b+i)='\0';
printf("string a is:%s\n",a);
printf("string b is:");
for(i=0;b[i]!='\0';i++)
printf("%c",b[i]);
printf("\n");
return 0;
}
用指针变量处理。
#include
#include
int main()
{
char a[]="I am a boy.",b[20],*p1,*p2;
int i;
p1=a;p2=b;
for(;*p1!='\0';p1++,p2++)
*p2=*p1;
*p2='\0';
printf("string a is:%s\n",a);
printf("string b is:");
for(i=0;b[i]!='\0';i++)
printf("%c",b[i]);
printf("\n");
return 0;
}
30.用指向函数的指针作函数参数:设一个函数process,在调用它的时候,每次实现不同的功能。输入a和b两个数,第一次调用process时找出a和b中大者,第二次找出其中小者,第三次求a与b之和。
#include
#include
int main()
{
int max(int,int);
int min(int,int);
int add(int,int);
void process(int,int,int(*fun)(int,int));
int a,b;
printf("enter a and b:");
scanf("%d,%d",&a,&b);
printf("max=");
process(a,b,max);
printf("min=");
process(a,b,min);
printf("sum=");
process(a,b,add);
return 0;
}
int max(int x,int y)
{
int z;
if(x>y) z=x;
else z=y;
return(z);
}
int min(int x,int y)
{
int z;
if(x
31.返回指针值的函数:由若干个学生的成绩(每个学生有4门课程),要求在用户输入学生序号后,能输出该学生的全部成绩。
#include
#include
int main()
{
float score[][4]={{60,70,80,90},{56,89,67,88},{34,78,90,66}};
float *search(float (*pointer)[4],int n);
float *p;
int i,m;
printf("enter the number of student:");
scanf("%d",&m);
printf("The scores of No. %d are:\n",m);
p=search(score,m);
for(i=0;i<4;i++)
printf("%5.2f\t",*(p+i));
printf("\n");
return 0;
}
float *search(float (*pointer)[4],int n)
{
float *pt;
pt=*(pointer+n);
return(pt);
}
32.指针数组:将若干字符串按字母顺序(由小到大)输出。
#include
#include
#include
int main()
{
void sort(char *name[],int n);
void print(char *name[],int n);
char *name[]={"Follow me","BASIC","Great Wall","FORTRAN","Computer design"};
int n=5;
sort(name,n);
print(name,n);
return 0;
}
void sort(char *name[],int n)
{
char *temp;
int i,j,k;
for(i=0;i0) k=j;
if(k!=i)
{
temp=name[i];name[i]=name[k];name[k]=temp;
}
}
}
void print(char *name[],int n)
{
int i;
for(i=0;i
33.指向指针的指针:
#include
#include
int main()
{
char *name[]={"Follow me","BASIC","Great Wall","FORTRAN","Computer design"};
char **p;
int i;
for(i=0;i<5;i++)
{
p=name+i;
printf("%s\n",*p);
}
return 0;
}
34.指针数组的元素指向整型数据。
#include
#include
int main()
{
int a[5]={1,3,5,7,9};
int *num[5]={&a[0],&a[1],&a[2],&a[3],&a[4]};
int **p,i;
p=num;
for(i=0;i<5;i++)
{
printf("%d ",**p);
p++;
}
printf("\n");
return 0;
}
指针部分终于告一段落了,用了一个下午的时间,还是蛮值得的,在打代码的时候,也在想一些事情,想想现在,竟然能在暑假里把大把大把的时间放在学习上,甚至超越了当初高三的时候,因为后悔,后悔当初没有再加把劲,考个更加好的学校,也许未来的路会更加平坦一些,可能机会会更多一点,可惜,现在后悔还有什么用。起码,唯一庆幸的是,当初没有更差,也许也正是因为考到了并非名校,才会有那么一段经历,才会意识到努力的重要性,才知道什么叫诱惑,什么叫抵制诱惑。
35.对候选人得票的统计程序。设有3个候选人,每次输入一个得票的候选人的名字,要求最后输出各人得票结果。
#include
#include
#include
struct person
{
char name[20];
int count;
}leader[3]={"Li",0,"Zhang",0,"Fun",0};
int main()
{
int i,j;
char leader_name[20];
for(i=1;i<=10;i++)
{
scanf("%s",leader_name);
for(j=0;j<3;j++)
if(strcmp(leader_name,leader[j].name)==0) leader[j].count++;
}
printf("\n");
for(i=0;i<3;i++)
printf("%5s:%d\n",leader[i].name,leader[i].count);
return 0;
}
36.指向结构体变量的指针的应用。
#include
#include
#include
int main()
{
struct student
{
long num;
char name[20];
char sex;
float score;
};
struct student stu_1;
struct student *p;
p=&stu_1;
stu_1.num=89101;
strcpy(stu_1.name,"Li Lin");
stu_1.sex='M';
stu_1.score=89.5;
printf("No.:%ld\nname:%s\nsex:%c\nscore:%f\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
printf("No.:%ld\nname:%s\nsex:%c\nscore:%f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
return 0;
}
37.指向结构体数组的指针的应用。
#include
#include
struct student
{
int num;
char name[20];
char sex;
int age;
};
struct student stu[3]={{10101,"Li Lin",'M',18},{10102,"Zhang Fun",'M',19},{10104,"Wang Min",'F',20}};
int main()
{
struct student *p;
printf("No. Name sex age\n");
for(p=stu;pnum,p->name,p->sex,p->age);
return 0;
}
38.对链表的综合操作(建立,输出,删除,插入):
#include
#include
#include
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %5.1f\n",p->num,p->score);
p=p->next;
} while(p!=NULL);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL) {printf("\nlist null! \n");goto end;}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{p2=p1;p1=p1->next;}
if(num==p1->num)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("delete:%ld\n",num);
n=n-1;
}
else printf("%ld not been found! \n",num);
end:
return(head);
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else {p1->next=p0;p0->next=NULL;}
}
n=n+1;
return(head);
}
int main()
{
struct student *head,*stu;
long del_num;
printf("input records:\n");
head=creat();
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
while(del_num!=0)
{
head=del(head,del_num);
print(head);
printf("input the deleted number:");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{
head=insert(head,stu);
print(head);
printf("input the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
return 0;
}
目测是到现在为止编过的最长的程序了,不过记得当时自己编过这个链表,貌似也是参考的书上的程序,链表的操作,先缓一缓,因为数据结构里还会学,所以不急。先把书看完再说。
39.用共用体处理学生和教师的数据,学生的数据包括:姓名、号码、性别、职业、班级,教师的数据包括:姓名、号码、性别、职业、职务。
#include
#include
struct
{
int num;
char name[10];
char sex;
char job;
union
{
int banji;
char position[10];
}category;
}person[2];
int main()
{
int i;
for(i=0;i<2;i++)
{
scanf("%d %s %c %c",&person[i].num,&person[i].name,&person[i].sex,&person[i].job);
if(person[i].job=='s')
scanf("%d",&person[i].category.banji);
else if(person[i].job=='t')
scanf("%s",person[i].category.position);
else printf("input error!");
}
printf("\n");
printf("No. Name sex job class/position\n");
for(i=0;i<2;i++)
{
if(person[i].job=='s')
printf("%-6d%-10s%-3c%-3c%-6d\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].category.banji);
else
printf("%-6d%-10s%-3c%-3c%-6s\n",person[i].num,person[i].name,person[i].sex,person[i].job,person[i].category.position);
}
return 0;
}
40.口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋中先后取出3个球,问得到3种不同色的球的可能取法,输出每种排列的情况。
#include
#include
int main()
{
enum color {red,yellow,blue,white,black};
enum color i,j,k,pri;
int n,loop;
n=0;
for(i=red;i<=black;i++)
for(j=red;j<=black;j++)
if(i!=j)
{
for(k=red;k<=black;k++)
if((k!=i)&&(k!=j))
{
n=n+1;
printf("%-4d",n);
for(loop=1;loop<=3;loop++)
{
switch(loop)
{
case 1:pri=i;break;
case 2:pri=j;break;
case 3:pri=k;break;
default:break;
}
switch(pri)
{
case red: printf("%-10s","red");break;
case yellow: printf("%-10s","yellow");break;
case blue: printf("%-10s","blue");break;
case white: printf("%-10s","white");break;
case black: printf("%-10s","black");break;
default: break;
}
}
printf("\n");
}
}
printf("\ntotal:%5d\n",n);
return 0;
}
41.从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个”#“为止。
#include
#include
int main()
{
FILE *fp;
char ch,filename[10];
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=getchar();
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);putchar(ch);
ch=getchar();
}
putchar(10);
fclose(fp);
return 0;
}
42.将一个磁盘文件中的信息复制到另一个磁盘文件中。
#include
#include
int main()
{
FILE *in,*out;
char ch,infile[10],outfile[10];
printf("Enter the infile name:\n");
scanf("%s",infile);
printf("Enter the outfile name:\n");
scanf("%s",outfile);
if((in=fopen(infile,"r"))==NULL)
{
printf("cannot open outfile\n");
exit(0);
}
while(!feof(in)) fputc(fgetc(in),out);
fclose(in);
fclose(out);
return 0;
}