题目34.文件的读写
题目要求:创建一个后缀名为.txt的文件。并向文件中写入一个字符串,保存起来。在打开该文件,读出文件中的内容。
用到的I/O函数:
(1)FILE *fopen(char *filename,char *type);//打开指定路径的文件
(2)int fclose(FILE *fp);//关闭文件
(3)int fread(void *buf,int size,int count,FILE *fp);//读文件函数
(4)int fwrite(void *buf,int size,int count,FILE *fp);//写文件函数
程序代码:
#include<stdio.h>
main(){
FILE * fp;
char txtname[20],txt1[30]={'/0'},txt2[30]={'/0'};
int len;
printf("please input the txtname:/n");
scanf("%s",txtname);
fp=fopen(txtname,"w");
printf("please input some character to the txt:/n");
scanf("%s",txt1);
len=strlen(txt1);
fwrite(txt1,len,1,fp);
fclose(fp);
printf("the file has been saved/n");
printf("the content of the file is:/n");
fp=fopen(txtname,"r");
fread(txt2,len,1,fp);
printf("%s/n",txt2);
getche();
}
题目35.计算文件的大小
题目要求:编写一个C程序,用来计算指定文件的大小。
常用的函数:
(1)fseek(FILE *fp,long offset,int base)重定位流上的文件指针,即将fp指向的文件的位置指针移向以base为基准,以offset为偏移量的位置。
(2)ftell(FILE *fp)返回当前文件指针的位置。这个位置是指当前文件指针相对于文件开头的位移量。
程序代码:
#include<stdio.h>
main(){
FILE * fp;
char txtname[20];
int len;
fp=fopen("D://hello.txt","r");
fseek(fp,0,SEEK_END);
len=ftell(fp);
fclose(fp);
printf("the length of the file is:%d/n",len);
}
程序说明:
(1)首先用fseek()函数重定位文件的指针,将文件的指针fp指向文件的尾部。
(2)然后通过ftell()函数将指针相对于文件开头的位移量,也就是文件大小返回。
3.记录程序的运行时间
题目要求:任意编写一段程序,要求记录并输出该段程序执行的时间。
#include<stdio.h>
#include<time.h>
#include<dos.h>
#include<windows.h>
main(){
clock_t start,end;
start=clock();
Sleep(300);
end=clock();
printf("**");
printf("the time was %s/n",(end-start));
}
程序存在调试问题
题目36.十进制/二进制转换器
题目要求:将输入的十进制数转换为二进制数表示。例如:输入十进制为64,输出二进制表示为10000000.
除2取余法的描述如下:
Repeat:
r=a/2;
s=a%2;
将s入栈Stack保存;
if r!=0 then a<-r
else 结束循环
程序代码:
#include<stdio.h>
#include<time.h>
#include<dos.h>
#include<windows.h>
void Change(int num){
int stack[20],shang,yu,i=0;
do{//当商不为0时
shang=num/2;//商
yu=num%2;//余数
stack[i]=yu;
if(shang!=0){
num=shang;//将a整除r的结果作为下一个整除2的对象。
i++;
}
}while(shang);//循环直到商为0位置。
for(;i>=0;i--)
printf("%d",stack[i]);
printf("/n");
}
main(){
int num;
printf("please input a decimal:/n");
scanf("%d",&num);
printf("the changed num is:/n");
Change(num);
}
题目37.打印特殊字符
在应用C语言开发程序时,有时为了程序运行界面的美观,需要在屏幕上用字符构成一些特殊的图案用以装饰。请设计一个C程序,实现在屏幕上输出一个类似于下面的图案。 *
***
*****
程序代码:
#include<stdio.h>
#include<time.h>
#include<dos.h>
#include<windows.h>
main(){
int num,i,j;
printf("please input a num as the row you want to show:/n");
scanf("%d",&num);
printf("the picture is:/n");
for(i=0;i<num;i++){
for(j=0;j<num-i;j++)
printf(" ");
for(j=0;j<2*i+1;j++)
printf("*");
printf("/n");
}
}
题目38.打印杨辉三角
题目要求:在屏幕上打印出一个6阶杨辉三角。
杨辉三角规律:(1)杨辉三角的两腰都为1.(2)杨辉三角除腰上的1以外的各数,都等于它“肩上”的两个数之和。因此可形象的抽象出这样的规律:杨辉三角中的第i行第j个数据的值(其中i不为0,1,;j不为1,i+1)等于第i-1行的第j-1和j个数据值之和;杨辉三角中的第i行第j个数据的值(其中i为0,1,;j为1,i+1)等于1.
算法分析:
开辟一个整形数组buf[7],它可存放7个整数。/*N=6时杨辉三角中数据为7个*/
i=0
repeat
if(i==0)打印1
if(i==1)打印两个1,并将两个1存放到buf[0],buf[1]中
else/*打印2-6行*/
j=1
repeat
if(j==0 or j==i+1)打印1
else 打印出buf[j-2]+buf[j-1]的值
j<-j+1
until j>i+1
将第i行的数据存入到buf[0]到buf[i]中
i<-i+1
until i>6
程序代码:
#include<stdio.h>
#include<time.h>
#include<dos.h>
#include<windows.h>
main(){
int i,j,k,buf[7],temp[7];
for(i=0;i<6;i++){
if(i==0)//打印第一行
printf("1/n");
if(i==1){//打印第二行
printf("1 1/n");
buf[0]=1;
buf[1]=1;
}
else{//打印2-6行
for(j=1;j<=i+1;j++){//注意j初始值
if(j==1 || j==i+1){//打印出两腰的1
printf("1 ");
temp[j-1]=1;//使用temp暂存两腰数值相加的结果
}
else{
printf("%d ",buf[j-2]+buf[j-1]);
temp[j-1]=buf[j-2]+buf[j-1];
}
}
printf("/n");
for(k=0;k<7;k++)
buf[k]=temp[k];
}
}
}
题目39.复杂级数的前n项和
题目要求:求级数的前n项和S10
程序代码:
#include<stdio.h>
#include<time.h>
#include<dos.h>
#include<windows.h>
main(){
int i,j;
double sum=0.0;
double m=1.0,n=1.0;
for(i=1;i<=10;i++){
for(j=1;j<=i;j++)
m=m*0.5;
for(j=1;j<=i;j++)
n=n*j;
sum=sum+m*n;
m=1.0;
n=1.0;
}
printf("the result is %g:/n",sum);
}
题目40.寻找矩阵中的“鞍点”
题目要求:在一个矩阵中,可能会有这样的元素:它在该行中最大,而在该列中最小。我们把这样的元素成为“鞍点”。一个矩阵中可能没有鞍点。任意输入一个5*5的矩阵,寻找该矩阵中的鞍点,并将它在矩阵中的位置(行,列)输出。
算法分析:(在一个矩阵中,最多出现一个鞍点)
对于一个m*n矩阵
i<-0;
Repeat:
找出第i行中最大的元素a[i][t];
if(本行中有与元素a[i][t]的值相等的元素)then本行中没有鞍点,跳出Repeat循环
else将元素a[i][t]与第t行的每一个元素逐一比较大小,
if(存在小于或等于a[i][t]的元素,则a[i][t]不是该矩阵的鞍点)then跳出Repeat循环
else a[i][t]是鞍点,返回该元素在矩阵中的位置(i,t),程序结束。
i<-i+1;
until i>=m
返回0,该矩阵中无鞍点.
程序代码:
#include<stdio.h>
void GetPoint();
main(){
int a[5][5];
int i,j;
printf("please input a Matrix(5,5)/n");
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf("%d",&a[i][j]);//输入矩阵元素
GetPoint(a);
}
void GetPoint(int a[5][5]){
int i,j;
int mr,col,row,flag=1;
for(i=0;i<5;i++){//在每一行中寻找鞍点
mr=a[i][0];
for(j=0;j<5;j++)
if(mr<a[i][j]){
mr=a[i][j];//找出每一行中的最大元素
col=j;//最大值对应的列
row=i;
}
for(j=0;j<5;j++)//该行中有两个相等的最大值
if(mr==a[i][j] && j!=col){
flag=0;
break;
}
if(flag==1)
for(i=0;i<5;i++){//对最大值对应的列进行比较
if(a[i][col]<=mr && i!=row){
flag=0;
break;
}
}
if(flag==1){
printf("鞍点所在的位置为:%d行,%d列/n",row+1,col+1);
}
else
printf("该矩阵没有鞍点/n");
}
}
题目41.n阶勒让德多项式求解
程序代码:
#include<stdio.h>
float Random(int n,float x){
if(n==0)
return 1;
else if(n==1)
return x;
else
return ((2*n-1)*x-Random(n-1,x)-(n-1)*Random(n-2,x))/n;
}
main(){
int n;
float p,x;
printf("please input a numnber n:/n");
scanf("%d",&n);
printf("please input a numnber x:/n");
scanf("%f",&x);
p=Random(n,x);
printf("the result of P(%d,%f) is %f/n",n,x,p);
}
题目42.递归反向输出字符串
题目要求:编写一个递归函数,实现将输入的任意长度的字符串反向输出的功能。例如输入字符串:ABCD,输出字符串:DCBA
程序代码:
#include<stdio.h>
void Print(){
char a;
scanf("%c",&a);
if(a!='#')
Print();
if(a!='#')
printf("%c",a);
}
main(){
printf("please input a string:/n");
Print();
printf("/n");
}
在该算法中,字符串的结束标志为#,并且#不作为字符串中的内容。首先输入字符串的一个字符,存放到变量a中。然后递归调用函数Print(),重复上述操作,知道输入字符串结束标志#为止,然后输出字符串中的字符。
题目43.一年中的第几天
题目要求:输入某年某月某日,判断这一天是这一年的第几天?
程序代码:
#include<stdio.h>
void Account(int year,int month,int day){
int i,days=0;
int months[13]={0,31,0,31,30,31,30,31,31,30,31,30,31};
if((year%4==0 && year%100!=0)||year%400==0)//闰年
months[2]=29;
else months[2]=28;
for(i=0;i<month;i++)
days+=months[i];
days+=day;
printf("today is the %d day of the year/n",days);
}
main(){
int year,month,day;
printf("please input a year and it's month and day:/n");
scanf("%d,%d,%d",&year,&month,&day);
Account(year,month,day);
}
程序中定义了一个数组months用来存放每月的天数。为了使每月的天数与months的下表对应,本数组第一个元素months[0]置0,从months[1]开始真正有效。