[英]1.14 task in Kernighan, Ritchie “Programming with C” book
I'm doing the task 1.14 in "The C Programming Language" book and don't know what's going wrong in my code, especially in printing the diagram. 我在“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;
}
You can get it in the ideone.com — http://ideone.com/1RZ1zU . 你可以在ideone.com上获得它 - http://ideone.com/1RZ1zU 。
if(mas[g] = k){
you probably mean 你可能意味着
if(mas[g] == k){
And this is wrong too: 这也是错的:
printf("%c", "#");
%c
expects a character, not a string: %c
需要一个字符,而不是一个字符串:
printf("%c", '#');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.