![](/img/trans.png)
[英]Word count program not working. Code sample From the Book The C-programming Language Ritchie & Kernighan
[英]1.14 task in Kernighan, Ritchie “Programming with C” book
我在“The C Programming Language”一書中完成了任務1.14,並且不知道我的代碼中出了什么問題,特別是在打印圖表時。
#include <stdio.h>
#define MAX 300
int main(void) {
int c,n,k,i,g,p,f;
int mas[MAX];
for(i = 0; i < MAX; i++) //array for the string's length
mas[i] = 0;
i = 0;
n = k = f = 0;
//getting strings to print
while((c = getchar()) != EOF) {
n++; //counter for each string in array
if(c == '\n'){
mas[i] = n;
i++;
if(n > k){ //the highest number to print
k = n;
}
n = 0;
}
}
//printing diagrams. I'm trying to do this:
/*
|
k |
|
|
|
|_____________________
i
*/
for(; k > 0; k--){ //diagram's height
for(g = 0; g < i; g++){ //diagram's length
if(mas[g] = k){ //if an array have an appropriate height to print
printf("%c", "#");
} else
putchar(' ');
}
putchar('\n');
}
return 0;
}
你可以在ideone.com上獲得它 - http://ideone.com/1RZ1zU 。
if(mas[g] = k){
你可能意味着
if(mas[g] == k){
這也是錯的:
printf("%c", "#");
%c
需要一個字符,而不是一個字符串:
printf("%c", '#');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.