欢迎光临散文网 会员登陆 & 注册

尚硅谷C语言零基础快速入门教程

2023-08-02 17:16 作者:writer  | 我要投稿

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


//1.解决一个问题,目前没有数据库,应该使用数结构体赖保存数据,才能达到效果

//2.

typedef struct Income{

double money;

char* state;

char* king;

double sum;

struct Income *next;

}Income;


Income* init(){ //初始化

Income* income = (Income*)malloc(sizeof(Income));

income -> money = 0.0;

income -> state = "";

income -> king = "";

income -> sum = 0.0;

income -> next = NULL;

return income;

}



void Income_init(Income* income,double data,char string[]){ //收入

//创建一个登录收入结构体需要多大的空间

double k = 0.0;

Income* node = (Income*)malloc(sizeof(Income));

node -> money = data;  //赋值

node -> state = string; //赋值(地址)

node -> king = "收入";

k = income -> sum;

k += data;

node -> sum = k;

income -> sum = k;

node -> next = income -> next;

income -> next = node;

}


void expend_init(Income* income,double data,char string[]){ //支出

//创建一个登录收入结构体需要多大的空间

double k = 0.0;

Income* node = (Income*)malloc(sizeof(Income));

node -> money = data;  //赋值

node -> state = string; //赋值

node -> king = "支出";

k = income -> sum;

k -= data;

node -> sum = k;

income -> sum = k;

node -> next = income -> next;

income -> next = node;

}



void forprint(Income* income){//明细

income = income -> next;

printf("————————当前收支明细记录————————\n");

printf("收支\t\t收支金额\t说明\t账号金额\n");

while(income){

printf("%s\t\t%.2f\t\t%s\t\t%.2f\n",income -> king,income -> money,income -> state,income -> sum);

income = income -> next;

}

printf("\n");

}




void main(){

int tmp = 0;

double kl = 0; //金额

char name[30];

char turn = ' ';

Income* income = init();

while(1){

printf("————————家庭收支记账软件————————\n");

printf("\t\t1 收到明细\n");

printf("\t\t2 登录收入\n");

printf("\t\t3 登录支出\n");

printf("\t\t4 退  出\n");


printf("请选择(1-4):");

scanf("%d",&tmp);

if(tmp == 2){//添加金额

printf("本次收入金额:");

scanf("%lf",&kl);

printf("本次收入说明:");

scanf("%s",name);

Income_init(income,kl,name);

}else if(tmp == 1){ //显示明细

forprint(income);

}else if(tmp == 4){ //退出

getchar();

printf("\n确认是否退出(Y/N):");

scanf("%c",&turn);

if(turn == 'Y' || turn == 'y'){

break;

}else if(turn == 'N' || turn == 'n'){

continue;

}else{

printf("请输入有效值!!!\n");

}

}else if(tmp == 3){//支出

printf("本次支出金额:");

scanf("%lf",&kl);

printf("本次支出说明:");

scanf("%s",name);

expend_init(income,kl,name);

}

}


getchar();


getchar();

}






尚硅谷C语言零基础快速入门教程的评论 (共 条)

分享到微博请遵守国家法律