一:
1.题目
2.程序:
//考察范围:1.结构体的定义,关键字使用。2.链表节点的相关操作。3.malloc的应用
#include <stdio.h>
#include <stdlib.h>
typedef struct aa
{ int data;//数据域
struct aa *next;//指针域
} NODE;
NODE *Creatlink(int n, int m)//n表示链表的节点个数,m表示节点的随机数
{ NODE *h=NULL, *p, *s;
int i;//用来做循环变量
/**********found***********/
p=(NODE * )malloc(sizeof(NODE));//分配内存单元
h=p;//h为链表的头结点
p->next=NULL;
for(i=1; i<=n; i++)//创建n个节点
{ s=(NODE *)malloc(sizeof(NODE));//s是新分配出来的节点
s->data=rand()%m; s->next=p->next;
p->next=s; p=p->next;//p始终指向链表的最后一个节点
}
/**********found***********/
return h;
}
void outlink(NODE *h)
{ NODE *p;
p=h->next;
printf("\n\nTHE LIST :\n\n HEAD ");
while(p)
{ printf("->%d ",p->data);
p=p->next;
}
printf("\n");
}
void main()
{ NODE *head;
head=Creatlink(8,22);
outlink(head);
system("pause");
}
运行结果:
二:
2.题目:
2.代码:
#include <stdio.h>
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n)
{ FILE *fp;
/**********found**********/
fp = fopen(filename, "rb+");
/**********found**********/
fseek(fp, -(long)sizeof(STU), SEEK_END);
/**********found**********/
fwrite(&n, sizeof(STU), 1, fp);
fclose(fp);
}
void main()
{ STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},
{10005,"ZhangSan", 95, 80, 88}};
STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe original data :\n\n");
for (j=0; j<N; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
fun("student.dat", n);
printf("\nThe data after modifing :\n\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j<N; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++) printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
}
3.运行结果
三:
1.题目
2.代码:
#include<string.h>
#include<stdio.h>
#define N 80
int fun(char *s)
{
int i;//循环遍历的变量
int count=0;//变量的个数初值为0
for(i=0;s[i]!='\0';i++){//s[i]不能等于空字符的情况下才能遍历
if(s[i]==' '|| s[i+1]=='\0'){//如果遇到了空格,统计单词数
count++;
}
}
return count;
}
void main()
{
FILE *wf;
char line[N];
int num=0;
printf("Enter a string:\n ");
gets(line);
num=fun(line);
printf("The number of word is:%d\n\n ",num);
/******************************/
wf=fopen("out.dat","w");
fprintf(wf,"%d",fun("a big car"));
fclose(wf);
/*****************************/
}
3.结果
但是若两个单词之间有好几个空格程序就不对了
4.修改if()
代码:
//考点1:注意空格,比如在两个单词之间插入了两个空格怎么解决
#include<string.h>
#include<stdio.h>
#define N 80
int fun(char *s)
{
int i;//循环遍历的变量
int count=0;//变量的个数初值为0
for(i=0;s[i]!='\0';i++){//s[i]不能等于空字符的情况下才能遍历
if(s[i]!=' '&&(s[i+1]==' '|| s[i+1]=='\0')){//如果遇到了空格,统计单词数
count++;
}
}
return count;
}
void main()
{
FILE *wf;
char line[N];
int num=0;
printf("Enter a string:\n ");
gets(line);
num=fun(line);
printf("The number of word is:%d\n\n ",num);
/******************************/
wf=fopen("out.dat","w");
fprintf(wf,"%d",fun("a big car"));
fclose(wf);
/*****************************/
}