c语言结构体demo

#include"stdafx.h"
#include 
#include 
#include 
// #include 

#define LEN 20
#define FUNDLEN 50
#define SIZE 81

int loop(int);
void trysat(void);
// double sum(const struct funds*);
double sum(const struct funds obj);

struct namect {
	char * fname;
	char * lname;
	int letters;
};

struct names{
	char first[LEN];
	char last[LEN];
};

struct guy {
	struct names handle;
	char favfood[LEN];
	char job[LEN];
	float income;
};

struct funds {
	char bank[FUNDLEN];
	double bankfund;
	char save[FUNDLEN];
	double savefund;
};

void getinfo(struct namect *);
void makeinfo(struct namect *);
void showinfo(const struct namect *);
void cleanup(struct namect *);
char *s_gets(char*st, int n);

int main(int, char**)
{
	/*
	int x = 30;
	printf("x in outer block: %d at %p\n", x, &x);
	{
		int x = 77;
		printf("x in inner block: %d at %p\n", x, &x);
	}
	printf("x in inner block: %d at %p\n", x, &x);

	while (x++ < 33) {
		int x = 100;
		x++;
		printf("x in inner block: %d at %p\n", x, &x);
	}
	printf("x in inner block: %d at %p\n", x, &x);

	register int a = 10;
	printf("register a = %d\n", a);

	int count;
	for (count = 1; count <= 3; count++) {
		printf("iteration %d:\n", count);
		trysat();
	}

	struct guy fellow[2] = {
		{
			{"Even", "Villard"},
			"grilled salmon",
			"personality coach",
			68112.00
		},
		{
			{"Renody", "Villard"},
			"apple",
			"personality editor",
			78977.00
		}
	};

	struct guy*him;
	printf("address #1: %p #2: %p\n", &fellow[0], &fellow[1]);
	him = &fellow[0];

	printf("pointer #1: %p #2: %p\n", him, him + 1);
	printf("him->income is $%.2f: (*him).income is $%.2f\n", him->income, (*him).income);
	him++;
	printf("him->favfood is %s: (*him).job is %s\n", him->favfood, (*him).job);

	struct funds stan = {
		"Gary Bank",
		4032.27,
		"Lucky's saving and loan",
		8543.94
	};
	printf("stan has a total of $%.2f.\n", sum(stan));
	*/
	
	struct namect person;
	getinfo(&person);
	makeinfo(&person);
	showinfo(&person);
	cleanup(&person);

	return 0;
}

void getinfo(struct namect * pst) {
	char temp[SIZE];
	printf("please enter your first name:\n");
	s_gets(temp, SIZE);
	pst->fname = (char*)malloc(strlen(temp) + 1);
	strcpy(pst->fname, temp);
	printf("please enter your last name:\n");
	s_gets(temp, SIZE);
	pst->lname = (char*)malloc(strlen(temp) + 1);
	strcpy(pst->lname, temp);
}

void makeinfo(struct namect * pst) {
	pst->letters = strlen(pst->fname) + strlen(pst->lname);
}

void showinfo(const struct namect * pst) {
	printf("%s %s, your name contains %d letters.\n", 
		pst->fname, pst->lname, pst->letters);
}

void cleanup(struct namect * pst) {
	free(pst->fname);
	free(pst->lname);
}

char *s_gets(char*st, int n) {
	char *ret_val;
	char *find;

	ret_val = fgets(st, n, stdin);
	if (ret_val) {
		find = strchr(st, '\n');
		if (find)
			*find = '\0';
		else
			while (getchar() != '\n')
				continue;
	}
	return ret_val;
}

double sum(const struct funds money) {
	return money.bankfund + money.savefund;
}

void trysat(void){
	int fade = 1;
	static int stay = 1;
	static int test;
	printf("fade = %d and stay = %d test = %d\n", fade++, stay++, test);
}

int loop(int n){
	int m = 0;
	scanf("%d", &n);
	{
		int i;
		for (i = m; i < n; i++)
			puts(" i is local to a sub-block\n");
	}
	
	return m;
}

void copy_arr(double target[], const double source[], int n) {
	int i;
	for (i = 0; i < n; i++) {
		target[i] = source[i];
		// printf("target[%d] = %.1f ", i, target[i]);
	}
}
void copy_ptr(double*target, const double*source, int n) {
	int i;
	for (i = 0; i < n; i++)
		*target++ = *source++;
}
void copy_ptrs(double target[], const double source[], const double *p) {
	double *tptr = target;
	const double *sptr = source;
	for (; sptr <= p; sptr++, tptr++)
		*tptr = *sptr;
}
void show_ele(const double arr[], int n) {
	for (int i = 0; i < n; i++) {
		printf("arr[%d] = %.1f\t", i, arr[i]);
	}
	printf("\n");
}

void to_binary(unsigned long n) {
	int r;
	r = n % 2;
	if (n > 0)
		to_binary(n / 2);
	putchar(r == 0 ? '0' : '1');
}

int StrToInt(char*string) {
	int number = 0;
	if (string == NULL)
		return -1;
	while (*string != 0) {
		number = number * 10 + *string - '0';
		++string;
	}
	return number;
}

int sum(const int*arr) {
	int sum = 0;
	if (arr == NULL) {
		printf("Your arrray is NULL!\n");
		return -1;
	}
	for (int i = 0; i < 5; i++)
		sum += *arr++;
	return sum;
}

你可能感兴趣的:(C++代码练习)