user-0 提问: 这两道C语言编程题琢磨了半天还是不会

回复:
user: user-1
content: 多多指教！ #include <iostream> using namespace std; int main() { 	char buffer[80]; 	cout << "enter a line: "; 	gets(buffer); 	char *p = buffer; 	int a = 0; 	int b = 0;  	while( *p != '\0') 	{ 		if ('0' <= *p && *p <= '9') 		{ 			strcpy(buffer,p ); 			break; 		} 		++ p ; 	} 	sscanf(buffer,"%d",&a); 	while(*p != '\0') 	{ 		if ('a'<= *p && *p<= 'z') 		{ 			strcpy(buffer  , p); 			break; 		} 		++ p ; 	}  	while( *p != '\0') 	{ 		if ('0' <= *p && *p <= '9') 		{ 			strcpy(buffer,p ); 			break; 		} 		++ p ; 	} 	sscanf(buffer,"%d",&b); 	cout << a<<"*"<<2 << "="<<2*a  <<  "  ";  	cout << b<<"*"<<2 << "="<<2*b  << endl; 	return 0; }

user: user-1
content: 看完赵老师的程序，我再回复一次吧，第一题： #include <iostream> using namespace std; int main() { 	char s[40]; 	gets(s);  	char *p = s; 	int num = 0; 	int count = 0; 	int flag; 	while(*p != '\0') 	{ 		flag = sscanf(p ,"%d%n",&num ,&count );	 		if (flag == 1) 		{ 			cout << num << "*2="<<num*2<<"  "; 			p += count; 		}else if (flag == 0) 		{ 			++ p ; 		} 	} 	return 0; }

user: user-2
content: 第一个用isdigit()好一点 第二个用打印输出函数 printf 指定格式

user: user-3
content: 不难吧 1.遍历字符串，发现有数字出现的则开始进入数字模式，直到发现下一个不为数字而退出数字模式，这样即可获取到一个数，用atoi转换为数，这个数乘以2就输出一部分结果了，对于剩下的字符重复上述操作即可完成题目的要求。   2.首先要知道进制转换的方法，自己能够手动转换进制的数（这个搜索一下就知道了，比你琢磨了半天估计要好点），然后把想法做法改成代码就ok了！

user: user-0
content: 求大神帮忙做出来！万分感激！

user: user-4
content: 引用 7 楼 Falleyes 的回复:第二个程序： #include<stdio.h>  void print(int num){ 	printf("%d*2=%d ", num, num * 2); }  int main(){ 	int n, base, i = 0,origin; 	char* num = "0123456789ABCDEF"; 	char stack[30];  	scanf("%d %d", &n, &base); 	origin = n; 	while (n){ 		stack[i++] = num[n%base]; 		n /= base; 	} 	printf("%d(10)=", origin); 	for (--i; i >= 0; --i) 		putchar(stack[i]); 	printf("(%d)\n", base); }  忘了删除上个程序的函数，这样就好。 #include<stdio.h> int main(){ 	int n, base, i = 0,origin; 	char* num = "0123456789ABCDEF"; 	char stack[30];  	scanf("%d %d", &n, &base); 	origin = n; 	while (n){ 		stack[i++] = num[n%base]; 		n /= base; 	} 	printf("%d(10)=", origin); 	for (--i; i >= 0; --i) 		putchar(stack[i]); 	printf("(%d)\n", base); }

user: user-5
content: 仅供参考： #include <stdio.h> char s[]="123 ab 4"; char *p; int v,n,k; void main() {     p=s;     while (1) {         k=sscanf(p,"%d%n",&v,&n);         printf("k,v,n=%d,%d,%d\n",k,v,n);         if (1==k) {             p+=n;         } else if (0==k) {             printf("skip char[%c]\n",p[0]);             p++;         } else {//EOF==k             break;         }     }     printf("End.\n"); } //k,v,n=1,123,3 //k,v,n=0,123,3 //skip char[ ] //k,v,n=0,123,3 //skip char[a] //k,v,n=0,123,3 //skip char[b] //k,v,n=1,4,2 //k,v,n=-1,4,2 //End.

user: user-4
content: 楼主记得结贴

user: user-4
content: 第二个程序： #include<stdio.h>  void print(int num){ 	printf("%d*2=%d ", num, num * 2); }  int main(){ 	int n, base, i = 0,origin; 	char* num = "0123456789ABCDEF"; 	char stack[30];  	scanf("%d %d", &n, &base); 	origin = n; 	while (n){ 		stack[i++] = num[n%base]; 		n /= base; 	} 	printf("%d(10)=", origin); 	for (--i; i >= 0; --i) 		putchar(stack[i]); 	printf("(%d)\n", base); }

user: user-4
content: 第一个程序，写出来简单版本的，不过不会输出字符串中单个0。要考虑单个0，自己再添加变量判断。 #include<stdio.h> #include<ctype.h>  void print(int num){ 	printf("%d*2=%d ", num, num * 2); }  int main(){ 	char a[50]; 	int i = 0,num = 0;  	gets(a); 	while (a[i]!='\0'){ 		if (isdigit(a[i])) 			num = num * 10 + a[i]-0x30; 		else{ 			if (num!=0) 				print(num); 			num = 0; 		} 		++i; 	}  	if (num != 0) 		print(num); }

user: user-6
content: 第二题参考下这个（进制转换）：稍作修改就跟你的要求一样 “main.cpp” #include "SeqStack.h"  void Convert_16(unsigned long val,int m) { 	SeqStack st; 	InitSeqStack(st); 	 	int N = 0; 	char ch; 	while(val != 0) 	{ 		N = val % m; 		if(N >= 10) 		{ 			ch = 'A' +(N - 10);//A   65 			Push(st,ch); 		} 		else 		{ 			ch = N + '0'; 			Push(st,ch); 		} 		val = val / m; 	} 	ShowSeqStack(st); 	DestroySeqStack(st); }  int main() { 	unsigned long val; 	int m; 	cout<<"请输入要转换的数："; 	cin>>val; 	cout<<"请输入要转换为的进制："; 	cin>>m; 	Convert_16(val,m);  	return 0; }  /* int main() {     SeqStack myst; 	InitSeqStack(myst);  	ElemType item; 	ElemType val; 	int select = 1; 	while(select) 	{ 		cout<<"*******************************"<<endl; 		cout<<"*          SeqStack           *"<<endl; 		cout<<"* [1]Push          [2]Pop     *"<<endl; 		cout<<"* [3]ShowStack     [4]GetTop  *"<<endl; 		cout<<"* [5]StackLength   [6]Clear   *"<<endl; 		cout<<"* [7]DestroyStack  [0]Quit    *"<<endl;  		cout<<"*******************************"<<endl; 		cout<<"请选择：>"; 		cin>>select; 		switch(select) 		{ 		case 0: 			exit(0); 			break; 		case 1: 			cout<<"请输入要入栈的元素："; 			cin>>item; 			Push(myst,item); 			break; 		case 2: 			if(Pop(myst,val) == OK) 			{ 				cout<<val<<"出栈！"<<endl; 			} 			break; 		case 3: 			ShowSeqStack(myst); 			break; 		case 4: 			GetTop(myst,val); 			cout<<"栈顶元素是："<<val<<endl; 			break; 		case 6: 			ClearSeqStack(myst); 			break; 		case 7: 			DestroySeqStack(myst); 			break; 		default: 			break; 		} 	}  	return 0; }*/  “SeqStack.cpp” #include "SeqStack.h"  Status InitSeqStack(SeqStack &st) { 	st.base = (ElemType *)malloc(sizeof(ElemType) * STACK_INIT_SIZE); 	assert(st.base != NULL); 	st.size = STACK_INIT_SIZE;     st.top = 0; 	return OK; }  bool IsFull(SeqStack st) { 	return st.top >= st.size; }  bool IsEmpty(SeqStack st) { 	return st.top == 0; }  Status Push(SeqStack &st,ElemType e) { 	if(!IsFull(st)) 	{ 		st.base[st.top++] = e; 		return OK; 	} 	return ERROR; }  Status Pop(SeqStack &st,ElemType &v) { 	if(!IsEmpty(st)) 	{ 		v = st.base[--st.top]; 		return OK; 	} 	cout<<"栈为空！"<<endl; 	return ERROR; }  void ShowSeqStack(SeqStack st) { 	for(int i = st.top-1;i >= 0; i--) 	{ 		cout<<st.base[i]; 	} 	cout<<endl; }  Status GetTop(SeqStack st,ElemType &v) { 	if(!IsEmpty(st)) 	{ 		v = st.base[--st.top]; 		return OK; 	} 	return ERROR; }  Status ClearSeqStack(SeqStack &st) { 	st.top = 0; 	st.size = 0; 	return OK; }  Status DestroySeqStack(SeqStack &st) { 	if(st.base != NULL) 	{ 		free(st.base); 		st.base = NULL; 		st.top = 0; 		st.size = 0; 		return OK; 	} 	return ERROR; }

user: user-0
content: 求代码