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__
// 類型聲明
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語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單彈跳小球
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單彈跳小球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
C語(yǔ)言之?dāng)?shù)組名與數(shù)組起始地址的關(guān)系解析
這篇文章主要介紹了C語(yǔ)言之?dāng)?shù)組名與數(shù)組起始地址的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
總結(jié)C語(yǔ)言中const關(guān)鍵字的使用
一起雖然學(xué)過(guò)c語(yǔ)言,但是并沒(méi)有寫(xiě)過(guò)太多的代碼,最近想要拾起c語(yǔ)言,就寫(xiě)了一些代碼,但是對(duì)const關(guān)鍵字比較陌生,這里總結(jié)一下,方法自己和大家有需要的時(shí)候參考借鑒,下面跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
C++實(shí)現(xiàn)簡(jiǎn)單酒店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
關(guān)于CLion配置visual?studio(msvc)和JOM多核編譯的問(wèn)題
這篇文章主要介紹了CLion配置visual?studio(msvc)和JOM多核編譯,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
詳解利用C語(yǔ)言如何實(shí)現(xiàn)簡(jiǎn)單的內(nèi)存池
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言如何實(shí)現(xiàn)簡(jiǎn)單的內(nèi)存池的相關(guān)資料,設(shè)計(jì)內(nèi)存池的目標(biāo)是為了保證服務(wù)器長(zhǎng)時(shí)間高效的運(yùn)行,通過(guò)對(duì)申請(qǐng)空間小而申請(qǐng)頻繁的對(duì)象進(jìn)行有效管理,減少內(nèi)存碎片的產(chǎn)生,合理分配管理用戶內(nèi)存,需要的朋友可以參考下2021-08-08
Qt圖片繪圖類之QPixmap/QImage/QPicture詳解
這篇文章主要為大家詳細(xì)介紹了Qt圖片繪圖類中QPixmap、QImage和QPicture的使用方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03

