|
¤§«e¦Aºô¸ô¤W·j¶°¨ìªº¤@¥÷¸ê®Æ ¬OÃö©ó °}¦C ÁÙ¦³¦r¦êªº³¡¥÷
¤j®a±`¦³ªº ¦r¦ê Âàint ...µ¥ ©Î³\¥i¥H°Ñ¦Ò¤W±ªº¤èªk ¸Ñ¨M
[°}¦C¤Î¦r¦ê]
²¤¶
¬°¤°n¥Î°}¦C? ·Q¹³¤@¤U¦pªG§ÚÌn¼g¤@Óµ{¦¡, µ{¦¡n¨Dn¿é¤J¥þ¯Z¦P¾Çªº´Á¤¤¦Ò¼Æ¾Ç¬ì¦¨ÁZ, ¼g¦¨µ{¦¡·|ªø¤°»ò¼Ë¤l?#include < stdio.h >
void main(void)
{
int num1, num2, num3, num4, num5;
scanf("%d",&num1);
scanf("%d",&num2);
scanf("%d",&num3);
scanf("%d",&num4);
scanf("%d",&num5);
}
¤W±ªºµ{¦¡¬O¥þ¯Z¥u¦³5¦ì¾Ç¥Í®Éªº±¡§Î, ¨ºn¬O¥þ¯Z¦³50¦ì¾Ç¥Í®É, µ{¦¡n«ç»ò¼g? ³o®É´N¥i¨D§U©ó°}¦C(Array)
#include < stdio.h >
void main(void)
{
int i;
int student[50];
for ( i=0; i<50; i++ )
{
scanf("%d", &student);
}
}
µ{¦¡½X©úÅ㪺ºë²³\¦h, ¥Îì¥ýªº¼gªk³Ì¤Ön«Å§i50ÓÅܼÆ, ÁÙn¼g50Óscanf.
¨Ï¥Î#defineªº¦n³B¦b³oùØ´N¥i¥H¬Ý¥X¨Ó.
#include < stdio.h >
#define STUDENTS 5
void main(void)
{
int i;
int student[STUDENTS];
// ¥un§ïdefine¤¤STUDENTSªº¼ÆÈ, °}¦C¤j¤p©M°j°én¶]ªº¦¸¼Æ³£·|¦P®É§ïÅÜ
for ( i=0; i < STUDENTS; i++ )
{
scanf("%d", &student);
printf("I get %d\n",student);
}
}
²³æªº¨Ó»¡, °}¦C´N¬O¤@¦¸«Å§i¥X³\¦hÓ¬Û¦P«¬ºAªºÅܼƨӨϥÎ.
int a[5];
¤W±ªº»yªk·|«Å§i¥X5Ó¾ã¼Æ, n¨ú¥Î°}¦C¤¤ªº¬YÓÅܼƨӥήÉ, nµ¹¤@Ó¯Á¤ÞÈ, n¨ú¥Î³o5Ó¾ã¼Æªº¤èªk¤À§O¬O
a[0]=1; // §âa[0]³]©w¬°1
a[1]=2; // §âa[1]³]©w¬°2
a[2]=3; // §âa[2]³]©w¬°3
a[3]=4; // §âa[3]³]©w¬°4
a[4]=5; // §âa[4]³]©w¬°5
a[5]¬O¤£¦s¦bªº.
«Å§i¤j¤p¬°N®É, ¨ú¥Îªº¯Á¤ÞȽd³ò¬°0,1,2,.....N-1
²Ä¤G¸`:¤@ºû°}¦Cªº¨Ï¥Î
«Å§i°}¦C®É, ¦P¼Ë¤@¶}©l¥i¥H³]©w¦n¥¦ÌªºªìÈ
void main(void)
{
int a[5]={1,2,3,4,5};
// ³]©w a[0]=1, a[1]=2, a[2]=3, a[3]=4, a[4]=5
int b[]={1,2,3};
// Ysize¨S«ü©w, ¦]¬°µ¹¤F3ӼƦr, ¦b¦¹·|¦Û°Ê³]©w¬°b[3];
}
³]©w°}¦C®É, ÁÙ±`©M°j°é¤@°_¨Ï¥Î
void main(void)
{
int a[5];
int i;
for ( i=0; i<5; i++ )
{
a=i;
}
// ³]©wa[0]=0, a[1]=1, a[2]=2, a[3]=3, a[4]=4
}
Ū¤J5ӼƦr, ¨D¥X¥¦Ìªº¥§¡È.
#include < stdio.h >
#define NUM 5
void main(void)
{
int i;
float sum=0;
float aver;
float num[NUM];
// ¤À§OŪ¤J5Ó¼ÆÈ
for ( i=0; i < NUM; i++ )
{
scanf("%f", &num);
}
// pºâÁ`©M
for ( i=0; i < NUM; i++ )
{
sum+=num;
}
// ¨D¥§¡È
aver=sum/(float)NUM;
printf("average=%f\n",aver);
}
¦hºû°}¦C
¨Ï¥Î°}¦C®É,¥u¥Î¤F¤@Ó¯Á¤ÞÈ, ¥s°µ¤@ºû°}¦C. §ÚÌ¥i¥H«Å§i¥X»Ýn¦hÓ¯Á¤ÞȪº°}¦C¨Ó.
void main(void)
{
int a[2][2];
// ³o®ÉÔ, ¦³a[0][0], a[0][1], a[1][0], a[1][1]µ¥4ÓÅܼƥi¥H¨Ï¥Î
}
³]©w¤Gºû°}¦CªìȪº¤èªk¬°
void main(void)
{
int a[2][2]={ {00, 01},
{10, 11} };
int b[2][2]={ 00,01,10,11 };
}
¹ê¹ê¤W, °}¦C¦b°O¾ÐÅ餤³£¬O¤@¶ô³sÄòªº°O¾ÐÅéªÅ¶¡. ¤@ºû°}¦C®É«Ü®e©ö·Q¹³:
int a[5];
a[0]ªº¦ì§}¬O&a+0;
a[1]ªº¦ì§}¬O&a+1;
a[2]ªº¦ì§}¬O&a+2;
a[3]ªº¦ì§}¬O&a+3;
a[4]ªº¦ì§}¬O&a+4;
¤Gºû°}¦C¦P¼Ë·|¬O¤@¶ô³sÄòªº°O¾ÐÅéªÅ¶¡
int a[5][5];
a[0][0]ªº¦ì§}¬O&a+0*5+0;
a[0][1]ªº¦ì§}¬O&a+0*5+1;
a[0][2]ªº¦ì§}¬O&a+0*5+2;
.....
a[1][1]ªº¦ì§}¬O&a+1*5+1;
.....
a[x][y]ªº¦ì§}¬O&a+x*5+y;
µ²½×
int b[N][M];
b[x][y]ªº¦ì§}¬O&b+x*M+y;
int c[N][M][P];
c[x][y][z]ªº¦ì§}¬O&c+x*M*P+y*P+z;
½d¨Ò:¦L¥X°}¦C¤¤©Ò¦³Åܼƪº°O¾ÐÅé¦ì§}
#include < stdio.h >
#define N 3
#define M 2
void main(void)
{
char a[N][M];
for ( int i=0; i < N; i++ )
{
for ( int j=0; j < M; j++ )
{
printf("%d\n",&a[j]);
}
}
}
°}¦CªºÀ³¥Î(1)
½d¨Ò:¥Î¤@ºû°}¦C¨ÓÀx¦s¦V¶q, §â¨âÓ¦V¶q¬Û¥[, µ²ªG°O¿ý¦b¥t¤@Ó°}¦C¤¤
#include < stdio.h >
#include < stdlib.h >
#include < time.h >
#define DIMENSION 3
#define MAX_NUMBER 10
void main(void)
{
int vector1[DIMENSION];
int vector2[DIMENSION];
int vector3[DIMENSION];
int i;
// µ¹©wpºâ¶Ã¼ÆªºªìÈ
srand( time(NULL) );
for ( i=0; i < DIMENSION; i++ )
{
// §Q¥Î¶Ã¼Æ¨Ó³]©w¼ÆÈ
vector1=rand()%(MAX_NUMBER+1);
vector2=rand()%(MAX_NUMBER+1);
}
// ¦L¥Xvector1ªº¤º®e
printf("(");
for ( i=0; i < DIMENSION; i++ )
{
printf("%d ",vector1);
}
printf(")+");
// ¦L¥Xvector2ªº¤º®e
printf("(");
for ( i=0; i < DIMENSION; i++ )
{
printf("%d ",vector2);
}
printf(")=");
// pºâ¨Ã¦L¥Xvector3ªº¤º®e
printf("(");
for ( i=0; i < DIMENSION; i++ )
{
vector3=vector1+vector2;
printf("%d ",vector3);
}
printf(")\n");
}
½d¨Ò:°O¿ý¥þ¯Z¦P¾Çªº¦Ò¸Õ¦¨ÁZ, §â§C©ó60¤À¤£¤Î®æªº¦P¾Ç¸¹½X¦L¥X¨Ó
#include < stdio.h >
#include < stdlib.h >
#include < time.h >
#define STUDENTS 30
#define MAX_NUMBER 100
void main(void)
{
int i;
int student[STUDENTS];
// µ¹©wpºâ¶Ã¼ÆªºªìÈ
srand( time(NULL) );
for ( i=0; i < STUDENTS; i++ )
{
// §Q¥Î¶Ã¼Æ¨Ó³]©w¼ÆÈ
student=rand()%(MAX_NUMBER+1);
}
for ( i=0; i < STUDENTS; i++ )
{
if ( student < 60 )
{
printf("Student %d get %d points, fail!\n", i, student);
}
}
}
¦r¦ê
¦bµ{¦¡»y¨¥¤¤, ¤@Ó^¤å³æ¦r, ¤@Ó¥y¤l, ³£¥i¥H·í¦¨¤@Ó¦r¦ê. ²³æªº»¡, n°O¿ýsize¶W¹L¤@Ó¦r¥ÀªºªF¦è, ´N¥s°µ¤@Ó¦r¦ê. ¦bC»y¨¥¤¤, ¤@Ó¤@ºûªºªº¦r¤¸°}¦C¥i¥H·í¦¨¤@Ó¦r¦ê.
#include < stdio.h >
void main(void)
{
char a[]={"Hello"};
printf("%s \n",a);
}
¤W±ªº¼gªk, ¬Û·í©ó
#include < stdio.h >
void main(void)
{
char a[6];
a[0]='H';
a[1]='e';
a[2]='l';
a[3]='l';
a[4]='o';
a[5]='\0'; // a[5]=0; 0¬O¦r¦êªºµ²§ô²Å¸¹
printf("%s \n",a);
}
©Î¬O
#include < stdio.h >
void main(void)
{
char a[6]={'H','e','l','l','o','\0'};
printf("%s \n",a);
}
§Q¥Îscanf¨ÓŪ¨ú¤@Ó¦r¦êªº¤èªk¦p¤U
#include < stdio.h >
void main(void)
{
char a[80];
scanf("%s",a);
printf("%s \n",a);
}
¥Îscanf¨ÓŪ¦r¦ê, ¦r¦ê¤¤¤£¯à¦³ªÅ¥Õ. Y¦³ªÅ¥Õ·|³Q·í¦¨¨âÓ¤£¦Pªº¦r¦ê. nŪ¨ú¦³ªÅ¥Õªº¦r¦ên¥Îgets;
#include < stdio.h >
void main(void)
{
char a[80];
gets(a);
printf("%s \n",a);
}
¦r¦ê¤£¯àª½±µ¤¬¬Û°µ¤ñ¹ï, ¤U±ªºµ{¦¡¬O¨S¦³·N¸qªº
#include < stdio.h >
void main(void)
{
char a[]="Hello";
char b[]="How are you";
// ¤U±ªº¤ñ¸û·|Åܦ¨ &a ©M &b ³o¨âÓ¦ì§}¤¬¬Û¥h¤ñ¸û
if ( a==b )
{
printf("a==b\n");
}
}
¤ñ¸û¦r¦ên¥ÎCªº®w¦s¨ç¦¡strcmp
#include < stdio.h >
#include < string.h >
void main(void)
{
char a[]="Hello";
char b[]="How are you";
if ( strcmp(a,b)==0 )
{
printf("a==b\n");
}
else
{
printf("a!=b\n");
}
}
©w¸q¦bstring.h¤¤¸g±`¨Ï¥Îªº¨ç¦¡¦³ ¨ç¦¡¦WºÙ ¥Î³~
strcpy copy¦r¦ê
strcat §â¤@Ó¦r¦ê´¡¨ì¥t¤@Ó¦r¦êªº«á±
strlen pºâ¦r¦êªºªø«×
½d¨Ò:
#include < stdio.h >
#include < string.h >
void main(void)
{
char a[80]="Hello,";
char b[]=" how are you?";
char c[80];
strcat(a,b);
printf("%s\n",a);
strcpy(c,a);
printf("%s\n",c);
printf("%d\n", strlen(c) );
}
¦r¦êÀ³¥Î(1)
½d¨Ò:§â¤@Ó¦r¦ê¤Ï¹L¨Ó
#include < stdio.h >
#include < string.h >
#define MAX_STRING 80
void main(void)
{
char a[MAX_STRING];
char b[MAX_STRING];
int len;
int i,j;
gets(a);
len=strlen(a);
for ( i=0, j=len-1; i < len; i++, j-- )
{
b[j]=a;
}
b[len]='\0';
printf("%s \n",b);
}
½d¨Ò:§â¦r¦ê¤¤ªºªÅ¥Õ¦r¤¸³£®³±¼
#include < stdio.h >
#include < string.h >
#define MAX_STRING 80
void main(void)
{
char a[MAX_STRING];
char b[MAX_STRING];
int len;
int i,j;
gets(a);
len=strlen(a);
for ( i=0, j=0; i <= len; i++ )
{
if ( a!=' ' )
b[j++]=a;
}
printf("%s \n",b);
}
¥t¤@ºØ¸Ñªk
#include < stdio.h >
#include < string.h >
#define MAX_STRING 80
void main(void)
{
char a[MAX_STRING];
int len;
int i,j;
gets(a);
len=strlen(a);
for ( i=0, j=0; i < len; i++ )
{
if ( a!=' ' )
a[j++]=a;
}
a[j]='\0';
printf("%s \n",a);
}
²Ä¤¸`:¦r¦ê¶i¶¥
¦pªG¦r¦ê·í¤¤ªº¦r¤¸³£¬O¼Æ¦r, Cªº®w¦s¨ç¦¡¤¤¦³´£¨Ñ§â¦r¦êÂà´«¦^¼Æ¦rªº¨ç¦¡.
#include < stdio.h >
#include < stdlib.h >
void main(void)
{
char string[]="100";
char string2[]="0.5";
int num;
double num2;
num=atoi(string);
// ¨ç¦¡atoi¥i¥H§â¦r¦ê´«ºâ¦¨¾ã¼Æ
num2=atof(string2);
// ¨ç¦¡atof¥i¥H§â¦r¦ê´«ºâ¦¨¯BÂI¼Æ
printf("%d %lf\n",num,num2);
}
¥Îsscanf¤]¥i¥H°µ¨ì¦P¼Ëªº®ÄªG
#include < stdio.h >
#include < stdlib.h >
void main(void)
{
char string[]="100";
char string2[]="0.5";
int num;
double num2;
sscanf(string,"%d",&num);
sscanf(string2,"%lf",&num2);
// sscanf©Mscanf¥Îªk¤@¼Ë, ¥u¬O¿é¤Jªº¨Ó·½§ï¦¨±q¦r¦ê¤¤Åª¨ú¦Ó¤£¬O±qÁä½L
printf("%d %lf\n",num,num2);
}
¬Û¹ï¦a, ¼Æ¦r¤]¥i¥H¤Ï¦^¥hÂন¦r¦ê, ³on¥Îsprintf¨Ó°µ¨ì.
#include < stdio.h >
#include < stdlib.h >
void main(void)
{
char string[80];
int num=5;
float fnum=0.5;
sprintf(string,"num=%d fnum=%f",num,fnum);
// sprintfªº¥Îªk©Mprintf§¹¥þ¬Û¦P,
// ¥u¬Oprintf§âµ²ªG¦L¦b¿Ã¹õ¤W, sprintf§âµ²ªG¦L¨ì¤@Ó¦r¦ê¤¤.
printf("%s\n",string);
}
atoi¤Îatof¨ç¦¡¥»¨ªº¹B§@ì²z«Ü²³æ, ¨ä¹ê´N¬O¤@Ó¤@Ó¦r¤¸¥h§â¼Æ¦rºâ¥X¨Ó¦Ó¤w. ¤U±¬O¤@Ó²³æªºatoi¨ç¦¡ªº¹Lµ{.
// ¼ÒÀÀatoi¨ç¦¡ªº¹B§@
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
void main(void)
{
char string[80];
int i;
int len;
int num;
gets(string);
len=strlen(string);
num=0;
for ( i=0; i < len; i++ )
{
num*=10;
num+=string-'0';
}
printf("%d\n",num);
}
sprintfªºì²z¤]¬O¤@Ó¤@ӼƦr§â¥¦¦A´«¦^¥h¦¨¬°¦r¤¸¦Ó¤w, ¤U±¬O¤@Ó§â¼Æ¦rÂà¦^¦r¦êªºµ{¦¡
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
void main(void)
{
char string[80];
char temp;
int i,j;
int len;
int num;
scanf("%d",&num);
len=0;
do
{
string[len++]='0'+num%10;
num/=10;
}while(num>0);
string[len]='\0';
// ¦r¦ê¤ÏÂà
for ( i=0, j=len-1; i < len/2; i++,j-- )
{
temp=string[j];
string[j]=string;
string=temp;
}
printf("%s\n",string);
}
¬°¤°»ò·|»Ýn¥Î¨ìsprintf?¦³«Ü¦h±¡§Î¤U, ¤£·|¨Ï¥Î¨ìprintf¨Ó°µ¿é¥X. ¹³¬O¦bµøµ¡§@·~¨t²Î¶}±Ò¤@ÓMessage Box¨ÓÅã¥Ü°T®§. ¦¹®É´N»Ýn¥ý§â»Ýn¿é¥Xªº¼Æ¦r³£Âন¦r¦ê, ¦A³z¹LWIN32 API ¨ç¦¡¨Ó¨q¥X°T®§.
½d¨Ò:¶}±Ò¤@ÓMessageBox¨ÓÅã¥Üpºâ¨D±oªº¥¿¤è§Î±¿n
#include < stdio.h >
#include < windows.h >
void main(void)
{
char string[80];
int r;
int area;
scanf("%d",&r);
area=r*r;
sprintf(string,"Area=%d",area);
MessageBox(NULL,string,"Hello",MB_OK);
// Win32 API¤§¤@, ·|¶}±Ò¤@Ó²³æªºmessage box, ¦L¥X²Ä2ӰѼƪº¦r¦ê¤º®e
}
¨º¬°¤°»ò»Ýn¥Î¨ìatoi/atof?¥Îscanf¨ÓŪÁä½L®É,¨Ï¥ÎªÌ¥i¯à·|¿é¤J¿ù»~. ¨Ò¦p»¡n¿é¤J¼Æ¦r«o¿é¤J¦r¤¸. ¤ñ¸û¦nªº¤èªk´N¬Oscanf¤¤Åª¶i¥hªºªF¦è³£¥ý§â¥¦·í¦¨¦r¦ê, Àˬd¦r¦ê¤¤¦³¨S¦³¤£¦X²zªºªF¦è¥X²{. ¤£¦X²z´Nn¨D«·s¿é¤J, ¦X²zªº¸Ü¤~§â¦r¦êÂন¼Æ¦r.
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
void main(void)
{
char string[80];
int num;
int len;
while(1)
{
printf("Please input an integer:");
scanf("%s",string);
// Àˬd¦r¦ê¤¤¦³¨S¦³¤£¬O¼Æ¦rªº¦r¤¸¥X²{
len=strlen(string);
for ( int i=0; i < len; i++ )
{
if ( string < '0' || string > '9' )
break;
}
// ¦pªG¨CÓ¦r¤¸³£¬O'0','1',...'9'·í¤¤ªº¥ô¤@Ó, ´N¸õ¥X°j°é
if ( i==len )
break;
}
num=atoi(string);
printf("I get %d\n",num);
} |
|