C++ learning-function overloading and variable scope

Requirements:
1. Use overloaded functions to calculate the area of ​​circles, rectangles, trapezoids, and triangles. If no parameters are specified for circle area calculation, the area of ​​the unit circle will be calculated;
2. Obtain the program input, file path and file path from a text file The name is: D:\Test.txt
3. The input format is as follows:
single character [] parameter 1[] parameter 2[]...<carriage return and line feed>
Description:
(1) Separate by spaces, and end a group of inputs with carriage return and line feed , And then read the next line of input until the end of the file;
(2) A single character represents the shape type: C (circle) Parameters: radius
R (rectangular) Parameters: length, width
L (trapezoid) Parameters: top bottom, bottom bottom, High
T (triangular) parameters: bottom, high
(3) If the number of input parameters is greater than the required number of parameters, the redundant input will be ignored; if the number of input parameters is less than the required number of parameters, the previous input value will be called (if there is an input before ) Or 0 (if there is no input before);
(4) The calculation of the circle area, given the default parameters, output the area of ​​the unit circle;
4. The output result is displayed in the console, and the format is as follows:
XX-shaped area = XXXXXX
5. Code Neat and standard
6. Attached operation result diagram, and explain and analyze the test case, test purpose, and operation result.
C++ implementation code:

#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
using namespace std;

double Area(double r = 1);//计算圆形的面积
double Area(double length, double width);//计算矩形的面积
double Area(double uSide, double lSide, double height);//计算梯形的面积
double Area(double lSide, double height, int i);//计算三角形的面积,为与计算矩形的
                                              //面积区分开来,增加一个输入参数int i;

int main()
{
    
    
	char ch[256];
	int i;
	float result;          //最终结果
	float backup[20][10];  //用于备份记录每一轮输入的数据
	float Num[10];         //存放每一轮float类型数据
	int k = 0;             //记录轮数
	string tempName;       
	ifstream ifile;
	ifile.open("D:\\我学你妹\\大三上\\面向对象程序设计\\实验\\Test.txt");
	do 
	{
    
    
		ifile.getline(ch, 255);  //按行读取字符串

		//将得到的字符串按空格切割放入数组
		char* p;
		vector<string> num;   //字符串数组
		const char* delim = " ";
		p = strtok(ch, delim);
		while (p)
		{
    
    
			i = 0;
			string s = p;
			num.push_back(s);
			p = strtok(NULL, delim);
			i++;
		}
		//将数组中的字符转换为float类型
		for (int i = 0; i < num.size(); ++i)
		{
    
    
			Num[i] = atof(num[i].c_str());
			//std::cout << "b=" << Num[i] << std::endl;
			backup[k][i] = Num[i];     //备份
		}
		k++;
		if (ch[0] == 'C')
		{
    
    
			tempName = "圆形";
			if (Num[1] != NULL)
			{
    
    
				result = Area(Num[1]);
			}
			else 
			{
    
    
				result = Area();//利用默认参数求单位圆面积
			}
		}
		else if (ch[0] == 'R')
		{
    
    
			tempName = "矩形";
			if (Num[2] != NULL)          
			{
    
    
				result = Area(Num[1], Num[2]);
			}
			else               
			{
    
    
				result = Area(Num[1], backup[k-2][1]);
			}
		}
		else if (ch[0] == 'L')
		{
    
    
			tempName = "梯形";
			if (Num[3] != NULL)     //从后往前判断是否有数据输入,如果没有则从备份数据中提取
			{
    
    
				result = Area(Num[1], Num[2], Num[3]);
			}
			else if (Num[2] != NULL )
			{
    
    
				result = Area(Num[1], Num[2], backup[k - 2][1]);
			}
			else if (Num[1] != NULL)
			{
    
    
				if (backup[k - 2][2] != NULL)
				{
    
    
					result = Area(Num[1], backup[k - 2][1], backup[k - 2][2]);
				}
				else 
				{
    
    
					result = Area(Num[1], backup[k - 2][1], backup[k - 3][1]);
				}
			}
		}
		else if (ch[0] == 'T')
		{
    
    
			tempName = "三角形";
			if (Num[2] != NULL)
			{
    
    
				result = Area(Num[1], Num[2],1);
			}
			else
			{
    
    
				result = Area(Num[1], backup[k - 2][1],1);
			}
		}
		cout << tempName << "面积 = " << result << endl;
	} while (ifile.eof() == 0);
	ifile.close();
	return 0;
}

double Area(double r) 
{
    
    
	return (3.14) * r * r;
}
double Area(double length, double width) {
    
    
	return length * width;
}

double Area(double uSide, double lSide, double height) {
    
    
	return (uSide + lSide)*height / 2;
}

double Area(double lSide, double height, int i) {
    
    
	return lSide * height / 2;
	}

Input data:
Insert picture description here
program running result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/113030568