我想知道我怎么能这样做,在CI中使用printf打印一定数量的空格正在考虑这样的事情,而且我的代码在第一个printf语句之后没有打印,我的程序编译完全很好.我猜我必须打印N-1个空格,但我不太清楚如何这样做.
谢谢.
#include <stdio.h>
#include <limits.h>
#include <math.h>
int f(int);
int main(void){
int i, t, funval,tempL,tempH;
int a;
// Make sure to change low and high when testing your program
int low=-3, high=11;
for (t=low; t<=high;t++){
printf("f(%2d)=%3d\n",t,f(t));
}
printf("\n");
if(low <0){
tempL = low;
tempL *=-1;
char nums[low+high+1];
for(a=low; a <sizeof(nums)/sizeof(int);a+5){
printf("%d",a);
}
}
else{
char nums[low+high];
for(a=low; a <sizeof(nums)/sizeof(int);a+5){
printf("%d",a);
}
}
// Your code here...
return 0;
}
int f(int t){
// example 1
return (t*t-4*t+5);
// example 2
// return (-t*t+4*t-1);
// example 3
// return (sin(t)*10);
// example 4
// if (t>0)
// return t*2;
// else
// return t*8;
}
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的:
1 6 11 16 21 26 31
| | | | | | |
Run Code Online (Sandbox Code Playgroud)
ldg*_*bay 38
n空间printf有一个很酷的宽度说明符格式,允许您传递一个int指定宽度.如果空格数n大于零:
printf("%*c", n, ' ');
Run Code Online (Sandbox Code Playgroud)
应该做的伎俩.我也可以通过以下方式对n大于或等于零执行此操作:
printf("%*s", n, "");
Run Code Online (Sandbox Code Playgroud)
它仍然不完全清楚你想要什么,但要生成你在帖子底部描述的确切模式,你可以这样做:
for (i=1; i<=31; i+=5)
printf("%3d ", i);
printf("\n");
for (i=1; i<=31; i+=5)
printf(" | ");
printf("\n");
Run Code Online (Sandbox Code Playgroud)
这输出:
1 6 11 16 21 26 31
| | | | | | |
Run Code Online (Sandbox Code Playgroud)