1.编程求两个复数的和
结构体
函数
返回值是结构体
参数 两个结构体
#include <stdio.h>
#include <stdlib.h>
struct plural
{
double real;
double empty;
};
struct plural add(struct plural s1,struct plural s2)
{
struct plural s;
s.real=s1.real+s2.real;
s.empty=s1.empty+s2.empty;
return s;
}
int main1()
{
struct plural a1={1.2,2.3};
struct plural a2={5.6,3.1};
struct plural a=add(a1,a2);
printf("%f,%f\n",a.real,a.empty);
return 0;
}
2.已知一维整型数组a中的数已按由小到大的顺序排列,
编写程序,删去一维数组中所有相同的数,使之只剩一个。
unique()
void unique(int *str,int len)
{
int *p1=str;
int *p2=str;
int i,count=0;
for (i=0;i<len;i++)
{
if(*p1==*(p1+1))
{
p1++;
}
else
{
*p2=*p1;
p2++;
p1++;
count++;
}
}
for(i=0;i<count;i++)
{
printf("%d ",*(str+i));
}
printf("\n");
}
int main2()
{
int a[]={1,2,5,5,6,7,7,7,8,8,48,89};
int len=sizeof(a)/sizeof(int);
unique(a,len);
return 0;
}
3.统计一个英文句子中含有英文单词的个数,单词之间用空格隔开。
void count(char *pstr)
{
int i = 1;
while(*pstr != '\0')
{
if(*pstr == ' ')
{
i++;
}
pstr++;
}
printf("%d\n",i);
}
int main()
{
char b[]="my name is wang lu lu";
count(b);
return 0;
}