c语言程序设计实例

c语言程序设计实例,第1张

#include<stdio.h>

#include<stdlib.h>

#define MAX 100

struct addr{

char name[30]

char street[40]

char city[20]

char state[3]

unsigned long int zip

}add_list[MAX]

void init_list(void),enter(void)

void delete(void),list(void)

void load(void),save(void)

int menu_select(void),find_free(void)

int main(void)

{

char choice

init_list()/*initialize the structure array*/

for(){

choice=menu_select()

switch(choice){

case 1:enter()

break

case 2:delete()

break

case 3:list()

break

case 4:save()

break

case 5:load()

break

case 6:exit(0)

}

}

return 0

}

/*Initialize the list*/

void init_list(void)

{

register int t

for(t=0t<MAX++t)

add_list[t].name[0]='0'

}

/*Get a menu selection*/

int menu_select(void)

{

char s[80]

int c

printf("1.Enter a name\n")

printf("2.Delete a name\n")

printf("3.List the file\n")

printf("4.Save the file\n")

printf("5.Load the file\n")

printf("6.Quit\n")

do{

printf("\nEnter your choice:")

gets(s)

c=atoi(s)

}while(c<0||c>6)

return c

}

/*Input addresses into the list*/

void enter(void)

{

int slot

char s[80]

slot=find_free()

if(slot==-1){

printf("\nList Full")

return

}

printf("Enter name:")

gets(add_list[slot].name)

printf("Enter street:")

gets(add_list[slot].street)

printf("Enter city:")

gets(add_list[slot].city)

printf("Enter state:")

gets(add_list[slot].state)

printf("Enter zip:")

gets(s)

add_list[slot].zip=strtoul(s,'\0',10)

}

/*Find an unused structure*/

int find_free(void)

{

register int t

for(t=0add_list[t].name[0]&&t<MAX++t)

if(t==MAX)

return -1/*no slots free*/

return t

}

/*Delete an address*/

void delete(void)

{

register int slot

char s[80]

printf("enter record # :")

gets(s)

slot=atoi(s)

if(slot>=0&&slot<MAX)

add_list[slot].name[0]='\0'

}

/*Display the list on the screen*/

void list(void)

{

register int t

for(t=0t<MAX++t){

if(add_list[t].name[0]){

printf("%s\n",add_list[t].name)

printf("%s\n",add_list[t].street)

printf("%s\n",add_list[t].city)

printf("%s\n",add_list[t].state)

printf("%s\n",add_list[t].zip)

}

}

printf("\n\n")

}

/*Save the list*/

void save(void)

{

FILE *fp

register int i

if((fp=fopen("maillist","wb"))==NULL)

printf("Cannot open file\n")

return

for(i=0i<MAXi++)

if(*add_list[i].name)

if(fwrite(&add_list[i],sizeof(struct addr),1,fp)!=1)

printf("File write error\n")

fclose(fp)

}

/*Load the file*/

void load(void)

{

FILE *fp

register int i

if((fp=fopen("maillist","rb"))==NULL){

printf("Cannot open file\n")

return

}

init_list()

for(i=0i<MAXi++)

if(fread(&add_list[i],sizeof(struct addr),1,fp)!=1){

if(feof(fp))break

printf("File read error\n")

}

fclose(fp)

}

题目1

#include "stdio.h"

void main(void)

{

int n=5,m=10,i=1

long sum=1

for(i<=ni++)

{

sum*=i

}

printf("\n5!=%d",sum)

for(i=1i<=10i++)

{

sum*=i

}

printf("\n10!=%d",sum)

}

题目2

#include "stdio.h"

#include "string.h"

struct Student

{

char s_Name[25]

long n_Code

int n_English

int n_Math

int n_Computer

}student_1,student_2

void main(void)

{

printf("\nStudent1:\nName:")

scanf("%s",&student_1.s_Name)

printf("StudentNum:")

scanf("%d",&student_1.n_Code)

printf("English Score:")

scanf("%d",&student_1.n_English)

printf("Math Score:")

scanf("%d",&student_1.n_Math)

printf("Computer Score:")

scanf("%d",&student_1.n_Computer)

printf("\nStudent2:\nName:")

scanf("%s",&student_2.s_Name)

printf("StudentNum:")

scanf("%d",&student_2.n_Code)

printf("English Score:")

scanf("%d",&student_2.n_English)

printf("Math Score:")

scanf("%d",&student_2.n_Math)

printf("Computer Score:")

scanf("%d",&student_2.n_Computer)

printf("\nStudent1:\nName:%s\nStudent Number:%d\nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\n",student_1.s_Name,student_1.n_Code,student_1.n_English,student_1.n_Math,student_1.n_Computer)

printf("\nStudent2:\nName:%s\nStudent Number:%d\nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\n",student_2.s_Name,student_2.n_Code,student_2.n_English,student_2.n_Math,student_2.n_Computer)

}

#include <stdio.h>/*头函数*/

int main(void) /*主函数*/

{

char c/*指定c成为字符变量*/

printf("请输入0到9的任意一个数字\n")/*输出请输入0到9的任意一个数字*/

c=getchar()/*输入一个字符*/

while(c>=48&&c<=57) /*c的取值范围*/

{

getchar()

switch(c) /*根据c的值转换*/

{

case '0': printf("你喜欢奋斗吗?\n")/*假如c=0,输出你喜欢奋斗吗?*/

break/*中断跳出,执行c=getchar()*/

case '1': printf("你喜欢当模特吗?\n")/*假如c=1,输出你喜欢当模特吗?*/

break/*中断跳出,执行c=getchar()*/

case '2': printf("你喜欢和谐吗?\n")/*假如c=2,输出你喜欢和谐吗?*/

break/*中断跳出,执行c=getchar()*/

case '3': printf("你喜欢道家吗?\n")/*假如c=3,输出你喜欢道家吗?*/

break/*中断跳出,执行c=getchar()*/

case '4': printf("你是广东人吗?\n")/*假如c=4,输出你是广东人吗?*/

break/*中断跳出,执行c=getchar()*/

case '5': printf("你喜欢武术吗?\n")/*假如c=5,输出你喜欢武术吗?*/

break/*中断跳出,执行c=getchar()*/

case '6': printf("祝你一帆风顺!\n")/*假如c=6,输出祝你一帆风顺!*/

break/*中断跳出,执行c=getchar()*/

case '7': printf("观察等待好机会!\n")/*假如c=7,输出观察等待好机会!*/

break/*中断跳出,执行c=getchar()*/

case '8': printf("你喜欢交际吗?\n")/*假如c=8,输出你喜欢交际吗?*/

break/*中断跳出,执行c=getchar()*/

case '9': printf("追求完美吧!\n")/*假如c=9,输出追求完美吧!*/

break/*中断跳出,执行c=getchar()*/

}

c=getchar()/*再输入一个字符*/

}

return 0/*返回值*/

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/10990238.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-12
下一篇 2023-05-12

发表评论

登录后才能评论

评论列表(0条)

保存