如何用C语言实现atoi()函数
用 标准函数 sscanf 就可以了。
long my_atoi(char *str){
long i;
sscanf(str,pgsql 源码分析"%ld",&i);
return i;
}
字符常量调用:
printf("%d %d",my_atoi("-"), my_atoi(""));
字符变量调用:
char a[]="-";
int k;
k = my_atoi(a);
---------------------------
考究一点,函数里加一个转换错误检查:
if ( sscanf(str,"%ld",&i) != 1) { printf("Covertion error in my_atoi !"); ....};
C语言中从键盘输入一个英文句子,输出所有首字母大写的单词.
#include <stdio.h>
int main(void)
{
char str[];
char *cp;
printf("Input sentence:");
gets(str);
for(cp = str; *cp != '\0'; cp++)
{
/* 判断单词首字母是否是大写 */
if((cp == str || *(cp -1) == ' ') && *cp >= 'A' && *cp <= 'Z')
{
while((*cp >= 'A' && *cp <= 'Z') || (*cp >= 'a' && *cp <= 'z'))
{
printf("%c", *cp);
cp++;
}
printf("\n");
}
}
return 0;
}
纯Cè¯è¨å®ç°å¾åå¤çï¼
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define ONE
#define ZERO 0
/
*typedef struct tagBITMAPFILEHEADER { // bmfh
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{ // bmih
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
*/
void main (int argc,char *argv[])
{
FILE *fi,*fo;//I/O file
char fin[],fon[];//I/O file name
unsigned char **ri,**ro;
unsigned char buff;
long w,h;
int t;
int i,j;
if(argc<3)
{
printf("orginfile name:");
scanf("%s",fin);
printf("resultfile name:");
scanf("%s",fon);
}else{
sscanf(argv[1],"%s",fin);
sscanf(argv[2],"%s",fon);
}
if(argc==4)
sscanf(argv[4],"%d",&t);
else{
printf("theshold [0,]:");
scanf("%d",&t);
}
if (((fi=fopen(fin,"rb"))==NULL)||((fo=fopen(fon,"wb"))==NULL))
{
puts("\nfile open failed");
return;
}
fseek(fi,L,SEEK_SET);
fread(&w,sizeof(long),1,fi);
fread(&h,sizeof(long),1,fi);
fseek(fi,0L,SEEK_SET);
ri=(unsigned char **)malloc(sizeof(unsigned *)*h);
for (i=0;i<h;i++)
*(ri+i)=(unsigned char *)malloc(sizeof(unsigned)*w);
ro=(unsigned char **)malloc(sizeof(unsigned *)*h);
for (i=0;i<h;i++)
*(ro+i)=(unsigned char *)malloc(sizeof(unsigned)*w);
//åé 失败åæèªè´!
for (i=0;i<;i++){
fread(&buff,sizeof(buff),1,fi);
fwrite(&buff,sizeof(buff),1,fo);}
for (i=0;i<h;i++)
for (j=0;j<w;j++)
fread(*(ri+i)+j,sizeof(unsigned char),1,fi);
for (i=0;i<h;i++)
for (j=0;j<w;j++)
*(*(ro+i)+j)=((*(*(ri+i)+j)<=t)?ZERO:ONE);
for (i=0;i<h;i++)
for (j=0;j<w;j++)
fwrite(*(ro+i)+j,sizeof(unsigned char),1,fo);
fclose(fo);
}
2024-12-28 23:59
2024-12-28 23:58
2024-12-28 23:37
2024-12-28 23:02
2024-12-28 22:20