技术标签: C语言/C++
目录
convert.c
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
void main()
{
FILE *fp;
char str[100];
int i;
if((fp=fopen("file.txt", "w"))==NULL)
{
printf("无法打开文件\n");
exit(0);
}
printf("请输入一个字符串:\n");
gets(str);
/* 将字符串中的小写字符转换成大写字符,直到遇到"."为止 */
for(i=0; str[i]!='.'; i++)
{
if(str[i]>='a' && str[i]<='z')
str[i] = str[i] - 32;
fputc(str[i], fp); /* 将转换后的字符存入文件 */
}
fclose(fp);
fp = fopen("file.txt", "r");
for(i=0; str[i]!='.'; i++)
str[i] = fgetc(fp);
printf("%s\n", str);
fclose(fp);
}
运行
[email protected]:~/test/clanguage$ ./a.out
请输入一个字符串:
shilasgjtg.
SHILASGJTG.
rewind.c
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
void main()
{
char str[80];
FILE *fp; /* 定义一个文件类型的指针 */
/* 以写的方式打开文件test */
if((fp=fopen("test.txt", "w"))==NULL)
{
printf("Cannot open file.\n");
exit(0);
}
do{
printf("Please enter a string: \n");
gets(str);
strcat(str, "\n"); /* 增加一个新行 */
fputs(str, fp);
} while(*str!='\n');
/* 从文件中读出字符串,并将它们显示出来 */
rewind(fp); /* 重置文件指针 */
while(!feof(fp))
{
fgets(str, 79, fp);
printf(str);
}
fclose(fp);
}
运行
[email protected]:~/test/clanguage$ cat test.txt
lashdidsagh
idhsig
hjiheiw
asg
fread.c
# include <stdio.h>
# include <stdlib.h>
void main()
{
FILE *fp;
int i = 156;
long l = 9701076L;
double d = 3.456;
if((fp=fopen("test", "w"))==NULL)
{
printf("不能打开文件.\n");
exit(0);
}
fwrite(&i, sizeof(int), 1, fp);
fwrite(&l, sizeof(long), 1, fp);
fwrite(&d, sizeof(double), 1, fp);
rewind(fp);
fread(&i, sizeof(int), 1, fp);
fread(&l, sizeof(long), 1, fp);
fread(&d, sizeof(double), 1, fp);
printf("i = %d\n", i);
printf("l = %ld\n", l);
printf("d = %f\n", d);
fclose(fp);
}
运行
[email protected]:~/test/clanguage$ ./a.out
i = 156
l = 9701076
d = 3.456000
fprintf.c
# include <stdio.h>
# include <io.h>
# include <stdlib.h>
void main()
{
FILE *fp;
char str[80];
int i;
if((fp=fopen("test", "w"))==NULL)
{
printf("不能打开文件.\n");
exit(0);
}
printf("Please enter a string and a number: \n");
fscanf(stdin, "%s %d", str, &i); /* 参数stdin表示从键盘读入 */
fprintf(fp, "%s %d", str, i);
fclose(fp);
if((fp=fopen("test", "r"))==NULL)
{
printf("不能打开文件.\n");
exit(0);
}
fscanf(fp, "%s %d", str, &i);
fprintf(stdout, "%s %d\n", str, i); /* 参数stdout表示写向屏幕 */
fclose(fp);
}
运行
[email protected]:~/test/clanguage$ ./a.out
Please enter a string and a number:
sahig 12
sahig 12
[email protected]:~/test/clanguage$ cat test
sahig [email protected]:~/test/clanguage$
fseek.c
# include <stdio.h>
# include <stdlib.h>
void main(int argc, char *argv[])
{
FILE *fp;
if(argc!=3)
{
printf("缺少字节位置,无法进行操作。\n");
exit(0);
}
if((fp=fopen(argv[1], "rb"))==NULL)
{
printf("无法打开文件。\n");
exit(0);
}
if(fseek(fp, atol(argv[2]), 0))
{
printf("寻找出现错误。\n");
exit(0);
}
printf("在%ld处的字符是%c。\n", atol(argv[2]), getc(fp));
fclose(fp);
}
运行
[email protected]:~/test/clanguage$ ./a.out test 2
在2处的字符是h。
ferror.c
# include <stdio.h>
# include <stdlib.h>
# define TAB_NUM 8 /* 定义应换的空格数 */
# define IN 0
# define OUT 1
void error(int e)
{
if(e == IN)
printf("输入错误。\n");
else
printf("输出错误。\n");
exit(1); /* 跳出程序 */
}
/* 为使用该程序,应该指定命令行中的输入和输出出文件名 */
void main(int argc, char *argv[])
{
FILE *in, *out;
int tab, i;
char ch;
if(argc != 3)
{
printf("用法出错。\n");
exit(1);
}
if((out=fopen(argv[1], "wb"))==NULL)
{
printf("不能打开输入文件%s。\n", argv[1]);
exit(1);
}
if((out=fopen(argv[2], "wb"))==NULL)
{
printf("不能打开输出文件%s。\n", argv[2]);
exit(1);
}
tab = 0;
do{
ch = getc(in);
if(ferror(in))
error(IN);
/* 如果发现制表符,则输出相同数目的空格符 */
if(ch == '\t')
{
for(i=tab; i<8; i++)
{
putc(' ', out);
if(ferror(out))
error(OUT);
}
tab = 0;
}
else
{
putc(ch, out);
if(ferror(out))
error(OUT);
tab++;
if(tab==TAB_NUM)
tab = 0;
if(ch=='\n'||ch=='\r')
tab = 0;
}
} while(!feof(in));
fclose(in);
fclose(out);
}
commu.c
# include <stdio.h>
# include <stdlib.h>
# define MAX 100
struct addr
{
char name[20];
char street[40];
char city[20];
char state[4];
unsigned long zip;
} addr_list[MAX];
void init_list(void);
void enter(void);
void dele(void);
void list(void);
void save(void);
void load(void);
int menu_select(void);
int find_free(void);
void main()
{
char choice;
init_list();
for( ; ; )
{
choice = menu_select();
switch(choice)
{
case 1: enter();
break;
case 2: dele();
break;
case 3: list();
break;
case 4: save();
break;
case 5: load();
break;
case 6: exit(0);
}
}
}
void init_list(void)
{
register int t;
for(t=0; t<MAX; t++)
addr_list[t].name[0] = '\0';
}
void enter(void)
{
int slot;
char str[80];
slot = find_free();
if(slot == -1)
printf("\nList Full");
printf("Enter name: ");
gets(addr_list[slot].name);
printf("Enter street: ");
gets(addr_list[slot].street);
printf("Enter city: ");
gets(addr_list[slot].city);
printf("Enter state: ");
gets(addr_list[slot].state);
printf("Enter zip: ");
gets(str);
addr_list[slot].zip = strtoul(str, '\0', 10);
}
void dele(void)
{
register int slot;
char str[80];
printf("Enter record #: ");
gets(str);
slot = atoi(str);
if(slot>=0 && slot<MAX)
addr_list[slot].name[0] = '\0';
}
void list(void)
{
register int t;
for(t=0; t<MAX; t++)
{
if(addr_list[t].name[0])
{
printf("%s\n", addr_list[t].name);
printf("%s\n", addr_list[t].street);
printf("%s\n", addr_list[t].city);
printf("%s\n", addr_list[t].state);
printf("%s\n\n", addr_list[t].zip);
}
}
printf("\n\n");
}
void save(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist", "wb"))==NULL)
printf("Cannot open file.\n");
for(i=0; i<MAX; i++)
if(*addr_list[i].name)
if(fwrite(&addr_list[i], sizeof(struct addr), 1, fp)!=1)
printf("File write error.\n");
fclose(fp);
}
void load(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist", "rb"))==NULL)
printf("Cannot open file.\n");
init_list();
for(i=0; i<MAX; i++)
if(fread(&addr_list[i], sizeof(struct addr), 1, fp)!=1)
{
if(feof(fp))
break;
printf("File read error.\n");
}
fclose(fp);
}
int menu_select(void)
{
char str[80];
int c;
printf("1. Enter a name\n");
printf("2. Delete a name\n");
printf("3. List the file\n");
printf("4. Save the file\n");
printf("5. Load the file\n");
printf("6. Quit\n");
do{
printf("\nEnter your choice: ");
gets(str);
c = atoi(str);
} while(c<0 || c>6);
return c;
}
int find_free(void)
{
register int t;
for(t=0; addr_list[t].name[0]&&t<MAX; t++);
if(t==MAX)
return -1;
return t;
}
运行
[email protected]:~/test/clanguage$ ./a.out
1. Enter a name
2. Delete a name
3. List the file
4. Save the file
5. Load the file
6. Quit
Enter your choice: 1
Enter name: gll
Enter street: s
Enter city: china
Enter state: sx
Enter zip: z
1. Enter a name
2. Delete a name
3. List the file
4. Save the file
5. Load the file
6. Quit
Enter your choice: 6
dynamic.c
# include <stdio.h>
# include <stdlib.h>
# define NUM 10
int main()
{
char *str[NUM]; /* 定义一个字符性的指针数组 */
int t;
/* 为数组中的每个指针分配内存 */
for(t=0; t<NUM; t++)
{
if((str[t]=(char *)malloc(128))==NULL)
{
printf("Allocation Error.\n");
exit(1);
}
/* 在分配的内存中存放字符串 */
printf("Enter string %d: ", t);
gets(str[t]);
}
/* 释放内存 */
for(t=0; t<NUM; t++)
free(str[t]);
/* 由于主函数有返回值,故返回0值 */
return 0;
}
运行
[email protected]:~/test/clanguage$ ./a.out
Enter string 0: s
Enter string 1: f
Enter string 2: g
Enter string 3: y
Enter string 4: h
Enter string 5: r
Enter string 6: h
Enter string 7: c
Enter string 8: a
Enter string 9: x
time.c
# include <time.h>
# include <stdio.h>
int main()
{
struct tm *local;
time_t tm;
tm = time(NULL);
local = localtime(&tm);
printf("Local time and date: %s\n", asctime(local));
local = gmtime(&tm);
printf("UTC time and date: %s\n", asctime(local));
return 0;
}
运行‘
[email protected]:~/test/clanguage$ ./a.out
Local time and date: Sun Mar 8 08:44:08 2020
UTC time and date: Sun Mar 8 15:44:08 2020
convert.c
# include <stdlib.h>
# include <stdio.h>
int main()
{
char num1[80], num2[80];
double sum1;
int sum2;
long sum3;
printf("Enter first: ");
gets(num1);
printf("Enter second: ");
gets(num2);
sum1 = atof(num1)+atof(num2);
printf("The sum is: %f\n", sum1);
printf("Enter three: ");
gets(num1);
printf("Enter four: ");
gets(num2);
sum2 = atoi(num1)+atoi(num2);
printf("The sum is: %d\n", sum2);
printf("Enter five: ");
gets(num1);
printf("Enter six: ");
gets(num2);
sum3 = atol(num1)+atol(num2);
printf("The sum is: %ld\n", sum3);
return 0;
}
运行
[email protected]:~/test/clanguage$ ./a.out
Enter first: 12
Enter second: 45
The sum is: 57.000000
Enter three: 77
Enter four: 98
The sum is: 175
Enter five: 90
Enter six: 111
The sum is: 201
search.c
# include <stdio.h>
# include <stdlib.h>
# include <ctype.h>
char *alpha = "abcdefghijklmnopqrstuvwxyz";
int comp(const void *ch, const void *s);
int main()
{
char ch;
char *p;
printf("Enter a character: ");
ch = getchar();
ch = tolower(ch); /* 将变元ch转换成小写字符 */
p = (char *)bsearch(&ch, alpha, 26, 1, comp);
if(p)
printf("%c is in alphabet\n", *p);
else
printf("is not in alphabet\n");
return 0;
}
int comp(const void *ch, const void *s)
{
return *(char *)ch -*(char *)s;
}
运行
[email protected]:~/test/clanguage$ ./a.out
Enter a character: i
i is in alphabet
jump.c
# include <setjmp.h>
# include <stdio.h>
jmp_buf ebuf; /* 类型在<setjmp.h>中定义 */
void fun(void);
int main()
{
int i;
printf("1 ");
i = setjmp(ebuf); /* 第一次调用时返回值为零 */
if(i == 0)
{
fun();
printf("This will not be printed.");
}
printf("%d\n", i);
return 0;
}
void fun(void)
{
printf("3 ");
longjmp(ebuf, 5); /* 跳转到执行setjmp()的地方,但此时函数setjmp()返回值为3 */
}
运行
[email protected]:~/test/clanguage$ ./a.out
1 3 5
sort.c
# include <stdlib.h>
# include <stdio.h>
int num[12] = {
14, 5, 9, 7, 6, 0, 91, 4, 1, 3, 2, 8
};
int comp(const void *, const void *);
int main()
{
int i;
printf("Original array: ");
for(i=0; i<12; i++)
printf("%d ", num[i]);
printf("\n");
qsort(num, 12, sizeof(int), comp);
printf("Sorted array: ");
for(i=0; i<12; i++)
printf("%d ", num[i]);
printf("\n");
return 0;
}
int comp(const void *i, const void *j)
{
return *(int *)i - *(int *)j;
}
运行
[email protected]:~/test/clanguage$ ./a.out
Original array: 14 5 9 7 6 0 91 4 1 3 2 8
Sorted array: 0 1 2 3 4 5 6 7 8 9 14 91
rand.c
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
/* 利用系统时间来寻找随机数,并将前十个随机数显示出来 */
int main()
{
long time1;
int i, time2;
/* 获得正确的日历时间 */
time1 = time(NULL); /* 返回系统的当前日历时间 */
printf("%ld\n", time1);
time2 = (unsigned)time1/2;
printf("%ld\n", time2);
/* 以系统时间为参数,为即将生成的伪随机数序列设置起点 */
srand(time2);
/* 生成十个伪随机数序列 */
for(i=0; i<10; i++)
printf("%d ", rand());
printf("\n");
return 0;
}
运行
[email protected]:~/test/clanguage$ ./a.out
1583682555
791841277
937882791 822067410 1112968171 837771753 1392239192 870112025 1562303515 1412078305 1705961954 752498124
change.c
# include <stdio.h>
# include <stdarg.h>
double sum_series(int num, ...);
int main()
{
double d;
d = sum_series(4, 0.5, 0.25, 0.125, 0.0625);
printf("Sum of series is %f.\n", d);
return 0;
}
double sum_series(int num, ...)
{
double sum = 0.0, t;
va_list argptr;
/* 初始化argptr */
va_start(argptr, num);
/* 计算序列之和 */
for( ; num; num--)
{
t = va_arg(argptr, double);
sum = sum + t;
}
va_end(argptr);
return sum;
}
运行
trust1[email protected]:~/test/clanguage$ ./a.out
Sum of series is 0.937500.
怎样利用通达信公式选股?
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ---------------------- 一.String概述/* * string:字符串类 public final class String{} * 1、 只要是""定义的都是String的对象 * 2、字符串的最大特点:一旦被初始化就
版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/wx1528159409三个核心选举原则:(1)Zookeeper集群中只有超过半数以上的服务器启动,集群才能正常工作;(2)在集群正常工作之前,myid小的服务器给myid大的服务器投票,直到集群正常工作,选出Leader;(3)选出Leader之后,之前的服务器状态由Looking...
目的:矢量图画好后,检查是否有伪节点,悬挂节点等,线要素和面要素都可以检查。伪节点,两条线应该相交但是画的没相交;悬挂节点,两条线看似相交了但是没有节点,因此路径不同(类似于高架桥和交叉口,悬挂节点就类似高架桥)基本步骤:1、建立拓扑2、验证拓扑拓扑的建立可在arcCatalog里完成,也可以在arcMap里右侧目录下完成(本人常称小catalog)。catalog是管理文件的一个软件。 注意!!...
1 基于均值的图像二值化抽象类 AbstractImageOptionFilterpackage binimage.utils;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;public abstract class AbstractImageOptionFilter { pub...
<br />转帖地址:http://www.cnblogs.com/Fooo/archive/2010/10/13/1850179.html<br /> <br />排除浏览器等其他因素,只考虑程序的问题,<br />基本上是因为没在 HttpWebRequest.GetResponse 之前先对 request 的 CookieContainer 实例化<br /><br />简单代码如下:<br /> HttpWebRequest myRequest = (Http
5.编写一个Java应用程序,输出全部的希腊字母。提示:找到第一个希腊字母α在Unicode中的编码,最后一个希腊字母ω在Unicode中的编码。然后循环输出这个范围内所有的字符,就得到了希腊字母表public class my_work { public static void main(String []args){ for(int i=(int)'α';i<=(int)'ω';i++){ System.out.print((char)i);
Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的MultipartResolver实现的。Spring用Jakarta Commons FileUpload技术实现了一个MultipartResolver实现类:CommonsMultipartResovlerSpringMVC上下文中默认没有装配MultipartResovler,因此默认情况下不能处理文件的上传工作...
列表: 可以存储多个任意类型的数据,好比是一个容器,列表属于容器类型列表的表现形式:[x,y, ....]列表的类型: list# 列表可以存储多个任意类型的数据my_list = [4, 3.14, True, range(10), 'hi python', [1, 3], 100]print(my_list, type(my_list))# 获取列表中的某个数据,可以通过下标来完成result = my_list[0]print(result)result = my_list[
如何通过正则表达式获取img标签的src属性1、部分正则语法介绍\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。? 匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \?。| 指明两项之间的一个选择。要匹配 |,请使用 \|。. 匹配除换行符 \n 之外的任何单字符。要匹配 ....
1:登陆对应的数据库服务器 2:找到oracle磁盘空间(d:oracle\product\10.2.0\db_1\RDBMS\Admin) 3:执行cmd-cd d:回车 4: cd d:oracle\product\10.2.0\db_1\RDBMS\Admin 回车 5:sqlplus 用户名/密码@服务连接名(例:sqlplus carmot_esz_1/[email protected]
1 推荐系统基础1.1 个性化推荐概述1.1.1 推荐系统概述首先,需要申明一点的就是推荐系统!=推荐算法。推荐系统是一套完善的推荐机制,包括前期数据的准备、具体推荐的过程(这个过程可能是一套复杂的算法模型,也可能是一个简单的规则,也可能是多种模型的混合结果等等)、后期数据的预测、AB测试效果评估等等。1.1.2 推荐算法模型概述在算法模型上大体可以分基于内