網(wǎng)站首頁 編程語言 正文
? ? 一、
#include<stdio.h> typedef struct complex { int real; //實部 int imag; //虛部 }complex; /* 功能:復(fù)數(shù)加法 參數(shù):兩個復(fù)數(shù) 返回值:兩個復(fù)數(shù)的和 */ complex complexadd(complex x,complex y) { complex sum; sum.real = x.real + y.real; sum.imag = x.imag + y.imag; return sum; } /* 功能:復(fù)數(shù)減法 參數(shù):兩個復(fù)數(shù) 返回值:兩個復(fù)數(shù)的差 */ complex complexsub(complex x,complex y) { complex sum; sum.real = x.real - y.real; sum.imag = x.imag - y.imag; return sum; } /* 功能:打印復(fù)數(shù) 參數(shù):一個復(fù)數(shù) */ void printfcomplex(complex x) { printf("%d",x.real); if(x.imag > 0) { printf("+"); } printf("%d\n",x.imag); } int main() { complex f1 = {3,-5}; complex f2 = {-5,8}; printfcomplex(f1); printfcomplex(f2); complex f3 = complexadd(f1,f2); printfcomplex(complexadd(f1,f2)); printfcomplex(f3); printfcomplex(complexsub(f1,f2)); return 0; }
二、分文件
complex.c
#include<stdio.h> #include"complex.h" /* 功能:復(fù)數(shù)加法 參數(shù):兩個復(fù)數(shù) 返回值:兩個復(fù)數(shù)的和 */ complex complexadd(complex x,complex y) { complex sum; sum.real = x.real + y.real; sum.imag = x.imag + y.imag; return sum; } /* 功能:復(fù)數(shù)減法 參數(shù):兩個復(fù)數(shù) 返回值:兩個復(fù)數(shù)的差 */ complex complexsub(complex x,complex y) { complex sum; sum.real = x.real - y.real; sum.imag = x.imag - y.imag; return sum; } /* 功能:打印復(fù)數(shù) 參數(shù):一個復(fù)數(shù) */ void printfcomplex(complex x) { printf("%d",x.real); if(x.imag > 0) { printf("+"); } printf("%di\n",x.imag); }
main.c
#include<stdio.h> #include"complex.h" // complex 頭文件 int main() { complex f1 = {3,-5}; // 結(jié)構(gòu)體初始化 complex f2 = {-5,8}; printfcomplex(f1); // 打印復(fù)數(shù) printfcomplex(f2); complex f3 = complexadd(f1,f2); printfcomplex(complexadd(f1,f2)); printfcomplex(f3); printfcomplex(complexsub(f1,f2)); return 0; }
complex.h
#ifndef __COMPLEX_H__ #define __COMPLEX_H__ // 類型聲明 typedef struct complex { int real; //實部 int imag; //虛部 }complex; /* 功能:復(fù)數(shù)加法 參數(shù):兩個復(fù)數(shù) 返回值:兩個復(fù)數(shù)的和 */ complex complexadd(complex x,complex y); /* 功能:復(fù)數(shù)減法 參數(shù):兩個復(fù)數(shù) 返回值:兩個復(fù)數(shù)的差 */ complex complexsub(complex x,complex y); /* 功能:打印復(fù)數(shù) 參數(shù):一個復(fù)數(shù) */ void printfcomplex(complex x); #endif
原文鏈接:https://blog.csdn.net/zillll/article/details/122867841
相關(guān)推薦
- 2023-10-16 微信小程序瀏覽docx,pdf等文件在線預(yù)覽使用wx.openDocument
- 2023-06-18 c#關(guān)于非托管內(nèi)存的釋放問題及解讀_C#教程
- 2023-01-10 Go?Slice進行參數(shù)傳遞如何實現(xiàn)詳解_Golang
- 2023-05-30 基于CUDA?out?of?memory的一種神奇解決方式_python
- 2023-06-20 React?DOM-diff?節(jié)點源碼解析_React
- 2022-07-15 Python中列表索引?A[?:?2?]與A[?:?,?2]的區(qū)別說明_python
- 2022-04-09 SpringMVC 基礎(chǔ)配置文件(簡潔版本)
- 2023-03-26 CSS填充和寬高詳解_基礎(chǔ)教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支