C語(yǔ)言復(fù)數(shù)的加減及輸出結(jié)構(gòu)體
一、
#include<stdio.h> typedef struct complex { int real; //實(shí)部 int imag; //虛部 }complex; /* 功能:復(fù)數(shù)加法 參數(shù):兩個(gè)復(fù)數(shù) 返回值:兩個(gè)復(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ù):兩個(gè)復(fù)數(shù) 返回值:兩個(gè)復(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ù):一個(gè)復(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ù):兩個(gè)復(fù)數(shù) 返回值:兩個(gè)復(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ù):兩個(gè)復(fù)數(shù) 返回值:兩個(gè)復(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ù):一個(gè)復(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__ // 類(lèi)型聲明 typedef struct complex { int real; //實(shí)部 int imag; //虛部 }complex; /* 功能:復(fù)數(shù)加法 參數(shù):兩個(gè)復(fù)數(shù) 返回值:兩個(gè)復(fù)數(shù)的和 */ complex complexadd(complex x,complex y); /* 功能:復(fù)數(shù)減法 參數(shù):兩個(gè)復(fù)數(shù) 返回值:兩個(gè)復(fù)數(shù)的差 */ complex complexsub(complex x,complex y); /* 功能:打印復(fù)數(shù) 參數(shù):一個(gè)復(fù)數(shù) */ void printfcomplex(complex x); #endif
到此這篇關(guān)于C語(yǔ)言復(fù)數(shù)的加減及輸出結(jié)構(gòu)體的文章就介紹到這了,更多相關(guān)C語(yǔ)言復(fù)數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解C++如何實(shí)現(xiàn)在Word文檔中創(chuàng)建列表
這篇文章主要為大家詳細(xì)介紹了介紹如何使用C++在Word文檔中創(chuàng)建編號(hào)列表、項(xiàng)目符號(hào)列表和多級(jí)列表,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05用C/C++代碼檢測(cè)ip能否ping通(配合awk和system可以做到批量檢測(cè))
今天小編就為大家分享一篇關(guān)于用C/C++代碼檢測(cè)ip能否ping通(配合awk和system可以做到批量檢測(cè)),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04如何在C語(yǔ)言中判斷socket是否已經(jīng)斷開(kāi)
如果不主動(dòng)關(guān)閉socket的話(huà),系統(tǒng)不會(huì)自動(dòng)關(guān)閉的,除非當(dāng)前進(jìn)程掛掉了,操作系統(tǒng)把占用的socket回收了才會(huì)關(guān)閉。小編今天跟大家簡(jiǎn)單介紹下如何在C語(yǔ)言中判斷socket是否已經(jīng)斷開(kāi)2019-05-05詳細(xì)分析C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象
這篇文章主要介紹了C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06