C++ 一个函数调用其他.cpp文件中的函数

使用VC或VS创建C++项目的时候,会自动产生许多文件夹,其中有一个文件夹->源文件:

在该文件下可以自定义许多.cpp文件,但是需要注意的是这里面的各个文件只能有一个文件中含有main()函数,

而且各个文件中不能使用相同的函数名进行定义;

那么要那么多文件放在项目中有什么用呢?

当然这里C++是提供一个文件调用其他文件中函数的功能的,

这就可以让我们自定义一个只包含main()函数的文件,通过在该函数中调用其他文件中的函数就可以将各个文件链接起来,

而且更重要的一点就是,通过调用其他,cpp文件中的函数的时候,如果调用的某函数又调用了它自己文件中的一个函数,

那么只用调用“父级函数”就可以实现间接调用~~~

【 示例 】



  • 首先是资源管理窗口:

  • 功能主函数.cpp
// C++上机作业.cpp : 定义控制台应用程序的入口点。
//'0-9': 48-57

#include "stdafx.h"
using namespace std;
extern void gotoxy(short x, short y);
extern void sort_by_name();
extern int Strtoint();

int main()
{
	system("title 功能主函数");
	gotoxy(23, 2); cout << "功能列表";
	gotoxy(15, 3); cout << "1:字符串转换为数值类型";
	gotoxy(15, 4); cout << "2:对中文字符进行排序";

	gotoxy(0, 10);
	int choice = 0;
	cout << "请输入您要执行的功能:";
	cin >> choice;
	getchar();    //吸收回车
	switch (choice)
	{
		case 1:
			Strtoint();
			break;
		case 2:
			sort_by_name();
			break;
		default:
			cout << "选择失败,感谢使用,再见!" << endl << endl;
	}	
    return 0;
}

 

  • stdafx.h(stdandard application framework extensions
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <Windows.h>
#include <string>    //注意这里的string与cstring中的使用差别,在定义与使用cout输出string类型字符串的时候,最好使用string库,否则可能会出现乱码以及错误等一系列错误



// TODO:  在此处引用程序需要的其他头文件
  • gotoxy().cpp
#include "stdafx.h"
using namespace std;

void gotoxy(short x, short y)
{
	COORD pos = { x,y };
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
}
  • 对中文字符的排序.cpp
//对中文字符串进行排序时,默认是按照第一个字符的第一个拼音的顺序进行排序

#include "stdafx.h"
using namespace std;

void sort_by_name()
{
	string s[4] = { "一号","二号","三号","四号" }, t;
	for (int i = 0; i<4; i++)
	{
		for (int j = i; j<4; j++)
		{
			if (s[i]>s[j])
			{
				t = s[i];
				s[i] = s[j];
				s[j] = t;
			}
		}
	}
	for (int i = 0; i < 4; i++)
	{
		cout << s[i] << endl;
	}
	cout << "功能运行结束!" << endl << endl;
}
  • 字符串转换为数值型.cpp
#include "stdafx.h"
using namespace std;

int Strtoint_0(const char str[])  //字符串数字转换为整形
{
	int i = 0, j = 0;
	long long number1 = 0;    //定义一个长整形变量,用来存储转换后得到的值
	int number[50] = { 0 };    //定义一个数组,用来存储转换后得到的值
	int symbol = 1;    //符号常量,0为负,1为正(默认为正)
	while (str[i] != '\0')	  //测试输出判断是否正确
	{
		while (str[i] == ' ')
		{
			i++;
		}
		if ((str[i] == '+' || str[i] == '-'))
		{
			i++;
			if (str[i] == '-')
			{
				symbol = 0;
			}
		}
		else if (str[i]<'9' && str[i]>'0')
		{
			number[j++] = str[i] - 48;    //存储数据,j++
										  //			cout << number[j - 1] << endl;
			i++;
		}
		if (str[i]>'9' || str[i]<'0')    //停止输出规则判断语句
		{
			break;
		}
	}
	cout << "数的位数为:" << j << endl;    //j到这里就已经得到数组的最大索引值+1了
	int x = 1;
	for (int k = j - 1; k >= 0; k--, x = x * 10)
	{
		number1 += number[k] * x;
	}
	if (symbol == 0)
	{
		number1 = number1*(-1);
	}
	cout << "转换后的数为:" << number1 << endl << endl;
	return 1;
}

int Strtoint()    //调用字符转换函数,确保变量不在主函数中定义
{
	char arr[50] = { 0 };
	int i = 0;
	char c;
	cout << "Please input the string :" << endl;
	while ((c = getchar()) != '\n')
	{
		arr[i++] = c;	//注意这里下面的i就开始++了
	}
	/*
	while ((c = cin.get()) != '\n')    //另一种控制输入的方法
	{
	arr[i++] = c;
	cout << arr[i - 1];
	}
	*/
	Strtoint_0(arr);
	return 0;
}

 

 

 


             最快的脚步不是跨越,而是继续,最慢的步伐不是小步,而是徘徊。


 

  • 30
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值