sprintf函数源码,sprintf用法举例

http://www.itjxue.com  2023-01-14 16:25  来源:未知  点击次数: 

printf函数的实现方式

其实printf和getchar()类似,它们都是一个”外壳“,真正实现功能的不是它本身,而是通过调用别的函数。

getchar() is equivalent to getc(stdin).

printf有一家子print函数

printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - formatted output conversion

它们的声明在不同的header file里面

#include stdio.h

int printf(const char *format, ...);

int fprintf(FILE *stream, const char *format, ...);

int sprintf(char *str, const char *format, ...);

int snprintf(char *str, size_t size, const char *format, ...);

#include stdarg.h

int vprintf(const char *format, va_list ap);

int vfprintf(FILE *stream, const char *format, va_list ap);

int vsprintf(char *str, const char *format, va_list ap);

int vsnprintf(char *str, size_t size, const char *format, va_list ap);

snprintf(), vsnprintf():

这两个函数是C99新加的,编译的时候 注意 -std=c99

实现之前还是“复习”一下printf比较好,就当是铺垫

有意思的是printf的declaration。

int printf(const char *format, ...);

返回值是int,第一个参数是const字符串指针,第二个参数是个...

先看看返回值int有哪些情况

Return value

Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).

嗯哼。。。返回的是成功打印的字符的个数,这里不包括NULL

demo:

#includestdio.h

int main()

{

int counter = 0;

counter = printf("hello world! %d\n",10);

printf("counter is %d\n",counter);

return 0;

}

jasonleaster@ubuntu:~$ ./a.out

hello world! 10

counter is 16

接着,第一个参数是一个指针,指向const字符串

Format of the format string

The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of

zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications,

each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the character %,

and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width,

an optional precision and an optional length modifier.

很少人会用下面这种用法

printf("%*d",10,5);

我第一次遇到的时候,可以说是“惊愕”,究竟会打印什么东西。折腾了好久,最后搞定了。总结在这里

Format of the format string

The format string is a character string, beginning and ending in its initial shift state, if any. The format string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments. Each conversion specification is introduced by the character %, and ends with a conversion specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, an optional precision and an optional length modifier.

The arguments must correspond properly (after type promotion) with the conversion specifier. By default, the arguments are used in the order given, where each '*' and each conversion specifier asks for the next argument (and it is an error if insufficiently many arguments are given). One can also specify explicitly which argument is taken, at each place where an argument is required, by writing "%m$" instead of '%' and "*m$" instead of '*', where the decimal integer m denotes the position in the argument list of the

desired argument, indexed starting from 1. Thus,

printf("%*d", width, num);

and

printf("%2$*1$d", width, num);

are equivalent. The second style allows repeated references to the same argument. The C99 standard does not include the style using '$', which comes from the Single UNIX Specification. If the style using '$' is used, it must be used throughout for all conversions taking an argument and all width and precision arguments, but it may be mixed with "%%" formats which do not consume an argument.

There may be no gaps in the numbers of arguments specified using '$'; for example, if arguments 1 and 3 are specified, argument 2

must also be specified somewhere in the format string.

第三个参数 ...

嗯,这家伙有点屌,叫做变长参数。把这个搞定,C总会有点长进的

这个stdarg.h 我在现在的GCC和现在的linux 3.0版本的内核里面找了好久,都木有,估计是封装到被的地方了。。。。

__builtin_va_start(v,l) 线索就死在这个地方。。。之后就找不到__builtin_va_start的定义了

还是看早起内核的实现吧

0.12内核里面的stdarg.h

#ifndef _STDARG_H

#define _STDARG_H

typedef char *va_list;

/* Amount of space required in an argument list for an arg of type TYPE.

TYPE may alternatively be an expression whose type is used. */

#define __va_rounded_size(TYPE) \

(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))

#ifndef __sparc__

#define va_start(AP, LASTARG) \

(AP = ((char *) (LASTARG) + __va_rounded_size (LASTARG)))

#else

#define va_start(AP, LASTARG) \

(__builtin_saveregs (), \

AP = ((char *) (LASTARG) + __va_rounded_size (LASTARG)))

#endif

void va_end (va_list); /* Defined in gnulib */

#define va_end(AP)

#define va_arg(AP, TYPE) \

(AP += __va_rounded_size (TYPE), \

*((TYPE *) (AP - __va_rounded_size (TYPE))))

#endif /* _STDARG_H */

va_list 是一个指向字符串的指针

分析上面的宏定义#define __va_rounded_size(TYPE) \

(((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof (int))

这个用来得到TYPE元素类型的字节大小,若不足4字节(例如short 和char),那么认为这个元素的大小为4字节,简单的说就是检测元素的大小,不足4字节的当作4字节看待。。。#define va_start(AP, LASTARG) \

(AP = ((char *) (LASTARG) + __va_rounded_size (LASTARG)))

AP一般都是va_list,LASTARG则是 指向参数变长函数的格式化字符串的指针.

va_start的作用就很明显了。取得变长参数列表的第一个参数的地址。

va_end 则是把指针va_list 置0 (通过google知道的,这个va_end真没找到定义,代码里面就一句#define 我无能为力啊。。。)

不过知道用va_start 和va_end 就OK啦

下面先来个变长参数的demo

/*****************************************************************************

code writer : EOF

code date : 2014.04.26

e-mail:jasonleaster@gmail.com

code purpose:

just a demo for varible parameter function.

usage: va_sum(a number,anohter number...,0);

va_sum(1,2,3,4,5,0); return 15

******************************************************************************/

#include stdarg.h

#include stdio.h

int va_sum(int* a,...);

int main()

{

int number = 1;

int foo = 0;

foo = va_sum(number,2,3,4,5,0);

return 0;

}

int va_sum(int* a,...)

{

int counter = 0;

int element = 0;

va_list arg;

va_start(arg,a);

while((element = va_arg(arg,int)) != 0)

{

counter += element;

}

va_end(arg);

return counter;

}

#define va_arg(AP, TYPE) \

(AP += __va_rounded_size (TYPE), \

*((TYPE *) (AP - __va_rounded_size (TYPE))))

初级C语言源代码

好的哦

#define N 200

#include graphics.h

#include stdlib.h

#include dos.h

#define LEFT 0x4b00

#define RIGHT 0x4d00

#define DOWN 0x5000

#define UP 0x4800

#define ESC 0x011b

int i,key;

int score=0;/*得分*/

int gamespeed=50000;/*游戏速度自己调整*/

struct Food

{

int x;/*食物的横坐标*/

int y;/*食物的纵坐标*/

int yes;/*判断是否要出现食物的变量*/

}food;/*食物的结构体*/

struct Snake

{

int x[N];

int y[N];

int node;/*蛇的节数*/

int direction;/*蛇移动方向*/

int life;/* 蛇的生命,0活着,1死亡*/

}snake;

void Init(void);/*图形驱动*/

void Close(void);/*图形结束*/

void DrawK(void);/*开始画面*/

void GameOver(void);/*结束游戏*/

void GamePlay(void);/*玩游戏具体过程*/

void PrScore(void);/*输出成绩*/

/*主函数*/

void main(void)

{

Init();/*图形驱动*/

DrawK();/*开始画面*/

GamePlay();/*玩游戏具体过程*/

Close();/*图形结束*/

}

/*图形驱动*/

void Init(void)

{

int gd=DETECT,gm;

initgraph(gd,gm,"c:\\tc");

cleardevice();

}

/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/

void DrawK(void)

{

/*setbkcolor(LIGHTGREEN);*/

setcolor(11);

setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/

for(i=50;i=600;i+=10)/*画围墙*/

{

rectangle(i,40,i+10,49); /*上边*/

rectangle(i,451,i+10,460);/*下边*/

}

for(i=40;i=450;i+=10)

{

rectangle(50,i,59,i+10); /*左边*/

rectangle(601,i,610,i+10);/*右边*/

}

}

/*玩游戏具体过程*/

void GamePlay(void)

{

randomize();/*随机数发生器*/

food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/

snake.life=0;/*活着*/

snake.direction=1;/*方向往右*/

snake.x[0]=100;snake.y[0]=100;/*蛇头*/

snake.x[1]=110;snake.y[1]=100;

snake.node=2;/*节数*/

PrScore();/*输出得分*/

while(1)/*可以重复玩游戏,压ESC键结束*/

{

while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/

{

if(food.yes==1)/*需要出现新食物*/

{

food.x=rand()%400+60;

food.y=rand()%350+60;

while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/

food.x++;

while(food.y%10!=0)

food.y++;

food.yes=0;/*画面上有食物了*/

}

if(food.yes==0)/*画面上有食物了就要显示*/

{

setcolor(GREEN);

rectangle(food.x,food.y,food.x+10,food.y-10);

}

for(i=snake.node-1;i0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/

{

snake.x[i]=snake.x[i-1];

snake.y[i]=snake.y[i-1];

}

/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/

switch(snake.direction)

{

case 1:snake.x[0]+=10;break;

case 2: snake.x[0]-=10;break;

case 3: snake.y[0]-=10;break;

case 4: snake.y[0]+=10;break;

}

for(i=3;isnake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/

{

if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])

{

GameOver();/*显示失败*/

snake.life=1;

break;

}

}

if(snake.x[0]55||snake.x[0]595||snake.y[0]55||

snake.y[0]455)/*蛇是否撞到墙壁*/

{

GameOver();/*本次游戏结束*/

snake.life=1; /*蛇死*/

}

if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/

break;

if(snake.x[0]==food.xsnake.y[0]==food.y)/*吃到食物以后*/

{

setcolor(0);/*把画面上的食物东西去掉*/

rectangle(food.x,food.y,food.x+10,food.y-10);

snake.x[snake.node]=-20;snake.y[snake.node]=-20;

/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/

snake.node++;/*蛇的身体长一节*/

food.yes=1;/*画面上需要出现新的食物*/

score+=10;

PrScore();/*输出新得分*/

}

setcolor(4);/*画出蛇*/

for(i=0;isnake.node;i++)

rectangle(snake.x[i],snake.y[i],snake.x[i]+10,

snake.y[i]-10);

delay(gamespeed);

setcolor(0);/*用黑色去除蛇的的最后一节*/

rectangle(snake.x[snake.node-1],snake.y[snake.node-1],

snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);

} /*endwhile(!kbhit)*/

if(snake.life==1)/*如果蛇死就跳出循环*/

break;

key=bioskey(0);/*接收按键*/

if(key==ESC)/*按ESC键退出*/

break;

else

if(key==UPsnake.direction!=4)

/*判断是否往相反的方向移动*/

snake.direction=3;

else

if(key==RIGHTsnake.direction!=2)

snake.direction=1;

else

if(key==LEFTsnake.direction!=1)

snake.direction=2;

else

if(key==DOWNsnake.direction!=3)

snake.direction=4;

}/*endwhile(1)*/

}

/*游戏结束*/

void GameOver(void)

{

cleardevice();

PrScore();

setcolor(RED);

settextstyle(0,0,4);

outtextxy(200,200,"GAME OVER");

getch();

}

/*输出成绩*/

void PrScore(void)

{

char str[10];

setfillstyle(SOLID_FILL,YELLOW);

bar(50,15,220,35);

setcolor(6);

settextstyle(0,0,2);

sprintf(str,"score:%d",score);

outtextxy(55,20,str);

}

/*图形结束*/

void Close(void)

{

getch();

closegraph();

}

#include dos.h /*DOS接口函数*/

#include math.h /*数学函数的定义*/

#include conio.h /*屏幕操作函数*/

#include stdio.h /*I/O函数*/

#include stdlib.h /*库函数*/

#include stdarg.h /*变量长度参数表*/

#include graphics.h /*图形函数*/

#include string.h /*字符串函数*/

#include ctype.h /*字符操作函数*/

#define UP 0x48 /*光标上移键*/

#define DOWN 0x50 /*光标下移键*/

#define LEFT 0x4b /*光标左移键*/

#define RIGHT 0x4d /*光标右移键*/

#define ENTER 0x0d /*回车键*/

void *rar; /*全局变量,保存光标图象*/

struct palettetype palette; /*使用调色板信息*/

int GraphDriver; /* 图形设备驱动*/

int GraphMode; /* 图形模式值*/

int ErrorCode; /* 错误代码*/

int MaxColors; /* 可用颜色的最大数值*/

int MaxX, MaxY; /* 屏幕的最大分辨率*/

double AspectRatio; /* 屏幕的像素比*/

void drawboder(void); /*画边框函数*/

void initialize(void); /*初始化函数*/

void computer(void); /*计算器计算函数*/

void changetextstyle(int font, int direction, int charsize); /*改变文本样式函数*/

void mwindow(char *header); /*窗口函数*/

int specialkey(void) ; /*获取特殊键函数*/

int arrow(); /*设置箭头光标函数*/

/*主函数*/

int main()

{

initialize();/* 设置系统进入图形模式 */

computer(); /*运行计算器 */

closegraph();/*系统关闭图形模式返回文本模式*/

return(0); /*结束程序*/

}

/* 设置系统进入图形模式 */

void initialize(void)

{

int xasp, yasp; /* 用于读x和y方向纵横比*/

GraphDriver = DETECT; /* 自动检测显示器*/

initgraph( GraphDriver, GraphMode, "" );

/*初始化图形系统*/

ErrorCode = graphresult(); /*读初始化结果*/

if( ErrorCode != grOk ) /*如果初始化时出现错误*/

{

printf("Graphics System Error: %s\n",

grapherrormsg( ErrorCode ) ); /*显示错误代码*/

exit( 1 ); /*退出*/

}

getpalette( palette ); /* 读面板信息*/

MaxColors = getmaxcolor() + 1; /* 读取颜色的最大值*/

MaxX = getmaxx(); /* 读屏幕尺寸 */

MaxY = getmaxy(); /* 读屏幕尺寸 */

getaspectratio( xasp, yasp ); /* 拷贝纵横比到变量中*/

AspectRatio = (double)xasp/(double)yasp;/* 计算纵横比值*/

}

/*计算器函数*/

void computer(void)

{

struct viewporttype vp; /*定义视口类型变量*/

int color, height, width;

int x, y,x0,y0, i, j,v,m,n,act,flag=1;

float num1=0,num2=0,result; /*操作数和计算结果变量*/

char cnum[5],str2[20]={""},c,temp[20]={""};

char str1[]="1230.456+-789*/Qc=^%";/* 定义字符串在按钮图形上显示的符号 */

mwindow( "Calculator" ); /* 显示主窗口 */

color = 7; /*设置灰颜色值*/

getviewsettings( vp ); /* 读取当前窗口的大小*/

width=(vp.right+1)/10; /* 设置按钮宽度 */

height=(vp.bottom-10)/10 ; /*设置按钮高度 */

x = width /2; /*设置x的坐标值*/

y = height/2; /*设置y的坐标值*/

setfillstyle(SOLID_FILL, color+3);

bar( x+width*2, y, x+7*width, y+height );

/*画一个二维矩形条显示运算数和结果*/

setcolor( color+3 ); /*设置淡绿颜色边框线*/

rectangle( x+width*2, y, x+7*width, y+height );

/*画一个矩形边框线*/

setcolor(RED); /*设置颜色为红色*/

outtextxy(x+3*width,y+height/2,"0."); /*输出字符串"0."*/

x =2*width-width/2; /*设置x的坐标值*/

y =2*height+height/2; /*设置y的坐标值*/

for( j=0 ; j4 ; ++j ) /*画按钮*/

{

for( i=0 ; i5 ; ++i )

{

setfillstyle(SOLID_FILL, color);

setcolor(RED);

bar( x, y, x+width, y+height ); /*画一个矩形条*/

rectangle( x, y, x+width, y+height );

sprintf(str2,"%c",str1[j*5+i]);

/*将字符保存到str2中*/

outtextxy( x+(width/2), y+height/2, str2);

x =x+width+ (width / 2) ; /*移动列坐标*/

}

y +=(height/2)*3; /* 移动行坐标*/

x =2*width-width/2; /*复位列坐标*/

}

x0=2*width;

y0=3*height;

x=x0;

y=y0;

gotoxy(x,y); /*移动光标到x,y位置*/

arrow(); /*显示光标*/

putimage(x,y,rar,XOR_PUT);

m=0;

n=0;

strcpy(str2,""); /*设置str2为空串*/

while((v=specialkey())!=45) /*当压下Alt+x键结束程序,否则执行下面的循环*/

{

while((v=specialkey())!=ENTER) /*当压下键不是回车时*/

{

putimage(x,y,rar,XOR_PUT); /*显示光标图象*/

if(v==RIGHT) /*右移箭头时新位置计算*/

if(x=x0+6*width)

/*如果右移,移到尾,则移动到最左边字符位置*/

{

x=x0;

m=0;

}

else

{

x=x+width+width/2;

m++;

} /*否则,右移到下一个字符位置*/

if(v==LEFT) /*左移箭头时新位置计算*/

if(x=x0)

{

x=x0+6*width;

m=4;

} /*如果移到头,再左移,则移动到最右边字符位置*/

else

{

x=x-width-width/2;

m--;

} /*否则,左移到前一个字符位置*/

if(v==UP) /*上移箭头时新位置计算*/

if(y=y0)

{

y=y0+4*height+height/2;

n=3;

} /*如果移到头,再上移,则移动到最下边字符位置*/

else

{

y=y-height-height/2;

n--;

} /*否则,移到上边一个字符位置*/

if(v==DOWN) /*下移箭头时新位置计算*/

if(y=7*height)

{

y=y0;

n=0;

} /*如果移到尾,再下移,则移动到最上边字符位置*/

else

{

y=y+height+height/2;

n++;

} /*否则,移到下边一个字符位置*/

putimage(x,y,rar,XOR_PUT); /*在新的位置显示光标箭头*/

}

c=str1[n*5+m]; /*将字符保存到变量c中*/

if(isdigit(c)||c=='.') /*判断是否是数字或小数点*/

{

if(flag==-1) /*如果标志为-1,表明为负数*/

{

strcpy(str2,"-"); /*将负号连接到字符串中*/

flag=1;

} /*将标志值恢复为1*/

sprintf(temp,"%c",c); /*将字符保存到字符串变量temp中*/

strcat(str2,temp); /*将temp中的字符串连接到str2中*/

setfillstyle(SOLID_FILL,color+3);

bar(2*width+width/2,height/2,15*width/2,3*height/2);

outtextxy(5*width,height,str2); /*显示字符串*/

}

if(c=='+')

{

num1=atof(str2); /*将第一个操作数转换为浮点数*/

strcpy(str2,""); /*将str2清空*/

act=1; /*做计算加法标志值*/

setfillstyle(SOLID_FILL,color+3);

bar(2*width+width/2,height/2,15*width/2,3*height/2);

outtextxy(5*width,height,"0."); /*显示字符串*/

}

if(c=='-')

{

if(strcmp(str2,"")==0) /*如果str2为空,说明是负号,而不是减号*/

flag=-1; /*设置负数标志*/

else

{

num1=atof(str2); /*将第二个操作数转换为浮点数*/

strcpy(str2,""); /*将str2清空*/

act=2; /*做计算减法标志值*/

setfillstyle(SOLID_FILL,color+3);

bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/

outtextxy(5*width,height,"0."); /*显示字符串*/

}

}

if(c=='*')

{

num1=atof(str2); /*将第二个操作数转换为浮点数*/

strcpy(str2,""); /*将str2清空*/

act=3; /*做计算乘法标志值*/

setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width/2,3*height/2);

outtextxy(5*width,height,"0."); /*显示字符串*/

}

if(c=='/')

{

num1=atof(str2); /*将第二个操作数转换为浮点数*/

strcpy(str2,""); /*将str2清空*/

act=4; /*做计算除法标志值*/

setfillstyle(SOLID_FILL,color+3);

bar(2*width+width/2,height/2,15*width/2,3*height/2);

outtextxy(5*width,height,"0."); /*显示字符串*/

}

if(c=='^')

{

num1=atof(str2); /*将第二个操作数转换为浮点数*/

strcpy(str2,""); /*将str2清空*/

act=5; /*做计算乘方标志值*/

setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/

bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/

outtextxy(5*width,height,"0."); /*显示字符串*/

}

if(c=='%')

{

num1=atof(str2); /*将第二个操作数转换为浮点数*/

strcpy(str2,""); /*将str2清空*/

act=6; /*做计算模运算乘方标志值*/

setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/

bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/

outtextxy(5*width,height,"0."); /*显示字符串*/

}

if(c=='=')

{

num2=atof(str2); /*将第二个操作数转换为浮点数*/

switch(act) /*根据运算符号计算*/

{

case 1:result=num1+num2;break; /*做加法*/

case 2:result=num1-num2;break; /*做减法*/

case 3:result=num1*num2;break; /*做乘法*/

case 4:result=num1/num2;break; /*做除法*/

case 5:result=pow(num1,num2);break; /*做x的y次方*/

case 6:result=fmod(num1,num2);break; /*做模运算*/

}

setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/

bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆盖结果区*/

sprintf(temp,"%f",result); /*将结果保存到temp中*/

outtextxy(5*width,height,temp); /*显示结果*/

}

if(c=='c')

{

num1=0; /*将两个操作数复位0,符号标志为1*/

num2=0;

flag=1;

strcpy(str2,""); /*将str2清空*/

setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/

bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆盖结果区*/

outtextxy(5*width,height,"0."); /*显示字符串*/

}

if(c=='Q')exit(0); /*如果选择了q回车,结束计算程序*/

}

putimage(x,y,rar,XOR_PUT); /*在退出之前消去光标箭头*/

return; /*返回*/

}

/*窗口函数*/

void mwindow( char *header )

{

int height;

cleardevice(); /* 清除图形屏幕 */

setcolor( MaxColors - 1 ); /* 设置当前颜色为白色*/

setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); /* 设置视口大小 */

height = textheight( "H" ); /* 读取基本文本大小 */

settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*设置文本样式*/

settextjustify( CENTER_TEXT, TOP_TEXT );/*设置字符排列方式*/

outtextxy( MaxX/4, 2, header ); /*输出标题*/

setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*设置视口大小*/

drawboder(); /*画边框*/

}

void drawboder(void) /*画边框*/

{

struct viewporttype vp; /*定义视口类型变量*/

setcolor( MaxColors - 1 ); /*设置当前颜色为白色 */

setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*设置画线方式*/

getviewsettings( vp );/*将当前视口信息装入vp所指的结构中*/

rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*画矩形边框*/

}

/*设计鼠标图形函数*/

int arrow()

{

int size;

int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*定义多边形坐标*/

setfillstyle(SOLID_FILL,2); /*设置填充模式*/

fillpoly(8,raw); /*画出一光标箭头*/

size=imagesize(4,4,16,16); /*测试图象大小*/

rar=malloc(size); /*分配内存区域*/

getimage(4,4,16,16,rar); /*存放光标箭头图象*/

putimage(4,4,rar,XOR_PUT); /*消去光标箭头图象*/

return 0;

}

/*按键函数*/

int specialkey(void)

{

int key;

while(bioskey(1)==0); /*等待键盘输入*/

key=bioskey(0); /*键盘输入*/

key=key0xff? key0xff:key8; /*只取特殊键的扫描值,其余为0*/

return(key); /*返回键值*/

}

#include dos.h /*DOS接口函数*/

#include math.h /*数学函数的定义*/

#include conio.h /*屏幕操作函数*/

#include stdio.h /*I/O函数*/

#include stdlib.h /*库函数*/

#include stdarg.h /*变量长度参数表*/

#include graphics.h /*图形函数*/

#include string.h /*字符串函数*/

#include ctype.h /*字符操作函数*/

#define UP 0x48 /*光标上移键*/

#define DOWN 0x50 /*光标下移键*/

#define LEFT 0x4b /*光标左移键*/

#define RIGHT 0x4d /*光标右移键*/

#define ENTER 0x0d /*回车键*/

void *rar; /*全局变量,保存光标图象*/

struct palettetype palette; /*使用调色板信息*/

int GraphDriver; /* 图形设备驱动*/

int GraphMode; /* 图形模式值*/

int ErrorCode; /* 错误代码*/

int MaxColors; /* 可用颜色的最大数值*/

int MaxX, MaxY; /* 屏幕的最大分辨率*/

double AspectRatio; /* 屏幕的像素比*/

void drawboder(void); /*画边框函数*/

void initialize(void); /*初始化函数*/

void computer(void); /*计算器计算函数*/

void changetextstyle(int font, int direction, int charsize); /*改变文本样式函数*/

void mwindow(char *header); /*窗口函数*/

int specialkey(void) ; /*获取特殊键函数*/

int arrow(); /*设置箭头光标函数*/

/*主函数*/

int main()

{

initialize();/* 设置系统进入图形模式 */

computer(); /*运行计算器 */

closegraph();/*系统关闭图形模式返回文本模式*/

return(0); /*结束程序*/

}

/* 设置系统进入图形模式 */

void initialize(void)

{

int xasp, yasp; /* 用于读x和y方向纵横比*/

GraphDriver = DETECT; /* 自动检测显示器*/

initgraph( GraphDriver, GraphMode, "" );

/*初始化图形系统*/

ErrorCode = graphresult(); /*读初始化结果*/

if( ErrorCode != grOk ) /*如果初始化时出现错误*/

{

printf("Graphics System Error: %s\n",

grapherrormsg( ErrorCode ) ); /*显示错误代码*/

exit( 1 ); /*退出*/

}

getpalette( palette ); /* 读面板信息*/

MaxColors = getmaxcolor() + 1; /* 读取颜色的最大值*/

MaxX = getmaxx(); /* 读屏幕尺寸 */

MaxY = getmaxy(); /* 读屏幕尺寸 */

getaspectratio( xasp, yasp ); /* 拷贝纵横比到变量中*/

AspectRatio = (double)xasp/(double)yasp;/* 计算纵横比值*/

}

/*计算器函数*/

void computer(void)

{

struct viewporttype vp; /*定义视口类型变量*/

int color, height, width;

int x, y,x0,y0, i, j,v,m,n,act,flag=1;

float num1=0,num2=0,result; /*操作数和计算结果变量*/

char cnum[5],str2[20]={""},c,temp[20]={""};

char str1[]="1230.456+-789*/Qc=^%";/* 定义字符串在按钮图形上显示的符号 */

mwindow( "Calculator" ); /* 显示主窗口 */

color = 7; /*设置灰颜色值*/

getviewsettings( vp ); /* 读取当前窗口的大小*/

width=(vp.right+1)/10; /* 设置按钮宽度 */

height=(vp.bottom-10)/10 ; /*设置按钮高度 */

x = width /2; /*设置x的坐标值*/

y = height/2; /*设置y的坐标值*/

setfillstyle(SOLID_FILL, color+3);

bar( x+width*2, y, x+7*width, y+height );

/*画一个二维矩形条显示运算数和结果*/

setcolor( color+3 ); /*设置淡绿颜色边框线*/

rectangle( x+width*2, y, x+7*width, y+height );

/*画一个矩形边框线*/

setcolor(RED); /*设置颜色为红色*/

outtextxy(x+3*width,y+height/2,"0."); /*输出字符串"0."*/

x =2*width-width/2; /*设置x的坐标值*/

y =2*height+height/2; /*设置y的坐标值*/

for( j=0 ; j4 ; ++j ) /*画按钮*/

{

for( i=0 ; i5 ; ++i )

{

setfillstyle(SOLID_FILL, color);

setcolor(RED);

bar( x, y, x+width, y+height ); /*画一个矩形条*/

rectangle( x, y, x+width, y+height );

sprintf(str2,"%c",str1[j*5+i]);

/*将字符保存到str2中*/

outtextxy( x+(width/2), y+height/2, str2);

x =x+width+ (width / 2) ; /*移动列坐标*/

}

y +=(height/2)*3; /* 移动行坐标*/

x =2*width-width/2; /*复位列坐标*/

}

x0=2*width;

y0=3*height;

x=x0;

y=y0;

gotoxy(x,y); /*移动光标到x,y位置*/

arrow(); /*显示光标*/

putimage(x,y,rar,XOR_PUT);

m=0;

n=0;

strcpy(str2,""); /*设置str2为空串*/

while((v=specialkey())!=45) /*当压下Alt+x键结束程序,否则执行下面的循环*/

{

while((v=specialkey())!=ENTER) /*当压下键不是回车时*/

{

putimage(x,y,rar,XOR_PUT); /*显示光标图象*/

if(v==RIGHT) /*右

C语言的sprintf函数问题,到底怎么用啊???

那个缓存区只能自己定义了,不想自己定义的话那么用stringstream

stringstream?stream;

stream"frist"12333333;

string?temp=stream.str();

求C语言标准函数库的源代码

标准库只是定义接口,具体怎么实现就得看操作系统,你说win下和linux下这些函数的实现会一样吗。当然不一样,看这些学源码,不如看看c标准,c89或c99.

那可以看内核,看系统调用是怎么样实现的,你说的那些都是基于系统调用的

c语言中查看小数长度

c语言中查看小数长度的方法代码如下:

1)如果按%s输入,也就是按字符串格式输入,先找到小数点的位置,然后统计下小数点后数字的长度。

具体实现可参考:

#include

#include

#include

intmain(intargc,char*argv[])

{

chara[128];

while(scanf("%s",a)!=EOF)

{

if(strstr(a,".")!=NULL)

printf("%d\n",strlen(strstr(a,".")+1));

else

printf("0\n");

}

system("pause");

return0;

}

2)如果按%f(对应float类型小数)或者%lf(double类型的小数)方式读入,则需要先去掉整数部分,然后看小数部分一直乘10减去整数部分,知道等于0,统计,乘了几次10,但是由于浮点数在计算机中存的并不是准确值,这个往往得不到正确的结果。可以使用sprintf(str,"%g",f);,然后由于f毕竟不是准确值,还是会出现有些问题,源码如下:

#include

#include

#include

intmain(intargc,char*argv[])

{

chara[128];

floatf;

while(scanf("%f",f)!=EOF)

{

sprintf(a,"%g",f);

if(strstr(a,".")!=NULL)

printf("%d\n",strlen(strstr(a,".")+1));

else

printf("0\n");

}

system("pause");

return0;

}

%在c语言中有两种释义,一是求余符号,经常会用到判断一个数是不是能被另一个整除。二是引导符,用于引导输入输出项表列的格式。

在scanf、printf这样的函数中,将参数类型与输入/输出句子中的相应位置相对应所使用的转义标志符。%c单个字符输出的意思;%s是输出字符串;%d是输出整型;%f是输出整型。其中常用的有%c--对应字符型char%s--对应字符串型char*(char[])%d--对应整形int%ld--对应长整形long%x--对应十六进制整数,大于9的字母小写%X--对应十六进制整数,大于9的字母大写%u--对应无符号整形unsignedint。

(责任编辑:IT教学网)

更多

推荐Painter教程文章