Ⅰ 一個簡單的c++程序 輸入糖果的名稱重量卡路里 然後輸出
汗,剛才我看沒圖來著,格式控制你自己嘗試下吧,就該加\t的加,該加\n就加\n,呵呵...下次如果能有分數獎勵就更好了,哈哈
#include<iostream>
#include<string>
using
namespace
std;
void
main()
{
string
name[10];
string
s;
char
c;
double
m,k;
c,m,k
=0;
while
(c!='n')
{
cout
<<
"請輸入糖果名稱"<<endl;
cin
>>
s;
cout
<<
"請輸入糖果重量(單位:千克)"<<endl;
cin
>>
m;
cout
<<
"請輸入糖果卡路里(單位:焦耳)"<<endl;
cin
>>
k;
cout
<<
"
糖果:
"<<
s
<<endl;
cout
<<
"
重量:
"<<
m
<<"千克"<<endl;
cout
<<
"
卡路里:"<<
k
<<"焦耳"<<endl;
cout
<<
"輸入n退出,輸入任意數繼續"<<endl;
cin>>c;
}
}
Ⅱ 用c語言編寫程序:輸入體重和身高,輸出體重指數BMI
例子如下:
知識擴展:
BMI指數(即身體質量指數,簡稱體質指數又稱體重,英文為Body Mass Index,簡稱BMI),是用體重公斤數除以身高米數平方得出的數字,是目前國際上常用的衡量人體胖瘦程度以及是否健康的一個標准。主要用於統計用途,當我們需要比較及分析一個人的體重對於不同高度的人所帶來的健康影響時,BMI值是一個中立而可靠的指標。
BMI值原來的設計是一個用於公眾健康研究的統計工具。當我們需要知道肥胖是否對某一疾病的致病原因時,我們可以把病人的身高及體重換算成BMI值,再找出其數值及病發率是否有線性關聯。不過,隨著科技進步,現時BMI值只是一個參考值。要真正量度病人是否肥胖,還需要利用微電力量度病人的阻抗,以推斷病者的脂肪厚度。因此,BMI的角色也慢慢改變,從醫學上的用途,變為一般大眾的纖體指標。
Ⅲ C語言編程問題
#include"stdio.h"
typedefstruct
{
char*name;
doubleweight;
intcalorie;
}CandyBar;
voidShow_CandyBar(CandyBar*Candies)
{
intMode=0;
printf("是否顯示默認值? ");
printf("輸入0顯示默認值,輸入1或其他數字自定義輸入 ");
scanf_s("%d",&Mode);
if(Mode)
{
printf("請輸入糖果名:");
//這里寫scanf字元串賦值糖果名
printf("請輸入糖果質量:");
scanf_s("%lf",&Candies->weight);
printf("請輸入糖果卡路里值:");
scanf_s("%d",&Candies->calorie);
}
printf("name:%s ",Candies->name);
printf("weight:%lf ",Candies->weight);
printf("calorie:%d ",Candies->calorie);
}
intmain()
{
CandyBarCandies;
Candies.name="MillenniumMunch";
Candies.weight=2.85;
Candies.calorie=350;
Show_CandyBar(&Candies);
return0;
}
Ⅳ C語言編寫代碼
代碼:
#include
int main()
{
printf("Hello World.
");
return 0;
}
(4)c語言編寫卡路里代碼擴展閱讀:
代碼解析:
1.其中第一行的#include ,這行代碼是編譯預處理指令,用來提供輸入輸出函數的聲明和宏的定義,全局量的定義這些信息。stdio.h是系統提供的一個文件名,stdio是為standard input & output的縮寫,意為標准輸入輸出,文件後綴.h的意思是頭文件(header file),因為這些文件都是放在程序各文件模塊的開頭的。總的來說,輸入輸出函數的相關信息已事先放在stdio.h文件中,然後,要用#include指令把這些信息調入供使用。
2.void main(){},main是主函數的函數名,表示這是一個主函數,每一個C源程序都必須有且只能有一個主函數。
3.函數調用語句,即調用printf("Hello World
");語句 ,printf函數的功能是把要輸出的內容送到顯示器去顯示,即列印輸出Hello World到windows 控制台下,其中
為轉義字元,具有轉行的功能。
4.printf函數是一個由系統定義的標准函數,可在程序中直接調用。
Ⅳ 用C語言編寫一段代碼
#include<stdio.h>
char a[1024];
char b[1024];
void main()
{
char a[1024];
char b[1024];
char *p=a,*q=b;
printf("Please input text:\n");
gets(a);
while(*p)
{
switch(*p)
{
case 'a':
p++;
switch(*p)
{
case 'n':
p++;
switch(*p)
{
case 'd':p++;break;
default:*q++=*p++;break;
}
break;
default:*q++=*p++;break;
}
break;
case 't':
*q++=*p++;
switch(*p)
{
case 'e':
*q++=*p++;
switch(*p)
{
case 'x':
*q++=*p++;
switch(*p)
{
case 't':q-=3;p++;break;
default:*q++=*p++;break;
}
break;
default:*q++=*p++;break;
}
break;
default:*q++=*p++;break;
}
break;
default:*q++=*p++;break;
}
}
*q='\0';
printf("..............\n");
printf("%s\n", b);
getch();
}
Ⅵ 求一個用C語言編寫的小游戲代碼
#include <graphics.h>
#include <conio.h>
#include <time.h>
/////////////////////////////////////////////
// 定義常量、枚舉量、結構體、全局變數
/////////////////////////////////////////////
#define WIDTH 10 // 游戲區寬度
#define HEIGHT 22 // 游戲區高度
#define SIZE 20 // 每個游戲區單位的實際像素
// 定義操作類型
enum CMD
{
CMD_ROTATE, // 方塊旋轉
CMD_LEFT, CMD_RIGHT, CMD_DOWN, // 方塊左、右、下移動
CMD_SINK, // 方塊沉底
CMD_QUIT // 退出遊戲
};
// 定義繪制方塊的方法
enum DRAW
{
SHOW, // 顯示方塊
HIDE, // 隱藏方塊
FIX // 固定方塊
};
// 定義七種俄羅斯方塊
struct BLOCK
{
WORD dir[4]; // 方塊的四個旋轉狀態
COLORREF color; // 方塊的顏色
} g_Blocks[7] = { {0x0F00, 0x4444, 0x0F00, 0x4444, RED}, // I
{0x0660, 0x0660, 0x0660, 0x0660, BLUE}, // 口
{0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA}, // L
{0x2260, 0x0E20, 0x0644, 0x0470, YELLOW}, // 反L
{0x0C60, 0x2640, 0x0C60, 0x2640, CYAN}, // Z
{0x0360, 0x4620, 0x0360, 0x4620, GREEN}, // 反Z
{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}}; // T
// 定義當前方塊、下一個方塊的信息
struct BLOCKINFO
{
byte id; // 方塊 ID
char x, y; // 方塊在游戲區中的坐標
byte dir:2; // 方向
} g_CurBlock, g_NextBlock;
// 定義游戲區
BYTE g_World[WIDTH][HEIGHT] = {0};
/////////////////////////////////////////////
// 函數聲明
/////////////////////////////////////////////
void Init(); // 初始化游戲
void Quit(); // 退出遊戲
void NewGame(); // 開始新游戲
void GameOver(); // 結束游戲
CMD GetCmd(); // 獲取控制命令
void DispatchCmd(CMD _cmd); // 分發控制命令
void NewBlock(); // 生成新的方塊
bool CheckBlock(BLOCKINFO _block); // 檢測指定方塊是否可以放下
void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW); // 畫方塊
void OnRotate(); // 旋轉方塊
void OnLeft(); // 左移方塊
void OnRight(); // 右移方塊
void OnDown(); // 下移方塊
void OnSink(); // 沉底方塊
/////////////////////////////////////////////
// 函數定義
/////////////////////////////////////////////
// 主函數
void main()
{
Init();
CMD c;
while(true)
{
c = GetCmd();
DispatchCmd(c);
// 按退出時,顯示對話框咨詢用戶是否退出
if (c == CMD_QUIT)
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("您要退出遊戲嗎?"), _T("提醒"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
Quit();
}
}
}
// 初始化游戲
void Init()
{
initgraph(640, 480);
srand((unsigned)time(NULL));
// 顯示操作說明
setfont(14, 0, _T("宋體"));
outtextxy(20, 330, _T("操作說明"));
outtextxy(20, 350, _T("上:旋轉"));
outtextxy(20, 370, _T("左:左移"));
outtextxy(20, 390, _T("右:右移"));
outtextxy(20, 410, _T("下:下移"));
outtextxy(20, 430, _T("空格:沉底"));
outtextxy(20, 450, _T("ESC:退出"));
// 設置坐標原點
setorigin(220, 20);
// 繪制游戲區邊界
rectangle(-1, -1, WIDTH * SIZE, HEIGHT * SIZE);
rectangle((WIDTH + 1) * SIZE - 1, -1, (WIDTH + 5) * SIZE, 4 * SIZE);
// 開始新游戲
NewGame();
}
// 退出遊戲
void Quit()
{
closegraph();
exit(0);
}
// 開始新游戲
void NewGame()
{
// 清空游戲區
setfillstyle(BLACK);
bar(0, 0, WIDTH * SIZE - 1, HEIGHT * SIZE - 1);
ZeroMemory(g_World, WIDTH * HEIGHT);
// 生成下一個方塊
g_NextBlock.id = rand() % 7;
g_NextBlock.dir = rand() % 4;
g_NextBlock.x = WIDTH + 1;
g_NextBlock.y = HEIGHT - 1;
// 獲取新方塊
NewBlock();
}
// 結束游戲
void GameOver()
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("游戲結束。\n您想重新來一局嗎?"), _T("游戲結束"), MB_YESNO | MB_ICONQUESTION) == IDYES)
NewGame();
else
Quit();
}
// 獲取控制命令
DWORD m_oldtime;
CMD GetCmd()
{
// 獲取控制值
while(true)
{
// 如果超時,自動下落一格
DWORD newtime = GetTickCount();
if (newtime - m_oldtime >= 500)
{
m_oldtime = newtime;
return CMD_DOWN;
}
// 如果有按鍵,返回按鍵對應的功能
if (kbhit())
{
switch(getch())
{
case 'w':
case 'W': return CMD_ROTATE;
case 'a':
case 'A': return CMD_LEFT;
case 'd':
case 'D': return CMD_RIGHT;
case 's':
case 'S': return CMD_DOWN;
case 27: return CMD_QUIT;
case ' ': return CMD_SINK;
case 0:
case 0xE0:
switch(getch())
{
case 72: return CMD_ROTATE;
case 75: return CMD_LEFT;
case 77: return CMD_RIGHT;
case 80: return CMD_DOWN;
}
}
}
// 延時 (降低 CPU 佔用率)
Sleep(20);
}
}
// 分發控制命令
void DispatchCmd(CMD _cmd)
{
switch(_cmd)
{
case CMD_ROTATE: OnRotate(); break;
case CMD_LEFT: OnLeft(); break;
case CMD_RIGHT: OnRight(); break;
case CMD_DOWN: OnDown(); break;
case CMD_SINK: OnSink(); break;
case CMD_QUIT: break;
}
}
// 生成新的方塊
void NewBlock()
{
g_CurBlock.id = g_NextBlock.id, g_NextBlock.id = rand() % 7;
g_CurBlock.dir = g_NextBlock.dir, g_NextBlock.dir = rand() % 4;
g_CurBlock.x = (WIDTH - 4) / 2;
g_CurBlock.y = HEIGHT + 2;
// 下移新方塊直到有局部顯示
WORD c = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
while((c & 0xF) == 0)
{
g_CurBlock.y--;
c >>= 4;
}
// 繪制新方塊
DrawBlock(g_CurBlock);
// 繪制下一個方塊
setfillstyle(BLACK);
bar((WIDTH + 1) * SIZE, 0, (WIDTH + 5) * SIZE - 1, 4 * SIZE - 1);
DrawBlock(g_NextBlock);
// 設置計時器,用於判斷自動下落
m_oldtime = GetTickCount();
}
// 畫方塊
void DrawBlock(BLOCKINFO _block, DRAW _draw)
{
WORD b = g_Blocks[_block.id].dir[_block.dir];
int x, y;
int color = BLACK;
switch(_draw)
{
case SHOW: color = g_Blocks[_block.id].color; break;
case HIDE: color = BLACK; break;
case FIX: color = g_Blocks[_block.id].color / 3; break;
}
setfillstyle(color);
for(int i=0; i<16; i++)
{
if (b & 0x8000)
{
x = _block.x + i % 4;
y = _block.y - i / 4;
if (y < HEIGHT)
{
if (_draw != HIDE)
bar3d(x * SIZE + 2, (HEIGHT - y - 1) * SIZE + 2, (x + 1) * SIZE - 4, (HEIGHT - y) * SIZE - 4, 3, true);
else
bar(x * SIZE, (HEIGHT - y - 1) * SIZE, (x + 1) * SIZE - 1, (HEIGHT - y) * SIZE - 1);
}
}
b <<= 1;
}
}
// 檢測指定方塊是否可以放下
bool CheckBlock(BLOCKINFO _block)
{
WORD b = g_Blocks[_block.id].dir[_block.dir];
int x, y;
for(int i=0; i<16; i++)
{
if (b & 0x8000)
{
x = _block.x + i % 4;
y = _block.y - i / 4;
if ((x < 0) || (x >= WIDTH) || (y < 0))
return false;
if ((y < HEIGHT) && (g_World[x][y]))
return false;
}
b <<= 1;
}
return true;
}
// 旋轉方塊
void OnRotate()
{
// 獲取可以旋轉的 x 偏移量
int dx;
BLOCKINFO tmp = g_CurBlock;
tmp.dir++; if (CheckBlock(tmp)) { dx = 0; goto rotate; }
tmp.x = g_CurBlock.x - 1; if (CheckBlock(tmp)) { dx = -1; goto rotate; }
tmp.x = g_CurBlock.x + 1; if (CheckBlock(tmp)) { dx = 1; goto rotate; }
tmp.x = g_CurBlock.x - 2; if (CheckBlock(tmp)) { dx = -2; goto rotate; }
tmp.x = g_CurBlock.x + 2; if (CheckBlock(tmp)) { dx = 2; goto rotate; }
return;
rotate:
// 旋轉
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.dir++;
g_CurBlock.x += dx;
DrawBlock(g_CurBlock);
}
// 左移方塊
void OnLeft()
{
BLOCKINFO tmp = g_CurBlock;
tmp.x--;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x--;
DrawBlock(g_CurBlock);
}
}
// 右移方塊
void OnRight()
{
BLOCKINFO tmp = g_CurBlock;
tmp.x++;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x++;
DrawBlock(g_CurBlock);
}
}
// 下移方塊
void OnDown()
{
BLOCKINFO tmp = g_CurBlock;
tmp.y--;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.y--;
DrawBlock(g_CurBlock);
}
else
OnSink(); // 不可下移時,執行「沉底方塊」操作
}
// 沉底方塊
void OnSink()
{
int i, x, y;
// 連續下移方塊
DrawBlock(g_CurBlock, HIDE);
BLOCKINFO tmp = g_CurBlock;
tmp.y--;
while (CheckBlock(tmp))
{
g_CurBlock.y--;
tmp.y--;
}
DrawBlock(g_CurBlock, FIX);
// 固定方塊在游戲區
WORD b = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
for(i = 0; i < 16; i++)
{
if (b & 0x8000)
{
if (g_CurBlock.y - i / 4 >= HEIGHT)
{ // 如果方塊的固定位置超出高度,結束游戲
GameOver();
return;
}
else
g_World[g_CurBlock.x + i % 4][g_CurBlock.y - i / 4] = 1;
}
b <<= 1;
}
// 檢查是否需要消掉行,並標記
int row[4] = {0};
bool bRow = false;
for(y = g_CurBlock.y; y >= max(g_CurBlock.y - 3, 0); y--)
{
i = 0;
for(x = 0; x < WIDTH; x++)
if (g_World[x][y] == 1)
i++;
if (i == WIDTH)
{
bRow = true;
row[g_CurBlock.y - y] = 1;
setfillstyle(WHITE, DIAGCROSS2_FILL);
bar(0, (HEIGHT - y - 1) * SIZE + SIZE / 2 - 2, WIDTH * SIZE - 1, (HEIGHT - y - 1) * SIZE + SIZE / 2 + 2);
}
}
if (bRow)
{
// 延時 200 毫秒
Sleep(200);
// 擦掉剛才標記的行
IMAGE img;
for(i = 0; i < 4; i++)
{
if (row[i])
{
for(y = g_CurBlock.y - i + 1; y < HEIGHT; y++)
for(x = 0; x < WIDTH; x++)
{
g_World[x][y - 1] = g_World[x][y];
g_World[x][y] = 0;
}
getimage(&img, 0, 0, WIDTH * SIZE, (HEIGHT - (g_CurBlock.y - i + 1)) * SIZE);
putimage(0, SIZE, &img);
}
}
}
// 產生新方塊
NewBlock();
}
Ⅶ 用C語言編寫一個有關順序表的程序代碼
#include<stdio.h>
#include<stdlib.h>
#define Size 50
//定義順序表結構體類型
typedef struct Table{
int*head;//順序表頭指針,為整型
int length;//順序表中元素個數
int size; //順序表的長度
}Table;
//創建順序表
Table CreateTable(){
Table T;
if(!(T.head=(int*)malloc(Size*sizeof(int)))){
printf("內存分配錯誤!\n");
exit(0);
}
T.size=Size;
T.length=0;
return T;
}
//增加
void Insert(Table T,int set,int values)//set為插入的位置,values為值
{
//首先判斷插入位置是否正確
if(set<0||set>T.length){
printf("插入位置不正確!\n");
reutrn;
}
//判斷是否有足夠的內存空間存儲增加的數
if(T.length==T.size){
if(!(T.head=(int*)realloc(T.head,(T.size+10)*sizeof(int)))){
printf("內存分配錯誤!\n");
exit(0);
}
T.size+=10;
}
//插入數據
for(int i=s.length;i>set;i--)
T.head[i]=T.head[i-1];
T.head[set]=values;
T.length++;
}
//刪除
void Delete(Tabel T,int set)
{
if(set<0||set>=T.length)
{
printf("刪除數的位置不爭確,刪除失敗!\n");
return;
}
//輸出數據
for(int i=set;i<s.length;i++)
T.head[i]=T.head[i+1];
T.length--;
}
//查找
int Search(Tabel T,int value)
{
for(int i=0;i<T.length;i++)
if(T.head[i]==value)
return i;
return -1;
}
Ⅷ C語言編寫的記事本源代碼
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define NULL 0
#define MAX 100
typedef struct lnode{
char date[MAX]; //存放數據
struct lnode * prior ; //前驅
struct lnode * next ; //後繼
int number ; //記錄一個節點的字元數!如果是頭節點就記錄他的節點個數
int quese ; //記錄節點在鏈表中的位置
}lnodetype;
lnodetype * l ; //設置兩個全局變數,分別是頭節點指針和尾節點指針
lnodetype * end ;
//**********這個函數是用來初始化的**********//
int iniatelist (lnodetype ** l , lnodetype ** end)
{
(*l) = (lnodetype *)malloc (sizeof (lnodetype) ) ;
if ( (*l) == NULL )
{
printf ("沒有隻夠的內存空間!程序即將退出!");
return 0 ;
}
(*l)->prior =(*l)->next = NULL ; //這是雙鏈表
(*l)->number = (*l)->quese = 0;
(*end) = (*l) ;
printf ("程序初始化完畢!");
return 0;
}
//**********這個函數是用來建立節點,並且插入元素的**********//
int link(lnodetype ** l, lnodetype ** end)
{
lnodetype *s ;
s = (lnodetype *)malloc ( sizeof (lnodetype) ) ;
if ( s == NULL ){
printf ("內存空間不夠,程序即將退出!") ;
return 0 ;
}
(*end)->next = s ;
s->prior = (*end) ;
(*end) = (*end)->next ;
(*l)->number++ ; //增加一個節點,頭節點的number就加1
s->quese = (*l)->number ; //這個是記錄節點在鏈表中的位置
printf ("%d行", s->quese ) ; //這個是節點在整個鏈表中的位置
gets(s->date) ;
s -> number = strlen(s->date) ;
return 0 ;
}
//**********這個是列印鏈表的函數**********//
int prin (lnodetype ** l, lnodetype ** end)
{
lnodetype * p ;
int i ;
int j = 0;
int couter = (*l)->number ;
p = (*l)->next ;
for (i=0; i < couter; i++){
printf ( "%d行" , i+1 ) ;
j = 0;
while ( ( p->date[j]>='a' && p->date[j]<='z') ||
(p->date[j]>='A' && p->date[j]<='z') ||
p->date[j]>='0' && p->date[j]<='9')
{
printf ( "%c" , p->date[j] ) ;
j++ ;
}
printf ("n") ;
p = p->next ;
}
return 0 ;
}
//*********這個查找和編輯相應行的函數**********//
int search (lnodetype ** l, lnodetype ** end )
{
int number ;
scanf ("%d" , &number) ;
int i ;
lnodetype * p ;
p = (*l)->next ;
for ( i=0; i<number-1; i++ )
p = p->next ;
printf ("%d行" , number ) ;
gets (p->date) ;
return 0 ;
}
//**********這個是在文本文件里搜索字元串的函數**********//
int searchstr(lnodetype ** l , lnodetype ** end)
{
char ptr[100] ;
int arrycouter ;
int mystrcmp( char *, char * , int ) ;
printf ( "ok!現在輸入你查找的字元串!" ) ;
scanf ( "%s" , ptr ) ;
arrycouter = strlen (ptr) ;
lnodetype * s ;
s = (*l)->next ;
char * p ;
int i = 1 ;
int couter = 0 ;
int number = (*l)->number ;
p = (char *)s->date ;
while ( i && number ){
// i=1是,證明沒有找到,向第二個節點繼續尋找
while ( i && ( ( (*p >= 'a') && (*p <= 'z') ) || ( (*p >= 'A') && (*p <= 'Z') ) ) ){
i = mystrcmp ( p , ptr , arrycouter );
if ( i == 1 ){
printf ("字元串已經在第%d行,第%d個字元開始,",s->quese, couter+1 );
i = 0 ;
}
else
{
p++ ;
couter++;
i = 1 ;
}
}
s = s->next ;
p = (char *)s->date ;
number -- ;
}
if ( i == 1 )
printf ("字元串在本文檔中不存在!");
return 0 ;
}
//**********(1)這個函數是用來實現退出不保存功能的**********//
int exitunsave(lnodetype ** l, lnodetype ** end)
{
lnodetype * s ;
while( (*l) != (*end) ){ //如果不保存的話,最好是把節點的空間都釋放,節省空間
s = (*end) ;
(*end) = (*end)->prior ;
free( s ) ;
}
return 0 ;
}
//**********(2)這個函數是用來實現退出但保存功能的**********//
int quitandsave( lnodetype ** l , lnodetype ** end )
{
FILE * fp ;
char ch ;
char filename[20] ;
lnodetype * p;
int i ;
int j ;
int couter = (*l)->number ;
p = (*l)->next ;
printf ("請輸入文件名:") ;
scanf ("%s" , filename ) ;
if ( (fp = fopen( filename , "w" )) == NULL ){
printf ("文件不能打開!n");
return 0 ;
}
for ( i=0; i<couter; i++ ){ //有幾個節點就進行多少次的存貯
ch = p->date[0] ;
j = 1 ;
while (ch != '\0'){
fputc (ch , fp) ;
ch = p->date[j] ;
j++ ;
}
p = p->next ;
fputc ( '#' , fp ) ; //注意在每個節點的後面加上結束的符號
}
fputc ( '@' , fp ) ; //整個文件關閉的標志
fclose ( fp ) ; //注意關閉文件,
return 0 ;
}
//**********由於庫函數比較字元串提供的功能不滿足要求,故自己寫了一個**********/
int mystrcmp( char * p ,char * sour ,int number )
{
while ( number && (*p) == (*sour) &&
( ( (*p >= 'a') && (*p <= 'z') ) || ( (*p >= 'A') && (*p <= 'Z') ) )){
p++ ;
sour++ ;
number-- ;
}
if ( number == 0 )
return 1 ;
else
return 0 ;
}
//**********這個函數是用來實現統計字元串功能的**********//
int coutword(lnodetype ** l , lnodetype ** end)
{ //考慮到只統計一行的單詞沒有意義,故統計整個文本
int yes = 1 ; //這個是進入單詞的標志
int no = 0 ; //在單詞外面的時候的標志
int i , j ,inaword ,count = 0 ;
inaword = no ;
lnodetype * s = (*l)->next ;
for (j=0; j<(*l)->number; j++){
for ( i=0; (s->date[i]>='a' && s->date[i]<='z') ||
(s->date[i]>='A' && s->date[i]<='z') ||
(s->date[i]>='0' && s->date[i]<='9' ) ||
(s->date[i]==' '); i++ )
{
if ( s->date[i] == ' ' )
inaword = no ;
else
if ( inaword == no )
{
inaword = yes ;
count++ ; //計算單詞
}
}
s = s->next ;
inaword = 0 ; //注意這里,把標志置為0了!
}
printf ( "n文本一共有 %d 行" , (*l)->number ) ;
printf ("n此文本一共有 %d 個單詞!" , count ) ;
return count ;
}
//**********這個函數是用來實現計算文本行數功能的**********//
int linecouter(lnodetype ** l , lnodetype ** end )
{
int couter ;
couter = (*l)->number ;
return couter ;
}
//**********這個函數是整和一上所有功能的菜單函數**********//
int editmenu(lnodetype ** l , lnodetype ** end )
{
char choice ;
char * p = "cls" ;
int i = 1 ; //這兩個變數是用來控制循環的
int j= 1 ;
system (p) ;
prin (&(*l) , &(*end)) ;
while (j){
printf ("*********************************** e: 編輯相應行 *************************************n") ;
printf ("*********************************** s: 搜索字元串 *************************************n") ;
printf ("*********************************** t: 統計單詞個數 ***********************************n") ;
printf ("*********************************** q: 退出編輯 ***************************************n") ;
scanf("%c",&choice);
scanf("%c" , &choice) ; //????莫名其妙的問題,非要兩個請求輸入語句才肯停下來!
switch (choice)
{
case 'e' : {
i = 1 ;
while (i)
{
search( &(*l) , &(*end) );
system (p) ;
prin( &(*l) , &(*end) ) ;
printf ("n1 繼續編輯 0 結束編輯n") ;
scanf ("%d" , &i) ;
}
}
break;
case 's' : {
i = 1 ;
while (i)
{
searchstr( &(*l) , &(*end) );
getchar();
getchar();
system (p) ;
prin( &(*l) , &(*end) ) ;
printf ("n1 繼續搜索 0 結束搜索n") ;
scanf ("%d" , &i) ;
}
}
break;
case 't' : {
coutword ( &(*l) , &(*end) ) ;
getchar() ;
}break;
default : return 0 ;
}
system (p) ;
prin( &(*l) , &(*end) ) ;
printf ("n1 回到編輯菜單 0 結束編輯n") ;
scanf ("%d" , &j) ;
if (j == 1)
system (p) ;
else
return 0 ;
}
return 0 ;
}
//**********實現第一個模塊:新建空白文檔**********//
int newtext( lnodetype ** l ,lnodetype ** end )
{
printf ( "新文本文件:n" ) ;
int i = 1 ;
char judstr[MAX] ;
lnodetype * temp ;
char jud ;
char * p = "cls" ;
while ( 1 ){
link( &(*l) , &(*end) ) ;
jud = (*end)->date[0] ;
if ( jud == '5' ){ //輸入『5』結束一切
temp = (*end) ;
(*end) = (*end)->prior ;
free (temp) ;
while (1){
printf ( "******************************* out :退出不保存 ****************************n") ;
printf ( "******************************* edit :編輯信息 *****************************n") ;
printf ( "******************************* quit :退出而不存檔 **************************n") ;
printf ( "******************************* qas :退出且存檔 ****************************n") ;
printf ( "******************************* con :繼續輸入! *****************************n") ;
gets(judstr) ;
if ( !strcmp(judstr , "out") )
{
exitunsave( &(*l) , &(*end) ) ;
return 0 ;
}
else
if ( !strcmp(judstr , "qas") )
{
quitandsave( &(*l) , &(*end) ) ;
return 0 ;
}
else
if ( !strcmp(judstr , "edit") )
{
editmenu (l , end) ;
return 0;
}
system (p) ;
}
return 0 ;
}
}
return 0 ;
}
//**********這個是裝入文件的函數**********//
int loadtaxt( char * filename )
{
FILE * fp ;
lnodetype * l ;
char ch ;
int i = 0 ;
char * p = "cls" ;
char judstr[MAX] ;
lnodetype * head ;
lnodetype * end ;
iniatelist ( &head , &end) ;
l = end = head ;
if ( (fp = fopen( filename, "r+")) == NULL ){
printf ("文件不能打開!n") ;
return 0 ;
}
ch = fgetc ( fp ) ;
while ( ch != '@' ){
lnodetype *s ;
s = (lnodetype *)malloc ( sizeof (lnodetype) ) ;
if ( s == NULL ){
printf ("內存空間不夠,程序即將退出!") ;
return 0 ;
}
end->next = s ;
s->prior = end ;
end = end->next ;
l->number++ ;
s->quese = l->number ;
printf ("%d行", s->quese ) ;
while ( ch != '#'){
s->date[i] = ch ;
ch = fgetc (fp) ;
i++ ;
}
i = 0;
while ( (end->date[i]>='a' && end->date[i]<='z') ||
(end->date[i]>='A' && end->date[i]<='z') ||
(end->date[i]>='0' && end->date[i]<='9' ) ||
(end->date[i]==' ') )
{
printf ( "%c" , end->date[i] ) ;
i++ ;
}
end->date[i] = '\0' ; //注意在節點的最好加上這個,以讓退出保存功能函數知道此節點已結束
printf ( "n" ) ;
i = 0;
ch = fgetc ( fp ) ;
}
fclose (fp) ;
printf ("n文件成功裝入!n") ;
while (1){
printf ( "******************************* out :退出不保存 ****************************n") ;
printf ( "******************************* edit :編輯信息 *****************************n") ;
printf ( "******************************* qas :退出且存檔 ****************************n") ;
printf ( "******************************* con :繼續輸入! *****************************n") ;
scanf("%s",judstr);
if ( !strcmp(judstr , "out") )
{
exitunsave( &l , &end ) ;
return 0 ;
}
else
if ( !strcmp(judstr , "qas") )
{
quitandsave( &l , &end ) ;
return 0 ;
}
else
if ( !strcmp(judstr , "edit") )
{
editmenu (&l , &end) ;
return 0 ;
}
system (p) ;
}
return 0 ;
}
//**********主函數**********//
void main ( void )
{
//iniatelist (&l , &end) ;
//newtext(&l , &end) ;
char filename[MAX] ;
scanf ( "%s" , filename ) ;
loadtaxt( filename ) ;
}
很不容易做的程序!!!
通過編譯
Ⅸ 用C語言寫一組代碼
用C語言編寫的代碼可以是多個,多文件形式,可以存在多個源文件
用C語言編寫的代碼生成的可執行文件是一個exe,說白一點就是一個程序
用C語言編程的代碼生成的軟體,之間可以互相溝通交流的.就像TCP通信編程
用C語言編程的代碼,點擊我頭像,加群.更多精彩.