可以用一空行作为字符串结束的标志.
倚天照海花无数,流水高山心自知。
干脆把我自己的行编译器程序贴出来,大家帮忙找不足之处.
#include<stdio.h>
#include<string.h>
int main()
{
#ifndef ONLINE_JUDGE
freopen ("hangbianyiqi.txt","r",stdin);
#endif
char c,str[300];
int i=0;
while(EOF!=(c=getchar()))//采取文件输入
{
if(c=='#')
{
if(i>0&&str[i-1]!='\n')
{
i--;//退格
}
}
else
{
if(c=='@')//可以先用一数组保存每一个换行符出现的位置,方便查找。
{
if(i>0)
{
while(str[--i]!='\n');
i++;
}
}
else
{
str[i++]=c;
}
}
}
str[i]='\0';
printf("%s\n",str);
return(0);
}
调式的话请在和程序同目录下建一hangbianyiqi.txt文件,里面输入你要测试的字符串.
贴上我的代码,.刚看过几个高手写的,我都不愿意贴出来的,如果大家要的话,我可以把几个高手写的发上来.
#include<stdio.h>
#include<math.h>
int main()
{
int n,length;
int data[15];
while(EOF!=(scanf("%d",&n))&&n!=0)
{
length=0;
n--;
while(n>0)//根据数据关系,化成二进制
{
data[length++]=n%2;
n/=2;
}
printf("{ ");
if(length!=0)
{
printf("%ld",(long)pow(3,--length));
}
while(--length>=0)
{
if(data[length]==1)
{
printf(", %ld",(long)pow(3,length));
}
}
printf(" }\n");
}
return 0;
}
请大家继续关注...
以下皆转载,看看别人是怎么写的.
#include <stdio.h>
//Viusal C++ 6.0
#define BIGINT __int64
#define FORMAT "%I64d"
/*
mingw use this macro
#define BIGINT long long
#define FORMAT "%I64d"
linux use this macro
#define BIGINT long long
#define FORMAT "%lld"
*/
BIGINT ThreePower[32];
void HeroNeedThree(BIGINT* subset, BIGINT n)
{
int t, p;
BIGINT m;
m = n-1;
for (t = 0; m; m&=m-1) t++;
p = 0; m = n-1; subset[0] = t;
while (t)
{
if (m & 1) subset[t--] = ThreePower[p];
p++;
m >>= 1;
}
}
int main()
{
int i;
BIGINT n, subset[33];
ThreePower[0] = 1;
for (i = 1; i < 32; i++) ThreePower[i] = ThreePower[i-1]*3;
while (EOF != scanf(FORMAT, &n))
{
HeroNeedThree(subset, n);
printf("{ ");
for (i = 1; i < subset[0]; i++) printf(FORMAT", ", subset[i]);
if (subset[0]) printf(FORMAT" ", subset[subset[0]]);
puts("}");
}
return 0;
}
typedef long long unsigned INT64;
void HeroNeedThree (INT64* subset, unsigned int n)
{
static INT64 three[41];
static bool ini = false;
if (ini == false) {
INT64 a = 1;
for (int i = 0; i < 41; ++i, a*=3) {
three[i] = a;
}
ini == true;
}
int bitn = 31;
unsigned int mask = (1 << 31);
for (; bitn >= 0 && ((mask&n)==0); --bitn, mask >>= 1);
int amount = 0;
for (int i = 1; bitn >= 0; mask>>=1, --bitn) {
if (n > mask) {
subset[i] = three[bitn];
amount++;
n -= mask;
i++;
}
}
subset[0] = amount;
}
方法:这跟二进制有点关系.