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]={'