1. 求大神讲解一下c语言定义的问题
定义数组时,是 数组名 [常量表达式]
但是现在很多编译器做了扩展,可以设置成可变的数组,不会报错,可能会引起内存溢出
2. c语言的题,求大神解答
解:1、A项错误:有些不可见字符可放入缓冲区,例如:回车、空格。
B项错误:有些输入函数有缓冲区,有些没有,例如:getchar() 有缓冲区,getch() 无缓冲区,getche() 无缓冲区。
C项错误:缓冲区不需要定义。
所以选D。
2、getchar()函数有缓冲区。getchar函数的返回值是用户输入的字符的ASCII码,若文件结尾则返回-1(EOF),且将用户输入的字符回显到屏幕。如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取。也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完后,才等待用户按键。
该程序调用getchar()函数三次,所以getchar()函数依次读取前三个输入的数据,空格也算,所以选B。
3、回车是用\n表示,且((c=getchar())!='\n')注意这里的圆括号不能丢,因为!=的优先级比=高,如果去掉了外面的括号就等价于 c = (getchar()!='\n')。所以选C。
3. C语言急求大神解答
void process(char s1[], char s2[], char s3[])
{
int n = 0;
char* str[2] = { s1,s2 };
for (int i = 0; i < 2; i++)
{
char* p = str[i];
for (int j = 0; *p != 0; j++)
{
s3[n] = *p;
p++;
n++;
}
}
s3[n] = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (s3[j] > s3[j + 1])
{
int t = s3[j];
s3[j] = s3[j + 1];
s3[j + 1] = t;
}
}
}
}
4. C语言编程题,求大神解答!!!!
按照题目要求用二分法求方程的实根的C语言程序如下
#include <stdio.h>
#include <math.h>
double f(double x) {return 2*x*x*x - 4*x*x + x - 2;}
int main(){
double root, x1 = 0, x2 = 10,y;
root = x1+(x2-x1)/2;
y = f(root);
while(fabs(y) > 1e-6) {
if(y > 0) x2 = root;
else x1 = root;
root = x1+(x2 - x1)/2;
y = f(root);
}
printf("%.2f ",root);
return 0;
}
5. c语言程序编写问题,求大神解答!!!求求了急!!!万分感谢
#include <stdio.h>
char *func(char *s)
{
char *p,*q,t;
for(q=s;*q;q++);
for(q--,p=s;p<q;p++,q--)
{
t=*p;
*p=*q;
*q=t;
}
return s;
}
int main()
{
char s[100];
scanf("%[^ ]",s);
puts(func(s));
return 0;
}
6. C语言问题,求大神解答
#include<stdio.h>
void main()
{
char ch;
int a;
ch=getchar();
if(ch>='a'&&ch<='z')
ch='1';
if(ch>='A'&&ch<='Z')
ch='2';
if(ch==' ')
ch='3';
switch(ch)
{
case'1':
printf("小写字母 ");
break;
case'2':
printf("大写字母 ");
break;
case'3':
printf("空格 ");
break;
default:
printf("其他");
}
}
7. c语言,求大神解答。
#include "stdio.h"
int main()
{
int a[10],i,j,num=0,max,max_num;
for (i=0;i<10;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==1)
num++;
}
for (i=0;i<9;i++)
{
max=a[0];
max_num=0;
for (j=0;j<9-i;j++)
{
if(max<a[j+1])
{
max=a[j+1];
max_num=j+1;
}
}
if(max_num!=9-i)
{
a[max_num]=a[9-i];
a[9-i]=max;
}
}
printf("奇数个数为%d
",num);
printf("排序后数组:
");
for (i=0;i<10;i++)
printf("%d ",a[i]);
printf("
");
return 0;
}
8. 求大神解答C语言编程
#include <stdio.h>
#include <stdlib.h>
//定义一个结构体保存裁判id和分数
typedef struct{
unsigned char judegId;//裁判ID
unsigned char judegScore;//裁判给的分数
}judge;
int main()
{
judge jg[10];
int i;
int score;
for(i = 0;i < 10;i++)
{
printf("请输入裁判%d打的分数\n",i+1);
scanf("%d",&score);
getchar();
if(score <= 0 || score > 100)
{
printf("输入分数无效,重新输入,分数要求1-100!\n");
printf("请输入裁判%d打的分数\n",i+1);
scanf("%d",&score);
getchar();
if(score <= 0 || score > 100)
{
printf("无效输入,程序退出!\n");
return -1;
}
}
jg[i].judegId = i;
jg[i].judegScore = score;
}
//对数组从分数低到分数高排序
for(i = 0;i < 9;i++)
{
if(jg[i].judegScore > jg[i+1].judegScore)
{
unsigned char tmpId = jg[i].judegId;
unsigned char tmpScore = jg[i].judegScore;
jg[i].judegId = jg[i+1].judegId;
jg[i].judegScore = jg[i+1].judegScore;
jg[i+1].judegScore = tmpScore;
jg[i+1].judegId = tmpId;
}
}
//打印所有得分
printf("选手得分为:\n");
for(i = 0;i < 10;i++)
{
printf("%d ",jg[i].judegScore);
}
printf("\n");
//计算评价得分
unsigned short sum = 0;
for(i = 1;i < 9;i++)
{
sum+=jg[i].judegScore;
}
unsigned char average = sum/8;
printf("去掉最高分%d,最低分%d,评价得分为%d分\n",jg[9].judegScore,jg[0].judegScore,average);
unsigned char fairIndex0 = 0;
unsigned char unfairIndex = 0;
//查找与平均值差距最小的
for(i = 1;i < 10;i++)
{
//abs为求绝对值函数
if(abs(jg[i].judegScore-average) < abs(jg[fairIndex].judegScore-average))
{
fairIndex = i;
}
}
printf("最公平的教练是%d,打分为%d\n",jg[fairIndex].judegId,jg[fairIndex].judegScore);
//查找与平均分差距最大的
for(i = 1;i < 10;i++)
{
if(abs(jg[i].judegScore-average) > abs(jg[unfairIndex].judegScore-average))
{
unfairIndex = i;
}
}
printf("最不公平的教练是%d,打分为%d\n",jg[unfairIndex].judegId,jg[unfairIndex].judegScore);
return 0;
}
//留一个问题给你,可能有两个裁判是最公平或者最不公平的,这个自己想想怎么做
9. c语言编程 求大神解答!
#include<stdio.h>
int main()
{//输入UUURR,等价于上上上右右;(0,0)→(2,3)
int x=0,y=0;
int n;
char ch;
scanf("%d",&n);
getchar();//吃掉回车符
for(int i=0;i<n;i++)
{
ch=getchar();//结束一个代表方向的字符
getchar();//吃掉回车符
if(ch=='L')//左
x--;
if(ch=='R')//右
x++;
if(ch=='U')//上
y++;
if(ch=='D')//下
y--;
}
printf("%d %d",x,y);
}
10. C语言问题,求大神解答
//Node* head表示头指针
//头指针指向头结点,头结点的值无用,头结点的指针指向链表内第一个元素
//当NULL == head->next时链表为空,当NULL == head时链表为无效链表
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int val;
struct Node* next;
}Node;
void* _malloc(size_t size) {
void* res = malloc(size);
if (NULL == res) {
printf("内存不足,程序正在退出 ");
exit(1);
}
return res;
}
void Append(Node* node, int val) {
Node* next = node->next;
node->next = _malloc(sizeof(Node));
node->next->val = val;
node->next->next = next;
}
void SortInsert(Node* head, int val) {
if (NULL != head) {
Node* last;
for (;;) {
last = head;
head = head->next;
if (NULL == head || head->val >= val) {
break;
}
}
Append(last, val);
}
}
void EraseP(Node* last) {
if (NULL != last && NULL != last->next) {
Node* next = last->next->next;
free(last->next);
last->next = next;
}
}
void Erase(Node* head, int n) {
while (n-- > 0 && NULL != head) {
head = head->next;
}
if (NULL != head) {
EraseP(head);
}
}
void ShowAll(Node* head) {
if (NULL != head) {
while (NULL != (head = head->next)) {
printf("%d -> ", head->val);
}
printf(" ");
}
}
int main(int argc, char* argv[]) {
Node* head = _malloc(sizeof(Node));
head->next = NULL;
SortInsert(head, 4); ShowAll(head);
SortInsert(head, 0); ShowAll(head);
SortInsert(head, 1); ShowAll(head);
SortInsert(head, 3); ShowAll(head);
SortInsert(head, 2); ShowAll(head);
printf(" ");
Erase(head, 3); ShowAll(head);
Erase(head, 3); ShowAll(head);
Erase(head, 0); ShowAll(head);
Erase(head, 1); ShowAll(head);
Erase(head, 0); ShowAll(head);
return 0;
}
运行截图