c语言文件编程题
在C语言中,“^”是双目运算符,用于完成两个数据的按位异或操作。该运算符可以用来进行两个数据的比较或者对一个数据中的某些位进行处理。
1、if(a^0x8)a=0;//如果a不等于08H则a=0
2、a=a^0xf0;//将a的高4位取反低4位不变后存回a
㈡ C语言编程题
long fun(int k)
{
if(i<2)
return 1L;
return k*fun(k-1);
}
或:
#include "stdio.h"
main()
{
double h,c;
//printf("Input h ");
scanf("%lf",&h);
c=5.0/9*(h-32);
printf("c=%lf",c);
}
(2)c语言文件编程题扩展阅读:
C语言包含的各种控制语句仅有9种,关键字也只有32 个,程序的编写要求不严格且以小写字母为主,对许多不必要的部分进行了精简。实际上,语句构成与硬件有关联的较少,且C语言本身不提供与硬件相关的输入输出、文件管理等功能,如需此类功能,需要通过配合编译系统所支持的各类库进行编程,故c语言拥有非常简洁的编译系统。
㈢ C语言的一道数据文件的编程题
void readBytes(FILE *fp,long int nOffset,int nCount) //a
{ int i;
char c;
fseek(fp,nOffset,SEEK_SET);
for ( i=0;i<nCount;i++ ) { fread(&c,1,1,fp); printf("%c",c); }
printf("\n");
}
void readBytes(FILE *fp,long int nOffset,int nCount,char *buffer) //b
{ fseek(fp,nOffset,SEEK_SET);
fread(buffer,1,nCount,fp);
}
㈣ 关于C语言里文件的编程题!考试要用的!急用!多谢!
int=1;
使用while(1){getline逐行读取数据;i++;如果该数据小于50则break}
使用while(1)循环, 读取下一行,{如果大于上一个数据number,并且小于50,则RecordNumber=i;number=y,否则继续读取下一行比较}
最后输出i就可以了
㈤ c语言编程题
用一个简单的函数说吧。
main()
{
printf("你好!\n");
}
说明:
l
main是主函数的函数名,表示这是一个主函数。
l
每一个C源程序都必须有,且只能有一个主函数(main函数)。
l
函数调用语句,printf函数的功能是把要输出的内容送到显示器去显示。
printf函数是一个由系统定义的标准函数,可在程序中直接调用。如果不是标准函数,就要用到预处理命令了,比如说如果你要求sin值,那么要用到sin(),而这个不是标准函数,就要用预处理命令把含有该函数的函数文件加到程序里面,sin()是数学函数,就在原来的第一行上面插入一行#include<math.h>
㈥ C语言文件编程题!!急!!!
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>void main()
{
int *buf1,*buf2;
int *num,i,j,k,tmp,filelen1,filelen2,numlen1,numlen2,numlen;
FILE *fp1,*fp2,*fp3;
if((fp1=fopen("f1.c","r"))==NULL)
{
printf("open f1 error!\n");
exit(0);
}
if((fp2=fopen("f2.c","r"))==NULL)
{
printf("open f2 error!\n");
exit(0);
}
if((fp3=fopen("f3.c","w"))==NULL)
{
printf("open f3 error!\n");
exit(0);
}
fseek(fp1, 0, SEEK_END);
filelen1 = ftell(fp1);
fseek(fp2, 0, SEEK_END);
filelen2 = ftell(fp2); buf1=(int*)malloc(sizeof(int)*filelen1);
buf2=(int*)malloc(sizeof(int)*filelen2);
numlen1=0;
numlen2=0;
rewind(fp1); //指针移到文件开始
rewind(fp2); //指针移到文件开始 i=0;
while(!feof(fp1))
{
fscanf(fp1,"%d",&buf1[i++]);//读取文件中的数字
numlen1++;
}
for(i=0;i<numlen1;i++)
printf("%d ",buf1[i]);
printf("\n"); i=0;
while(!feof(fp2))
{
fscanf(fp2,"%d",&buf2[i++]);//读取文件中的数字
numlen2++;
}
for(i=0;i<numlen2;i++)
printf("%d ",buf2[i]);
printf("\n"); numlen=numlen1+numlen2;
num=(int*)malloc(sizeof(int)*(numlen));//num存储所有数字
for(i=0,j=0;i<numlen;i++)
{
if(i<numlen1)
num[i]=buf1[i];//buf1中的数字存到num中
else
num[i]=buf2[j++];//buf2中的数字存到num中
} for(i=0;i<numlen;i++)
printf("%d ",num[i]);
printf("\n"); for(j=0;j<numlen-1;j++)
{
for(i=0;i<numlen-1-j;i++)
if(num[i]<num[i+1]) //冒泡法排序
{
tmp=num[i];
num[i]=num[i+1];
num[i+1]=tmp;
}
}
for(i=0;i<numlen;i++)
printf("%d ",num[i]);
printf("\n"); for(i=0;i<numlen-1;i++)
for(j=i+1;j<numlen;)
{
if(num[i]==num[j])//存在相同的数字 就删掉靠后的一个
{
for(k=j;k<numlen-1;k++)
num[k]=num[k+1];
numlen--;//删掉一个数字之后就把num的长度减1
}
else//有相同的数时j保持,否则加1
j++;
} for(i=0;i<numlen;i++)
printf("%d ",num[i]);
printf("\n"); for(i=0;i<numlen;i++)
{
fprintf(fp3,"%d ",num[i]);//每行写入10个数字
printf("%d ",num[i]);//每行写入10个数字
if((i+1)%10==0)
{
fprintf(fp3,"\n");//写入换行符
printf("\n");
}
}
free(buf1);
free(buf2);
free(num);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
㈦ C语言,文件操作编程题,请教一下这个程序该怎么写
㈧ C语言结构体与文件编程题
VC++测试通过,,,,有问题到我空间留言~
#include<stdio.h>
#include<string.h>
struct Person
{
char name[100];
int age;
int sex;
char add[100];
};
int main(int argc, char* argv[])
{
FILE *FIN;
char STRING_COMMAND[30];
if((FIN=fopen("c:/abc.txt","a+"))==NULL)
{
printf("不能打开该文件,或者找不到该文件~");
return 0;
}
else
{
printf("请使用以下命令来完成操作:\n");
}
printf("-lsall list all the person`s info! \n");
printf("-insert insert one person`s info \n");
printf("-exit exit the progronam \n");
printf("-lsname list all the person`s name \n");
printf("-lsnameinfo[name] display the info of one person \n");
while(1)
{
gets(STRING_COMMAND);
if(strcmp(STRING_COMMAND,"-lsall")==0)
{ fseek(FIN,0,SEEK_SET);
char in;
in=getc(FIN);
while(in!=EOF)
{
printf("%c",in);
in=getc(FIN);
}
printf("Query successfully!\n");
continue;
}
else if(strcmp(STRING_COMMAND,"-insert")==0)
{
struct Person newPerson;
printf("name:");
scanf("%s",&newPerson.name);
printf("age:");
scanf("%d",&newPerson.age);
printf("sex:");
scanf("%d",&newPerson.sex);
printf("address:");
scanf("%s",&newPerson.add);
fseek(FIN,0,SEEK_END);
fprintf(FIN,"name:%s\nage:%d\nsex:%d\naddress:%s\n",newPerson.name,newPerson.age,newPerson.sex,newPerson.add);
printf("Insert successfully!\n");
continue;
}
else if(strcmp(STRING_COMMAND,"-lsname")==0)
{
fseek(FIN,0,SEEK_SET);
struct Person QueryName;
while(feof(FIN)==0)
{
fscanf(FIN,"name:%s\n",&QueryName.name);
fscanf(FIN,"age:%d\n",&QueryName.age);
fscanf(FIN,"sex:%d\n",&QueryName.sex);
fscanf(FIN,"add:%s\n",&QueryName.add);
printf("%s\n",QueryName.name);
}
printf("Query successfully!");
}
else if(strcmp(STRING_COMMAND,"-lsnameinfo")==0)
{ char ForCmp[20];
gets(ForCmp);
fseek(FIN,0,SEEK_SET);
struct Person QueryName;
while(feof(FIN)==0)
{
fscanf(FIN,"name:%s\n",&QueryName.name);
fscanf(FIN,"age:%d\n",&QueryName.age);
fscanf(FIN,"sex:%d\n",&QueryName.sex);
fscanf(FIN,"add:%s\n",&QueryName.add);
if(strcmp(QueryName.name,ForCmp)==0)
{
printf("name:%s\n",QueryName.name);
printf("age:%d\n",QueryName.age);
printf("sex:%d\n",QueryName.sex);
printf("address:%s\n",QueryName.add);
}
}
printf("Query successfully!");
}
else if(strcmp(STRING_COMMAND,"-exit")==0||strcmp(STRING_COMMAND,"-end")==0)
{
break;
}
else
{
printf("Bad command!\n");
}
}
fclose(FIN);
return 0;
}
㈨ 求教一个C语言关于文件的编程题
把file1.txt 放在编译好的exe文件所在文件夹。代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineMAXWORDS100
//qsort用到的字符串比较函数,用来比较单词
intcompare(constvoid*pa,constvoid*pb)
{
returnstrcmp((char*)pa,(char*)pb);
}
intmain()
{
FILE*fp1=NULL,*fp2=NULL;
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","wt");
if(!fp1||!fp2){
printf("Erroropenfile! ");
exit(-1);
}
charWords[MAXWORDS][64]={NULL};
charstr[64];
inti,cnt=0;//cnt保存了实际读入的单词个数,最多为MAXWORDS个
printf("file1content: ");
for(i=0;!feof(fp1)&&i<MAXWORDS;i++)
{
fscanf(fp1,"%s",str);
strcpy(Words[i],str);
Words[i][strlen(str)]='