網站首頁 編程語言 正文
一個函數調用其他.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;
}
在主文件cpp中調用其他文件函數的方法
直接用
和我們的數據成員必須加extern不同的是,你只需把待調用函數的聲明寫在其頭文件中,然后在主函數中直接用就可以
//test.h
#ifndef TEST_H //注意,這里千萬不要寫成TEST.H,必須用下劃線,用點不行
#define TEST_H
void print();
#endif
//test.cpp
#include<iostream>
#include"test.h"
using namespace std;
void print() {
cout << "test函數被調用" << endl;
}
//main.cpp
#include<iostream>
#include"test.h"
using namespace std;
int main() {
print();
}
extern方法
使用extern的時候你甚至不需要在main.cpp文件中加上引用文件的聲明,直接就可以用。
#include<iostream>
using namespace std;
extern void print();
int main() {
print();
}
但是這樣寫其實作用不大,在一些大的工程中反而不如以好用。
總結
原文鏈接:https://kylee.blog.csdn.net/article/details/82856633
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-11-17 python中?OpenCV和Pillow處理圖像操作及時間對比_python
- 2022-09-18 Rust實現grep命令行工具的方法_相關技巧
- 2022-07-25 python數據清洗中的時間格式化實現_python
- 2022-04-10 MalformedByteSequenceException: 1字節的 UTF-8 序列的字節 1
- 2023-10-15 MVCC和BufferPool緩存機制
- 2022-07-16 Date 轉為 LocalDate
- 2022-07-14 Jquery+Ajax實現跨域訪問_jquery
- 2022-11-09 Android性能優化之plt?hook與native線程監控詳解_Android
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支