尚硅谷C語言零基礎(chǔ)快速入門教程

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//1.解決一個(gè)問題,目前沒有數(shù)據(jù)庫,應(yīng)該使用數(shù)結(jié)構(gòu)體賴保存數(shù)據(jù),才能達(dá)到效果
//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[]){ //收入
//創(chuàng)建一個(gè)登錄收入結(jié)構(gòu)體需要多大的空間
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[]){ //支出
//創(chuàng)建一個(gè)登錄收入結(jié)構(gòu)體需要多大的空間
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){//明細(xì)
income = income -> next;
printf("————————當(dāng)前收支明細(xì)記錄————————\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 收到明細(xì)\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){ //顯示明細(xì)
forprint(income);
}else if(tmp == 4){ //退出
getchar();
printf("\n確認(rè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();
}