#include "stdafx.h"
int Pheponatch(int n);
int main(int argc, char* argv[])
{
int n;
printf("輸入n的值:");
scanf("%d", &n);
printf("%d\n",Pheponatch(n));
return 0;
}
int Pheponatch(int n)
{
unsigned int result,result_1,result_2;
result_1=1;
result_2=1;
if(n<=2)
{
result=1;
}
for(int i=3;i<=n;++i)
{
result=result_1+result_2;
result_1=result_2; //此處注意先給result_1 賦值
result_2=result;
}
return result;
}
2.輸入6個整數,求其中偶數的平均值和奇數的累加和
#include "stdafx.h"
int main(int argc, char* argv[])
{
int n[6];
printf("輸入6個n的值:");
scanf("%d%d%d%d%d%d", &n[0], &n[1], &n[2], &n[3], &n[4], &n[5]);
int EvenSum=0,OddSum=0;
int j=0; //標記偶數個數
for(int i=0;i<6;i++)
{
if(0==(n[i]%2))
{
j++;
EvenSum+=n[i];
}
else
{
OddSum+=n[i];
}
}
printf("偶數平局數:%d\n奇數和:%d \n",EvenSum/j,OddSum);
return 0;
}
3.txt讀取
#include "stdafx.h"
#include<stdio.h>
void main()
{
FILE *fp,*fp1;
int cap=0, i=1;
char mid,filename[10];
printf("Input the filename like *.txt!\n");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("Can not open the file!\n");
}
if((fp1=fopen("stdout.txt","w+"))==NULL)
{
printf("Can not open the file!\n");
}
while(!feof(fp))
{
mid=fgetc(fp);
if(mid=='\n') cap++;
}
fclose(fp);
if((fp=fopen(filename,"r"))==NULL)
{
printf("Can not open the file!\n");
}
fprintf(fp1,"%d ",i++);
while(!feof(fp))
{
if(fputc(fgetc(fp),fp1)=='\n')
fprintf(fp1,"%d ",i++);
}
printf("cap=%d \n",cap+1);
fclose(fp);
fclose(fp1);
}
4打印
*
*.*.
*..*..*..
*...*...*...*...
*....*....*....*....*....
*.....*.....*.....*.....*.....*.....
*......*......*......*......*......*......*......
#include "stdafx.h"
void show(int n);
int main(int argc, char* argv[])
{
show(10);
getchar();
return 0;
}
void show(int n)
{
int i,j,k;
for(k=1;k<=n;k++)
{
for(i=0;i<k-1;i++)
{
printf("*");
for(j=0;j<k-2;j++)
{
printf(".");
}
}
printf("\n");
}
}
5寫一個函數,將其中的\t都轉換成4個空格。
#include "stdafx.h"
#include<stdio.h>
#include<string.h>
#define MAXLEN 100
void TabToSpace(char *str,int len)
{
int i=0,j;
while(str[i]!='\0')
{
if(str[i]=='\t')
{
for(j=len+3;j-3>i;j--)
{
str[j] = str[j-3];
}
str[i++] = '.';
str[i++] = '.';
str[i++] = '.';
str[i++] = '.';
len = len +3;//這兒不要忘了哦,這樣移動後實際上你的字符串的長度已經增加了
}
else i++;
}
}
int main()
{
char a[MAXLEN]="ab\tcd\t123\tasc111111";
int len=strlen(a);
TabToSpace(a,len);
printf("%s\n",a);
return 0;
}
MFC 只允許程序運行一個實例
// 此程序只能運行一次,用互斥量來判斷程序是否已運行
HANDLE m_hMutex=CreateMutex(NULL,TRUE, m_pszAppName);
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
AfxMessageBox("程序正在運行",MB_OK,0);
return FALSE;
}