指针部分
1.从键盘输入n(个数不限)个整形数字,放到数组PArray中,然后调用函数int *Calc(int *PA,int Num);计算出数组中所有元素的和返回给main函数,并将其输出。注意:只能在/start/和/end/之间添加代码。注意:冒号全部使用英文的。
#include <stdio.h>
#include <stdlib.h>
int *Calc(int *PA,int Num);//计算数组中所有元素的和
int main()
{
int *PArray = NULL;
int Total = 0,i = 0;
printf("输入数组中元素的个数:\n");
scanf("%d",&Total);
/******start******/
PArray = (int *)malloc(Total*sizeof(int));
for(i=0;i<Total;i++)
{
scanf("%d",&PArray[i]);
}
Calc(PArray,Total);
printf("output:\n");
printf("数组元素的和是:%d",*Calc(PArray,Total));
/******end******/
printf("\n");
free(PArray);
return 0;
}
int *Calc(int *PA,int Num)
{
/******start******/
int *sum = (int *)malloc(sizeof(int));
*sum = 0;
int i=0;
for(i=0;i<Num;i++)
{
*sum = *sum+PA[i];
}
return sum;
/******end******/
}
2.假如有一个字符串a,通过函数gets键盘输入字符串a,然后把字符串a复制到字符串b,
字符串a的大小不超过30个字符。
要求:采用字符数组进行赋值:例如 b[i]=a[i];
程序运行界面:
#include <stdio.h>
int main()
{
char a[31], b[31];
int i;
printf("Please input a string a is:");
gets(a);
/******start******/
for(i=0;a[i]!='\0';i++)
{
b[i] = a[i];
}
/* 因为字符数组最后需要一个结束符
而循环体中只运行到 a[i]!='\0',
所以需要手动添加,因为循环体中有
i++,所以下面只需要 i 即可。
*/
b[i] = '\0';
/******end******/
printf("Output:\n");
printf("string b is:");
for(i=0;b[i]!='\0';i++)
printf("%c", b[i]);
printf("\n");
return 0;
}
3.通过键盘输入两整型变量a和b,然后定义俩指针变量p1和p2指向整型变量a和b,
然后a和b之间相互比较,a若小于b,则调用函数swap,实现a和b两数交换,
然后在按大小顺序输出。注意:只允许在/start/和/end/之间添加代码。
运行结果如下:
#include <stdio.h>
/******start******/
void swap(int *p1,int *p2);
/******end******/
int main()
{
int a,b;
int*pointer_1,*pointer_2;
printf("please enter a and b:");
scanf("%d,%d",&a,&b);
pointer_1=&a;
pointer_2=&b;
if(a<b) swap(pointer_1,pointer_2);
printf("Output:\nmax=%d,min=%d\n",a,b);
return 0;
}
/******start******/
void swap(int *p1,int *p2)
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
/******end******/
4.假设有一个数组a,整型,有5个元素,通过键盘输入数组各元素,要输出数组各元素的值。
注意:只允许在/start/和/end/之间添加代码。
程序运行界面
#include <stdio.h>
int main()
{
int a[5];
int *p,i;
printf("please enter 5 integer numbers:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Output:\n");
/******start******/
for(p=a,i=0;i<5;p++,i++)
/******end******/
{
printf("%d ",*p);
}
printf("\n");
return 0;
}
5.假设有一个数组a,整型,有5个元素,通过键盘输入数组各元素,要输出数组各元素的值。
要求:采用指针法,如*(a+i)形式输出数组各元素,其中a为数组名
程序运行结果如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a;
int i;
printf("please enter 5 integer numbers:");
/******start******/
// a 需要动态开辟内存地址
a = (int *)malloc(5*sizeof(int));
/******end******/
for(i=0;i<5;i++)
{
/******start******/
scanf("%d",a+i);
/******end******/
}
printf("Output:\n");
for(i=0;i<5;i++)
{
/******start******/
printf("%d ",*(a+i));
/******end******/
}
printf("\n");
return 0;
}
6.假设有一个数组a,整型,有5个元素,通过键盘输入数组各元素,要输出数组各元素的值。
要求:采用数组下标法,如a[i]形式输出数组各元素
程序运行界面如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5];
int i;
printf("please enter 5 integer numbers:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Output:\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
}
根据要求编写程序的指定部分:
程序已编写部分代码,请根据程序中的要求完善程序。注意:只允许在/start/和/end/之间添加代码。 程序的功能是:求S=1/1!+1/2!+1/3!+…+1/N!并输出结果。N为任意自然数(只考虑int型),从键盘读入。
程序的运行效果应类似地如图1所示,图1中的18是从键盘输入的内容。
#include <stdio.h>
void fun(double *sn, int n);
int main(void)
{
int n;
double Sum;
printf("Please input n: ");
scanf("%d", &n);
/* 本部分代码功能建议:调用相应的函数计算S */
/* Limit: lines<=1, lineLen<=50, 考生可在本行后添加代码、最多1行、行长<=50字符 */
/******start******/
fun(&Sum, n);
/******end******/
/* 考生添加代码结束 */
printf("Output:\nS=1/1!+1/2!+...+1/%d!=%.16f\n", n, Sum);
return 0;
}
void fun(double *sn, int n)
{
int i;
double Sum=0, jc=1;
for (i=1; i<=n; i++)
{
jc *= i;
Sum += 1 / jc;
}
/* 考生可在本行后添加代码、最多1行、行长<=30字符) */
/******start******/
*sn = Sum;
/******end******/
/* 考生添加代码结束。 */
}
8.假设有一个数组a,有5个元素,通过键盘输入数组5个元素。
定义一个指针变量p,指向数组首地址;然后调用函数inv实现将数组元素相反存放,
然后在屏幕输出相反顺序存放的数组元素。
注意:只允许在/start/和/end/之间添加代码。
程序运行界面如下:
#include <stdio.h>
void inv(int *x,int n);
int main()
{
int i,arr[5],*p=arr;
printf("The original array:\n");
for(i=0;i<5;i++,p++)
scanf("%d",p);
printf("Output:\n");
/******start******/
// 此处的含义是,因为前面的 for 循环使得指针 p 已经指向数组的末尾了
// 现在要重新指向首地址
p = arr;
/******end******/
inv(p,5);
printf("The array has been inverted:\n");
for(p=arr;p<arr+5;p++)
printf("%d ",*p);
printf("\n");
return 0;
}
void inv(int *x,int n)
{
/******start******/
int i;
int temp;
for(i=0;i<n/2;i++)
{
temp = *(x+i);
*(x+i) = *(x+4-i);
*(x+4-i) = temp;
}
/******end******/
}
9.假设有一个数组a,有5个元素,通过键盘输入数组5个元素。
然后调用函数inv实现将数组元素相反存放,
然后在屏幕输出相反顺序存放的数组元素。
注意:只允许在/start/和/end/之间添加代码。
程序运行界面如下:
#include <stdio.h>
void inv(int *x,int n);
int main()
{
int i,a[5];
printf("The original array:\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("Output:\n");
inv(a,5);
printf("The array has been inverted:\n");
for(i=0;i<5;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
void inv(int *x,int n) //形参x是指针变量
{
/******start******/
int temp;
int i;
for(i=0;i<n/2;i++)
{
temp = *(x+i);
*(x+i) = *(x+4-i);
*(x+4-i) = temp;
}
/******end******/
}
10.假设有一个数组a,有5个元素,通过键盘输入数组5个元素。
然后调用函数inv实现将数组元素相反存放,
然后在屏幕输出相反顺序存放的数组元素。
要求:其中反序函数inv的形参和实参都用数组名,即
函数声明原型为: void inv(int x[ ],int n)
注意:只允许在/start/和/end/之间添加代码。
程序运行界面如下:
#include <stdio.h>
void inv(int x[ ],int n);
int main()
{
int i,a[5]={0};
printf("The original array:\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]); // 输出未交换时数组各元素的值
printf("Output:\n");
inv(a,5); // 调用inv函数,进行交换
printf("The array has been inverted:\n");
for(i=0;i<5;i++)
printf("%d ",a[i]); // 输出交换后数组各元素的值
printf("\n");
return 0;
}
void inv(int x[ ],int n) // 形参x是数组名
{
/******start******/
int temp;
int i;
for(i=0;i<n/2;i++)
{
temp = *(x+i);
*(x+i) = *(x+4-i);
*(x+4-i) = temp;
}
/******end******/
}
11.假如有一个字符串a,b,通过函数gets键盘输入字符串a,b,然后在屏幕输出字符串a,b。
接着调用函数copy_string函数完成字符串a复制到字符串b,再在屏幕输出字符串a,b。
要求:字符串a的大小不超过30个字符。函数copy_string的原型声明为:void copy_string(char from[ ],char to[ ])
函数copy_string的功能为字符串from复制到字符串 to中,函数形参和实参都为数组名
程序运行界面:
#include <stdio.h>
void copy_string(char from[], char to[]);
int main()
{
char a[31];
char b[31];
printf("Please input a string a is:");
gets(a);
printf("Please input a string b is:");
gets(b);
printf("Output:\n");
printf("string a=%s\nstring b=%s\n",a,b);
printf("copy string a to string b:");
/******start******/
copy_string(a, b);
/******end******/
printf("\nstring a=%s\nstring b=%s\n",a,b);
return 0;
}
void copy_string(char from[], char to[])
{
/******start******/
int i;
for(from;from[i]!='\0';i++)
{
to[i] = from[i];
}
to[i] = '\0';
/******end******/
}
12.编写程序将两个数按大小输出。注意:请不要修改程序框架和结构,也不允许删除已有任何代码,只允许在/start/和/end/之间添加代码。
#include <stdio.h>
/******start******/
void fun(int *a,int *b);
/******end******/
int main()
{
int a,b;
printf("Input a, b:");
scanf("%d,%d",&a,&b);
printf("Output:\n");
fun(&a,&b);
return 0;
}
/******start******/
void fun(int *a,int *b)
{
if(*a>*b)
{
printf("max=%d,min=%d\n",*a,*b);
}
else if(*a<*b)
{
printf("max=%d,min=%d\n",*b,*a);
}
}
/******end******/
要求通过键盘输入5串字符串,然后调用函数sort完成字符串从小到大排列,然后调用print函数完成字符串输出。
提示:字符串输出可以采用gets函数,
sort函数原型为 void sort(char *name[],int n)
print函数原型为 void print(char *name[ ],int n)
多个字符串可以用字符串指针变量数组来描述
程序界面运行结果:
这道题采用了冒泡排序,关于冒泡排序算法的思路参见连接。
#include <stdio.h>
#include <string.h>
void sort(char *name[],int n);
void print(char *name[ ],int n);
main()
{
char *name[5];
char a[100],b[100],c[100],d[100],e[100];
int n=5,i=0;
name[0] = a;name[1] = b;name[2] = c;name[3] = d;name[4] = e;
printf("Please input 5 strings:");
while(i<5)
{
gets(name[i]);
i++;
}
printf("Output:\n");
printf("After the strings are sorted the result:\n");
sort(name,n);
print(name,n);
}
void sort(char *name[],int n)
{
char temp[100];
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
}
}
}
void print(char *name[ ],int n)
{
int i=0;
for(i=0;i<n;i++)
{
printf("%s\n",name[i]);
}
}
结构体部分
1.输入输出一个学生的基本信息(如图所示)。注意:只允许在/start/和/end/之间添加代码。
其中:
1001 zhang 19 87.5
是键盘输入的。
#include<stdio.h>
/******start******/
struct stu
{
int number;
char name[20];
int age;
float score;
};
main()
{
struct stu st1;
printf("Input the info:\n");
scanf("%d %s %d %f",&st1.number,st1.name,&st1.age,&st1.score);
/******end******/
printf("Output:\n");
printf("number:%d\n", st1.number);
printf("name:%s\n", st1.name);
printf("age:%d\n", st1.age);
printf("score:%.2f\n", st1.score);
return 0;
}
2.输入输出n个职员的基本信息(如图所示)。注意:只允许在/start/和/end/之间添加代码。
其中:
3
1001 zhao 100
1002 qian 150
1003 sun 130
是键盘输入的。
#include<stdio.h>
/******start******/
struct Em
{
int number;
char name[20];
float prize;
};
main()
{
struct Em em[50];
int n,i;
printf("Input the number(<50):\n");
scanf("%d",&n);
printf("Input the info:\n");
for(i=0;i<n;i++)
{
scanf("%d %s %f",&em[i].number,em[i].name,&em[i].prize);
}
/******end******/
printf("Output:\n");
printf("number\tname\tprize\n");
for (i = 0; i < n; i++)
{
printf("%d\t", em[i].number);
printf("%s\t", em[i].name);
printf("%.2f\n", em[i].prize);
}
return 0;
}
3.完成一个对候选人得票的统计程序。假设有3个候选人,名字分别为Li,Zhang和Fun。使用结构体存储每一个候选人的名字和得票数。记录每一张选票的得票人名,输出每个候选人最终的得票数。结构体可以定义成如下的格式:
struct person {
char name[20];
int count;
}leader[3] = {“Li”, 0, “Zhang”, 0, “Fun”, 0};
输入格式
第一行有一个整数n,表示以下有n张选票信息将会输入。保证n不大于100。
以后的n行中,每一行包含一个人名,为选票的得票人。保证每一个人名都是Li,Zhang和Fun中的某一个。
输出
有三行,分别为Li,Zhang和Fun每人的得票数。格式为首先输出人名,其后输出一个冒号,最后输出候选人的得票数。
请注意行尾输出换行。
样例如图:
/******start******/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct person {
char name[20];
int count;
}leader[3] = {"Li", 0, "Zhang", 0, "Fun", 0};
main()
{
int n;
int i;
char temp[20];
printf("Please input total number:\n");
scanf("%d",&n);
printf("Please input the candidate's name:\n");
for(i=0;i<n;i++)
{
scanf("%s",temp);
if(strcmp(temp,"Li")==0)
leader[0].count++;
else if(strcmp(temp,"Zhang")==0)
leader[1].count++;
else if(strcmp(temp,"Fun")==0)
leader[2].count++;
}
/******end******/
printf("Output:\nThe result is:\n");
for (i = 0;i < 3;i++)
{
printf("%s:%d\n", leader[i].name, leader[i].count);
}
return 0;
}
4.输入输出动物园的n个动物的基本信息(如图所示)。注意:只允许在/start/和/end/之间添加代码。
其中:
2
1 snake 9
2 monkey 23
是键盘输入的
#include<stdio.h>
struct animal
{
/******start******/
int No;
char name[20];
int count;
};
main()
{
struct animal ani[100];
int n,i;
printf("Input the number(<50):\n");
scanf("%d",&n);
printf("Input the info:\n");
for(i=0;i<n;i++)
{
scanf("%d %s %d",&ani[i].No,ani[i].name,&ani[i].count);
}
/******end******/
printf("Output:\n");
printf("No\tname\tnumber\n");
for (i = 0; i < n; i++)
{
printf("%d\t", ani[i].No);
printf("%s\t", ani[i].name);
printf("%d\n", ani[i].count);
}
return 0;
}
用一维数组存储学号和成绩,然后,按成绩排序输出。
输入格式
输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
输出
按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
如果学生的成绩相同,则按照学号的大小进行从小到大排序。
样例输入
这道题的考点在结构体之间的赋值,即结构体可以相互赋值。
#include <stdio.h>
#include <stdlib.h>
struct Student
{
int number;
int score;
};
main()
{
struct Student stu[100];
int n,i,j;
struct Student temp;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d",&stu[i].number,&stu[i].score);
}
printf("Output:\n");
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(stu[j].score>stu[j+1].score)
{
temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
else if(stu[j].score==stu[j+1].score)
{
if(stu[j].number>stu[j+1].number)
{
temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}
for(i=0;i<n;i++)
{
printf("%d %d\n",stu[i].number,stu[i].score);
}
}
6.设有10名歌手(编号为1-10)参加歌咏比赛,另有6名评委打分,每位歌手的得分从键盘输入:先提示“Please input singer’s score: ”,再依次输入第1个歌手的6位评委打分(10分制,分数为整型,分数之间使用空格分隔),第2个歌手的6位评委打分…以此类推。计算出每位歌手的最终得分(扣除一个最高分和一个最低分后的平均分,最终得分保留2位小数),最后按最终得分由高到低的顺序输出每位歌手的编号及最终得分
#include <stdio.h>
typedef struct _singer
{
int number;
int score[6];
float average;
} singer;
int main(void)
{
int i, j, sum, min, max;
singer temp;
singer people[10];
// 先录入序号
for (i = 0; i < 10; i++)
{
people[i].number = i + 1;
}
// 输入分数
printf("Please input singer's score: ");
for (i = 0; i < 10; i++)
{
for (j = 0; j < 6; j++)
{
scanf("%d", &people[i].score[j]);
if(people[i].score[j] > 10 || people[i].score[j] < 0)
{
printf("output:\n成绩必须为10分制:\n");
return 0;
}
}
}
//求平均成绩
for (i = 0; i < 10; i++)
{
sum = 0;
min = max = people[i].score[0];
// 找出 min 和 max
for (j = 0; j < 6; j++)
{
if (min > people[i].score[j])
{
min = people[i].score[j];
}
if (max < people[i].score[j])
{
max = people[i].score[j];
}
//计算总成绩
sum = sum + people[i].score[j];
}
people[i].average = (float)((sum - min - max) / 4.0);
}
//排序
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10 - i - 1; j++)
{
if (people[j].average < people[j + 1].average)
{
temp = people[j];
people[j] = people[j + 1];
people[j + 1] = temp;
}
}
}
//成绩输出
printf("output:\nscores:\n");
for (i = 0; i < 10; i++)
{
printf("No.%d:%.2f\n", people[i].number, people[i].average);
}
return 0;
}
文件
1.程序功能:将斐波拉契数列的前n项用二进制方式写入自命名文件中,然后从该文件中读取这n个数输出。
注:a1=1, a2=1, a3=2, …, an=an-1+an-2,只允许在/start/和/end/之间添加代码。
图中te.dat和10是键盘输入。
#include <stdio.h>
#include <stdlib.h>
void save(char str[20], int n);
void prnt(char str[20], int n);
int main()
{
int n;
char str[20];
printf("Input filename:");
scanf("%s", str);
printf("Input n:");
scanf("%d", &n);
save(str, n);
prnt(str, n);
return 0;
}
void save(char str[20], int n)
{
/******start******/
FILE *fp;
fp = fopen(str,"wb");
int f1=1,f2=1;
int i;
for(i=1;i<=n;i++)
{
if(i==1)
// 注意 fwrite 函数的第一个参数是一个地址
fwrite(&f1,sizeof(int),1,fp);
else if(i==2)
fwrite(&f2,sizeof(int),1,fp);
else if(i%2==1&&i!=1)
{
f1 = f1+f2;
fwrite(&f1,sizeof(int),1,fp);
}
else if(i%2==0&&i!=2)
{
f2 = f1+f2;
fwrite(&f2,sizeof(int),1,fp);
}
}
fclose(fp);
/******end******/
}
void prnt(char str[20], int n)
{
int i, te;
FILE *fp;
fp = fopen(str, "rb");
if (fp == NULL)
{
exit(0);
}
printf("Output:\n");
for (i = 0; i < n; i++)
{
fread(&te, sizeof(int), 1, fp);
printf("%d ", te);
}
printf("\n");
fclose(fp);
}
2.有文本文件lb8102.txt存储类点阵数字字库(每个数字8行5列)。从键盘输入n(0-9),输出该数字。
图中6是键盘输入的。
#include <stdio.h>
#include <stdlib.h>
main()
{
int n,i;
FILE *fp;
char s[20];
fp = fopen("lb8102.txt","r");
printf("Input n:");
scanf("%d",&n);
for(i=0;i<n*8;i++)
{
fgets(s,11,fp);
}
printf("Output:\n");
for(i=0;i<8;i++)
{
fgets(s,11,fp);
printf("%s",s);
}
}
3.有两个文本文件lb8103a.txt和lb8103b.txt。键盘输入需要读的文件名,然后从该文件中读出前n(<=26)个字符。
图1中的lb8103a.txt和3,图2中的lb8103b.txt和8,图3中的lb8103.txt和3是从键盘输入的。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
char filename[20];
char all[100];
int n,i;
FILE *fp;
printf("Input filename:");
scanf("%s",filename);
printf("Input n:");
scanf("%d",&n);
if(strcmp(filename,"lb8103a.txt")==0)
{
fp = fopen("lb8103a.txt","r");
fgets(all,n+1,fp);
printf("Output:\n");
for(i=0;i<n;i++)
{
printf("%c",all[i]);
}
}
else if(strcmp(filename,"lb8103b.txt")==0)
{
fp = fopen("lb8103b.txt","r");
fgets(all,n+1,fp);
printf("Output:\n");
for(i=0;i<n;i++)
{
printf("%c",all[i]);
}
}
else
{
printf("Output:\n");
printf("Open Error!\n");
}
}
4.按要求写前n个数据到文本文件lb8204.txt,然后从该文件读数据并显示。
写入的数据是1到1000中是7的倍数或者是数字中含7的数(如:37,71,72等)。
图中80是键盘输入的,每个数占4个宽度,每10个数换一行。
注:只允许在/start/和/end/之间添加代码。
#include <stdio.h>
#include <stdlib.h>
void save(int n);
void out(void);
int main(void)
{
int n;
printf("Input n:");
scanf("%d", &n);
save(n);
out();
return 0;
}
void save(int n)
{
/******start******/
FILE *fp;
fp = fopen("lb8204.txt","w");
int i,j=0;
for(i=1;i<=1000;i++)
{
// 49 17 171 711 测试数据
if(i%7==0 || i%10==7 || i/10%10==7 || i/100%10==7)
{
j++;
fprintf(fp,"%4d",i);
// 换行符也需要插入
if(j%10==0)
fprintf(fp,"\n");
if(j == n)
break;
}
}
fclose(fp);
/******end******/
}
void out(void)
{
char str[48];
char *pch = NULL;
FILE *fp;
fp = fopen("lb8204.txt", "r");
if (fp == NULL)
{
exit(0);
}
printf("Output:\n");
while (!feof(fp))
{
pch = fgets(str, 48, fp);
if (pch == NULL)
{
break;
}
printf("%s", str);
}
printf("\n");
fclose(fp);
}
程序功能:将斐波拉契数列的前n项用二进制方式写入自命名文件中,然后从该文件中读取这n个数输出。
注:a1=1, a2=1, a3=2, …, an=an-1+an-2,只允许在/start/和/end/之间添加代码。
图中te.dat和10是键盘输入。
#include <stdio.h>
#include <stdlib.h>
void save(char str[20], int n);
void prnt(char str[20], int n);
int main()
{
int n;
char str[20];
printf("Input filename:");
scanf("%s", str);
printf("Input n:");
scanf("%d", &n);
save(str, n);
prnt(str, n);
return 0;
}
void save(char str[20], int n)
{
/******start******/
FILE *fp;
fp = fopen(str,"wb");
int f1=1;
int f2=1;
int i;
for(i=1;i<=n;i++)
{
if(i==1)
{
fwrite(&f1,sizeof(int),1,fp);
}
else if(i==2)
{
fwrite(&f2,sizeof(int),1,fp);
}
else if(i%2!=0&&i!=1)
{
f1 = f1+f2;
fwrite(&f1,sizeof(int),1,fp);
}
else if(i%2==0&&i!=2)
{
f2 = f1+f2;
fwrite(&f2,sizeof(int),1,fp);
}
}
fclose(fp);
/******end******/
}
void prnt(char str[20], int n)
{
int i, te;
FILE *fp;
fp = fopen(str, "rb");
if (fp == NULL)
{
exit(0);
}
printf("Output:\n");
for (i = 0; i < n; i++)
{
fread(&te, sizeof(int), 1, fp);
printf("%d ", te);
}
printf("\n");
fclose(fp);
}
6.文本文件lb8101.txt中存放了50个整数。从键盘输入n值,求该文件中后n个数的和。
图中13是键盘输入的。
#include <stdio.h>
int main(void)
{
int i, n, sum = 0;
int arr[50];
FILE *fp;
fp = fopen("lb8101.txt", "r");
printf("Input n:");
scanf("%d", &n);
for (i = 0; i < 50; i++)
{
fscanf(fp, "%d", &arr[i]);
}
for (i = 50-n; i < 50; i++)
{
sum += arr[i];
}
printf("Output:\n%d\n", sum);
return 0;
}
7.编写一程序D1402.C实现以下功能
程序运行时,先从键盘输入一个文本文件的文件名(约定:字符数≤127字节,可含路径)和一个字符串(约定:字符数≤20字节,其中不含空格、TAB等,后面称之为Str),再在屏幕上显示该文件的内容。要求显示完内容后,在屏幕上输出文件的行数(行之间以’\n’为分隔、每行的长度不定但均≤200个字节)、字符串Str在文件中第1次出现的行号和最后一次出现的行号(查找时不区分大小写、不跨行查找,若未找到,则行号显示为-1)。注意,程序中不能使用库函数fgets或使用同名的变量、函数、单词,行的编号从1开始计。
下载程序运行时测试用的文件Test.txt。编程可用素材:printf(“input the file’s name and the string: “)…、printf(”\nfile open error!”)…、printf(“------------------------File content:----------------------\n”)…、printf(“\n------------------------File summary:----------------------\n”)…、printf(“… lines, first line: …, last line: …\n”…。
程序的运行效果应类似地如图1所示,图1中的“input the file’s name and the string: test.txt value”中的“test.txt value”是从键盘输入的内容(“test.txt”是文件名,“value”是需查找的字符串)。图1中的“10 lines, first line: 6, last line: 9”表示文件一共有10行,字符串“value”在文件中第一次出现的行号为6、最后一次出现的行号为9。不存在的字符串,出现行号为-1。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char filename[20];
char String[127];
// Str 用于存放临时匹配的数组
char Str[127];
char ch;
int line=1;
int i=0,j=0,k;
// linecount 用于记录匹配到 String 字符的行数
int linecount[100];
for(i=0;i<100;i++)
{
linecount[i] = 1000;
}
// flag 用于确定是否文章中有匹配的字符
int flag = 0;
FILE *fp;
fp = fopen("Test.txt","r");
if(fp==NULL)
{
printf("\nfile open error!");
}
printf("input the file's name and the string: ");
scanf("%s %s",filename,String);
printf("Output:\n");
printf("------------------------File content:----------------------\n");
while(!feof(fp))
{
ch = fgetc(fp);
if(ch=='\n')
line++;
putchar(ch);
if((ch>='A'&&ch<='Z') || (ch>='a'&&ch<='z'))
{
// 如果输入的字符是大写,转换成小写
if((ch>='A'&&ch<='Z'))
{
ch = ch+32;
}
Str[i] = ch;
i++;
}
else if(ch==' '||ch==','||ch=='.'||ch=='*'||ch=='_'||ch=='('||ch==')'||ch==';' || ch=='\n')
{
if(strcmp(Str,String)==0)
{
flag = 1;
linecount[j] = line;
j++;
}
for(k=0;k<127;k++)
{
Str[k] = '\0';
}
// i 置零开始新的匹配
i=0;
}
}
printf("\n");
printf("------------------------File summary:----------------------\n");
// 判断是否有数字
if(flag==0)
{
printf("%d lines, first line: -1, last line: -1\n",line);
}
else
{
j=0;
for(i=0;linecount[i]!=1000;i++)
{
j = i;
}
printf("%d lines, first line: %d, last line: %d\n",line,linecount[0],linecount[j]);
}
return 0;
}