⑴ 數據結構與演算法C語言的一段代碼
struct 不能這樣賦值吧
⑵ c語言如何運算面積,周長
代碼如下:
#include<stdio.h>
int main()
{
float a,b,c,d;
scanf("%f %f",&a,&b);//輸入長和寬
c=a*b;
d=2*(a+b);
printf("S=%.2f L=%.2f
",c,d);//S是面積,L是周長
return 0;
}
(2)火焰面積特性增長演算法c語言代碼擴展閱讀:
C語言是一門通用計算機編程語言,廣泛應用於底層開發。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。
盡管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標准規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
二十世紀八十年代,為了避免各開發廠商用的C語言語法產生差異,由美國國家標准局為C語言制定了一套完整的美國國家標准語法,稱為ANSI C,作為C語言最初的標准。[1]目前2011年12月8日,國際標准化組織(ISO)和國際電工委員會(IEC)發布的C11標準是C語言的第三個官方標准,也是C語言的最新標准,該標准更好的支持了漢字函數名和漢字標識符,一定程度上實現了漢字編程。
C語言是一門面向過程的計算機編程語言,與C++,Java等面向對象的編程語言有所不同。
⑶ 設計C語言程序,游戲規則:21根火柴,一次只能拿走1或2或3或4根,不能不拿不能棄權,人與電腦比賽。
#include<stdio.h>
int f(int n){
switch(n){
case 0:return 4;
case 4:return 3;
case 3:return 2;
case 2:return 1;
}
return 0;
}
void main(){
int m=21,n;
do{
scanf("%d",&n);
m-=n;
n=f(m%5);
printf ("%d\n",n);
m-=n;
}while(m>1);
}
其實有更簡單的,如下:
#include<stdio.h>
void main(){
int n;
do{
scanf("%d",&n);
printf ("%d\n",5-n);
}while(1);
}
⑷ C語言的問題!十萬火急!
你的程序有問題,Merge(int a[],int m,int b[],int n)這個函數里的c[],是新定義的,對主函數的c【】不會產生影響。應該把c【】,最為一個參數傳遞給Merge函數。
一下是修改過的程序,運行過了沒有任何問題,有什麼問題可以交流下。
#include <stdio.h>
#define M 5
#define N 5
void main()
{
int i;
int a[M],b[N],c[M+N];
void Merge(int a[],int m,int b[],int n,int c[]);
printf("請輸入一個序列:\n");
for(i=0;i<M;i++)
scanf("%d",&a[i]);
printf("請輸入一個序列:\n");
for(i=0;i<N;i++)
scanf("%d",&b[i]);
Merge(a,M,b,N,c);
for(i=0;i<M+N;i++)
printf("%d, ",c[i]);
printf("\n");
}
void Merge(int a[],int m,int b[],int n,int c[])
{
int i=0,j=0,k=0;
while(i<m&&j<n)
{
if(a[i]<=b[j])
{
c[k]=a[i];
i++; k++;
}
else
{
c[k]=b[j];
j++; k++;
}
}
while(j<n)
{
c[k++]=b[j++];
}
while(i<m)
{
c[k++]=a[i++];
}
}
⑸ 如何用C語言製作一個3D的動態火焰效果
嗯,我來說兩句。
C語言是可以實現火焰粒子特效的
你的創作思路是:在網上搜集關於火焰粒子特效的文章,比如網路文庫,新浪文庫、
然後著手編程
編程要注意,既然是C,你可以包含DirectX的庫,然後調用別人寫好的庫函數實現一些基本功能,比如畫點,上色,定時,Z緩存,你可以搜directx的使用說明,多得很
動態火焰效果是游戲編程的一部分,額。。涉及挺多的東西,代碼無法給你,抱歉
⑹ 兩道編程演算法題(兩圖一道),題目如下,可以給出演算法思路或者源代碼,源代碼最好是C語言的
就會個第一題(因為第一題上已經給出了大致思路)
思路:用map容器(它的內部數據結構是一顆紅黑樹,查找和插入數據速度非常快)
map<int,st>a;//key(int):設置為1~n的數;value(st):設置為key的前驅和後繼;
這樣一來就可以像鏈錶快速插入數據,又可以像數組隨機訪問元素(key,就相當於數組的下標)
下面是代碼和運行截圖;
看代碼前建議先了解一下map容器的具體用法;
#include<iostream>
#include<map>
#include<vector>
using namespace std;
struct st{//兩個成員變數用來儲存前驅和後繼
int left;//0
int right;//1
st()
{
left=0;
right=0;
}
};
void input(map<int,st> &a)//輸出
{
st t;
int s=0;
map<int,st>::iterator it;//迭代器(指針)
for(it=a.begin();it!=a.end();it++)//循環迭代
{
t=it->second;
if(t.left==0)//左邊等於0,說明該數是第一個數
{
s=it->first;//記錄key
break;
}
}
t=a[s];
cout<<s<<" ";
while(t.right!=0)//循環找當前數的右邊的數(右邊的為0,就說明該數是最後一個數)
{
cout<<t.right<<" ";
t=a[t.right];
}
}
int main()
{
st t,t_i,t_x,t_k,s;
map<int,st>a;
map<int,st>::iterator it;
int n,x,p,x_l,x_r;
cin>>n;
for(int i=1;i<=n;i++)//map容器賦初值(i,t)
//i:(key)下標;t:(value)結構體變數
{
a.insert(make_pair(i,t));
}
for(int i=2;i<=n;i++)
{
cin>>x>>p;
if(p==0)//x的左邊插入i
{
t=a[x];
if(t.left==0)//x的左邊沒有數
{
t_x.left=i;
t_i.right=x;
a[x]=t_x;
a[i]=t_i;
}
else//x的左邊有數
{
int x_left;
t_x=a[x];
x_left=t_x.left;
t_k=a[x_left];
t_i.right=x;
t_i.left=t_x.left;
t_k.right=i;
t_x.left=i;
a[x]=t_x;
a[i]=t_i;
a[x_left]=t_k;
}
}
else//x的右邊插入i
{
t=a[x];
if(t.right==0)//x的右邊沒有數
{
t_x.right=i;
t_i.left=x;
a[x]=t_x;
a[i]=t_i;
}
else//x的左邊有數
{
int x_right;
t_x=a[x];
x_right=t_x.right;
t_k=a[x_right];
t_i.left=x;
t_i.right=t_x.right;
t_k.left=i;
t_x.right=i;
a[x]=t_x;
a[i]=t_i;
a[x_right]=t_k;
}
}
}
for(it=a.begin();it!=a.end();it++)//循環迭代列印各個數之間的關系
{
cout<<it->first<<" ";
cout<<"左邊:";
cout<<it->second.left<<" ";
cout<<"右邊:";
cout<<it->second.right<<endl;
}
input(a);//列印序列
return 0;
}
/*
4
1 0
2 1
1 0
2 3 4 1
*/
⑺ 如何在VC++6.0里進行C語言程序設計(十萬火急!))
打開以後的操作步驟
new
->win32
console
application(在右邊的location裡面選要放的路徑,在project
name裡面寫要起的工程名字)->OK->點Finish
新建了一個空的控制台應用程序的工程;
接下來添加C語言文件,
new->C++
source
file(在右邊的file裡面填寫要建立的C語言文件名,注意要把擴展名寫上,如建立的是main.c不要寫main,否則系統默認的是C++文件);
建好以後就可以編寫程序了
程序寫好以後編譯運行可以選擇buile->excute
工程名.exe
沒有錯誤的話可以運行出結果
有錯誤的話會給出相應的提示....
⑻ 如何使用c語言編寫計算圓面積的程序
1、首先打開DEV C++軟體,在編輯頁面輸入以下代碼,如下圖所示。
⑼ 幫我解釋下這段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語言編寫學生信息管理系統,十萬火急!
可以參考
#include "stdio.h" /*I/O函數*/
#include "stdlib.h" /*其它說明*/
#include "string.h" /*字元串函數*/
#include "conio.h" /*屏幕操作函數*/
#include "mem.h" /*內存操作函數*/
#include "ctype.h" /*字元操作函數*/
#include "alloc.h" /*動態地址分配函數*/
struct score
{
int mingci;
char xuehao[8];
char mingzi[20];
float score[6];
}data,info[1000];
int i,j,k=0;
char temp[20],ch;
FILE *fp,*fp1;
void shuru()
{
if((fp=fopen("s_score.txt","ab+"))==NULL)
{
printf("cannot open this file.\n");
getch();exit(0);
}
for(i=0;i<=1000;i++)
{
printf("\nPlease shuru xuehao:");
gets(data.xuehao);
printf("Please shuru mingzi:");
gets(data.mingzi);
printf("Please shuru yuwen score:");
gets(temp);data.score[0]=atof(temp);
printf("Please shuru shuxue score:");
gets(temp);data.score[1]=atof(temp);
printf("Please input yingyu score:");
gets(temp);data.score[2]=atof(temp);
printf("Please shuru wuli score:");
gets(temp);data.score[3]=atof(temp);
printf("Please shur huaxue score:");
gets(temp);data.score[4]=atof(temp);
data.score[5]=data.score[0]+data.score[1]+data.score[2]+data.score[3]+data.score[4];
fwrite(&data,sizeof(data),1,fp);
printf("another?y/n");
ch=getch();
if(ch=='n'||ch=='N')
break;
} fclose(fp);
}
void xianshi()
{
float s;int n;
if((fp=fopen("s_score.txt","rb+"))==NULL)
{
printf("Cannot reading this file.\n");
exit(0);
}
for(i=0;i<=1000;i++)
{
if((fread(&info[i],sizeof(info[i]),1,fp))!=1)
break;
}
printf("\nxuehao mingzi yuwen shuxue yingyu wuli huauxue zhongfen\n");
for(j=0,k=1;j<i;j++,k++)
{
info[j].mingci=k;
printf("%6s %8s %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f\n",info[j].xuehao,info[j].mingzi,info[j].score[0],info[j].score[1],info[j].score[2],info[j].score[3],info[j].score[4],
info[j].score[5]);
}
getch();
fclose(fp);
}
void xiugai()
{
if((fp=fopen("s_score.txt","rb+"))==NULL||(fp1=fopen("temp.txt","wb+"))==NULL)
{
printf("Cannot open this file.\n");
exit(0);
}
printf("\nPLease shuru xiugai xuehao:");
scanf("%d",&i); getchar();
while((fread(&data,sizeof(data),1,fp))==1)
{
j=atoi(data.xuehao);
if(j==i)
{
printf("xuehao:%s\nmingzi:%s\n",data.xuehao,data.mingzi);
printf("Please shuru mingzi:");
gets(data.mingzi);
printf("Please shuru yuwen score:");
gets(temp);data.score[0]=atof(temp);
printf("Please shuru shuxue score:");
gets(temp);data.score[1]=atof(temp);
printf("Please input yingyu score:");
gets(temp);data.score[2]=atof(temp);
printf("Please input wuli score:");
gets(temp);data.score[3]=atof(temp);
printf("Please input huaxue score:");
gets(temp);data.score[4]=atof(temp);
data.score[5]=data.score[0]+data.score[1]+data.score[2]+data.score[3]+data.score[4];
} fwrite(&data,sizeof(data),1,fp1);
}
fseek(fp,0L,0);
fseek(fp1,0L,0);
while((fread(&data,sizeof(data),1,fp1))==1)
{
fwrite(&data,sizeof(data),1,fp);
}
fclose(fp);
fclose(fp1);
}
void chazhao()
{
if((fp=fopen("s_score.txt","rb"))==NULL)
{
printf("\nCannot open this file.\n");
exit(0);
}
printf("\nPLease shuru xuehao chakan:");
scanf("%d",&i);
while(fread(&data,sizeof(data),1,fp)==1)
{
j=atoi(data.xuehao);
if(i==j)
{
printf("xuehao:%s mingzi:%s\nyuwen:%f\n shuxue:%f\n yingyu:%f\n wuli:%f\n huaxue:%f\n ",data.xuehao,data.mingzi,data.score[0],data.score[1],data.score[2],data.score[3],data.score[4],data.score[5]);
}getch();
}
}
void shanchu()
{
if((fp=fopen("s_score.txt","rb+"))==NULL||(fp1=fopen("temp.txt","wb+"))==NULL)
{
printf("\nopen score.txt was failed!");
getch();
exit(0);
}
printf("\nPlease input ID which you want to del:");
scanf("%d",&i);getchar();
while((fread(&data,sizeof(data),1,fp))==1)
{
j=atoi(data.xuehao);
if(j==i)
{
printf("Anykey will delet it.\n");
getch();
continue;
}
fwrite(&data,sizeof(data),1,fp1);
}
fclose(fp);
fclose(fp1);
remove("s_score.txt");
rename("temp.txt","s_score.txt");
printf("Data delet was succesful!\n");
printf("Anykey will return to main.");
getch();
}
main()
{
while(1)
{
clrscr(); /*清屏幕*/
gotoxy(1,1); /*移動游標*/
textcolor(YELLOW); /*設置文本顯示顏色為黃色*/
textbackground(BLUE); /*設置背景顏色為藍色*/
window(1,1,99,99); /* 製作顯示菜單的窗口,大小根據菜單條數設計*/
clrscr();
printf("*************welcome to use student manage******************\n");
printf("*************************menu********************************\n");
printf("* ========================================================= * \n");
printf("* 1>shuru 2>xiugai * \n");
printf("* 3>shanchu 4>chazhao * \n");
printf("* 5>xianshi 6>exit * \n");
printf("* * \n");
printf("* --------------------------------------------------------- * \n");
printf(" Please input which you want(1-6):");
ch=getch();
switch(ch)
{
case '1':shuru();break;
case '2':xiugai(); break;
case '3':shanchu(); break;
case '4':chazhao(); break;
case '5':xianshi(); break;
case '6':exit(0);
default: continue;
}
}
}