【求助】幾題C問題,有關有關getch'陣列



贊助商連結


金田一
2004-07-11, 09:56 AM
1.
#include <stdio.h>
void main()
{
char c=0;
int i,loc=0;
printf("A\r");
while(c!=27)
{
c=getchar(void);
if(c==75)
{
loc--;
}
if (c==77)
{
loc++;
}

}
loc+=10;
loc%=10;
printf(" \r");
for(i=0;i<loc;i++)
printf(" ");
printf("A\r");
}
2.
#include <stdio.h>
#define getrand(x) random(x)
void main()
{
int arr[10];
float random(int);

int i,j,x;
for(i=0;i<10;i++)
{
x=100;
j=getrand(x);
arr[i]=j;
printf("arr[%d]=%d\n",i,arr[i]);
}
}
1.
題:執行時,按向左'右鍵.螢幕上的字母A會左右移動
問:執行時,按左右鍵沒有任何反應
2.
題:寫函式getrand(),呼叫隨機函式產生10個(0~100)數值,並將產生數值放致陣列中
問:產生錯誤.unresolved external symbol "float __cdecl random(int)" (?random@@YAMH@Z)
謝謝回答

贊助商連結


ranger
2004-07-12, 12:23 AM
1.左右鍵無法由getch取得鍵值,必須引用bios.h
2.直接函式andom即可
#include <stdio.h>
#include <stdlib.h>
void main()
{
int arr[10];
int i,j,x;
randomize();
for(i=0;i<10;i++) {
arr[i]=random(100);
printf("arr[%d]=%d\n",i,arr[i]);
}
}

金田一
2004-07-12, 11:12 PM
嗯....找不到bios.h耶..要去那裡找呢
謝謝回答

ranger
2004-07-14, 09:37 AM
最初由 金田一 發表
嗯....找不到bios.h耶..要去那裡找呢
謝謝回答

應該是在include(turbo c++)底下啊...
您的開發環境為何?

金田一
2004-07-14, 09:23 PM
我是用Visual C++跑的

金田一
2004-07-24, 11:47 PM
#include <stdio.h>
#include <stdlib.h>

struct list
{
int data;
struct list *next;
};
typedef struct list node;
typedef node *link;

void main()
{
link ptr,head,tail;
int num,i;
tail=(link)malloc(sizeof(node));
tail->next=NULL;
ptr=tail;
printf("input\n");
for(i=0;i<=4;i++)
{
scanf("%d",&num);
ptr->data=num;
head=(link)malloc(sizeof(node));
head->next=ptr;
ptr=head;
}
ptr=ptr->next;
printf("list\n");
while(ptr!=NULL)
{
printf("the value is %d\n",ptr->data);
ptr=ptr->next;
}
}
---------------
1.tail=(link)malloc(sizeof(node));這是指tail分配一個位址是node的記憶体大小的開頭嗎

2. ptr->data=num;/*將輸入至ptr->data*/
head=(link)malloc(sizeof(node));/*head=node大小*/
head->next=ptr;/*為何將head->next=ptr然後又將ptr=head呢*/
ptr=head;
在記憶体是的圖形是否如下呢?
|----|----|----| |-----|-----|--- |
|head|data|next|-- -> |ptr |data |next|-->NULL
|----|----|----| |-----|-----|--- |
|--------------------> | |-->num
|>tail
謝謝回答