c語言小程序
❶ 利用c語言做一個小程序
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}
以上是產生隨機數例子
void main( void )
{
FILE *stream;
char list[30];
int i, numread, numwritten;
/* Open file in text mode: */
if( (stream = fopen( "fread.out", "w+t" )) != NULL )
{
for ( i = 0; i < 25; i++ )
list[i] = (char)('z' - i);
/* Write 25 characters to stream */
numwritten = fwrite( list, sizeof( char ), 25, stream );
printf( "Wrote %d items\n", numwritten );
fclose( stream );
}
else
printf( "Problem opening the file\n" );
if( (stream = fopen( "fread.out", "r+t" )) != NULL )
{
/* Attempt to read in 25 characters */
numread = fread( list, sizeof( char ), 25, stream );
printf( "Number of items read = %d\n", numread );
printf( "Contents of buffer = %.25s\n", list );
fclose( stream );
}
else
printf( "File could not be opened\n" );
}
寫入文件例子。。。
組合一下總會吧。。。俺就不細改了,MSDN隨處可見
❷ c語言編程小程序
#include <stdio.h>
#include <string.h>
#define size 100
void main()
{
int a[size],b[size];
int n1,n2;
n1=0; n2=0;
printf("input arry a[n]: 結束時輸入 :-1\n ");
do
{
scanf("%d",&a[n1]);
} while (a[n1++]!=-1);
printf("input arry b[n]: 結束時輸入 :-1\n ");
do
{
scanf("%d",&b[n2]);
} while (a[n2++]!=-1);
int i,j,flag=1;
i=j=0;
while(a[i]!=-1&&flag){
j=0;
while(b[j]!=-1){
if(a[i]==b[j])
{
printf("第一個相同的元素為: %d\n",a[i]);
flag=0;
break;
}
j++;
}
i++;
}
if(flag==1)
printf("沒有相同的元素\n");
}
❸ c語言小程序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedefstruct
{
char*str_result;
floatf_result;
inti_result;
}RESULT;
RESULT*fun(char*s)
{
RESULTre;//定義一個返回值結構體
re.str_result=(char*)malloc(sizeof(char)*10);//分配內存用於存儲返回信息字元串,必須在堆上分配,也就是說不能用char數組
intlen=strlen(s);//獲取字元串長度
inti,dot=0;//dot:字元串中有幾個小數點dot等於幾,如果大於1則返回報錯信息
for(i=0;i<len;i++)
{
if(s[i]=='.')
{
dot++;
if(dot>1)//如果有多個小數點
{
strcpy(re.str_result,"anerror!");
re.f_result=0.0;
re.i_result=0;
return&re;
}
}
}
if(dot==0)//如果沒有小數點,則返回整數
{
strcpy(re.str_result,"整數");
re.i_result=atoi(s);
re.f_result=0.0;
return&re;
}
elseif(dot==1)//如果只有一個小數點,則返回float
{
strcpy(re.str_result,"浮點數");
re.f_result=atof(s);
re.i_result=0;
return&re;
}
}
intmain(intargc,char*argv[])
{
chars[10]={'