例1.cpp

来自「c语言书籍&课件&示例代码」· C++ 代码 · 共 23 行

CPP
23
字号
#include<stdio.h>
struct complex
{  float real,imag;
};
void main()
{  struct complex add(struct complex,struct complex);
   void output(struct complex*);
   struct complex a,b,c;
   scanf("%f,%f",&a.real,&a.imag);//逐个输入结构体成员
   scanf("%f,%f",&b.real,&b.imag);
   c=add(a,b);//结构体变量作为函数参数和赋值运算
   output(&a);output(&b);output(&c);//结构体变量的地址作为实参
}
struct complex add(struct complex x,struct complex y)
{  struct complex z;
   z.real=x.real+y.real;//结构体成员进行加运算和赋值运算
   z.imag=x.real+y.imag;
   return z;//结构体变量作为返回值
}
void output(struct complex*p)//结构体指针作为形参
{  printf("%0.2f+%0.2fi\n",p->real,p->imag);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?