#include<stdio.h>
main() {
struct bill {
unsigned char class;
unsigned char detail;
float money;
unsigned char date;
};
struct bill num[1000] = {
"收入","生活費",3000.0,"2020 - 04 - 01",
"支出","午餐",12.5,"2020 - 04 - 30",
"支出","買書",25.0,"2020 - 04 - 30",
"支出","晚餐",16.0,"2020 - 04 - 30",
"收入","生活費",3500.0,"2020 - 05 - 01",
"支出","峨眉山旅游",500.0,"2020 - 05 - 01"
};
printf("%c%c%.1f%c",num[2].class,num[2].detail,num[2].money,num[2].date);
}
為什么列印錯誤?
uj5u.com熱心網友回復:
這不做的挺好的uj5u.com熱心網友回復:
??那為什么列印錯誤呢
uj5u.com熱心網友回復:
unsigned char class 改成 unsigned char clazz[256];注意字符和字串的區別,其它的detail和date也是同樣的問題
另外,class這種關鍵字盡量不要用來做變數
uj5u.com熱心網友回復:
謝謝 可是列印出來還是不對,是不是printf寫錯了啊
uj5u.com熱心網友回復:
printf("%c%c%.1f%c") %c列印單個字符,%s列印字串uj5u.com熱心網友回復:
大佬 我寫完了 有個錯誤能在幫我看一下嗎 應該是在for回圈下的if函式里 那個字串的判斷怎么辦?
#include<stdio.h>
#include<math.h>
main() {
int i, m;
float income = 0, expend = 0, surplus;
struct bill {
unsigned char clazz[256];
unsigned char detail[256];
float money;
unsigned char date[256];
};
struct bill num[1000] = {
"收入","生活費",3000.0,"2020 - 04 - 01",
"支出","午餐",12.5,"2020 - 04 - 30",
"支出","買書",25.0,"2020 - 04 - 30",
"支出","晚餐",16.0,"2020 - 04 - 30",
"收入","生活費",3500.0,"2020 - 05 - 01",
"支出","峨眉山旅游",500.0,"2020 - 05 - 01"
};
printf("請輸入要查詢的賬單編號:");
scanf_s("%d", &i);
for (m = 0; m < i; m++) {
if (num[m].clazz == "收入")
income += num[m].money;
else
expend += num[m].money;
}
surplus = income - expend;
printf("編號:%d 類別:%s 明細:%s 金額:%.1f 日期:%s\n截至目前總收入:%.1f 總支出:%.1f 結余:%.1f",i,num[i-1].clazz,num[i-1].detail,num[i-1].money,num[i-1].date,income,expend,surplus);
}
uj5u.com熱心網友回復:
if (num[m].clazz == "收入")這個比較改為使用字串比較函式strncmp,如果提示型別不一樣,自己改下型別或者強轉一下
uj5u.com熱心網友回復:
if (num[m].clazz == "收入") ===》》 if (strcmp(num[m].clazz, "收入")== 0)轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/58131.html
標籤:C語言
上一篇:MATLAB差分方程實作
下一篇:統計票數
