纪念四角,收集C++小程序,技术性散分

cphj 2009-09-25 05:03:50
2009/09/18 09:46 至 2009/09/25 16:22,由于大家的强力支持,顺利从三角升到四角,特此回馈大家的厚爱

为进行技术性散分,请大家回帖,贴出你最近写的C++代码及其简单功能描述

代码以轻薄短小为宜
代码功能任意
代码必须能编译执行
代码功能雷同的以先回复为准
无功能描述的会被忽略

本贴干燥,纯水请回避

感谢大家
...全文
353 48 打赏 收藏 转发到动态 举报
写回复
用AI写文章
48 条回复
切换为时间正序
请发表友善的回复…
发表回复
wesweeky 2009-11-24
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 xxcpqzm 的回复:]
引用 4 楼 wxsxiaok 的回复:
#include <iostream>
using namespace std;
void main()
{
cout < <"Hello World!" < <endl;//此程序相当牛X,多少成功的IT人士都曾经写过这段叫人刻苦铭心的代码~~~
}


恩,可能吧
[/Quote]

这个确实。
lovesi3344 2009-11-17
  • 打赏
  • 举报
回复
哇,好贴,精华帖
学习
shiweifu 2009-09-26
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

/* define two arrays to keep days*/
int leap_year[12] = {0,31,60,91,121,152,182,213,244,274,305,335};
int no_leap_year[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
int sum_days;

/* define a struct to kepp date */
typedef struct
{
unsigned int year;
unsigned int month;
unsigned int day;
} Date;


int is_leap_year(const int year)
{
return ( (!(year%4)&&(year%100)) || !(year%400) );
}

int get_days(const Date d)
{
int n = 0;
if(is_leap_year(d.year))
{
n = leap_year[d.month - 1];
}
else
{
n = no_leap_year[d.month - 1];
}

n += d.day;

return n;
}


int main()
{
sum_days = 0;
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );

Date current;
memset(¤t,0,sizeof(Date));

timeinfo = localtime ( &rawtime );
current.month = timeinfo->tm_mon + 1;
current.year = timeinfo->tm_year + 1900;
current.day = timeinfo->tm_mday;

Date my_birth;
memset(&my_birth,0,sizeof(Date));

//input
printf("%s","please input your birthday:\r\n");

while(1)
{

printf("%s\r\n","usage: 1989/6/4");
scanf("%d/%d/%d",&my_birth.year,&my_birth.month,&my_birth.day);
fflush(stdin);
if(my_birth.year <= 1900)
{
printf("%s\r\n","your input is error!");
continue;
}
if(my_birth.month >= 12 || my_birth.month < 1)
{
printf("%s\r\n","your input is error!");
continue;
}
if(my_birth.day > 31 || my_birth.day < 1)
{
printf("%s\r\n","your input is error!");
continue;
}
break;
}

int i = (my_birth.year + 1);
int temp = 0;
for(; i <= (current.year - 1); i++)
{
if(is_leap_year(i))
{
sum_days += 366;
}
else
{
sum_days += 365;
}
}

temp = get_days(current);
sum_days += temp;

temp = get_days(my_birth);

if(current.year == my_birth.year)
{
sum_days -= temp;
}
else
{
sum_days += temp;
}
printf("%d\r\n",sum_days);

return 0;
}


刚写的,计算一共活了多少天。。
其实用C中的结构可以更简单完成的。
kekailiyuan 2009-09-26
  • 打赏
  • 举报
回复
请大家写程序之后认真检查,错误太多了,误导我这样的新手,还以为那种写法也是允许的呢.
ele7enjr 2009-09-26
  • 打赏
  • 举报
回复

#include<iostream>//用递归调用求阶乘

using namespace std;
int fac(int n)
{
int back;
if(n<0)
cout<<"wrong number"<<endl;
if(n==0)
back=1;
if(n>0)
back=n*fac(n-1);
return (back);
}
int main(void)
{
cout<<"input a number"<<endl;
int x;
cin>>x;
x=fac(x);
cout<<x;
}
ele7enjr 2009-09-26
  • 打赏
  • 举报
回复


#include<iostream>//输出乘法口诀表
using namespace std;
void main()
{
int x,y,i;
for(x=1;x<=9;x++)
{
for(y=1;y<=x;y++)
{
i=x*y;
cout<<x<<'*'<<y<<'='<<i<<'\t';
}
cout<<endl;
}
}
ele7enjr 2009-09-26
  • 打赏
  • 举报
回复

//输入二进制数变成十进制,自己编的,见笑,刚刚学

#include <stdio.h>
void main()
{
int getnumber, h, i, n=0, d=0;
long a[100];
printf("请输入一列二进制数");
scanf("%d",&getnumber);
for(i=0;d==0;i=i*10)
{
a[n]=getnumber%10;
getnumber=getnumber/10;
if(getnumber==0&&a[n]==0)
d=1;
n++;

}
n=n-1;
printf("这是%d位数\n",n);

for(h=0;h<n;h++)
{
for(d=1;d<=h;d++)
{
a[h]=a[h]*2;

}
}
for(i=n-1;i>=1;i--)
{a[i-1]=a[i]+a[i-1];}
printf("%ld\n",a[0]);


}
ttchenwei 2009-09-26
  • 打赏
  • 举报
回复
那就补一个


int main(int argc,char** agrv)
{
return 0;
}
ttchenwei 2009-09-26
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!小技巧
OenAuth.Core 2009-09-26
  • 打赏
  • 举报
回复

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
cout << "请输入延时时间(单位:秒):";
float secs;
cin >> secs;
clock_t delay = secs*CLOCKS_PER_SEC;
cout << "starting\a\n";
clock_t start = clock();
while(clock()-start <delay); //延时
cout << "done\a\n";
return 0;
}

churenxh 2009-09-26
  • 打赏
  • 举报
回复
下面代码:以二进制读写.exe文件的最后41个字节
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

/* Macro to define the max path size. */
#define MAX_PATH_SIZE 1024

/* Macro to define the max data size. */
#define MAX_DATA_SIZE 41

/* Standard Windows entry point for the program. */
int main (void)
{
/* Array to store the filename of the currently running server file. */
char szFileName[MAX_PATH_SIZE+1] = "\0";

/* This is the format of the data we want to read:
DATA: password port exe name
SIZE: (20) (6) (15)
|------------------||----||-------------| */

/* Initialise the array to null chars (of size MAX_DATA_SIZE + 1 for a
null char) for the data to be read into. */
char szReadData[MAX_DATA_SIZE+1] = "\0";

/* Temporary buffer and position pointer, for use in interpreting the data.*/
char szTmpBuff[MAX_DATA_SIZE+1] = "\0";
unsigned int iTmpPos, i;

/* Declare the file handle. */
HANDLE fhSource;

/* To record the number of bytes read later on. */
DWORD iBytesRead = 0;

/* Get the running server file path. */
GetModuleFileName(GetModuleHandle(NULL), szFileName, MAX_PATH_SIZE);

/* Open the target file for reading from, using Win32 API. */
fhSource = CreateFile(szFileName, /* Address of file. */
GENERIC_READ, /* Access attributes, ie allow reads. */
FILE_SHARE_READ, /* Share Mode. Allow Read. */
NULL, /* Security Descriptor (NULL means default) */
OPEN_EXISTING, /* Creation Disposition (open existing file if exists) */
0, /* Flags and attributes (NONE) */
0); /* Template file (NONE) */

/* The file has been opened successfully if the file handle is valid. */
if (fhSource != INVALID_HANDLE_VALUE)
{
/* Set the pointer to MAX_DATA_SIZE bytes from the end of the file. */
SetFilePointer(fhSource, -MAX_DATA_SIZE, NULL, FILE_END);//FILE_BEGIN


/* Now read MAX_DATA_SIZE bytes from the end of the file. If the data
was NOT successfully appended to the end of the server, then there
will be an issue because you will read the program data, since there
is no appended data to read. Therefore some kind of check to see if
there is appended data is a good idea. I suggest some kind of key or
identifier at the beginning or end of the data, which the presence of
can be checked for once the data has been read. */
ReadFile(fhSource, szReadData, MAX_DATA_SIZE, &iBytesRead, NULL);

/* Now finished with the file, so we must close it now. */
CloseHandle(fhSource);

/* If the number of bytes read equals the number of bytes that we
wanted to read, then the operation was successful. */
if (iBytesRead == MAX_DATA_SIZE)
{

/* Get the password from the read data without trimming trailing spaces */
for (i = 0, iTmpPos = 0; i < 20; i++)
szTmpBuff[iTmpPos++] = szReadData[i];
/* Add terminating null character at end of copied string (saves
clearing the whole array each time, since null char marks the en
of the string in the array. */
szTmpBuff[iTmpPos] = '\0';
/* Print the data in the temporary buffer. */
printf("Password: %s\n", szTmpBuff);


/* Get the port from the read data... */
for (i = 20, iTmpPos = 0; i < 26; i++)
szTmpBuff[iTmpPos++] = szReadData[i];
/* As above. */
szTmpBuff[iTmpPos] = '\0';
/* Note that the port is actually converted to a long here! */
printf("Port: %d\n", strtol(szTmpBuff, NULL, 10));


/* Get the exe name from the read data without trimming trailing spaces */
for (i = 26, iTmpPos = 0; i < 41; i++)
szTmpBuff[iTmpPos++] = szReadData[i];
/* As above. */
szTmpBuff[iTmpPos] = '\0';
printf("Exename: %s\n", szTmpBuff);

/* Done! */
MessageBox(0, "File successfully read from!", "Success!", MB_OK | MB_ICONINFORMATION);
}
else
MessageBox(0, "Error reading from the file!", "Error!", MB_OK | MB_ICONERROR);

}

/* Error opening the file, so report the error. Doing something such as
creating defaults or quitting quietly is probably a good idea in a real
server. The message box is just to demonstrate that there may be errors
opening the file! */
else
{
/* Nice little error message box! */
MessageBox(0, "Error opening file!", "Error!", MB_OK | MB_ICONERROR);

/* Exit with failure error code. */
return 1;
}



return 0;
}
2009-09-26
  • 打赏
  • 举报
回复
http://blog.csdn.net/hpsmouse/archive/2009/09/23/4586044.aspx
胡乱写写,没怎么测试哈~~
kouwenlong 2009-09-26
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 100//³õÊÔ·ÖÅä¿Õ¼ä´óС
typedef struct
{
char *base;
char *top;
char stacksize;
}SqStack;

bool InitStack(SqStack &S)
{
S.base = (char *)malloc(STACK_INIT_SIZE * sizeof(char));
if (!S.base)
{
return false;
}
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return true;
}

char GetTop(SqStack S)
{
char e;
if (S.top == S.base)
{
return '@';
}
e = *(S.top - 1);
return e;
}

bool Push(SqStack &S, char e)
{
if (S.top - S.base >= S.stacksize)
{
return false;
}
*S.top++ = e;
return true;
}


bool Pop(SqStack &S, char&e)
{
if (S.base == S.top )
{
return false;
}
e = *--S.top;
return true;
}

int main()
{
char ch;
SqStack stack;
InitStack(stack);
while(cin>>ch)
{
Push(stack, ch);
}

while(stack.base != stack.top)
{
Pop(stack, ch);
cout<<ch<<endl;
}
getchar();
return 0;
}

入栈,出栈。
cattycat 2009-09-26
  • 打赏
  • 举报
回复
最近看stl的模板,见到了函数对象定义,以前没见过。

class strInStruVtr:public std::binary_function <struTriad, struTriad, bool>
{
public:
bool operator()(const struTriad& a, const struTriad& b) const {
return a.u==b.u;
}
};
love_route 2009-09-26
  • 打赏
  • 举报
回复
safedelete(a);
写错。。。
love_route 2009-09-26
  • 打赏
  • 举报
回复

安全删除一个指针,可写成一个宏
void safedelete(void *p)
{
if(p != NULL)
{
delete ;
p = NULL;
}
}

int main(int argc, void* argv[])
{
char* a = new char[10];
safedelete a;
return 0;
}
  • 打赏
  • 举报
回复
岛上码农 2009-09-26
  • 打赏
  • 举报
回复
程序功能:使用牛顿法求平方根

#include<iostream>

using namespace std;

double sqrt2(double x,int N);

void main()
{
cout<<sqrt2(3.0,5)<<endl;
}

double sqrt2(double x,int N)
{
int i;
double ret = x;
for(i = N;i>0; i--)
{
ret = ret - (ret*ret - x)/(2.0*ret);
}
return ret;
}
woods2001 2009-09-26
  • 打赏
  • 举报
回复
树的递归遍历和非递归遍历源码(C++)

#include <iostream>
#include <string>
#define MAX_SIZE 50
#define INCRE_SIZE 10
#define NULL 0
using namespace std;
class BiNode
{
//member data variant
public:
char data;
BiNode *lchild;
BiNode *rchild;
//member function
public:
BiNode();

};
BiNode::BiNode()
{
data='#';
lchild=NULL;
rchild=NULL;
}
typedef BiNode *BiTree;

class Stack
{
//member data variant
public:
BiTree *elem;
int top;
int base;
int stack_size;
int length;
//member function
public:
Stack();
void push(BiTree &ele);
void pop(BiTree &ele);
bool isEmpty(){ return !length; }
bool getTop(BiTree &ele);

};
Stack::Stack()
{
elem=new BiTree[MAX_SIZE];
top=0;
base=0;
stack_size=50;
length=0;
}
void Stack::push(BiTree &ele)
{
//if(size>=stack_size)
//process
elem[top++]=ele;
length++;
}
void Stack::pop(BiTree &ele)
{
ele=elem[--top];
length--;
}
bool Stack::getTop(BiTree &ele)
{
if(length==0)
return 0;
ele=elem[top-1];
return 1;
}

void createBitree(BiTree &root)
{
//preorder
char ch;
cin>>ch;
if(ch=='#')
{
root=NULL;
return ;
}
else
{
root=new BiNode;
root->data=ch;
createBitree(root->lchild);
createBitree(root->rchild);
}
}
void preprint(BiTree &root)
{
if(root)
{
cout<<root->data<<" ";
preprint(root->lchild);
preprint(root->rchild);
}
else
cout<<"NULL"<<" ";
}
void inprint(BiTree &root)
{
if(root)
{
inprint(root->lchild);
cout<<root->data<<" ";
inprint(root->rchild);
}
else
cout<<"NULL"<<" ";
}

//二叉树的中序非递归算法
void InOrder(BiTree &root)
{
Stack s;
s.push(root);
BiTree p;
while(!s.isEmpty())
{
while(s.getTop(p) && p)
s.push(p->lchild);
s.pop(p);
if(!s.isEmpty())
{
s.pop(p);
cout<<p->data<<" ";
s.push(p->rchild);
}
}
}
//二叉树的先序非递归算法
void PreOrder(BiTree &root)
{
Stack s;
s.push(root);
BiTree p;
while(!s.isEmpty())
{
while(s.getTop(p)&&p)
{
cout<<p->data<<" ";
s.push(p->lchild);
}
s.pop(p);
if(!s.isEmpty())
{
s.pop(p);
s.push(p->rchild);
}
}
}

int main()
{
/*
BiTree bt1=new BiNode;
BiTree bt2=new BiNode;
Stack sta;
bt1->data='f';
sta.push(bt1);
sta.pop(bt2);
cout<<bt2->data;*/
BiTree t=NULL;
preprint(t);
cout<<"Please create the Bitree:";
createBitree(t);
preprint(t);
cout<<'\n';
inprint(t);
cout<<'\n';
PreOrder(t);
cout<<'\n';
InOrder(t);

}
xxcpqzm 2009-09-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wxsxiaok 的回复:]
#include <iostream>
using namespace std;
void main()
{
cout < <"Hello World!" < <endl;//此程序相当牛X,多少成功的IT人士都曾经写过这段叫人刻苦铭心的代码~~~
}

[/Quote]
恩,可能吧
加载更多回复(28)

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧