A. 计算圆面积c语言问题,代码如下:
记住了,scanf()中double型对应的控制符必须是%lf,float型的控制符必须是%f,不然就必然出错。而在输出函数printf()中,在控制符%f下可以输出任何浮点数。这些规定在教科书中都有明确儒术。
B. 帮我解释下这段C++代码,做火焰和熔浆效果的,
class CFireRoutine //烟火程序
{
public:
CFireRoutine(); //构造函数
virtual ~CFireRoutine(); //虚态的析构函数
// Functs (public)
void InitFire(); //初始化
void ClrHotSpots(); //清除燃烧点
void InitPallette(); //初始化调色板
void SetHotSpots(); //设置燃烧点
void MakeLines(); //生成扫描线
//渲染(视频内存,宽度,高度)
void Render(DWORD* pVideoMemory,
int iwidth,
int iheight);
//均值(在点x,y处)(像素均值?)
unsigned char Average(int x, int y);
// props
int m_iFlameHeight; //火焰高度
int m_iWidth; //宽度
int m_iHeight;//高度(应该是作图区高度)
//火焰来源,即从点火处的y坐标
int m_iFireSource;//The y position for the lit spots
int m_iFireChance; //燃烧几率
int m_iAvgFlameWidth; //火焰平均宽度
int m_iAlpha; //α(深度)
COLORREF m_FireColors[4]; //火焰颜色
BYTE* m_pFireBits; //火焰数据
DWORD m_pPalletteBuffer[256]; //调色板
long* m_pYIndexes; //颜色索引
};
//熔浆:
class CPlasmaRoutine
{
public:
CPlasmaRoutine();//构造函数
virtual ~CPlasmaRoutine();//虚态析构函数
// Methods
void SetDefaultValues(VARIANT* pExtParms);//设置缺省参数值(外部变量)
void InitializePlasma(VARIANT* pExtParms);//初始化岩浆参数
void Create(int iWidth,int iHeight);//按视图生成岩浆
void Render(DWORD* pBits, //按照像素位,视图宽度,高度,扫描线渲染
int iwidth,
int iheight,
int iLineLength);
//设置颜色索引的RGB值
void SetRGB(int iIndex,int R,int G,int B);
void InitCostBLTable(); //初始化损耗平衡表?BL是不是balance的意思啊?不知道,反正不是对比度和亮度,还是色彩饱和度?好像都不是
void InitPallette(); //初始化调色板
void CalcPlasma(); //计算岩浆
//按照颜色开始,颜色结束,颜色步长创建渐变色,存到buffer里面
void CreateGradient(COLORREF clrStart,COLORREF clrEnd,long lSteps,COLORREF* pBuffer);
// Props
int m_iWidth;//视图宽度
int m_iHeight;//视图高度
int m_iAlpha;//视图深度
BYTE* m_pPlasmaBits; //岩浆序列缓冲
DWORD m_pPalletteBuffer[256];//调色板
int m_icostbl[256];//损耗平衡表
COLORREF m_PlasmaColors[16];// Yep 16 colors needed to generate our pallete... 采用16种颜色产生调色板
//以下应该是曲线拟合用的贝塞尔曲线四个控制点和矢量坐标
unsigned char m_a1,m_a2,m_a3,m_a4,m_b1,m_b2,m_b3,m_b4;
int m_iModifier1;
int m_iModifier2;
int m_iModifier3;
int m_iModifier4;
//双方向修改器(两点拉拽形成的矢量)
int m_iXModifier1;
int m_iXModifier2;
int m_iYModifier1;
int m_iYModifier2;
};
// FireRoutine.cpp: implementation of the CFireRoutine class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FireRoutine.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction 构造函数,初始化所有参数,注意火源初始值为2,燃烧概率初始值为10
//////////////////////////////////////////////////////////////////////
CFireRoutine::CFireRoutine()
{
m_iWidth = 0;
m_iHeight = 0;
m_pFireBits = NULL;
m_pYIndexes = NULL;
m_iFireSource = 2;
m_iFireChance = 10;
m_iFlameHeight = 50;
m_iAlpha = 255;
m_FireColors[0] = RGB(0,0,0);// Black
m_FireColors[1] = RGB(255,0,0);// Red
m_FireColors[2] = RGB(255,255,0);// Yellow
m_FireColors[3] = RGB(255,255,255);// White
m_iAvgFlameWidth = 35;
}
//析构函数,如果颜色索引和渲染缓冲区存在则注销
CFireRoutine::~CFireRoutine()
{
if(m_pFireBits != NULL)
delete [] m_pFireBits;
if(m_pYIndexes != NULL)
delete [] m_pYIndexes;
m_pFireBits = NULL;
m_pYIndexes = NULL;
}
void CFireRoutine::InitFire()
{
// Get Rid of anything already there 初始化火焰,如果存在颜色索引和渲染缓冲则首先注销
if(m_pFireBits != NULL)
delete [] m_pFireBits;
if(m_pYIndexes != NULL)
delete [] m_pYIndexes;
// Add three to the height 高度自动加三
m_iHeight+=3;
m_pYIndexes = new long[m_iHeight]; //颜色索引时以火焰高度为长度的一个长整形数组
m_pFireBits = new BYTE[m_iWidth*m_iHeight]; //渲染缓冲区为一个W*H长度的字符型数组
// Clear the Fire bits 将火焰缓冲区置零
memset(m_pFireBits,0,(m_iWidth*m_iHeight));
// do all the y index pre-calc.. 在计算之前先将颜色缓冲值初始化为每个元素=宽度*高度
for (int y = m_iHeight; y >0; y--)
m_pYIndexes[y] = y * m_iWidth;
// Create our pallete
InitPallette(); //初始化调色板
ClrHotSpots(); //清除燃烧点
}
//所谓清除燃烧点就是将渲染缓冲区的某一行清零。哪一行呢?就是颜色索引中火源指向的那一行。
void CFireRoutine::ClrHotSpots()
{
// clear the fire line
memset(&m_pFireBits[m_pYIndexes[m_iFireSource]],0,m_iWidth);
}
//初始化调色板。您可能需要首先了解一下调色板的概念:虽然一个图可能是彩色的,但是每个像素如果
//都按照RGB三个字节方式存储,则成本太高了。调色板在于,假设这个图还是彩图,但是经过色彩分析,
//其中只有256种颜色是图中的常用颜色,这样一个字节就可以代表一个像素了,不过这个字节的值指的
//并不是RGB这种色彩矢量,而是256种颜色的代码,这256种颜色就是这幅图的调色板。
void CFireRoutine::InitPallette()
{
// Create a gradient between all the colors we have for our fire... 为我们的火焰创造一个过渡色使用的所有颜色
long iCount = 0;
COLORREF clrStart;
COLORREF clrEnd;
for(int iColor = 1;iColor<4;iColor++) //火焰的四个颜色定义
{
clrStart = m_FireColors[iColor-1]; //设置过渡色的起始颜色
clrEnd = m_FireColors[iColor]; //设置过渡色的终止颜色
int r, g, b; // First distance, then starting value 先计算距离,再计算值
float rStep, gStep, bStep; // Step size for each color //每种颜色的过渡步进值
// Get the color differences 取得三个颜色RGB的起始颜色和过渡颜色色差
r = (GetRValue(clrEnd) - GetRValue(clrStart));
g = (GetGValue(clrEnd) - GetGValue(clrStart));
b = (GetBValue(clrEnd) - GetBValue(clrStart));
int nSteps = max(abs(r), max(abs(g), abs(b))); //过渡步长数量取为RGB红绿蓝中色差最大者
float fStep = (float)(255/3)/ (float)nSteps; //将色差步长值转换为浮点数
// Calculate the step size for each color
rStep = r/(float)nSteps;//求得各颜色分量的色差步长值
gStep = g/(float)nSteps;
bStep = b/(float)nSteps;
// Reset the colors to the starting position 将颜色设置为渐进色初始颜色
r = GetRValue(clrStart);
g = GetGValue(clrStart);
b = GetBValue(clrStart);
for (int iOnBand = 0; iOnBand < nSteps; iOnBand++) //按照RGB计算出在渐变色全程中每个颜色的实际值
{
//COLORREF color = RGB(r+rStep*iOnBand, g + gStep*iOnBand, b + bStep *iOnBand);
COLORREF color = RGB(b + bStep *iOnBand, g + gStep*iOnBand,r+rStep*iOnBand);
long lIndex = (int)(iOnBand * fStep);
if(lIndex+((iColor-1)*85) < 255)
m_pPalletteBuffer[lIndex+((iColor-1)*85)] = color; //计算结果放到调色板里面去
}
}
// Step on the second color a little bit...向终止颜色过渡,以下颜色生成的内容与上面基本一致。
clrStart = m_FireColors[0];
clrEnd = m_FireColors[1];
for(int kj=0;kj<m_iFlameHeight;kj++)
m_pPalletteBuffer[kj] = 0;
int r, g, b; // First distance, then starting value
float rStep, gStep, bStep; // Step size for each color
// Get the color differences
r = (GetRValue(clrEnd) - GetRValue(clrStart));
g = (GetGValue(clrEnd) - GetGValue(clrStart));
b = (GetBValue(clrEnd) - GetBValue(clrStart));
int nSteps = max(abs(r), max(abs(g), abs(b)));
float fStep = (float)(85-m_iFlameHeight)/ (float)nSteps;
// Calculate the step size for each color
rStep = r/(float)nSteps;
gStep = g/(float)nSteps;
bStep = b/(float)nSteps;
// Reset the colors to the starting position
r = GetRValue(clrStart);
g = GetGValue(clrStart);
b = GetBValue(clrStart);
for (int iOnBand = 0; iOnBand < nSteps; iOnBand++)
{
//COLORREF color = RGB(r+rStep*iOnBand, g + gStep*iOnBand, b + bStep *iOnBand);
COLORREF color = RGB(b + bStep *iOnBand, g + gStep*iOnBand,r+rStep*iOnBand);
long lIndex = (int)(iOnBand * fStep);
m_pPalletteBuffer[lIndex+(85-m_iFlameHeight)] = color; //填写颜色值
}
}
// Macro to get a random integer within a specified range */ 这是一个按照范围取随机数的宏
#define getrandom( min, max ) (( rand() % (int)((( max ) + 1 ) - ( min ))) + ( min ))
#include <time.h>
void CFireRoutine::SetHotSpots() //设置燃烧点
{
ClrHotSpots(); //首先清除燃烧点
//m_iAvgFlameWidth
long lPosition = 0; //按照横轴位置进行累加,直到抵达宽度界限
while(lPosition < m_iWidth)
{
// see if we should do a flame
if (getrandom(0,100) < m_iFireChance) //得到一个随机数,看看是不是在燃烧概率范围内,如果不是就跳过去
{
// get a flame width
long lFlameWidth = getrandom(1,m_iAvgFlameWidth); //随机取得一个火焰宽度
for(int i=0;i<lFlameWidth;i++)
{
if(lPosition < m_iWidth) //如果位置在屏幕范围内,设置一个燃烧点
{
m_pFireBits[m_pYIndexes[m_iFireSource]+lPosition] =254;// set a hot spot!
lPosition++;
}
}
}
lPosition++;
}
// for (x = 0; x < m_iWidth; x++)
// {
// if (getrandom(0,100) < m_iFireChance)
// {
// }
// }
}
void CFireRoutine::MakeLines() //生成渲染扫描线
{
int x, y;
for (x = 0; x < m_iWidth; x++) //横向循环,从屏幕左侧到右侧
{
for (y = m_iFireSource; y<m_iHeight-1;y++) //纵向循环,从火源到火焰高度
// for (y = m_iHeight; y > m_iFireSource; y--)
{
//m_pFireBits[m_pYIndexes[y-1]+x] =Average(x,y);
m_pFireBits[m_pYIndexes[y+1]+x] =Average(x,y); //火焰值为扫描线均值
}
}
}
unsigned char CFireRoutine::Average(int x, int y)
{
unsigned char ave_color;
unsigned char ave1, ave2, ave3, ave4, ave5, ave6, ave7;
// Make sure we are not at the last line... 只要不是扫描线最后一样,平均值按照下列方式计算。
if(y == m_iHeight)
ave1 = m_pFireBits[m_pYIndexes[y-1] + x];
else
ave1 = m_pFireBits[m_pYIndexes[y + 1] + x];
//扫描线均值的方法,以x,y为中心,小半径为1,大半径为2的横向椭圆,从这个范围内取得颜色,然后求均值:
//基本上是下面这个图的样式:格式:取点编号(坐标)
/*
1#(x, y+1)
6#(x-2, y) 4#(x-1, y) 7#(x,y) 3#(x+1, y) 5#(x+2, y)
2#(x, y-1)
*/
ave2 = m_pFireBits[m_pYIndexes[y - 1] + x];
ave3 = m_pFireBits[m_pYIndexes[y] + x + 1];
ave4 = m_pFireBits[m_pYIndexes[y] + x - 1];
ave5 = m_pFireBits[m_pYIndexes[y] + x + 2];
ave6 = m_pFireBits[m_pYIndexes[y] + x - 2];
ave7 = m_pFireBits[m_pYIndexes[y] + x];
ave_color = (ave1 + ave2 + ave3 + ave4 + ave5 + ave6 + ave7) / 7;
//求出颜色均值并返回
return(ave_color);
}
//按照视频内存进行渲染
void CFireRoutine::Render(DWORD* pVideoMemory,
int iwidth,
int iheight
)
{
SetHotSpots(); // generate random hotspots 产生燃烧点
MakeLines(); // do all the math and screen updating //产生扫描线,更新屏幕
// Right now Im just going to blit it right onto the video memory //向视频内存做BitBlt缓冲拷贝
unsigned char* pSrcBitlin;// = m_pFireBits+(m_iWidth*3);// get rid of our fire source //获取火源
BYTE *dst;//=(BYTE*)Dib->pVideoMemory;
BYTE r;
BYTE g;
BYTE b;
for(int i=0;i<m_iHeight-3;i++) //逐行扫描
{
dst = (BYTE*)&pVideoMemory[(iwidth*i)]; //取得视频当前行
pSrcBitlin =&m_pFireBits[m_pYIndexes[i+3]]; //设置扫描线数据指针
for(int x=0;x<m_iWidth;x++)
{
//合成颜色,注意,是索引颜色取RGB分量后加上深度和饱和度,移位
r = GetRValue(m_pPalletteBuffer[pSrcBitlin[x]]);
g = GetGValue(m_pPalletteBuffer[pSrcBitlin[x]]);
b = GetBValue(m_pPalletteBuffer[pSrcBitlin[x]]);
dst[0]=(BYTE)(((r-dst[0])*m_iAlpha+(dst[0]<<8))>>8);
dst[1]=(BYTE)(((g-dst[1])*m_iAlpha+(dst[1]<<8))>>8);
dst[2]=(BYTE)(((b-dst[2])*m_iAlpha+(dst[2]<<8))>>8);
dst+=4;
}
}
}
关于岩浆和水波的源代码,概念差不多,都是首先建立颜色模型、然后匹配到缓冲中去,仔细对比一下就看懂了
C. 求一个C语言问题:我的代码如下,这样的题有专门的算法吗
似乎程序里有问题
//1.没看到b的初始化
// 2. printf里面,x也是变量,不是字符常量
写个很直接的文字算法给你
//a[ ]是应到课人员名单,b[ ]是实际到课人员名单的话
//对于每一个a[i]
//应该是看a[i]是否在b[]里出现对吧。也就是说,在实际到课名单里b[ ]中搜索一个应该到课的学生a[i],如果找到,则不用列出这个名字,否则输出这个名字。而且需要每个跷课的人(b中找不到的人)都输出。
//所以:你的两层循环变量没有问题
//但是进入循环前 需要一个bool值NOTFound作为找到与否的标志,默认为真。
// 因为要和每一个b中的值进行比较,所以不到最后不知道是否缺课,
// 如果某个b[j]和a[i]相同
// 则置NOTFound为假,否则不变。
// 内层循环结束后,
// 如果NOTFound为真,输出这个名字,即输出a[i]
// 然后重置NOTFound为真.
如果不进行预排列,这个需要查找每个元素的算法,时间复杂度是O(n*n),双循环嘛,呵呵 。
你如果用open hash表一开始就对输入的数据b[]进行排序和优化的话,比如前面有人说到的按首字母建立静态数组,第0位代表字母a,第1位代表字母b,以此类推。然后首字母相同的单词链接在数组对应的位置后面,那么查找部分的效率可以变成接近O(n)。但是前面需要一些辅助函数,比如把字符转化为数值,以及对节点的定义和链表的操作。
D. c语言编程题任意输入一个长方形的长度与宽度值,编程求其面积。要求长,宽均为整型
依次在计算机中输入以下代码:
#include <stdio.h>void main(){int x,y;printf("请输入长:\n");scanf("%d",&x);printf("请输入宽:\n");scanf("%d",&y);
printf("周长: %d\n",2*(x+y));printf("面积: %d\n",x*y);}以上就是整个长方形周长面积求解过程。
E. 火焰纹章 火焰纹章多拉基亚776这个游戏的c语言代码是多少麻烦网友帮我找一下好吗谢谢!
这个不是商业机密嘛……虽然年代久远,但只要任天堂还正常就不会公开代码的吧……
再说了,不一定是用C编的,原游戏只有4MB,用C的话不止这点吧
战斗流程可以参考一下游戏的计算公式和各种武器相克、地形修正等等,大致就推出战斗判定时调用的函数了吧
玩家AI行动时用的大概是A*算法等等寻路算法,AI的行动规则大概就是评分值,按照评分的高低选择行动的模式,比如将攻击主角记为9分,攻击较弱的角色10分之类
还有最最重要的乱数表,就是从乱数表中取出一个随机数,从而判定命中、升级成长之类的……
总之分析一下的话,整个系统就能大致推敲出来,真要源代码很抱歉,真没有。
F. 求c语言 算法 求大神的代码
说白了这道题是数学题,首先要知道位数越多,值越大,那么也就是说在两个加号之间的位数越小,值就越小。
假设串的长度为size
那么1:如果M+1被串的长度整除,这个最简单,直接把这个串平分分成M+1分,在中间加入+号,也就是在size/(M+1)+(size/(M+1)+1)*i, i=0,1,2...处插入加号
但是不是所有的M+1都能被size整除,在不能被整除的情况下,先算出size/(M+1)的值,假设等于m1,再算出size%M的值,假设等于m2.这时候这个算式得到一个值,这种情况初始就是把加号加在m1+(m1+1)*i处,
for(j=0,j<m2,++j)
{
找出最小的一个数,将该数右边的加号依次右移一位}
大致逻辑应该是这样的吧
G. C语言算法问题,求解大佬解决,给出代码
#include "stdafx.h"
#include <stdio.h>
//定义矩阵大小
#define ROWCNT 3 //行
#define COLCNT 4 //列
typedef struct{
int row; //行,-1表表示未定义,0表示第一行
int col; //列,-1表表示未定义,0表示第一列
int val; //值
} Elem;
extern Elem A[ROWCNT*COLCNT];
extern Elem B[ROWCNT*COLCNT];
extern Elem Sum[ROWCNT*COLCNT];
void input(Elem *Data){
int row,col,val;
int cnt=0;
//先对数组初始化
for(cnt=0;cnt<ROWCNT*COLCNT;cnt++){
Data[cnt].row=-1;
Data[cnt].col=-1;
Data[cnt].val=0;
}
cnt=0;
do{
scanf("%d %d %d",&row,&col,&val);
if (row>=0){
Data->row=row;
Data->col=col;
Data->val=val;
Data++;//指针后移到下一个元素
cnt++;
}
}while((row>=0)&&(cnt<ROWCNT*COLCNT));
}
int Find(Elem *Data,int row,int col){
int result=0;
while(Data->row>=0){
if ((Data->row==row)&&(Data->col==col)){
result=Data->val;
break;
}
Data++;
}
return(result);
}
void ShowMatrix(Elem *A){
int i,j;
for(i=0;i<ROWCNT;i++){
for(j=0;j<COLCNT;j++){
printf("%d ",Find(A,i,j));
}
printf("\n");
}
}
void AddMatric(Elem *DataA,Elem *DataB,Elem *DataSum){
int i,j,k;
for(i=0;i<ROWCNT;i++){
for(j=0;j<COLCNT;j++){
k=Find(DataA,i,j)+Find(DataB,i,j);
if(k!=0){
DataSum->row=i;
DataSum->col=j;
DataSum->val=k;
DataSum++;
}
}
}
}
int main(int argc, char* argv[]){
printf("Input Matrix A \n");
printf("Format: Row Col Value\n");
printf("when Row=-1 to end\n");
input(A);
printf("---Matrix A is ----\n");
ShowMatrix(A);
printf("\nInput Matrix B \n");
printf("Format: Row Col Value\n");
printf("when Row=-1 to end\n");
input(B);
printf("---Matrix B is ----\n");
ShowMatrix(B);
AddMatric(A,B,Sum);
printf("---Matrix Sum is ----\n");
ShowMatrix(Sum);
scanf("%d",&i);//等待输入一个数字,方便在调试时观察结果,否则会运行完成直接退出,不容易看到输出
return 0;
}
H. C语言问题,编写一个程序计算矩形的面积和周长
1.代码参考:(边长可以是整数也可以是小数;实现乘法的运算符是*)
(8)火焰面积特性增长算法c语言代码扩展阅读
1.结构类型是在程序中定义的类型,以指定记录的格式,它包括成员名称和类型,以及成员在内存中的存储次序。
2.一旦定义了结构类型,就可以像使用其他所有类型一样使用这种结构类型,可以声明具有这种结构类型的对象,定义指向这种对象的指针,以及定义具有这种结构类型元素的数组。
3.结构类型的定义从关键字 struct 开始,大括号内包含声明结构成员的列表:struct [标签名称] {成员声明列表};
4.结构必须包含至少一个成员。下面的例子定义了 struct Date 类型,它有 3 个 short 类型的成员:struct Date { short month, day, year; };
5.标识符Date是该结构类型的标签(tag)。标识符 year、month 和 day 是成员名称。
6.结构类型的标签属于一个不同的命名空间:即使结构标签与变量名或函数名相同,编译器也仍然可以区分。类似地,对于每个结构类型,其中的每个结构成员名称都属于不同的命名空间。
7.结构的成员,可以定义为任何所需的完整类型,包括之前已定义的结构类型。但是不能是长度可变的数组,或者指向长度可变数组的指针。
I. 解释一下这段C语言代码的具体算法 解释的好的追加分啊!
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void encrypt(char *in, char *pwd, char *out)
//简单说这个函数是将文件in复制到文件out,中间使用pwd密码编码
{
FILE *infile, *outfile;
char ch;
int i=0, PwdLen=0;
if ((infile=fopen(in, "rb"))==NULL) //判断文件存在创建
{
printf("无法打开 %s\n", in);
getch();
exit(1);
}
if ((outfile=fopen(out, "wb"))==NULL) //判断文件存在创建
{
printf("无法打开或创建 %s\n", out);
getch();
exit(1);
}
while (pwd[PwdLen++]); //这里用来应付密码为空的情况
PwdLen--;
ch=fgetc(infile);
while (!feof(infile))
{
fputc(ch^pwd[i>=PwdLen?i=0:i++], outfile); //读文件in内容编码后写到out
ch=fgetc(infile);
}
fclose(infile);
fclose(outfile);
}
int main(int argc, char *argv[])
{
char in[256];
char out[256];
char pwd[256];
if (argc!=4)
{
printf("要加/解密的文件:\n");
gets(in);
printf("密码:\n");
gets(pwd);
printf("输出文件:\n");
gets(out);
encrypt(in, pwd, out);
}
else encrypt(argv[1], argv[2], argv[3]);
}
J. 如何用C语言制作一个3D的动态火焰效果
嗯,我来说两句。
C语言是可以实现火焰粒子特效的
你的创作思路是:在网上搜集关于火焰粒子特效的文章,比如网络文库,新浪文库、
然后着手编程
编程要注意,既然是C,你可以包含DirectX的库,然后调用别人写好的库函数实现一些基本功能,比如画点,上色,定时,Z缓存,你可以搜directx的使用说明,多得很
动态火焰效果是游戏编程的一部分,额。。涉及挺多的东西,代码无法给你,抱歉