源.cpp右键-c-预处理器- _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//需求:输入一个字符串将字符串逆序输出到txt文件中
int main()
{
char temp;
char c[50];
printf("请输入一个字符串:");
gets_s(c);
int length = strlen(c);
for (int i = 0; i < length / 2; i++)
{
temp = c[i];
c[i] = c[length - 1 - i];
c[length - 1 - i] = temp;
}
FILE * p = fopen("D:\\学习\\考研\\自学笔记\\计算机自学笔记\\考研c语言上机\\day01字符串练习\\输出结果.txt","w+");
for (int i = 0; i < length; i++)
{
fprintf(p,"%c",c[i]);
}
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
//需求输入一组数字,进行排序后每四个一组输出到txt文件中
int main()
{
int a[50];
int length;
int temp;
printf("请输入要输入的数的个数:\n");scanf_s("%d",&length);
if (length <= 0)
{
printf("个数必须大于0!请重新输入数的个数");
scanf_s("%d",&length);
}
printf("请输入各个数");
for (int i = 0; i < length; i++)
{
scanf_s("%d",&a[i]);
}
for (int i = 0; i < length - 1; i++)
{
for (int j = length - 1; j > i; j--)
{
if (a[j] < a[j - 1])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
FILE * p = fopen("D:\\学习\\考研\\自学笔记\\计算机自学笔记\\考研c语言上机\\day02数组练习\\输出结果.txt","w+");
for (int i = 0; i < length; i++)
{
if (i % 4 == 3)
{
fprintf(p,"%d \n",a[i]);
}
else
{
fprintf(p, "%d ", a[i]);
}
}
fclose(p);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//需求:从键盘输入一个字符串,统计其中的大写字母、小写字母、数字字符、其他字符的数量并将结果写入txt中
int main()
{
char c[50];
printf("请输入一个字符串:\n");
gets(c);
int i = 0, n1 = 0, n2 = 0, n3 = 0, n4 = 0;//都要赋初值0
while (c[i] != 0)//字符串结束的标志,注意0是没有引号的
{
if (c[i] >= 'A' && c[i] <= 'Z')
{
n1++;
}
else if (c[i] >= 'a' && c[i] <= 'z')
{
n2++;
}
else if (c[i] >= '0' && c[i] <= '9')
{
n3++;
}
else
{
n4++;
}
i++;
}
FILE * p = fopen("D:\\学习\\考研\\自学笔记\\计算机自学笔记\\考研c语言上机\\day01字符串练习\\输出结果.txt","w+");
fprintf(p,"大写字母个数为:%d,小写字母个数为:%d,数字字符个数为:%d,其他字符个数为:%d",n1,n2,n3,n4);
fclose(p);
system("pause");
return 0;
}
#include<stdlib.h>
#include<math.h>
//需求,输入一个四位数字如输入2018输出2_0_1_8并将结果写入text文件中
int digit(int num, int n);
int main()
{
char c[8];
int num,n1,n2,n3,n4;//n1到n4分别代表num的各个位上的值
printf("请输入一个四位的数字");
scanf_s("%d",&num);
n1 = digit(num, 1);
n2 = digit(num, 2);
n3 = digit(num, 3);
n4 = digit(num, 4);
c[0] = n4 + '0' ;
c[2] = n3 + '0';
c[4] = n2 + '0';
c[6] = n1 + '0';
c[7] = '\0';
c[1] = c[3] = c[5] = '_';
FILE * p = fopen("D:\\学习\\考研\\自学笔记\\计算机自学笔记\\考研c语言上机\\day01字符串练习\\输出结果.txt","w+");
for (int i = 0; i < 8; i++)
{
fprintf(p,"%c",c[i]);
}
fclose(p);
system("pause");
return 0;
}
int digit(int num,int n)
{
int d = 1;
if (n == 1)
return num % 10;
for (int i = 2; i <= n; i++)
{
d = d * 10;
}
return (int)(num / d) % 10;
}