C和C++练习

要点:

1、数组

2、冒泡排序BubbleSort

3、带指针的结构体(malloc,free)

4、字符串操作(拷贝、逆序、比较)

5、格式化输出printf,sprintf

6、格式化输入,scanf,sscanf

7、文件操作fopen,feof,EOF,fputc,fgetc,fputs,fgets,stdin,stdout

8、数组传参(需要指定长度)、字符串传参(不需指定长度,\0结尾)

#define _CRT_SECURE_NO_WARNINGS
//#pragma warning(diable:4996)
#include <iostream>
using namespace std;
#include <vector>


//遍历测试
void foreachTest()
{
	vector<int> vec{ 1,3,5,6,7,5,3 };
	for (auto a : vec)
	{
		cout << a << ",";
	}
	cout << endl;
	for (auto a : vec)
	{
		a *= 3;
	}
	for (auto a : vec)
	{
		cout << a << ",";
	}
	cout << endl;

	for (auto& a : vec)
	{
		a *= 3;
	}
	for (auto a : vec)
	{
		cout << a << ",";
	}
	cout << endl;
	//  1, 3, 5, 6, 7, 5, 3,
	//	1, 3, 5, 6, 7, 5, 3,
	//	3, 9, 15, 18, 21, 15, 9,
	//	请按任意键继续. . .
}
//拉姆达表达式测试
void lambdaTest()
{
	int a = 1;
	int b = 2;
	auto f = [a, &b] {
		cout << "a= " << a << endl;
		cout << "b= " << b << endl;
		//a++;//表达式必须是可修改的左值
		b++;//OK
	};
	f();
	f();
	f();
	cout << "final b= " << b << endl;

}
//a = 1
//b = 2
//a = 1
//b = 3
//a = 1
//b = 4
//final b = 5
//请按任意键继续. . .

#include <functional>

//拉姆达表达式作为返回值
function<int(int, int)> returnLambda()
{
	return [](int x, int y) {return x + y; };
}
//评分1
void scoreTest1()
{
	int score;
	printf("请输入分数:");
	scanf("%d", &score);
	if (score >= 90)
	{
		printf("优秀\n");
	}
	else if (score >= 80)
	{
		printf("良好\n");
	}
	else if (score >= 60)
	{
		printf("及格\n");
	}
	else
	{
		printf("不及格\n");
	}

}
//评分2
void scoreTest2()
{
	int score2;
	printf("请输入分数:");
	scanf("%d", &score2);
	switch (score2 / 10)
	{
	case(10):
		printf("优秀\n");
		break;
	case(9):
		printf("优秀\n");
		break;
	case(8):
		printf("良好\n");
		break;
	case(7):
		printf("及格\n");
		break;
	case(6):
		printf("及格\n");
		break;

	default:
		printf("不及格\n");
		break;
	}
}
//评分3
void scoreTest3()
{
	int score;
	printf("请输入分数:");
	scanf("%d", &score);
	switch (score / 10)
	{
	case(10):
	case(9):
		printf("优秀\n");
		break;
	case(8):
		printf("良好\n");
		break;
	case(7):
	case(6):
		printf("及格\n");
		break;
	default:
		printf("不及格\n");
		break;
	}

}
//三只小猪的游戏
void threePigs1()
{
	int pig1, pig2, pig3;
	printf("请输入三只小猪的重量,以空格分隔:\n");
	scanf("%d %d %d", &pig1, &pig2, &pig3);
	if (pig1 > pig2)
	{
		if (pig1 > pig3)
		{
			printf("第1只小猪最重:%d\n", pig1);
		}
		else
		{
			printf("第3只小猪最重:%d\n", pig3);
		}
	}
	else
	{
		if (pig2 > pig3)
		{
			printf("第2只小猪最重:%d\n", pig2);
		}
		else
		{
			printf("第3只小猪最重:%d\n", pig3);
		}
	}
}

void sevenGame()
{
	int start = 1;
	while (start <= 100)
	{
		if (start % 7 == 0 || start % 10 == 7 || start / 10 == 7)
		{
			printf("拍桌子\n");
		}
		else
		{
			printf("%d\n", start);
		}
		start++;
	}
}
//格式化打印 测试
void printTest1()
{
	int n = 70000;
	printf("%%hd= %hd\n", n);
	printf("%%d= %d\n", n);
	printf("%%ld= %d\n", n);



	printf("%%x= %x\n", n);
	printf("%%X= %X\n", n);
	printf("%%#x= %#x\n", n);
	printf("%%#X= %#X\n", n);

	char str1[] = "hello world";
	printf("%%s= %s\n", str1);
	printf("%%c= %c\n", str1);
	char str2[] = { 'h','e','l','l','o',' ','w','o','r','l','d' };
	printf("%%s= %s\n", str2);
	printf("%%c= %c\n", str2);

	float f = 3.14;
	printf("%%f= %f\n", f);

	printf("%%lf= %lf\n", f);
	printf("%%e= %e\n", f);
	printf("%%le= %le\n", f);
	printf("%%E= %E\n", f);
	printf("%%LE= %LE\n", f);
	printf("%%g= %g\n", f);
	printf("%%lg= %lg\n", f);
	printf("%%G= %G\n", f);
	printf("%%LG= %LG\n", f);

	double d = 123.456789;

	printf("%%f= %f\n", d);
	printf("%%8.1f= %5.1f %5.1f\n", d, d);
	printf("%%8.2f= %5.2f %5.2f\n", d, d);
	printf("%%5.3f= %5.3f\n", d);
	printf("%%5.4f= %5.4f\n", d);

	int m = 5678;
	printf("%%d= %d\n", m);
	printf("%%1d= %1d\n", m);
	printf("%%2d= %2d\n", m);
	printf("%%3d= %3d\n", m);
	printf("%%4d= %4d\n", m);


	unsigned int u = 10;
	unsigned short uh = 20;
	unsigned long ul = 30;
	unsigned long long ull = 40;
	printf("%%u= %u\n", u);
	printf("%%hu= %hu\n", uh);
	printf("%%lu= %lu\n", ul);
	printf("%%llu= %llu\n", ull);
}
//% hd = 4464
//% d = 70000
//% ld = 70000
//% x = 11170
//% X = 11170
//% #x = 0x11170
//% #X = 0X11170
//% s = hello world
//% c =
//% s = hello world烫烫烫烫蘦ello world
//% c = ?
//% f = 3.140000
//% lf = 3.140000
//% e = 3.140000e+00
//% le = 3.140000e+00
//% E = 3.140000E+00
//% LE = 3.140000E+00
//% g = 3.14
//% lg = 3.14
//% G = 3.14
//% LG = 3.14
//% f = 123.456789
//% 8.1f = 123.5 123.5
//% 8.2f = 123.46 123.46
//% 5.3f = 123.457
//% 5.4f = 123.4568
//% d = 5678
//% 1d = 5678
//% 2d = 5678
//% 3d = 5678
//% 4d = 5678
//% u = 10
//% hu = 20
//% lu = 30
//% llu = 40
//请按任意键继续. . .

//数组测试1
void arrTest1()
{
	int arr1[5] = { 1,2,3 };
	size_t len1 = sizeof(arr1) / sizeof(arr1[0]);
	printf("len of arr1= %d\n", len1);
	printf("arr1= %p\n", arr1);
	for (size_t i = 0; i < len1; i++)
	{
		printf("&arr1[%u]= %p , arr1[%u]= %p\n", i, &arr1[i], i, arr1[i]);
	}
	int arr2[10] = {  };//全部初始化为0
	size_t len2 = sizeof(arr2) / sizeof(arr2[0]);
	printf("len of arr2= %d\n", len2);
	for (size_t i = 0; i < len2; i++)
	{
		printf("arr2[%u]= %d\n", i, arr2[i]);
	}
	int arr3[] = { 3,4,5 };
	size_t len3 = sizeof(arr3) / sizeof(arr3[0]);
	printf("len of arr3= %d\n", len3);
	for (size_t i = 0; i < len3; i++)
	{
		printf("arr3[%u]= %d\n", i, arr3[i]);
	}
	int arr4[6];
	size_t len4 = sizeof(arr4) / sizeof(arr4[0]);
	printf("len of arr4= %d\n", len4);
	for (size_t i = 0; i < len4; i++)
	{
		printf("arr4[%u]= %d\n", i, arr4[i]);
	}
}

//len of arr1 = 5
//arr1 = 00CFF6D4
//& arr1[0] = 00CFF6D4, arr1[0] = 00000001
//& arr1[1] = 00CFF6D8, arr1[1] = 00000002
//& arr1[2] = 00CFF6DC, arr1[2] = 00000003
//& arr1[3] = 00CFF6E0, arr1[3] = 00000000
//& arr1[4] = 00CFF6E4, arr1[4] = 00000000
//len of arr2 = 10
//arr2[0] = 0
//arr2[1] = 0
//arr2[2] = 0
//arr2[3] = 0
//arr2[4] = 0
//arr2[5] = 0
//arr2[6] = 0
//arr2[7] = 0
//arr2[8] = 0
//arr2[9] = 0
//len of arr3 = 3
//arr3[0] = 3
//arr3[1] = 4
//arr3[2] = 5
//len of arr4 = 6
//arr4[0] = -858993460
//arr4[1] = -858993460
//arr4[2] = -858993460
//arr4[3] = -858993460
//arr4[4] = -858993460
//arr4[5] = -858993460


//数组测试2
void arrTest2()
{
	int arr[] = { 1,2,3 };
	int* p = arr;
	size_t len = sizeof(arr) / sizeof(arr[0]);
	for (size_t i = 0; i < len; i++)
	{
		printf("arr[%u]= %d\n", i, arr[i]);
		printf("*(arr+%u)= %d\n", i, *(arr + i));
		printf("p[%u]= %d\n", i, p[i]);
		printf("*(p+%u)= %d\n", i, *(p + i));
		printf("\n");
	}
}
//arr[0] = 1
//* (arr + 0) = 1
//p[0] = 1
//* (p + 0) = 1
//
//arr[1] = 2
//* (arr + 1) = 2
//p[1] = 2
//* (p + 1) = 2
//
//arr[2] = 3
//* (arr + 2) = 3
//p[2] = 3
//* (p + 2) = 3

//冒泡排序
void BubbleSort(int* arr, size_t len, bool ascending = true)
{
	int temp;
	for (size_t i = 0; i < len - 1; i++)
	{
		for (size_t j = 0; j < len - 1 - i; j++)
		{
			if (ascending ? arr[j] > arr[j + 1]:arr[j] < arr[j + 1])
			{
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}
//冒泡排序 测试
void SortTest1()
{
	int arr[] = { 4,65,33,5,7,88,2,6,23 };
	size_t len = sizeof(arr) / sizeof(arr[0]);
	BubbleSort(arr, len, false);
	for (size_t i = 0; i < len; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void printSize(int* arr)
{
	printf("size of arr arg = %u\n", sizeof(arr));
}

void sizeTest()
{
	int arr[] = { 4,5,6,7,8 };
	printf("size of arr = %u\n", sizeof(arr));
	printSize(arr);
}
//size of arr = 20
//size of arr arg = 4
//请按任意键继续. . .

//字符串比较1
int strCmp1(char* str1, char* str2)
{
	int i = 0;
	while (str1[i] == str2[i])
	{
		if (str1[i] == '\0')
		{
			return 0;
		}
		i++;
	}
	return str1[i] > str2[i] ? 1 : -1;
}
//字符串比较2
int strCmp2(char* str1, char* str2)
{
	int i = 0;
	while (*(str1 + i) == *(str2 + i))
	{
		if (*(str1 + i) == '\0')
		{
			return 0;
		}
		i++;
	}
	return (*(str1 + i)) > (*(str2 + i)) ? 1 : -1;
}
//字符串比较 测试
void strCmpTest()
{
	char str1[] = "hello";
	char str2[] = "hello";
	int res = strCmp2(str1, str2);
	printf("str1= %s\n", str1);
	printf("str2= %s\n", str2);
	if (res == 0)
	{
		printf("str1==str2\n");
	}
	else if (res == 1)
	{
		printf("str1>str2\n");

	}
	else if (res == -1)
	{
		printf("str1<str2\n");
	}
}
//str1 = hello
//str2 = hello world
//str1 < str2
//请按任意键继续. . .
//str1 = hello
//str2 = hella
//str1 > str2
//请按任意键继续. . .
//str1 = hello
//str2 = hello
//str1 == str2
//请按任意键继续. . .
void strTest1()
{
	char str1[] = "abc";
	char str2[] = { 'a','b','c' };
	//char* str3 = "abc";//错误	"const char *" 类型的值不能用于初始化 "char *" 类型的实体

	//char* str4 = { 'a','b','c' }; //错误 初始值设定项值太多	
	printf("str1= %s\n", str1);
	printf("str2= %s\n", str2);
	str1[0] = 'R';
	str2[0] = 'X';
	printf("str1= %s\n", str1);
	printf("str2= %s\n", str2);
}
//str1 = abc
//str2 = abc烫烫烫烫蘟bc
//str1 = Rbc
//str2 = Xbc烫烫烫烫蘎bc
//请按任意键继续. . .

//字符串拷贝1
void strCpy1(char* src, char* dst)
{
	int i = 0;
	while (src[i] != '\0')
	{
		dst[i] = src[i];
		i++;
	}
	dst[i] = '\0';
}
//字符串拷贝2
void strCpy2(char* src, char* dst)
{
	int i = 0;
	while (*(src + i) != '\0')
	{
		*(dst + i) = *(src + i);
		i++;
	}
	*(dst + i) = '\0';
}
//字符串拷贝3
void strCpy3(char* src, char* dst)
{
	while (*src != '\0')
	{
		*dst = *src;
		dst++;
		src++;
	}
	*dst = '\0';
}
//移除空格1
void removeSpace1(char* src, char* dst)
{
	int i = 0, j = 0;
	while (src[i] != '\0')
	{
		if (src[i] != ' ')
		{
			dst[j] = src[i];
			j++;
		}
		i++;
	}
	dst[j] = '\0';
}
//移除空格2
void removeSpace2(char* src, char* dst)
{
	int i = 0, j = 0;
	while (*(src + i) != '\0')
	{
		if (*(src + i) != ' ')
		{
			*(dst + j) = *(src + i);
			j++;
		}
		i++;
	}
	*(dst + j) = '\0';
}
//移除空格3
void removeSpace3(char* src, char* dst)
{
	while (*src != '\0')
	{
		if (*src != ' ')
		{
			*dst = *src;
			dst++;
		}
		src++;
	}
	*dst = '\0';
}
//字符串拷贝测试
void strCpyTest()
{
	char src[] = "ni chou sha chou ni za di";
	size_t len = sizeof(src);
	printf("len of src = %d\n", len);
	char dst1[sizeof(src)] = {};
	strCpy1(src, dst1);
	char dst2[sizeof(src)] = {};
	strCpy2(src, dst2);
	printf("src= %s\n", src);
	printf("copy dst1= %s\n", dst1);
	printf("copy dst2= %s\n", dst2);

	char dst3[sizeof(src)] = {};
	char dst4[sizeof(src)] = {};
	removeSpace1(src, dst3);
	removeSpace2(src, dst4);
	printf("remove space dst3= %s\n", dst3);
	printf("remove space dst4= %s\n", dst4);

	char dst5[sizeof(src)] = {};
	strCpy3(src, dst5);
	printf("copy dst5= %s\n", dst5);

	char dst6[sizeof(src)] = {};
	removeSpace3(src, dst6);
	printf("remove space dst6= %s\n", dst6);


}
//len of src = 26
//src = ni chou sha chou ni za di
//copy dst1 = ni chou sha chou ni za di
//copy dst2 = ni chou sha chou ni za di
//remove space dst3 = nichoushachounizadi
//remove space dst4 = nichoushachounizadi
//copy dst5 = ni chou sha chou ni za di
//remove space dst6 = nichoushachounizadi
//请按任意键继续. . .

//统计子串个数
int subCount(char* src, char* sub)
{
	int count = 0;
	char* p = strstr(src, sub);
	while (p != NULL)
	{
		count++;
		p++;
		p = strstr(p, sub);
	}
	return count;
}
//子串个数测试
void subCountTest()
{
	char src[] = "hello world,hello ladies hello gentleman";
	char sub[] = "llo";
	int ret = subCount(src, sub);
	printf("src= %s\n", src);
	printf("sub= %s\n", sub);
	printf("count= %d\n", ret);
}
//src = hello world, hello ladies hello gentleman
//sub = llo
//count = 3
//请按任意键继续. . .

//字符串逆序1
void strReverse1(char* str)
{
	char temp;
	size_t len = strlen(str);
	for (size_t i = 0; i < len - 1 - i; i++)
	{
		temp = str[i];
		str[i] = str[len - 1 - i];
		str[len - 1 - i] = temp;
	}
}
//字符串逆序2
void strReverse2(char* str)
{
	char* start = str;
	char* end = str + strlen(str) - 1;
	char temp;
	while (start < end)
	{
		temp = *start;
		*start = *end;
		*end = temp;
		start++;
		end--;
	}
}
//字符串逆序测试
void strReverseTest()
{
	char str[] = "hello";
	printf("str= %s\n", str);
	strReverse1(str);
	printf("reverse= %s\n", str);
	strReverse2(str);
	printf("recovery= %s\n", str);
}
//str = hello
//reverse = olleh
//recovery = hello
//请按任意键继续. . .

//判断回文字符串
int str_aba(char* str)
{
	char* start = str;
	char* end = str + strlen(str) - 1;
	while (start < end)
	{
		if (*start != *end)
		{
			return 0;
		}
		start++;
		end--;
	}
	return 1;
}

int strLen_noSpace(char* str)
{
	int count = 0;
	while (*str)
	{
		if (*str != ' ')
		{
			count++;
		}
		str++;
	}
	return	count;
}
//带一级指针的结构体
struct Person
{
	char* name;
	int age;
};
void allocPerson(Person*** pPersons, int len)
{
	if (pPersons == NULL)
	{
		return;
	}
	*pPersons = (Person**)malloc(sizeof(Person*) * len);
	for (size_t i = 0; i < len; i++)
	{
		(*pPersons)[i] = (Person*)malloc(sizeof(struct Person));
		(*pPersons)[i]->name = (char*)malloc(sizeof(char) * 64);
		sprintf((*pPersons)[i]->name, "person_%u", i + 1);
		(*pPersons)[i]->age = i + 20;
	}
}
void printPerson(Person** persons, int len)
{
	for (size_t i = 0; i < len; i++)
	{
		printf("name=%s,age=%d\n", persons[i]->name, persons[i]->age);
	}
}
void freePerson(Person** persons, int len)
{
	if (persons == NULL || len < 1)
	{
		return;
	}
	for (size_t i = 0; i < len; i++)
	{
		if (persons[i] != NULL)
		{
			if (persons[i]->name != NULL)
			{
				free(persons[i]->name);
				persons[i]->name = NULL;
			}
			free(persons[i]);
			persons[i] = NULL;
		}
	}
	free(persons);
	persons = NULL;
}
void personTest()
{
	Person** persons = NULL;
	int len = 4;
	allocPerson(&persons, len);
	printPerson(persons, len);
	freePerson(persons, len);
}
//name = person_1, age = 20
//name = person_2, age = 21
//name = person_3, age = 22
//name = person_4, age = 23
//请按任意键继续. . .

//带二级指针的结构体
struct Teacher
{
	char* name;
	char** students;
};
//分配堆空间
void allocTeacher(Teacher*** pTeachers, int lenTeacher, int lenStudent)
{
	if (pTeachers==NULL)
	{
		return;
	}
	(*pTeachers) =(Teacher**) malloc(sizeof(Teacher*)*lenTeacher);
	for (size_t i = 0; i < lenTeacher; i++)
	{
		(*pTeachers)[i] =(Teacher*) malloc(sizeof(Teacher));
		(*pTeachers)[i]->name = (char*)malloc(sizeof(char) * 64);
		sprintf((*pTeachers)[i]->name,"teacher_%d",i+1);
		(*pTeachers)[i]->students = (char**)malloc(sizeof(char*) * lenStudent);
		for (size_t j = 0; j < lenStudent; j++)
		{
			(*pTeachers)[i]->students[j] = (char*)malloc(sizeof(char) * 64);
			sprintf((*pTeachers)[i]->students[j], "teacher_%d_student_%d", i + 1, j + 1);
		}
	}

}
//打印数据
void printTeacher(Teacher** teachers, int lenTeacher, int lenStudent)
{
	for (size_t i = 0; i <lenTeacher; i++)
	{
		printf("%s\n",teachers[i]->name);
		for (size_t j = 0; j < lenStudent; j++)
		{
			printf("	%s\n", teachers[i]->students[j]);
		}
	}
}
//释放堆空间
void freeTeacher(Teacher** teachers, int lenTeacher, int lenStudent)
{
	if (teachers==NULL||lenTeacher<1)
	{
		return;
	}
	for (size_t i = 0; i < lenTeacher; i++)
	{
		if (teachers[i]!=NULL)
		{
			if (teachers[i]->name!=NULL)
			{
				free(teachers[i]->name);
				teachers[i]->name = NULL;
			}
			if (teachers[i]->students!=NULL&&lenStudent>0)
			{
				for (size_t j = 0; j < lenStudent; j++)
				{
					if (teachers[i]->students[j]!=NULL)
					{
						free(teachers[i]->students[j]);
						teachers[i]->students[j] = NULL;
					}
				}
				free(teachers[i]->students);
				teachers[i]->students = NULL;
			}
			free(teachers[i]);
		}
	}
	free(teachers);
	teachers = NULL;
}
//带二级指针的结构体 测试
void teacherTest()
{
	Teacher** teachers = NULL;
	int lenTeacher = 3;
	int lenStudent = 4;
	allocTeacher(&teachers,lenTeacher,lenStudent);
	printTeacher(teachers,lenTeacher,lenStudent);
	freeTeacher(teachers,lenTeacher,lenStudent);
}
//teacher_1
//teacher_1_student_1
//teacher_1_student_2
//teacher_1_student_3
//teacher_1_student_4
//teacher_2
//teacher_2_student_1
//teacher_2_student_2
//teacher_2_student_3
//teacher_2_student_4
//teacher_3
//teacher_3_student_1
//teacher_3_student_2
//teacher_3_student_3
//teacher_3_student_4
//请按任意键继续. . .

void fileTest1()
{
	char file[] = "C:\\Users\\Dell\\Desktop\\test.txt";
	FILE* pf = fopen(file,"w");
	if (pf == NULL)
	{
		perror("file open failed");
		return;
	}
	for (size_t i = 0; i < 26; i++)
	{
		putc('a'+i,pf);
	}
	fclose(pf);
}
void fileTest2()
{
	char file[] = "C:\\Users\\Dell\\Desktop\\test.txt";
	FILE* pf = fopen(file,"r");
	if (pf==NULL)
	{
		perror("file open for read failed");
		return;
	}
	char c;
	while (1)
	{
		c = getc(pf);
		if (c==EOF)
		{
			break;
		}
		printf("%c",c);
	}
	printf("\n");
	fclose(pf);

}
//abcdefghijklmnopqrstuvwxyz
void fileTest3()
{
	char file[] = "C:\\Users\\Dell\\Desktop\\test.txt";
	FILE* pf = fopen(file,"r");
	char c;
	while (!feof(pf))
	{
		c=getc(pf);
		printf("%c",c);
	}
	printf("\n");
	fclose(pf);
}
//abcdefghijklmnopqrstuvwxyz

void fgetsTest()
{
	char buf[10];
	printf("%s",fgets(buf,10,stdin));
}
//hello
//hello
//请按任意键继续. . .
//helloworld
//helloworl请按任意键继续. . .
//无论缓存长度是否足够,一定会添加\0,
//如果长度不够,则会舍弃\n和结尾的其他字符,以_MaxCount为准
void fputsTest()
{
	char str[] = "hello";
	fputs(str,stdout);
}
//hello请按任意键继续. . .
//写入的内容,不会添加\n

//将控制台的输入内容写入文件
//以:q表示退出
//可以支持多行文本写入
void consoleToFile()
{
	char buf[1024];

	char file[] = "C:\\Users\\Dell\\Desktop\\console.txt";
	FILE* fp = fopen(file,"w");
	if (fp==NULL)
	{
		perror("fopen error");
		return;
	}
	while (1)
	{
		fgets(buf, 1024, stdin);
		if (strcmp(buf,":q\n")==0)
		{
			break;
		}
		fputs(buf,fp);
	}
	fclose(fp);
}
//基于文件的计算器
void fileCalculate()
{
	char file1[] = "C:\\Users\\Dell\\Desktop\\calculate1.txt";
	char file2[] = "C:\\Users\\Dell\\Desktop\\calculate2.txt";
	FILE* pf1 = fopen(file1,"r");
	FILE* pf2 = fopen(file2,"w");
	if (pf1==NULL|| pf2 == NULL)
	{
		perror("fopen error");
		return;
	}
	char buf[1024];
	int a, b, r=0;
	char c;
	while (!feof(pf1))
	{
		fgets(buf,1024,pf1);
		sscanf(buf,"%d%c%d=\n",&a,&c,&b);
		switch (c)
		{
		case '+':
			r = a + b; break;
		case '-':
			r = a - b; break;
		case '*':
			r = a * b; break;
		case '/':
			r = a / b; break;
		default:
			break;
		}
		sprintf(buf, "%d%c%d=%d\n",a,c,b,r);
		fputs(buf,pf2);
	}
	fclose(pf1);
	fclose(pf2);

}
//输入文件
//1 + 2 =
//44 / 4 =
//8 - 3 =
//5 * 6 =
//输出文件
//1 + 2 = 3
//44 / 4 = 11
//8 - 3 = 5
//5 * 6 = 30

void cinTest()
{
	float f = 0.1f;
	cout << "请输入一个浮点数:" << endl;
	cin >> f;
	cout << "浮点数f= " << f << endl;
	bool b = false;
	cout << "请输入一个布尔值:" << endl;
	cin >> b;
	cout << "布尔值b= " << b << endl;
}
//请输入一个浮点数:
//6.5
//浮点数f = 6.5
//请输入一个布尔值:
//55
//布尔值b = 1
//请按任意键继续. . .
//请输入一个浮点数:
//4
//浮点数f = 4
//请输入一个布尔值:
//true
//布尔值b = 0
//请按任意键继续. . .





//Main
int main()
{



	cinTest();
	//fileCalculate();
	//consoleToFile();
	//fputsTest();
	//fgetsTest();
	//fileTest1();
	//fileTest2();
	//fileTest3();
	//teacherTest();
	//personTest();
	//strReverseTest();
	//subCountTest();
	//strCpyTest();
	//strTest1();
	//strCmpTest();
	//sizeTest();
	//SortTest1();
	//arrTest2();
	//arrTest1();
	//printTest1();
	//sevenGame();
	//threePigs1();
	//scoreTest3();
	//scoreTest2();
	//scoreTest1();
	//lambdaTest();
	//foreachTest();
	system("pause");
	return 0;
}
相关推荐
汉克老师8 分钟前
GESP4级考试语法知识(贪心算法(四))
开发语言·c++·算法·贪心算法·图论·1024程序员节
smj2302_7968265241 分钟前
用枚举算法解决LeetCode第3348题最小可整除数位乘积II
python·算法·leetcode
爱吃生蚝的于勒1 小时前
C语言最简单的扫雷实现(解析加原码)
c语言·开发语言·学习·计算机网络·算法·游戏程序·关卡设计
姆路1 小时前
QT中使用图表之QChart绘制动态折线图
c++·qt
秋说2 小时前
【数据结构 | C++】整型关键字的平方探测法散列
数据结构·c++·算法
weixin_478689762 小时前
【回溯法】——组合总数
数据结构·python·算法
戊子仲秋2 小时前
【LeetCode】每日一题 2024_11_14 统计好节点的数目(图/树的 DFS)
算法·leetcode·深度优先
TaoYuan__4 小时前
机器学习的常用算法
人工智能·算法·机器学习
槿花Hibiscus4 小时前
C++基础:Pimpl设计模式的实现
c++·设计模式
用户40547878374824 小时前
深度学习笔记 - 使用YOLOv5中的c3模块进行天气识别
算法