網站首頁 編程語言 正文
? ? 一、
#include<stdio.h> typedef struct complex { int real; //實部 int imag; //虛部 }complex; /* 功能:復數加法 參數:兩個復數 返回值:兩個復數的和 */ complex complexadd(complex x,complex y) { complex sum; sum.real = x.real + y.real; sum.imag = x.imag + y.imag; return sum; } /* 功能:復數減法 參數:兩個復數 返回值:兩個復數的差 */ complex complexsub(complex x,complex y) { complex sum; sum.real = x.real - y.real; sum.imag = x.imag - y.imag; return sum; } /* 功能:打印復數 參數:一個復數 */ 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" /* 功能:復數加法 參數:兩個復數 返回值:兩個復數的和 */ complex complexadd(complex x,complex y) { complex sum; sum.real = x.real + y.real; sum.imag = x.imag + y.imag; return sum; } /* 功能:復數減法 參數:兩個復數 返回值:兩個復數的差 */ complex complexsub(complex x,complex y) { complex sum; sum.real = x.real - y.real; sum.imag = x.imag - y.imag; return sum; } /* 功能:打印復數 參數:一個復數 */ 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}; // 結構體初始化 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.h
#ifndef __COMPLEX_H__ #define __COMPLEX_H__ // 類型聲明 typedef struct complex { int real; //實部 int imag; //虛部 }complex; /* 功能:復數加法 參數:兩個復數 返回值:兩個復數的和 */ complex complexadd(complex x,complex y); /* 功能:復數減法 參數:兩個復數 返回值:兩個復數的差 */ complex complexsub(complex x,complex y); /* 功能:打印復數 參數:一個復數 */ void printfcomplex(complex x); #endif
原文鏈接:https://blog.csdn.net/zillll/article/details/122867841
相關推薦
- 2022-12-12 Python字典使用技巧詳解_python
- 2022-06-06 web前端實現水平垂直居中、position、relative、absolute、transform
- 2023-06-18 Docker之實現掛載的三種方式匯總_docker
- 2022-04-10 Windows Terminal添加到右鍵菜單中
- 2022-08-19 Go語言fsnotify接口實現監測文件修改_Golang
- 2022-04-20 Python統計節假日剩余天數的腳本_python
- 2022-06-24 Python集合之set和frozenset的使用詳解_python
- 2022-10-06 QT獲取顯示當前時間和日期的方法(用QTime,QDate和QDateTime)_C 語言
- 最近更新
-
- 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同步修改后的遠程分支